Пример #1
0
def test_read_query():
    c = CFG()
    q = ['a', 'b', 'c', 'd']
    testdir = tempfile.gettempdir()
    with open(path.join(testdir, 'input.txt'), 'w') as f:
        f.write(' '.join(q))
    query = c.read_query_from_file(path.join(testdir, 'input.txt'))
    assert (q == query)
Пример #2
0
def test_parse_grammar():
    c = CFG()
    s = {'c', 'X', 'Z', 'S', 'b', 'a', 'Y'}
    c.parse_grammar(lines)
    assert (set(c.symb.keys()) == s)
    assert (len(c.nonterms) == 4)
    assert (len(c.terms) == 3)
    assert (len(c.rules) == 8)
Пример #3
0
def test_new_rule():
    c = CFG()
    rule = c.new_rule(c.new_nonterm("adsf"), "asdf")
    assert (rule.__name__ == "adsf")
    assert (rule.__qualname__ == "CFG.new_rule.<locals>.MyRule")
    assert (rule.rule[0][0].__name__ == "adsf")
    assert (rule.rule[0][0].__qualname__ == "CFG.new_nonterm.<locals>.Nonterm")
    assert (rule.rule[1] == "asdf")
Пример #4
0
def test_cyk_3():
    c = CFG()
    c.read_from_file(path.dirname(__file__) +
                     "/res/gram3.txt")
    assert (c.cyk("a b c".split()))
    assert (not c.cyk("a".split()))
    assert (c.cyk("a a b b c c c".split()))
    assert (c.cyk("a b b c c".split()))
    assert (not c.cyk("".split()))
    assert (c.cyk("a a b b c c".split()))
Пример #5
0
def test_term_to_string():
    c = CFG()
    nonterm = c.new_nonterm("zxcv")
    nonterm1 = c.new_nonterm("zxcvs")
    assert (c.term_to_string(EPS) == "eps")
    assert (c.term_to_string("asdf") == "asdf")
    assert (c.term_to_string(nonterm) == "T0")
    c.symb["T0"] = ""
    assert (c.term_to_string(nonterm1) == "T1")
Пример #6
0
def test_input():
    c = CFG()
    testdir = tempfile.gettempdir()
    test_keys = {'b', 'X', 'Z', 'c', 'Y', 'T0', 'a', 'S'}
    with open(path.join(testdir, 'input.txt'), 'w') as f:
        for i in cnf_lines:
            f.write(i + '\n')
    c.read_from_file(path.join(testdir, 'input.txt'))
    assert (len(c.grammar.rules) == 11)
    assert (c.symb.keys() == test_keys)
    assert (len(c.rules) == 12)
    assert (len(c.terms) == 3)
    assert (len(c.nonterms) == 5)
Пример #7
0
    def test_function_definition(self):
        sample_code = "def test_func():\n"
        sample_code += "    print('Test')\n"

        module = parse(sample_code)
        cfg = CFG(module.body)
        self.assertEqual(1, len(cfg.basic_blocks))
Пример #8
0
    def test_can_parse_multiple_basic_blocks(self):
        sample_code = "print('Test')\n"
        sample_code += "while True:\n"
        sample_code += "    x = 1\n"
        sample_code += "print('Test')\n"

        module = parse(sample_code)
        cfg = CFG(module.body)
        self.assertEqual(3, len(cfg.basic_blocks))
Пример #9
0
    def test_for_body_becomes_basic_block(self):
        sample_code = "print('Test')\n"
        sample_code += "for i in range(10):\n"
        sample_code += "    x = 1\n"
        sample_code += "print('Test')\n"

        module = parse(sample_code)
        cfg = CFG(module.body)
        self.assertIsInstance(cfg.basic_blocks[1].body[0], Assign)
Пример #10
0
 def test_if_elif_else(self):
     sample_code = "if True:\n"
     sample_code += "     print('Test')\n"
     sample_code += "elif True:\n"
     sample_code += "     x = 1\n"
     sample_code += "else:\n"
     sample_code += "     print('Test')\n"
     module = parse(sample_code)
     cfg = CFG(module.body)
     self.assertEqual(3, len(cfg.basic_blocks))
