def test_generation_words(self): """ Tests the generation of word """ ter_a = Terminal("a") ter_b = Terminal("b") var_s = Variable("S") productions = { Production(var_s, [ter_a, var_s, ter_b]), Production(var_s, []) } cfg = CFG(productions=productions, start_symbol=var_s) words0 = list(cfg.get_words(max_length=0)) self.assertIn([], words0) self.assertEqual(len(words0), 1) words1 = list(cfg.get_words(max_length=1)) self.assertIn([], words1) self.assertEqual(len(words1), 1) words2 = list(cfg.get_words(max_length=2)) self.assertIn([], words2) self.assertIn([ter_a, ter_b], words2) self.assertEqual(len(words2), 2) words3 = list(cfg.get_words(max_length=3)) self.assertIn([], words3) self.assertIn([ter_a, ter_b], words3) self.assertEqual(len(words3), 2) words4 = list(cfg.get_words(max_length=4)) self.assertIn([], words4) self.assertIn([ter_a, ter_a, ter_b, ter_b], words4) self.assertEqual(len(words4), 3)
def test_generation_words2(self): """ Tests the generation of word """ ter_a = Terminal("a") var_s = Variable("S") var_s1 = Variable("S1") var_s2 = Variable("S2") productions = { Production(var_s, [var_s1, ter_a]), Production(var_s1, [var_s2, ter_a]), Production(var_s1, []), Production(var_s2, []), Production(var_s, []) } cfg = CFG(productions=productions, start_symbol=var_s) words0 = list(cfg.get_words()) self.assertIn([], words0) self.assertIn([ter_a], words0) self.assertIn([ter_a, ter_a], words0) self.assertEqual(len(words0), 3)