MC: Turn MatchCompiler tests into python unit test

This commit is contained in:
Thomas Jarosch 2013-01-13 20:03:55 +01:00
parent d46789ee4a
commit 3bb8237cdc
2 changed files with 152 additions and 151 deletions

View File

@ -26,7 +26,6 @@ class MatchCompiler:
def __init__(self, verify_mode=False):
self._verifyMode = verify_mode
self._reset()
self._selftests()
def _reset(self):
self._rawMatchFunctions = []
@ -487,156 +486,6 @@ class MatchCompiler:
fout.write(header+stringList+strFunctions+code)
fout.close()
def _assertEquals(self, actual, expected):
if actual != expected:
print ('Assertion failed:')
print ('"' + actual + '"')
print ('"' + expected + '"')
assert actual == expected
def _selftest_parseMatch(self):
self._assertEquals(self.parseMatch(' Token::Match(tok, ";") ',2), ['Token::Match(tok, ";")','tok',' ";"'])
self._assertEquals(self.parseMatch(' Token::Match(tok,', 2), None) # multiline Token::Match is not supported yet
self._assertEquals(self.parseMatch(' Token::Match(Token::findsimplematch(tok,")"), ";")', 2), ['Token::Match(Token::findsimplematch(tok,")"), ";")', 'Token::findsimplematch(tok,")")', ' ";"']) # inner function call
def _selftest_replaceTokenMatch(self):
self._reset()
input = 'if (Token::Match(tok, "foobar")) {'
output = self._replaceTokenMatch(input)
self._assertEquals(output, 'if (match1(tok)) {')
self._assertEquals(1, len(self._matchStrs))
self._assertEquals(1, self._matchStrs['foobar'])
input = 'if (Token::Match(tok->next()->next(), "foobar %type% %num%")) {'
output = self._replaceTokenMatch(input)
self._assertEquals(output, 'if (match2(tok->next()->next())) {')
self._assertEquals(2, len(self._matchStrs))
input = 'if (Token::Match(tok, "foo\"special\"bar %num%")) {'
output = self._replaceTokenMatch(input)
# FIXME: Currently detected as non-static pattern
self._assertEquals(output, 'if (Token::Match(tok, "foo"special"bar %num%")) {')
# self._assertEquals(3, len(self._matchStrs))
def _selftest_replaceTokenMatchWithVarId(self):
self._reset()
input = 'if (Token::Match(tok, "foobar %varid%", 123)) {'
output = self._replaceTokenMatch(input)
self._assertEquals(output, 'if (match1(tok, 123)) {')
self._assertEquals(1, len(self._matchStrs))
input = 'if (Token::Match(tok->next()->next(), "%varid% foobar", tok->varId())) {'
output = self._replaceTokenMatch(input)
self._assertEquals(output, 'if (match2(tok->next()->next(), tok->varId())) {')
self._assertEquals(1, len(self._matchStrs))
input = 'if (Token::Match(tok, "foo\"special\"bar %type% %varid%", my_varid_cache)) {'
output = self._replaceTokenMatch(input)
# FIXME: Currently detected as non-static pattern
self._assertEquals(output, 'if (Token::Match(tok, "foo"special"bar %type% %varid%", my_varid_cache)) {')
# self._assertEquals(1, len(self._matchStrs))
# test caching: reuse existing matchX()
input = 'if (Token::Match(tok, "foobar %varid%", 123)) {'
output = self._replaceTokenMatch(input)
self._assertEquals(output, 'if (match1(tok, 123)) {')
self._assertEquals(1, len(self._matchStrs))
# two in one line
input = 'if (Token::Match(tok, "foobar2 %varid%", 123) || Token::Match(tok, "%type% %varid%", 123)) {'
output = self._replaceTokenMatch(input)
self._assertEquals(output, 'if (match3(tok, 123) || match4(tok, 123)) {')
self._assertEquals(3, len(self._matchStrs))
def _selftest_replaceTokenSimpleMatch(self):
self._reset()
input = 'if (Token::simpleMatch(tok, "foobar")) {'
output = self._replaceTokenMatch(input)
self._assertEquals(output, 'if (match1(tok)) {')
self._assertEquals(1, len(self._matchStrs))
self._assertEquals(1, self._matchStrs['foobar'])
input = 'if (Token::simpleMatch(tok->next()->next(), "foobar")) {'
output = self._replaceTokenMatch(input)
self._assertEquals(output, 'if (match1(tok->next()->next())) {')
self._assertEquals(1, len(self._matchStrs))
self._assertEquals(1, self._matchStrs['foobar'])
input = 'if (Token::simpleMatch(tok, "foo\"special\"bar")) {'
output = self._replaceTokenMatch(input)
# FIXME: Currently detected as non-static pattern
self._assertEquals(output, 'if (Token::simpleMatch(tok, "foo\"special\"bar")) {')
self._assertEquals(1, len(self._matchStrs))
def _selftest_replaceTokenFindSimpleMatch(self):
self._reset()
input = 'if (Token::findsimplematch(tok, "foobar")) {'
output = self._replaceTokenFindMatch(input)
self._assertEquals(output, 'if (findmatch1(tok)) {')
self._assertEquals(1, len(self._matchStrs))
self._assertEquals(1, self._matchStrs['foobar'])
input = 'if (Token::findsimplematch(tok->next()->next(), "foobar", tok->link())) {'
output = self._replaceTokenFindMatch(input)
self._assertEquals(output, 'if (findmatch2(tok->next()->next(), tok->link())) {')
self._assertEquals(1, len(self._matchStrs))
self._assertEquals(1, self._matchStrs['foobar'])
input = 'if (Token::findsimplematch(tok, "foo\"special\"bar")) {'
output = self._replaceTokenFindMatch(input)
# FIXME: Currently detected as non-static pattern
self._assertEquals(output, 'if (Token::findsimplematch(tok, "foo\"special\"bar")) {')
self._assertEquals(1, len(self._matchStrs))
def _selftest_replaceTokenFindMatch(self):
self._reset()
input = 'if (Token::findmatch(tok, "foobar")) {'
output = self._replaceTokenFindMatch(input)
self._assertEquals(output, 'if (findmatch1(tok)) {')
self._assertEquals(1, len(self._matchStrs))
self._assertEquals(1, self._matchStrs['foobar'])
# findmatch with varid
input = 'if (Token::findmatch(tok, "foobar %varid%", tok->varId())) {'
output = self._replaceTokenFindMatch(input)
self._assertEquals(output, 'if (findmatch2(tok, tok->varId())) {')
self._assertEquals(1, len(self._matchStrs))
self._assertEquals(1, self._matchStrs['foobar'])
# findmatch with end token
input = 'if (Token::findmatch(tok->next()->next(), "foobar %type%", tok->link())) {'
output = self._replaceTokenFindMatch(input)
self._assertEquals(output, 'if (findmatch3(tok->next()->next(), tok->link())) {')
self._assertEquals(2, len(self._matchStrs))
self._assertEquals(1, self._matchStrs['foobar'])
# findmatch with end token and varid
input = 'if (Token::findmatch(tok->next()->next(), "foobar %type% %varid%", tok->link(), 123)) {'
output = self._replaceTokenFindMatch(input)
self._assertEquals(output, 'if (findmatch4(tok->next()->next(), tok->link(), 123)) {')
self._assertEquals(2, len(self._matchStrs))
self._assertEquals(1, self._matchStrs['foobar'])
def _selftests(self):
self._selftest_parseMatch()
# Self test currently not compatible with the verify mode
# as it modifies the expected output
if self._verifyMode == False:
self._selftest_replaceTokenMatch()
self._selftest_replaceTokenMatchWithVarId()
self._selftest_replaceTokenSimpleMatch()
self._selftest_replaceTokenFindSimpleMatch()
self._selftest_replaceTokenFindMatch()
self._reset()
def main():
# Main program
build_dir = 'build'

