def test_search_string_factors(self): # Test against regression of incorrect behavior related strings' search methods searching for substrings P = Product('AB', repeat=3) bad_item = ('AB', 'A', 'B') self.assertFalse(bad_item in P) with self.assertRaises(ValueError): P.index(bad_item) self.assertEqual(P.count(bad_item), 0)
def test_empty_product(self): startstops = (None, 0, -1, 5, -5) steps = (None, 2, -1) product = Product() reference = ((), ) for sliceargs in itertools.product(startstops, startstops, steps): index = slice(*sliceargs) sliceobj = product[index] expected = reference[index] with self.subTest(index=index): # Length matches. self.assertEqual(len(sliceobj), len(expected)) # If there's an item, check that it's retrieved as expected if len(expected) > 0: for i in (0, -1): self.assertEqual(sliceobj[i], expected[i]) # Boundaries at expected locations. for bad_i in (-len(expected) - 1, len(expected)): with self.subTest(pos=bad_i): with self.assertRaises(IndexError): sliceobj[bad_i]
def test_Reversed(self): empty, single, A0, B3, ABC3 = self._testSubjects[:5] params = (((), {}), (((1, ), (2, ), (3, ), (4, )), {}), (("ABC", range(3), (False, True)), {}), (((0, 1), ), { 'repeat': 3 })) for args, kwargs in params: with self.subTest(args=args, kwargs=kwargs): instance = Reversed(Product(*args, **kwargs)) reference = tuple(itertools.product(*args, **kwargs))[::-1] # Length matches. self.assertEqual(len(instance), len(reference)) # Items match, if any for i in range(-len(reference), len(reference)): with self.subTest(pos=i): self.assertEqual(instance[i], reference[i]) # Boundaries match. for bad_i in (-len(reference) - 1, len(reference)): with self.subTest(pos=bad_i): with self.assertRaises(IndexError): instance[bad_i] # Iteration produces same items. for (i, (got, expected)) in enumerate(zip(instance, reference)): with self.subTest(pos=i): self.assertEqual(got, expected)
def setUpClass(cls): empty = Product() singles = Product((1, ), (2, ), (3, ), (4, )) A0 = Product("ABC", (0, 1)) B3 = Product(range(2), repeat=3) ABC3 = Product("ABC", repeat=3) huge = Product(range(10**6), repeat=10) cls._testSubjects = (empty, singles, A0, B3, ABC3, huge)
def test_iter(self): params = (((), {}), (((1, ), (2, ), (3, ), (4, )), {}), (("ABC", range(3), (False, True)), {}), (((0, 1), ), { 'repeat': 3 })) sentinel = object() for args, kwargs in params: with self.subTest(args=args, kwargs=kwargs): instance = Product(*args, **kwargs) reference = tuple(itertools.product(*args, **kwargs)) with self.subTest('forward'): for got, expected in itertools.zip_longest( instance, reference, fillvalue=sentinel): self.assertEqual(got, expected) with self.subTest('reverse'): for got, expected in itertools.zip_longest( reversed(instance), reversed(reference), fillvalue=sentinel): self.assertEqual(got, expected)
def test_slicing(self): """ Test that Product instances produce slice objects that behave correctly. """ factor_sets = (("ABC", "ABC", "ABC"), (range(3), range(4), range(3)), ("ABCD", (False, True), range(11))) startstops = (None, 0, -1, 3, -3, 6, -6, 20, -20, 99, -99) steps = (None, 1, -1, 3, -3, 99, -99) for factors in factor_sets: product = Product(*factors) reference = tuple(itertools.product(*factors)) for sliceargs in itertools.product(startstops, startstops, steps): index = slice(*sliceargs) sliceobj = product[index] expected = reference[index] with self.subTest(factors=factors, index=index): # Length matches. self.assertEqual(len(sliceobj), len(expected)) # Items equal at in-bounds indices. for i in range(-len(expected), len(expected)): with self.subTest(pos=i): self.assertEqual(sliceobj[i], expected[i]) # Boundaries at expected locations. for bad_i in (-len(expected) - 1, len(expected)): with self.subTest(pos=bad_i): with self.assertRaises(IndexError): sliceobj[bad_i] # Iteration produces expected items. for (i, (x, y)) in enumerate(zip(sliceobj, expected)): with self.subTest(pos=i): self.assertEqual(x, y)
def test_search(self): params = ((), ((0, ), (1, ), (2, )), ((0, 1, 2), (0, 1, 2), (0, 1, 2)), ((0, 1, 0, 1), (0, 0, 0), (1, 2, 2))) items = ((0, 0, 0), (0, 1, 2), (1, 0, 1)) for seqs in params: with self.subTest(seqs=seqs): instance = Product(*seqs) reference = tuple(itertools.product(*seqs)) for item in items: with self.subTest(item=item): self.assertEqual(item in instance, item in reference) if item in reference: self.assertEqual(instance.index(item), reference.index(item)) else: with self.assertRaises(ValueError): instance.index(item) self.assertEqual(instance.count(item), reference.count(item))
def test_len_huge(self): # Coverage: Reversed.len (`try` branch) from combinatorics import Product huge = Product(range(10**6), repeat=10) r_huge = Reversed(huge) self.assertEqual(r_huge.len(), 10**60)