MC: Switch to assertEqual function from python's unittest

This commit is contained in:
Thomas Jarosch 2013-01-13 20:11:28 +01:00
parent 3bb8237cdc
commit 06a7d301e2
1 changed files with 48 additions and 55 deletions

View File

@ -23,130 +23,123 @@ 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
self.assertEqual(self.mc.parseMatch(' Token::Match(tok, ";") ',2), ['Token::Match(tok, ";")','tok',' ";"'])
self.assertEqual(self.mc.parseMatch(' Token::Match(tok,', 2), None) # multiline Token::Match is not supported yet
self.assertEqual(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'])
self.assertEqual(output, 'if (match1(tok)) {')
self.assertEqual(1, len(self.mc._matchStrs))
self.assertEqual(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))
self.assertEqual(output, 'if (match2(tok->next()->next())) {')
self.assertEqual(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))
self.assertEqual(output, 'if (Token::Match(tok, "foo"special"bar %num%")) {')
# self.assertEqual(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))
self.assertEqual(output, 'if (match1(tok, 123)) {')
self.assertEqual(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))
self.assertEqual(output, 'if (match2(tok->next()->next(), tok->varId())) {')
self.assertEqual(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))
self.assertEqual(output, 'if (Token::Match(tok, "foo"special"bar %type% %varid%", my_varid_cache)) {')
# self.assertEqual(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))
self.assertEqual(output, 'if (match1(tok, 123)) {')
self.assertEqual(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))
self.assertEqual(output, 'if (match3(tok, 123) || match4(tok, 123)) {')
self.assertEqual(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'])
self.assertEqual(output, 'if (match1(tok)) {')
self.assertEqual(1, len(self.mc._matchStrs))
self.assertEqual(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'])
self.assertEqual(output, 'if (match1(tok->next()->next())) {')
self.assertEqual(1, len(self.mc._matchStrs))
self.assertEqual(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))
self.assertEqual(output, 'if (Token::simpleMatch(tok, "foo\"special\"bar")) {')
self.assertEqual(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'])
self.assertEqual(output, 'if (findmatch1(tok)) {')
self.assertEqual(1, len(self.mc._matchStrs))
self.assertEqual(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'])
self.assertEqual(output, 'if (findmatch2(tok->next()->next(), tok->link())) {')
self.assertEqual(1, len(self.mc._matchStrs))
self.assertEqual(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))
self.assertEqual(output, 'if (Token::findsimplematch(tok, "foo\"special\"bar")) {')
self.assertEqual(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'])
self.assertEqual(output, 'if (findmatch1(tok)) {')
self.assertEqual(1, len(self.mc._matchStrs))
self.assertEqual(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'])
self.assertEqual(output, 'if (findmatch2(tok, tok->varId())) {')
self.assertEqual(1, len(self.mc._matchStrs))
self.assertEqual(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'])
self.assertEqual(output, 'if (findmatch3(tok->next()->next(), tok->link())) {')
self.assertEqual(2, len(self.mc._matchStrs))
self.assertEqual(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'])
self.assertEqual(output, 'if (findmatch4(tok->next()->next(), tok->link(), 123)) {')
self.assertEqual(2, len(self.mc._matchStrs))
self.assertEqual(1, self.mc._matchStrs['foobar'])
if __name__ == '__main__':
unittest.main()