def test_shuffle_key(self): cases = [ (lambda x: x, ["a", "d", "b", "c"]), (lambda x: x.upper(), ["d", "c", "a", "b"]), ] for num, (key, expected) in enumerate(cases, start=1): with self.subTest(num=num): shuffler = Shuffler(seed=1234) actual = shuffler.shuffle(["a", "b", "c", "d"], key) self.assertEqual(actual, expected)
def test_shuffle_key(self): cases = [ (lambda x: x, ['a', 'd', 'b', 'c']), (lambda x: x.upper(), ['d', 'c', 'a', 'b']), ] for num, (key, expected) in enumerate(cases, start=1): with self.subTest(num=num): shuffler = Shuffler(seed=1234) actual = shuffler.shuffle(['a', 'b', 'c', 'd'], key) self.assertEqual(actual, expected)
def test_shuffle_consistency(self): seq = [str(n) for n in range(5)] cases = [ (None, ["3", "0", "2", "4", "1"]), (0, ["3", "2", "4", "1"]), (1, ["3", "0", "2", "4"]), (2, ["3", "0", "4", "1"]), (3, ["0", "2", "4", "1"]), (4, ["3", "0", "2", "1"]), ] shuffler = Shuffler(seed=1234) for index, expected in cases: with self.subTest(index=index): if index is None: new_seq = seq else: new_seq = seq.copy() del new_seq[index] actual = shuffler.shuffle(new_seq, lambda x: x) self.assertEqual(actual, expected)
def test_shuffle_same_hash(self): shuffler = Shuffler(seed=1234) msg = "item 'A' has same hash 'a56ce89262959e151ee2266552f1819c' as item 'a'" with self.assertRaisesMessage(RuntimeError, msg): shuffler.shuffle(["a", "b", "A"], lambda x: x.upper())