def test_shuffle(self): # Test lists, arrays (of various dtypes), and multidimensional versions # of both, c-contiguous or not: for conv in [ lambda x: np.array([]), lambda x: x, lambda x: np.asarray(x).astype(np.int8), lambda x: np.asarray(x).astype(np.float32), lambda x: np.asarray(x).astype(np.complex64), lambda x: np.asarray(x).astype(object), lambda x: [(i, i) for i in x], lambda x: np.asarray([[i, i] for i in x]), lambda x: np.vstack([x, x]).T, # gh-4270 lambda x: np.asarray([(i, i) for i in x], [("a", object, (1, )), ("b", np.int32, (1, ))]) ]: rnd.seed(self.seed, self.brng) alist = conv([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) rnd.shuffle(alist) actual = alist desired = conv([9, 8, 5, 1, 6, 4, 7, 2, 3, 0]) np.testing.assert_array_equal(actual, desired)
def test_shuffle_mixed_dimension(self): # Test for trac ticket #2074 for t in [[1, 2, 3, None], [(1, 1), (2, 2), (3, 3), None], [1, (2, 2), (3, 3), None], [(1, 1), 2, 3, None]]: rnd.seed(12345, brng='MT2203') shuffled = list(t) rnd.shuffle(shuffled) assert_array_equal(shuffled, [t[0], t[2], t[1], t[3]])
def test_shuffle_of_array_of_objects(self): # Test that permuting an array of objects will not cause # a segfault on garbage collection. # See gh-7719 rnd.seed(1234) a = np.array([np.arange(1), np.arange(4)]) for _ in range(1000): rnd.shuffle(a) # Force Garbage Collection - should not segfault. import gc gc.collect()
def test_shuffle_masked(self): # gh-3263 a = np.ma.masked_values(np.reshape(range(20), (5, 4)) % 3 - 1, -1) b = np.ma.masked_values(np.arange(20) % 3 - 1, -1) a_orig = a.copy() b_orig = b.copy() for i in range(50): rnd.shuffle(a) assert_equal(sorted(a.data[~a.mask]), sorted(a_orig.data[~a_orig.mask])) rnd.shuffle(b) assert_equal(sorted(b.data[~b.mask]), sorted(b_orig.data[~b_orig.mask]))
def test_shuffle_of_array_of_different_length_strings(self): # Test that permuting an array of different length strings # will not cause a segfault on garbage collection # Tests gh-7710 rnd.seed(1234) a = np.array(['a', 'a' * 1000]) for _ in range(100): rnd.shuffle(a) # Force Garbage Collection - should not segfault. import gc gc.collect()