Пример #11
0
    def test_try_except_finally(self):
        sample_code = "try:\n"
        sample_code += "    print('Test')\n"
        sample_code += "except Exception:\n"
        sample_code += "    x = 1\n"
        sample_code += "finally:\n"
        sample_code += "    print('Test')\n"

        module = parse(sample_code)
        cfg = CFG(module.body)
        self.assertEqual(3, len(cfg.basic_blocks))
        self.assertIsInstance(cfg.basic_blocks[0].body[0], Call)
Пример #12
0
    def test_nested_blocks(self):
        sample_code = "print('Test')\n"
        sample_code += "while True:\n"
        sample_code += "    x = 1\n"
        sample_code += "    while True:\n"
        sample_code += "        print('Test')\n"
        sample_code += "print('Test')\n"

        module = parse(sample_code)
        cfg = CFG(module.body)
        self.assertEqual(4, len(cfg.basic_blocks))
        self.assertIsInstance(cfg.basic_blocks[1].body[0], Assign)
        self.assertIsInstance(cfg.basic_blocks[2].body[0], Call)
Пример #13
0
    def test_tracks_equivalence_classes(self):
        sample_code = "print('Test')\n"
        sample_code += "while True:\n"
        sample_code += "    x = 1\n"
        sample_code += "print('Test')\n"

        module = parse(sample_code)
        cfg = CFG(module.body)

        self.assertEqual(3, len(cfg.basic_blocks))
        self.assertEqual(1, cfg.equivalence_classes.count)
        for basic_block in cfg.basic_blocks:
            self.assertIn(basic_block.identifier, cfg.equivalence_classes)
Пример #14
0
    def test_if_elif_else_disconnected(self):
        sample_code = "if True:\n"
        sample_code += "    print('Test')\n"
        sample_code += "elif True:\n"
        sample_code += "    x = 1\n"
        sample_code += "else:\n"
        sample_code += "    print('Test')\n"

        module = parse(sample_code)
        cfg = CFG(module.body)

        self.assertEqual(3, len(cfg.basic_blocks))
        self.assertEqual(3, cfg.equivalence_classes.count)
        for basic_block in cfg.basic_blocks:
            self.assertIn(basic_block.identifier, cfg.equivalence_classes)
Пример #15
0
    def test_nested_blocks_connect_either_end(self):
        sample_code = "print('Test')\n"
        sample_code += "if True:\n"
        sample_code += "    print('Test')\n"
        sample_code += "elif True:\n"
        sample_code += "    x = 1\n"
        sample_code += "else:\n"
        sample_code += "    print('Test')\n"
        sample_code += "print('Test')\n"

        module = parse(sample_code)
        cfg = CFG(module.body)

        self.assertEqual(5, len(cfg.basic_blocks))
        self.assertEqual(1, cfg.equivalence_classes.count)
Пример #16
0
    def test_try_except_connected_to_finally(self):
        sample_code = "try:\n"
        sample_code += "    print('Test')\n"
        sample_code += "except Exception:\n"
        sample_code += "    x = 1\n"
        sample_code += "finally:\n"
        sample_code += "    print('Test')\n"

        module = parse(sample_code)
        cfg = CFG(module.body)

        self.assertEqual(3, len(cfg.basic_blocks))
        self.assertEqual(1, cfg.equivalence_classes.count)
        for basic_block in cfg.basic_blocks:
            self.assertIn(basic_block.identifier, cfg.equivalence_classes)
Пример #17
0
def test_output():
    c = CFG()
    testdir = tempfile.gettempdir()
    c.parse_grammar(lines)
    c.print_cnf(path.join(testdir, 'output.txt'))

    with open(path.join(testdir, 'output.txt')) as f:
        lines1 = map(lambda x: re.sub("\n", "", x), f.readlines())

    assert (set(lines1) == set(cnf_lines))
Пример #18
0
def test_new_term_name():
    c = CFG()
    c.symb = {'a': None, 'b': None}
    assert (c.new_term_name() not in c.symb)
    c.symb = {'T0': None, 'T1': None, 'T2': None}
    assert (c.new_term_name() not in c.symb)
Пример #19
0
def test_rule_right_part():
    c = CFG()
    assert(c.rule_right_part("asdf") == "asdf")
    assert(c.rule_right_part("ASDF").__name__ == "ASDF")
    assert(c.rule_right_part("eps") == EPS)
Пример #20
0
def test_new_nonterm():
    c = CFG()
    assert (c.new_nonterm("asdf").__name__ == "asdf")
    assert (c.new_nonterm("adsf").__qualname__
            ==
            "CFG.new_nonterm.<locals>.Nonterm")