def test_dtype_eq(self): from _numpypy import dtype assert dtype("int8") == "int8" assert "int8" == dtype("int8") raises(TypeError, lambda: dtype("int8") == 3) assert dtype(bool) == bool
def test_dtype_with_types(self): from _numpypy import dtype assert dtype(bool).num == 0 assert dtype(int).num == 7 assert dtype(long).num == 9 assert dtype(float).num == 12
def test_binop_types(self): from _numpypy import array, dtype tests = [('b', 'B', 'h'), ('b', 'h', 'h'), ('b', 'H', 'i'), ('b', 'i', 'i'), ('b', 'l', 'l'), ('b', 'q', 'q'), ('b', 'Q', 'd'), ('B', 'h', 'h'), ('B', 'H', 'H'), ('B', 'i', 'i'), ('B', 'I', 'I'), ('B', 'l', 'l'), ('B', 'L', 'L'), ('B', 'q', 'q'), ('B', 'Q', 'Q'), ('h', 'H', 'i'), ('h', 'i', 'i'), ('h', 'l', 'l'), ('h', 'q', 'q'), ('h', 'Q', 'd'), ('H', 'i', 'i'), ('H', 'I', 'I'), ('H', 'l', 'l'), ('H', 'L', 'L'), ('H', 'q', 'q'), ('H', 'Q', 'Q'), ('i', 'l', 'l'), ('i', 'q', 'q'), ('i', 'Q', 'd'), ('I', 'L', 'L'), ('I', 'q', 'q'), ('I', 'Q', 'Q'), ('q', 'Q', 'd'), ('b', 'f', 'f'), ('B', 'f', 'f'), ('h', 'f', 'f'), ('H', 'f', 'f'), ('i', 'f', 'd'), ('I', 'f', 'd'), ('l', 'f', 'd'), ('L', 'f', 'd'), ('q', 'f', 'd'), ('Q', 'f', 'd'), ('q', 'd', 'd')] if dtype('i').itemsize == dtype('l').itemsize: # 32-bit tests.extend([('b', 'I', 'q'), ('b', 'L', 'q'), ('h', 'I', 'q'), ('h', 'L', 'q'), ('i', 'I', 'q'), ('i', 'L', 'q')]) else: tests.extend([('b', 'I', 'l'), ('b', 'L', 'd'), ('h', 'I', 'l'), ('h', 'L', 'd'), ('i', 'I', 'l'), ('i', 'L', 'd')]) for d1, d2, dout in tests: assert (array([1], d1) + array([1], d2)).dtype is dtype(dout)
def test_binop_types(self): from _numpypy import array, dtype tests = [ ("b", "B", "h"), ("b", "h", "h"), ("b", "H", "i"), ("b", "i", "i"), ("b", "l", "l"), ("b", "q", "q"), ("b", "Q", "d"), ("B", "h", "h"), ("B", "H", "H"), ("B", "i", "i"), ("B", "I", "I"), ("B", "l", "l"), ("B", "L", "L"), ("B", "q", "q"), ("B", "Q", "Q"), ("h", "H", "i"), ("h", "i", "i"), ("h", "l", "l"), ("h", "q", "q"), ("h", "Q", "d"), ("H", "i", "i"), ("H", "I", "I"), ("H", "l", "l"), ("H", "L", "L"), ("H", "q", "q"), ("H", "Q", "Q"), ("i", "l", "l"), ("i", "q", "q"), ("i", "Q", "d"), ("I", "L", "L"), ("I", "q", "q"), ("I", "Q", "Q"), ("q", "Q", "d"), ("b", "f", "f"), ("B", "f", "f"), ("h", "f", "f"), ("H", "f", "f"), ("i", "f", "d"), ("I", "f", "d"), ("l", "f", "d"), ("L", "f", "d"), ("q", "f", "d"), ("Q", "f", "d"), ("q", "d", "d"), ] if dtype("i").itemsize == dtype("l").itemsize: # 32-bit tests.extend( [("b", "I", "q"), ("b", "L", "q"), ("h", "I", "q"), ("h", "L", "q"), ("i", "I", "q"), ("i", "L", "q")] ) else: tests.extend( [("b", "I", "l"), ("b", "L", "d"), ("h", "I", "l"), ("h", "L", "d"), ("i", "I", "l"), ("i", "L", "d")] ) for d1, d2, dout in tests: assert (array([1], d1) + array([1], d2)).dtype is dtype(dout)
def test_dtype(self): from _numpypy import dtype d = dtype("?") assert d.num == 0 assert d.kind == "b" assert dtype("int8").num == 1 assert dtype(d) is d assert dtype(None) is dtype(float) raises(TypeError, dtype, 1042)
def test_str_dtype(self): from _numpypy import dtype, str_ raises(TypeError, "dtype('Sx')") d = dtype('S8') assert d.itemsize == 8 assert dtype(str) == dtype('S') assert d.kind == 'S' assert d.type is str_ assert d.name == "string64" assert d.num == 18
def test_unicode_dtype(self): from _numpypy import dtype, unicode_ raises(TypeError, "dtype('Ux')") d = dtype('U8') assert d.itemsize == 8 * 4 assert dtype(unicode) == dtype('U') assert d.kind == 'U' assert d.type is unicode_ assert d.name == "unicode256" assert d.num == 19
def test_ndarray(self): from _numpypy import ndarray, array, dtype assert type(ndarray) is type assert type(array) is not type a = ndarray((2, 3)) assert a.shape == (2, 3) assert a.dtype == dtype(float) raises(TypeError, ndarray, [[1], [2], [3]]) a = ndarray(3, dtype=int) assert a.shape == (3,) assert a.dtype is dtype(int)
def test_ndarray(self): from _numpypy import ndarray, array, dtype assert type(ndarray) is type assert type(array) is not type a = ndarray((2, 3)) assert a.shape == (2, 3) assert a.dtype == dtype(float) raises(TypeError, ndarray, [[1], [2], [3]]) a = ndarray(3, dtype=int) assert a.shape == (3, ) assert a.dtype is dtype(int)
def test_int32(self): import sys import _numpypy as numpy x = numpy.int32(23) assert x == 23 assert numpy.int32(2147483647) == 2147483647 assert numpy.int32('2147483647') == 2147483647 if sys.maxint > 2**31 - 1: assert numpy.int32(2147483648) == -2147483648 assert numpy.int32('2147483648') == -2147483648 else: raises(OverflowError, numpy.int32, 2147483648) raises(OverflowError, numpy.int32, '2147483648') assert numpy.dtype('int32') is numpy.dtype(numpy.int32)
def test_int32(self): import sys import _numpypy as numpy x = numpy.int32(23) assert x == 23 assert numpy.int32(2147483647) == 2147483647 assert numpy.int32('2147483647') == 2147483647 if sys.maxint > 2 ** 31 - 1: assert numpy.int32(2147483648) == -2147483648 assert numpy.int32('2147483648') == -2147483648 else: raises(OverflowError, numpy.int32, 2147483648) raises(OverflowError, numpy.int32, '2147483648') assert numpy.dtype('int32') is numpy.dtype(numpy.int32)
def test_repr_str(self): from _numpypy import dtype assert '.dtype' in repr(dtype) d = dtype('?') assert repr(d) == "dtype('bool')" assert str(d) == "bool"
def test_repr_str(self): from _numpypy import dtype assert repr(dtype) == "<type 'numpypy.dtype'>" d = dtype("?") assert repr(d) == "dtype('bool')" assert str(d) == "bool"
def test_uint8(self): import _numpypy as numpy assert numpy.uint8.mro() == [ numpy.uint8, numpy.unsignedinteger, numpy.integer, numpy.number, numpy.generic, object ] a = numpy.array([1, 2, 3], numpy.uint8) assert type(a[1]) is numpy.uint8 assert numpy.dtype("uint8").type is numpy.uint8 x = numpy.uint8(128) assert x == 128 assert x != -128 assert type(x) is numpy.uint8 assert repr(x) == "128" assert type(int(x)) is int assert int(x) == 128 assert numpy.uint8(255) == 255 assert numpy.uint8(256) == 0 assert numpy.uint8('255') == 255 assert numpy.uint8('256') == 0
def test_div(self): from math import isnan from _numpypy import array, dtype, inf a = array(range(1, 6)) b = a / a for i in range(5): assert b[i] == 1 a = array(range(1, 6), dtype=bool) b = a / a assert b.dtype is dtype("int8") for i in range(5): assert b[i] == 1 a = array([-1, 0, 1]) b = array([0, 0, 0]) c = a / b assert (c == [0, 0, 0]).all() a = array([-1.0, 0.0, 1.0]) b = array([0.0, 0.0, 0.0]) c = a / b assert c[0] == -inf assert isnan(c[1]) assert c[2] == inf b = array([-0.0, -0.0, -0.0]) c = a / b assert c[0] == inf assert isnan(c[1]) assert c[2] == -inf
def test_bool_binop_types(self): from _numpypy import array, dtype types = ["?", "b", "B", "h", "H", "i", "I", "l", "L", "q", "Q", "f", "d"] a = array([True], "?") for t in types: assert (a + array([0], t)).dtype is dtype(t)
def test_uint8(self): import _numpypy as numpy assert numpy.uint8.mro() == [ numpy.uint8, numpy.unsignedinteger, numpy.integer, numpy.number, numpy.generic, object, ] a = numpy.array([1, 2, 3], numpy.uint8) assert type(a[1]) is numpy.uint8 assert numpy.dtype("uint8").type is numpy.uint8 x = numpy.uint8(128) assert x == 128 assert x != -128 assert type(x) is numpy.uint8 assert repr(x) == "128" assert type(int(x)) is int assert int(x) == 128 assert numpy.uint8(255) == 255 assert numpy.uint8(256) == 0 assert numpy.uint8("255") == 255 assert numpy.uint8("256") == 0
def test_alternate_constructs(self): from _numpypy import dtype nnp = self.non_native_prefix byteorder = self.native_prefix assert dtype('i8') == dtype(byteorder + 'i8') == dtype('=i8') # XXX should be equal == dtype(long) assert dtype(nnp + 'i8') != dtype('i8') assert dtype(nnp + 'i8').byteorder == nnp assert dtype('=i8').byteorder == '=' assert dtype(byteorder + 'i8').byteorder == '='
def test_scalar(self): from _numpypy import array, dtype a = array(3) raises(IndexError, "a[0]") raises(IndexError, "a[0] = 5") assert a.size == 1 assert a.shape == () assert a.dtype is dtype(int)
def test_add_uint32(self): from _numpypy import array, dtype a = array(range(5), dtype="I") b = a + a assert b.dtype is dtype("I") for i in range(5): assert b[i] == i * 2
def test_bool_binop_types(self): from _numpypy import array, dtype types = [ '?', 'b', 'B', 'h', 'H', 'i', 'I', 'l', 'L', 'q', 'Q', 'f', 'd' ] a = array([True], '?') for t in types: assert (a + array([0], t)).dtype is dtype(t)
def test_create(self): from _numpypy import dtype, void raises(ValueError, "dtype([('x', int), ('x', float)])") d = dtype([("x", "int32"), ("y", "int32"), ("z", "int32"), ("value", float)]) assert d.fields['x'] == (dtype('int32'), 0) assert d.fields['value'] == (dtype(float), 12) assert d['x'] == dtype('int32') assert d.name == "void160" assert d.num == 20 assert d.itemsize == 20 assert d.kind == 'V' assert d.type is void assert d.char == 'V' assert d.names == ("x", "y", "z", "value") raises(KeyError, 'd["xyz"]') raises(KeyError, 'd.fields["xyz"]')
def test_int_(self): import _numpypy as numpy assert numpy.int_ is numpy.dtype(int).type assert numpy.int_.mro() == [ numpy.int_, numpy.signedinteger, numpy.integer, numpy.number, numpy.generic, int, object ]
def test_alternate_constructs(self): from _numpypy import dtype nnp = self.non_native_prefix byteorder = self.native_prefix assert dtype('i8') == dtype(byteorder + 'i8') == dtype( '=i8') # XXX should be equal == dtype(long) assert dtype(nnp + 'i8') != dtype('i8') assert dtype(nnp + 'i8').byteorder == nnp assert dtype('=i8').byteorder == '=' assert dtype(byteorder + 'i8').byteorder == '='
def test_arange(self): from _numpypy import arange, array, dtype a = arange(3) assert (a == [0, 1, 2]).all() assert a.dtype is dtype(int) a = arange(3.0) assert (a == [0., 1., 2.]).all() assert a.dtype is dtype(float) a = arange(3, 7) assert (a == [3, 4, 5, 6]).all() assert a.dtype is dtype(int) a = arange(3, 7, 2) assert (a == [3, 5]).all() a = arange(3, dtype=float) assert (a == [0., 1., 2.]).all() assert a.dtype is dtype(float) a = arange(0, 0.8, 0.1) assert len(a) == 8 assert arange(False, True, True).dtype is dtype(int)
def test_comparison(self): import operator from _numpypy import array, dtype a = array(range(5)) b = array(range(5), float) for func in [ operator.eq, operator.ne, operator.lt, operator.le, operator.gt, operator.ge ]: c = func(a, 3) assert c.dtype is dtype(bool) for i in xrange(5): assert c[i] == func(a[i], 3) c = func(b, 3) assert c.dtype is dtype(bool) for i in xrange(5): assert c[i] == func(b[i], 3)
def test_identity(self): from _numpypy import identity, array from _numpypy import int32, float64, dtype a = identity(0) assert len(a) == 0 assert a.dtype == dtype('float64') assert a.shape == (0, 0) b = identity(1, dtype=int32) assert len(b) == 1 assert b[0][0] == 1 assert b.shape == (1, 1) assert b.dtype == dtype('int32') c = identity(2) assert c.shape == (2, 2) assert (c == [[1, 0], [0, 1]]).all() d = identity(3, dtype='int32') assert d.shape == (3, 3) assert d.dtype == dtype('int32') assert (d == [[1, 0, 0], [0, 1, 0], [0, 0, 1]]).all()
def test_overflow(self): from _numpypy import array, dtype assert array([128], 'b')[0] == -128 assert array([256], 'B')[0] == 0 assert array([32768], 'h')[0] == -32768 assert array([65536], 'H')[0] == 0 if dtype('l').itemsize == 4: # 32-bit raises(OverflowError, "array([2**32/2], 'i')") raises(OverflowError, "array([2**32], 'I')") raises(OverflowError, "array([2**64/2], 'q')") raises(OverflowError, "array([2**64], 'Q')")
def test_binop_types(self): from _numpypy import array, dtype tests = [('b','B','h'), ('b','h','h'), ('b','H','i'), ('b','i','i'), ('b','l','l'), ('b','q','q'), ('b','Q','d'), ('B','h','h'), ('B','H','H'), ('B','i','i'), ('B','I','I'), ('B','l','l'), ('B','L','L'), ('B','q','q'), ('B','Q','Q'), ('h','H','i'), ('h','i','i'), ('h','l','l'), ('h','q','q'), ('h','Q','d'), ('H','i','i'), ('H','I','I'), ('H','l','l'), ('H','L','L'), ('H','q','q'), ('H','Q','Q'), ('i','l','l'), ('i','q','q'), ('i','Q','d'), ('I','L','L'), ('I','q','q'), ('I','Q','Q'), ('q','Q','d'), ('b','f','f'), ('B','f','f'), ('h','f','f'), ('H','f','f'), ('i','f','d'), ('I','f','d'), ('l','f','d'), ('L','f','d'), ('q','f','d'), ('Q','f','d'), ('q','d','d')] if dtype('i').itemsize == dtype('l').itemsize: # 32-bit tests.extend([('b','I','q'), ('b','L','q'), ('h','I','q'), ('h','L','q'), ('i','I','q'), ('i','L','q')]) else: tests.extend([('b','I','l'), ('b','L','d'), ('h','I','l'), ('h','L','d'), ('i','I','l'), ('i','L','d')]) for d1, d2, dout in tests: assert (array([1], d1) + array([1], d2)).dtype is dtype(dout)
def test_dtype(self): from _numpypy import dtype d = dtype('?') assert d.num == 0 assert d.kind == 'b' assert dtype('int8').num == 1 assert dtype(d) is d assert dtype(None) is dtype(float) assert dtype('int8').name == 'int8' assert dtype(int).fields is None assert dtype(int).names is None raises(TypeError, dtype, 1042) raises(KeyError, 'dtype(int)["asdasd"]')
def test_int_(self): import _numpypy as numpy assert numpy.int_ is numpy.dtype(int).type assert numpy.int_.mro() == [ numpy.int_, numpy.signedinteger, numpy.integer, numpy.number, numpy.generic, int, object, ]
def test_mul(self): import _numpypy a = _numpypy.array(range(5)) b = a * a for i in range(5): assert b[i] == i * i a = _numpypy.array(range(5), dtype=bool) b = a * a assert b.dtype is _numpypy.dtype(bool) assert b[0] is _numpypy.False_ for i in range(1, 5): assert b[i] is _numpypy.True_
def test_float64(self): import _numpypy as numpy assert numpy.float64.mro() == [numpy.float64, numpy.floating, numpy.inexact, numpy.number, numpy.generic, float, object] a = numpy.array([1, 2, 3], numpy.float64) assert type(a[1]) is numpy.float64 assert numpy.dtype(float).type is numpy.float64 assert "{:3f}".format(numpy.float64(3)) == "3.000000" assert numpy.float64(2.0) == 2.0 assert numpy.float64('23.4') == numpy.float64(23.4) raises(ValueError, numpy.float64, '23.2df')
def test_uint64(self): import sys import _numpypy as numpy assert numpy.uint64.mro() == [numpy.uint64, numpy.unsignedinteger, numpy.integer, numpy.number, numpy.generic, object] assert numpy.dtype(numpy.uint64).type is numpy.uint64 skip("see comment") # These tests pass "by chance" on numpy, things that are larger than # platform long (i.e. a python int), don't get put in a normal box, # instead they become an object array containing a long, we don't have # yet, so these can't pass. assert numpy.uint64(9223372036854775808) == 9223372036854775808 assert numpy.uint64(18446744073709551615) == 18446744073709551615 raises(OverflowError, numpy.uint64(18446744073709551616))
def test_dtype(self): from _numpypy import dtype d = dtype('?') assert d.num == 0 assert d.kind == 'b' assert dtype('int8').num == 1 assert dtype(d) is d assert dtype(None) is dtype(float) assert dtype('int8').name == 'int8' raises(TypeError, dtype, 1042)
def test_int64(self): import sys import _numpypy as numpy if sys.maxint == 2 ** 63 -1: assert numpy.int64.mro() == [numpy.int64, numpy.signedinteger, numpy.integer, numpy.number, numpy.generic, int, object] else: assert numpy.int64.mro() == [numpy.int64, numpy.signedinteger, numpy.integer, numpy.number, numpy.generic, object] assert numpy.dtype(numpy.int64).type is numpy.int64 assert numpy.int64(3) == 3 assert numpy.int64(9223372036854775807) == 9223372036854775807 assert numpy.int64(9223372036854775807) == 9223372036854775807 raises(OverflowError, numpy.int64, 9223372036854775808) raises(OverflowError, numpy.int64, 9223372036854775808L)
def test_float64(self): import _numpypy as numpy assert numpy.float64.mro() == [ numpy.float64, numpy.floating, numpy.inexact, numpy.number, numpy.generic, float, object ] a = numpy.array([1, 2, 3], numpy.float64) assert type(a[1]) is numpy.float64 assert numpy.dtype(float).type is numpy.float64 assert "{:3f}".format(numpy.float64(3)) == "3.000000" assert numpy.float64(2.0) == 2.0 assert numpy.float64('23.4') == numpy.float64(23.4) raises(ValueError, numpy.float64, '23.2df')
def test_uint64(self): import sys import _numpypy as numpy assert numpy.uint64.mro() == [ numpy.uint64, numpy.unsignedinteger, numpy.integer, numpy.number, numpy.generic, object ] assert numpy.dtype(numpy.uint64).type is numpy.uint64 skip("see comment") # These tests pass "by chance" on numpy, things that are larger than # platform long (i.e. a python int), don't get put in a normal box, # instead they become an object array containing a long, we don't have # yet, so these can't pass. assert numpy.uint64(9223372036854775808) == 9223372036854775808 assert numpy.uint64(18446744073709551615) == 18446744073709551615 raises(OverflowError, numpy.uint64(18446744073709551616))
def test_sign(self): from _numpypy import array, sign, dtype reference = [-1.0, 0.0, 0.0, 1.0] a = array([-5.0, -0.0, 0.0, 6.0]) b = sign(a) for i in range(4): assert b[i] == reference[i] a = sign(array(range(-5, 5))) ref = [-1, -1, -1, -1, -1, 0, 1, 1, 1, 1] for i in range(10): assert a[i] == ref[i] a = sign(array([True, False], dtype=bool)) assert a.dtype == dtype("int8") assert a[0] == 1 assert a[1] == 0
def test_int64(self): import sys import _numpypy as numpy if sys.maxint == 2**63 - 1: assert numpy.int64.mro() == [ numpy.int64, numpy.signedinteger, numpy.integer, numpy.number, numpy.generic, int, object ] else: assert numpy.int64.mro() == [ numpy.int64, numpy.signedinteger, numpy.integer, numpy.number, numpy.generic, object ] assert numpy.dtype(numpy.int64).type is numpy.int64 assert numpy.int64(3) == 3 assert numpy.int64(9223372036854775807) == 9223372036854775807 assert numpy.int64(9223372036854775807) == 9223372036854775807 raises(OverflowError, numpy.int64, 9223372036854775808) raises(OverflowError, numpy.int64, 9223372036854775808L)
def test_int8(self): import _numpypy as numpy assert numpy.int8.mro() == [ numpy.int8, numpy.signedinteger, numpy.integer, numpy.number, numpy.generic, object ] a = numpy.array([1, 2, 3], numpy.int8) assert type(a[1]) is numpy.int8 assert numpy.dtype("int8").type is numpy.int8 x = numpy.int8(128) assert x == -128 assert x != 128 assert type(x) is numpy.int8 assert repr(x) == "-128" assert type(int(x)) is int assert int(x) == -128 assert numpy.int8('50') == numpy.int8(50) raises(ValueError, numpy.int8, '50.2') assert numpy.int8('127') == 127 assert numpy.int8('128') == -128
def test_create_from_dict(self): skip("not yet") from _numpypy import dtype d = dtype({ 'names': ['a', 'b', 'c'], })
def test_alignment(self): from _numpypy import dtype assert dtype('i4').alignment == 4
def test_array_dtype_attr(self): from _numpypy import array, dtype a = array(range(5), long) assert a.dtype is dtype(long)