def test_conditional(self): """Test with conditions.""" s = "helloworld" r = 5 allowed_prefixes = {"o", "ol", "old", "oldh", "oldhl", "oldhlr"} all_perms = set("".join(p) for p in permutations(s, r)) cond_perms = set(conditional_perms(s, r, lambda x: x in allowed_prefixes)) for p in all_perms: if p in cond_perms: self.assertTrue(p[:-1] in allowed_prefixes) else: self.assertFalse(any(p[:-1] == pre for pre in allowed_prefixes))
def test_0_len_perms(self): """Test that there should be only 1 0-length permutation.""" self.assertListEqual(list(conditional_perms("helloworld", 0)), [""])
def _test_unconditional(self, s, r): perms1 = set("".join(p) for p in permutations(s, r)) perms2 = set(conditional_perms(s, r)) self.assertSetEqual(perms1, perms2)