152
tools/test_matchcompiler.py Executable file
View File

@ -0,0 +1,152 @@
#!/usr/bin/python
#
# Cppcheck - A tool for static C/C++ code analysis
# Copyright (C) 2007-2013 Daniel Marjamaeki and Cppcheck team.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import unittest
import matchcompiler
class MatchCompilerTest(unittest.TestCase):
def setUp(self):
self.mc = matchcompiler.MatchCompiler(verify_mode=False)
def _assertEquals(self, actual, expected):
if actual != expected:
print ('Assertion failed:')
print ('"' + actual + '"')
print ('"' + expected + '"')
assert actual == expected
def test_parseMatch(self):
self._assertEquals(self.mc.parseMatch(' Token::Match(tok, ";") ',2), ['Token::Match(tok, ";")','tok',' ";"'])
self._assertEquals(self.mc.parseMatch(' Token::Match(tok,', 2), None) # multiline Token::Match is not supported yet
self._assertEquals(self.mc.parseMatch(' Token::Match(Token::findsimplematch(tok,")"), ";")', 2), ['Token::Match(Token::findsimplematch(tok,")"), ";")', 'Token::findsimplematch(tok,")")', ' ";"']) # inner function call
def test_replaceTokenMatch(self):
input = 'if (Token::Match(tok, "foobar")) {'
output = self.mc._replaceTokenMatch(input)
self._assertEquals(output, 'if (match1(tok)) {')
self._assertEquals(1, len(self.mc._matchStrs))
self._assertEquals(1, self.mc._matchStrs['foobar'])
input = 'if (Token::Match(tok->next()->next(), "foobar %type% %num%")) {'
output = self.mc._replaceTokenMatch(input)
self._assertEquals(output, 'if (match2(tok->next()->next())) {')
self._assertEquals(2, len(self.mc._matchStrs))
input = 'if (Token::Match(tok, "foo\"special\"bar %num%")) {'
output = self.mc._replaceTokenMatch(input)
# FIXME: Currently detected as non-static pattern
self._assertEquals(output, 'if (Token::Match(tok, "foo"special"bar %num%")) {')
# self._assertEquals(3, len(self.mc._matchStrs))
def test_replaceTokenMatchWithVarId(self):
input = 'if (Token::Match(tok, "foobar %varid%", 123)) {'
output = self.mc._replaceTokenMatch(input)
self._assertEquals(output, 'if (match1(tok, 123)) {')
self._assertEquals(1, len(self.mc._matchStrs))
input = 'if (Token::Match(tok->next()->next(), "%varid% foobar", tok->varId())) {'
output = self.mc._replaceTokenMatch(input)
self._assertEquals(output, 'if (match2(tok->next()->next(), tok->varId())) {')
self._assertEquals(1, len(self.mc._matchStrs))
input = 'if (Token::Match(tok, "foo\"special\"bar %type% %varid%", my_varid_cache)) {'
output = self.mc._replaceTokenMatch(input)
# FIXME: Currently detected as non-static pattern
self._assertEquals(output, 'if (Token::Match(tok, "foo"special"bar %type% %varid%", my_varid_cache)) {')
# self._assertEquals(1, len(self.mc._matchStrs))
# test caching: reuse existing matchX()
input = 'if (Token::Match(tok, "foobar %varid%", 123)) {'
output = self.mc._replaceTokenMatch(input)
self._assertEquals(output, 'if (match1(tok, 123)) {')
self._assertEquals(1, len(self.mc._matchStrs))
# two in one line
input = 'if (Token::Match(tok, "foobar2 %varid%", 123) || Token::Match(tok, "%type% %varid%", 123)) {'
output = self.mc._replaceTokenMatch(input)
self._assertEquals(output, 'if (match3(tok, 123) || match4(tok, 123)) {')
self._assertEquals(3, len(self.mc._matchStrs))
def test_replaceTokenSimpleMatch(self):
input = 'if (Token::simpleMatch(tok, "foobar")) {'
output = self.mc._replaceTokenMatch(input)
self._assertEquals(output, 'if (match1(tok)) {')
self._assertEquals(1, len(self.mc._matchStrs))
self._assertEquals(1, self.mc._matchStrs['foobar'])
input = 'if (Token::simpleMatch(tok->next()->next(), "foobar")) {'
output = self.mc._replaceTokenMatch(input)
self._assertEquals(output, 'if (match1(tok->next()->next())) {')
self._assertEquals(1, len(self.mc._matchStrs))
self._assertEquals(1, self.mc._matchStrs['foobar'])
input = 'if (Token::simpleMatch(tok, "foo\"special\"bar")) {'
output = self.mc._replaceTokenMatch(input)
# FIXME: Currently detected as non-static pattern
self._assertEquals(output, 'if (Token::simpleMatch(tok, "foo\"special\"bar")) {')
self._assertEquals(1, len(self.mc._matchStrs))
def test_replaceTokenFindSimpleMatch(self):
input = 'if (Token::findsimplematch(tok, "foobar")) {'
output = self.mc._replaceTokenFindMatch(input)
self._assertEquals(output, 'if (findmatch1(tok)) {')
self._assertEquals(1, len(self.mc._matchStrs))
self._assertEquals(1, self.mc._matchStrs['foobar'])
input = 'if (Token::findsimplematch(tok->next()->next(), "foobar", tok->link())) {'
output = self.mc._replaceTokenFindMatch(input)
self._assertEquals(output, 'if (findmatch2(tok->next()->next(), tok->link())) {')
self._assertEquals(1, len(self.mc._matchStrs))
self._assertEquals(1, self.mc._matchStrs['foobar'])
input = 'if (Token::findsimplematch(tok, "foo\"special\"bar")) {'
output = self.mc._replaceTokenFindMatch(input)
# FIXME: Currently detected as non-static pattern
self._assertEquals(output, 'if (Token::findsimplematch(tok, "foo\"special\"bar")) {')
self._assertEquals(1, len(self.mc._matchStrs))
def test_replaceTokenFindMatch(self):
input = 'if (Token::findmatch(tok, "foobar")) {'
output = self.mc._replaceTokenFindMatch(input)
self._assertEquals(output, 'if (findmatch1(tok)) {')
self._assertEquals(1, len(self.mc._matchStrs))
self._assertEquals(1, self.mc._matchStrs['foobar'])
# findmatch with varid
input = 'if (Token::findmatch(tok, "foobar %varid%", tok->varId())) {'
output = self.mc._replaceTokenFindMatch(input)
self._assertEquals(output, 'if (findmatch2(tok, tok->varId())) {')
self._assertEquals(1, len(self.mc._matchStrs))
self._assertEquals(1, self.mc._matchStrs['foobar'])
# findmatch with end token
input = 'if (Token::findmatch(tok->next()->next(), "foobar %type%", tok->link())) {'
output = self.mc._replaceTokenFindMatch(input)
self._assertEquals(output, 'if (findmatch3(tok->next()->next(), tok->link())) {')
self._assertEquals(2, len(self.mc._matchStrs))
self._assertEquals(1, self.mc._matchStrs['foobar'])
# findmatch with end token and varid
input = 'if (Token::findmatch(tok->next()->next(), "foobar %type% %varid%", tok->link(), 123)) {'
output = self.mc._replaceTokenFindMatch(input)
self._assertEquals(output, 'if (findmatch4(tok->next()->next(), tok->link(), 123)) {')
self._assertEquals(2, len(self.mc._matchStrs))
self._assertEquals(1, self.mc._matchStrs['foobar'])
if __name__ == '__main__':
unittest.main()