Commit ed5cc11a authored by Igor Dejanovic's avatar Igor Dejanovic

Some more tests.

parent d8509485
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)*-45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
45-4*(23+7.45-67*32.3)/67*45.56-129.67*(343-56+33.4/45)
# -*- coding: utf-8 -*-
#######################################################################
# Name: test_peg_parser
# Purpose: Test for parser constructed using PEG textual grammars.
# Author: Igor R. Dejanović <igor DOT dejanovic AT gmail DOT com>
# Copyright: (c) 2014 Igor R. Dejanović <igor DOT dejanovic AT gmail DOT com>
# License: MIT License
#######################################################################
from unittest import TestCase
from arpeggio import Sequence, NonTerminal
from arpeggio.peg import ParserPEG
grammar = '''
number <- r'\d*\.\d*|\d+';
factor <- ("+" / "-")?
(number / "(" expression ")");
term <- factor (( "*" / "/") factor)*;
expression <- term (("+" / "-") term)*;
calc <- expression+ EndOfFile;
'''
class TestPEGParser(TestCase):
def test_construct_parser(self):
parser = ParserPEG(grammar, 'calc')
self.assertEqual(parser.parser_model.rule ,'calc')
self.assertTrue(isinstance(parser.parser_model, Sequence))
self.assertEqual(parser.parser_model.nodes[0].name ,'OneOrMore')
def test_parse_input(self):
parser = ParserPEG(grammar, 'calc')
input = "4+5*7/3.45*-45*(2.56+32)/-56*(2-1.34)"
result = parser.parse(input)
self.assertTrue(isinstance(result, NonTerminal))
self.assertEqual(str(result), "[ [ [ [ 4 ] ], +, [ [ 5 ], *, [ 7 ], /, [ 3.45 ], *, [ -, 45 ], *, [ (, [ [ [ 2.56 ] ], +, [ [ 32 ] ] ], ) ], /, [ -, 56 ], *, [ (, [ [ [ 2 ] ], -, [ [ 1.34 ] ] ], ) ] ] ], EOF ]")
def test_reduce_tree(self):
parser = ParserPEG(grammar, 'calc', reduce_tree=True)
input = "4+5*7/3.45*-45*(2.56+32)/-56*(2-1.34)"
result = parser.parse(input)
self.assertTrue(isinstance(result, NonTerminal))
self.assertEqual(str(result), "[ [ 4, +, [ 5, *, 7, /, 3.45, *, [ -, 45 ], *, [ (, [ 2.56, +, 32 ], ) ], /, [ -, 56 ], *, [ (, [ 2, -, 1.34 ], ) ] ] ], EOF ]" )
# -*- coding: utf-8 -*-
#######################################################################
# Name: test_python_parser
# Purpose: Test for parser constructed using Python-based grammars.
# Author: Igor R. Dejanović <igor DOT dejanovic AT gmail DOT com>
# Copyright: (c) 2014 Igor R. Dejanović <igor DOT dejanovic AT gmail DOT com>
# License: MIT License
#######################################################################
from unittest import TestCase
# Grammar
from arpeggio import Optional, ZeroOrMore, OneOrMore, EndOfFile, ParserPython, Sequence, NonTerminal
from arpeggio import RegExMatch as _
def number(): return _(r'\d*\.\d*|\d+')
def factor(): return Optional(["+","-"]), [number,
("(", expression, ")")]
def term(): return factor, ZeroOrMore(["*","/"], factor)
def expression(): return term, ZeroOrMore(["+", "-"], term)
def calc(): return OneOrMore(expression), EndOfFile
class TestPythonParser(TestCase):
def test_pp_construction(self):
'''
Tests parser construction from python internal DSL description.
'''
parser = ParserPython(calc)
self.assertEqual(parser.parser_model.rule ,'calc')
self.assertTrue(isinstance(parser.parser_model, Sequence))
self.assertEqual(parser.parser_model.nodes[0].desc ,'OneOrMore')
def test_parse_input(self):
parser = ParserPython(calc)
input = "4+5*7/3.45*-45*(2.56+32)/-56*(2-1.34)"
result = parser.parse(input)
self.assertTrue(isinstance(result, NonTerminal))
self.assertEqual(str(result), "[ [ [ [ 4 ] ], +, [ [ 5 ], *, [ 7 ], /, [ 3.45 ], *, [ -, 45 ], *, [ (, [ [ [ 2.56 ] ], +, [ [ 32 ] ] ], ) ], /, [ -, 56 ], *, [ (, [ [ [ 2 ] ], -, [ [ 1.34 ] ] ], ) ] ] ], EOF ]")
def test_reduce_tree(self):
parser = ParserPython(calc, reduce_tree=True)
input = "4+5*7/3.45*-45*(2.56+32)/-56*(2-1.34)"
result = parser.parse(input)
self.assertTrue(isinstance(result, NonTerminal))
self.assertEqual(str(result), "[ [ 4, +, [ 5, *, 7, /, 3.45, *, [ -, 45 ], *, [ (, [ 2.56, +, 32 ], ) ], /, [ -, 56 ], *, [ (, [ 2, -, 1.34 ], ) ] ] ], EOF ]" )
# -*- coding: utf-8 -*-
#######################################################################
# Name: test_speed
# Purpose: Performance test of arpeggio parser
# Author: Igor R. Dejanović <igor DOT dejanovic AT gmail DOT com>
# Copyright: (c) 2014 Igor R. Dejanović <igor DOT dejanovic AT gmail DOT com>
# License: MIT License
#######################################################################
import timeit, sys
if __name__ == "__main__":
setup = '''
from arpeggio import OneOrMore, ZeroOrMore, EndOfFile, ParserPython, Optional
from arpeggio import RegExMatch as _
def number(): return _(r'\d*\.\d*|\d+')
def factor(): return [(Optional(["+","-"]), number), ("(", expression, ")")]
def term(): return factor, ZeroOrMore(["*","/"], factor)
def expression(): return term, ZeroOrMore(["+", "-"], term)
def calcfile(): return OneOrMore(expression), EndOfFile
parser = ParserPython(calcfile, reduce_tree=True)
with open("input.txt", "r") as f:
input = f.read()
'''
print timeit.timeit("parser.parse(input)", setup=setup, number=20)
# 7.06 s
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment