Skip to content

Commit

Permalink
TST: Add basic tests for np.array
Browse files Browse the repository at this point in the history
  • Loading branch information
m-d-w committed Jun 6, 2013
1 parent b5fd429 commit 16c55ba
Showing 1 changed file with 149 additions and 1 deletion.
150 changes: 149 additions & 1 deletion numpy/core/tests/test_api.py
Expand Up @@ -9,7 +9,155 @@
from numpy.compat import sixu

# Switch between new behaviour when NPY_RELAXED_STRIDES_CHECKING is set.
NPY_RELAXED_STRIDES_CHECKING = np.ones((10,1), order='C').flags.f_contiguous
NPY_RELAXED_STRIDES_CHECKING = np.ones((10, 1), order='C').flags.f_contiguous


def test_array_array():
obj = object()
tobj = type(object)
ones11 = np.ones((1, 1), np.float64)
tndarray = type(ones11)
# Test is_ndarary
assert_equal(np.array(ones11, dtype=np.float64), ones11)
old_refcount = sys.getrefcount(tndarray)
np.array(ones11)
assert_equal(old_refcount, sys.getrefcount(tndarray))

# test None
assert_equal(np.array(None, dtype=np.float64),
np.array(np.nan, dtype=np.float64))
old_refcount = sys.getrefcount(tobj)
np.array(None, dtype=np.float64)
assert_equal(old_refcount, sys.getrefcount(tobj))

# test scalar
assert_equal(np.array(1.0, dtype=np.float64),
np.ones((), dtype=np.float64))
old_refcount = sys.getrefcount(np.float64)
np.array(np.array(1.0, dtype=np.float64), dtype=np.float64)
assert_equal(old_refcount, sys.getrefcount(np.float64))

# test string
S2 = np.dtype((str, 2))
S3 = np.dtype((str, 3))
S5 = np.dtype((str, 5))
assert_equal(np.array("1.0", dtype=np.float64),
np.ones((), dtype=np.float64))
assert_equal(np.array("1.0").dtype, S3)
assert_equal(np.array("1.0", dtype=str).dtype, S3)
assert_equal(np.array("1.0", dtype=S2), np.array("1."))
assert_equal(np.array("1", dtype=S5), np.ones((), dtype=S5))

# test unicode
_unicode = globals().get("unicode")
if _unicode:
U2 = np.dtype((_unicode, 2))
U3 = np.dtype((_unicode, 3))
U5 = np.dtype((_unicode, 5))
assert_equal(np.array(_unicode("1.0"), dtype=np.float64),
np.ones((), dtype=np.float64))
assert_equal(np.array(_unicode("1.0")).dtype, U3)
assert_equal(np.array(_unicode("1.0"), dtype=_unicode).dtype, U3)
assert_equal(np.array(_unicode("1.0"), dtype=U2),
np.array(_unicode("1.")))
assert_equal(np.array(_unicode("1"), dtype=U5),
np.ones((), dtype=U5))

builtins = getattr(__builtins__, '__dict__', __builtins__)
assert_(isinstance(builtins, dict))

# test buffer
_buffer = builtins.get("buffer")
if _buffer:
assert_equal(np.array(_buffer("1.0"), dtype=np.float64),
np.array(1.0, dtype=np.float64))
assert_equal(np.array(_buffer("1.0"), dtype=np.float64).dtype,
np.dtype("float64"))
assert_equal(np.array([_buffer("1.0")], dtype=np.float64),
np.array([1.0], dtype=np.float64))

# test memoryview, new version of buffer
_memoryview = builtins.get("memoryview")
if _memoryview:
assert_equal(np.array(_memoryview(bytearray(b'1.0')),
dtype=np.float64),
np.array([49.0, 46.0, 48.0],
dtype=np.float64))
assert_equal(np.array(_memoryview(bytearray(b'1.0')),
dtype=np.float64).dtype,
np.dtype("float64"))
assert_equal(np.array(_memoryview(bytearray(b'1.0'))).dtype,
np.dtype('uint8'))

# test array interface
a = np.array(100.0, dtype=np.float64)
o = type("o", (object,),
dict(__array_interface__=a.__array_interface__))
assert_equal(np.array(o, dtype=np.float64), a)

# test array_struct interface
a = np.array([(1, 4.0, 'Hello'), (2, 6.0, 'World')],
dtype=[('f0', int), ('f1', float), ('f2', str)])
o = type("o", (object,),
dict(__array_struct__=a.__array_struct__))
## wasn't what I expected... is np.array(o) supposed to equal a ?
## instead we get a array([...], dtype=">V18")
assert_equal(str(np.array(o).data), str(a.data))

# test array
o = type("o", (object,),
dict(__array__=lambda *x: np.array(100.0, dtype=np.float64)))()
assert_equal(np.array(o, dtype=np.float64), np.array(100.0, np.float64))

# test recursion
nested = 1.5
for i in range(np.MAXDIMS):
nested = [nested]

# no error
np.array(nested)

# Exceeds recursion limit
assert_raises(ValueError, np.array, [nested], dtype=np.float64)

# Try with lists...
assert_equal(np.array([None] * 10, dtype=np.float64),
np.empty((10,), dtype=np.float64) + np.nan)
assert_equal(np.array([[None]] * 10, dtype=np.float64),
np.empty((10, 1), dtype=np.float64) + np.nan)
assert_equal(np.array([[None] * 10], dtype=np.float64),
np.empty((1, 10), dtype=np.float64) + np.nan)
assert_equal(np.array([[None] * 10] * 10, dtype=np.float64),
np.empty((10, 10), dtype=np.float64) + np.nan)

assert_equal(np.array([1.0] * 10, dtype=np.float64),
np.ones((10,), dtype=np.float64))
assert_equal(np.array([[1.0]] * 10, dtype=np.float64),
np.ones((10, 1), dtype=np.float64))
assert_equal(np.array([[1.0] * 10], dtype=np.float64),
np.ones((1, 10), dtype=np.float64))
assert_equal(np.array([[1.0] * 10] * 10, dtype=np.float64),
np.ones((10, 10), dtype=np.float64))

# Try with tuples
assert_equal(np.array((None,) * 10, dtype=np.float64),
np.empty((10,), dtype=np.float64) + np.nan)
assert_equal(np.array([(None,)] * 10, dtype=np.float64),
np.empty((10, 1), dtype=np.float64) + np.nan)
assert_equal(np.array([(None,) * 10], dtype=np.float64),
np.empty((1, 10), dtype=np.float64) + np.nan)
assert_equal(np.array([(None,) * 10] * 10, dtype=np.float64),
np.empty((10, 10), dtype=np.float64) + np.nan)

assert_equal(np.array((1.0,) * 10, dtype=np.float64),
np.ones((10,), dtype=np.float64))
assert_equal(np.array([(1.0,)] * 10, dtype=np.float64),
np.ones((10, 1), dtype=np.float64))
assert_equal(np.array([(1.0,) * 10], dtype=np.float64),
np.ones((1, 10), dtype=np.float64))
assert_equal(np.array([(1.0,) * 10] * 10, dtype=np.float64),
np.ones((10, 10), dtype=np.float64))


def test_fastCopyAndTranspose():
# 0D array
Expand Down

0 comments on commit 16c55ba

Please sign in to comment.