diff options
author | Pauli Virtanen <pav@iki.fi> | 2010-10-30 00:43:29 +0200 |
---|---|---|
committer | Pauli Virtanen <pav@iki.fi> | 2010-10-31 18:29:21 +0100 |
commit | a32caf5d6db20c42e12ed17eb0f9ca835c25f9b0 (patch) | |
tree | dc1a6c9cbbbcf3dbb7b7e2b4cae9394c5607ecf2 | |
parent | b12429704242314703797ed2076d3036d19327eb (diff) | |
download | numpy-a32caf5d6db20c42e12ed17eb0f9ca835c25f9b0.tar.gz |
TST: core: refactor test_dtype slightly
-rw-r--r-- | numpy/core/tests/test_dtype.py | 100 |
1 files changed, 53 insertions, 47 deletions
diff --git a/numpy/core/tests/test_dtype.py b/numpy/core/tests/test_dtype.py index 74fee2402..40c3a3eea 100644 --- a/numpy/core/tests/test_dtype.py +++ b/numpy/core/tests/test_dtype.py @@ -1,6 +1,16 @@ import numpy as np from numpy.testing import * +def assert_dtype_equal(a, b): + assert_equal(a, b) + assert_equal(hash(a), hash(b), + "two equivalent types do not hash to the same value !") + +def assert_dtype_not_equal(a, b): + assert_(a != b) + assert_(hash(a) != hash(b), + "two different types hash to the same value !") + class TestBuiltin(TestCase): def test_run(self): """Only test hash runs at all.""" @@ -18,24 +28,23 @@ class TestBuiltin(TestCase): dt3 = dt.newbyteorder(">") if dt == dt2: self.assertTrue(dt.byteorder != dt2.byteorder, "bogus test") - self.assertTrue(hash(dt) == hash(dt2), "equivalent bytorders do not hash the same") + assert_dtype_equal(dt, dt2) else: self.assertTrue(dt.byteorder != dt3.byteorder, "bogus test") - self.assertTrue(hash(dt) == hash(dt3), "equivalent bytorders do not hash the same") + assert_dtype_equal(dt, dt3) + class TestRecord(TestCase): def test_equivalent_record(self): """Test whether equivalent record dtypes hash the same.""" a = np.dtype([('yo', np.int)]) b = np.dtype([('yo', np.int)]) - self.assertTrue(hash(a) == hash(b), - "two equivalent types do not hash to the same value !") + assert_dtype_equal(a, b) def test_different_names(self): # In theory, they may hash the same (collision) ? a = np.dtype([('yo', np.int)]) b = np.dtype([('ye', np.int)]) - self.assertTrue(hash(a) != hash(b), - "%s and %s hash the same !" % (a, b)) + assert_dtype_not_equal(a, b) def test_different_titles(self): # In theory, they may hash the same (collision) ? @@ -43,8 +52,7 @@ class TestRecord(TestCase): 'titles': ['Red pixel', 'Blue pixel']}) b = np.dtype({'names': ['r','b'], 'formats': ['u1', 'u1'], 'titles': ['RRed pixel', 'Blue pixel']}) - self.assertTrue(hash(a) != hash(b), - "%s and %s hash the same !" % (a, b)) + assert_dtype_not_equal(a, b) def test_not_lists(self): """Test if an appropriate exception is raised when passing bad values to @@ -55,39 +63,11 @@ class TestRecord(TestCase): self.assertRaises(TypeError, np.dtype, dict(names=['A', 'B'], formats=set(['f8', 'i4']))) -class TestShape(TestCase): - def test_equal(self): - """Test some data types that are equal""" - self.assertEqual(np.dtype('f8'), np.dtype(('f8',tuple()))) - self.assertEqual(np.dtype('f8'), np.dtype(('f8',1))) - self.assertEqual(np.dtype((np.int,2)), np.dtype((np.int,(2,)))) - self.assertEqual(np.dtype(('<f4',(3,2))), np.dtype(('<f4',(3,2)))) - d = ([('a','f4',(1,2)),('b','f8',(3,1))],(3,2)) - self.assertEqual(np.dtype(d), np.dtype(d)) - - def test_simple(self): - """Test some simple cases that shouldn't be equal""" - self.assertNotEqual(np.dtype('f8'), np.dtype(('f8',(1,)))) - self.assertNotEqual(np.dtype(('f8',(1,))), np.dtype(('f8',(1,1)))) - self.assertNotEqual(np.dtype(('f4',(3,2))), np.dtype(('f4',(2,3)))) - - def test_monster(self): - """Test some more complicated cases that shouldn't be equal""" - self.assertNotEqual(np.dtype(([('a','f4',(2,1)), ('b','f8',(1,3))],(2,2))), - np.dtype(([('a','f4',(1,2)), ('b','f8',(1,3))],(2,2)))) - self.assertNotEqual(np.dtype(([('a','f4',(2,1)), ('b','f8',(1,3))],(2,2))), - np.dtype(([('a','f4',(2,1)), ('b','i8',(1,3))],(2,2)))) - self.assertNotEqual(np.dtype(([('a','f4',(2,1)), ('b','f8',(1,3))],(2,2))), - np.dtype(([('e','f8',(1,3)), ('d','f4',(2,1))],(2,2)))) - self.assertNotEqual(np.dtype(([('a',[('a','i4',6)],(2,1)), ('b','f8',(1,3))],(2,2))), - np.dtype(([('a',[('a','u4',6)],(2,1)), ('b','f8',(1,3))],(2,2)))) - class TestSubarray(TestCase): def test_single_subarray(self): a = np.dtype((np.int, (2))) b = np.dtype((np.int, (2,))) - self.assertTrue(hash(a) == hash(b), - "two equivalent types do not hash to the same value !") + assert_dtype_equal(a, b) assert_equal(type(a.subdtype[1]), tuple) assert_equal(type(b.subdtype[1]), tuple) @@ -96,25 +76,51 @@ class TestSubarray(TestCase): """Test whether equivalent subarray dtypes hash the same.""" a = np.dtype((np.int, (2, 3))) b = np.dtype((np.int, (2, 3))) - self.assertTrue(hash(a) == hash(b), - "two equivalent types do not hash to the same value !") + assert_dtype_equal(a, b) def test_nonequivalent_record(self): """Test whether different subarray dtypes hash differently.""" a = np.dtype((np.int, (2, 3))) b = np.dtype((np.int, (3, 2))) - self.assertTrue(hash(a) != hash(b), - "%s and %s hash the same !" % (a, b)) + assert_dtype_not_equal(a, b) a = np.dtype((np.int, (2, 3))) b = np.dtype((np.int, (2, 2))) - self.assertTrue(hash(a) != hash(b), - "%s and %s hash the same !" % (a, b)) + assert_dtype_not_equal(a, b) a = np.dtype((np.int, (1, 2, 3))) b = np.dtype((np.int, (1, 2))) - self.assertTrue(hash(a) != hash(b), - "%s and %s hash the same !" % (a, b)) + assert_dtype_not_equal(a, b) + + def test_shape_equal(self): + """Test some data types that are equal""" + assert_dtype_equal(np.dtype('f8'), np.dtype(('f8',tuple()))) + assert_dtype_equal(np.dtype('f8'), np.dtype(('f8',1))) + assert_dtype_equal(np.dtype((np.int,2)), np.dtype((np.int,(2,)))) + assert_dtype_equal(np.dtype(('<f4',(3,2))), np.dtype(('<f4',(3,2)))) + d = ([('a','f4',(1,2)),('b','f8',(3,1))],(3,2)) + assert_dtype_equal(np.dtype(d), np.dtype(d)) + + def test_shape_simple(self): + """Test some simple cases that shouldn't be equal""" + assert_dtype_not_equal(np.dtype('f8'), np.dtype(('f8',(1,)))) + assert_dtype_not_equal(np.dtype(('f8',(1,))), np.dtype(('f8',(1,1)))) + assert_dtype_not_equal(np.dtype(('f4',(3,2))), np.dtype(('f4',(2,3)))) + + def test_shape_monster(self): + """Test some more complicated cases that shouldn't be equal""" + assert_dtype_not_equal( + np.dtype(([('a','f4',(2,1)), ('b','f8',(1,3))],(2,2))), + np.dtype(([('a','f4',(1,2)), ('b','f8',(1,3))],(2,2)))) + assert_dtype_not_equal( + np.dtype(([('a','f4',(2,1)), ('b','f8',(1,3))],(2,2))), + np.dtype(([('a','f4',(2,1)), ('b','i8',(1,3))],(2,2)))) + assert_dtype_not_equal( + np.dtype(([('a','f4',(2,1)), ('b','f8',(1,3))],(2,2))), + np.dtype(([('e','f8',(1,3)), ('d','f4',(2,1))],(2,2)))) + assert_dtype_not_equal( + np.dtype(([('a',[('a','i4',6)],(2,1)), ('b','f8',(1,3))],(2,2))), + np.dtype(([('a',[('a','u4',6)],(2,1)), ('b','f8',(1,3))],(2,2)))) class TestMonsterType(TestCase): """Test deeply nested subtypes.""" @@ -125,13 +131,13 @@ class TestMonsterType(TestCase): ('yi', np.dtype((np.int, (3, 2))))]) b = np.dtype([('yo', np.int), ('ye', simple1), ('yi', np.dtype((np.int, (3, 2))))]) - self.assertTrue(hash(a) == hash(b)) + assert_dtype_equal(a, b) c = np.dtype([('yo', np.int), ('ye', simple1), ('yi', np.dtype((a, (3, 2))))]) d = np.dtype([('yo', np.int), ('ye', simple1), ('yi', np.dtype((a, (3, 2))))]) - self.assertTrue(hash(c) == hash(d)) + assert_dtype_equal(c, d) class TestMetadata(TestCase): def test_no_metadata(self): |