From c67776c04dd37ace32cce813dd7a14eac66d16cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 22 Jul 2015 09:52:24 +0200 Subject: [PATCH] Testing: extract testcases, edit comparisons, run cppcheck, compare results --- lib/checkleakautovar.cpp | 4 +- tools/extract_and_run_more_tests.sh | 8 +++ tools/extracttests.py | 12 ++-- tools/run_more_tests.sh | 89 +++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+), 7 deletions(-) create mode 100755 tools/extract_and_run_more_tests.sh create mode 100755 tools/run_more_tests.sh diff --git a/lib/checkleakautovar.cpp b/lib/checkleakautovar.cpp index c0af12049..9d766c2e0 100644 --- a/lib/checkleakautovar.cpp +++ b/lib/checkleakautovar.cpp @@ -148,9 +148,9 @@ static bool isVariableComparison(const Token *tok, const std::string &comp, cons // Invert comparator std::string s = tok->str(); if (s[0] == '>') - s[1] = '<'; + s[0] = '<'; else if (s[0] == '<') - s[1] = '>'; + s[0] = '>'; if (s == comp) { *vartok = tok->astOperand2(); } diff --git a/tools/extract_and_run_more_tests.sh b/tools/extract_and_run_more_tests.sh new file mode 100755 index 000000000..c564f9f17 --- /dev/null +++ b/tools/extract_and_run_more_tests.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +cd ~/cppcheck +rm -rf test1 +python tools/extracttests.py --code=test1 test/testleakautovar.cpp +cd ~/cppcheck/test1 +~/cppcheck/tools/run_more_tests.sh + diff --git a/tools/extracttests.py b/tools/extracttests.py index d8c4ff729..41dcc8948 100755 --- a/tools/extracttests.py +++ b/tools/extracttests.py @@ -47,6 +47,7 @@ class Extract: testclass = None functionName = None + code = None fin = open(filename, 'r') for line in fin: @@ -76,19 +77,20 @@ class Extract: code = res.group(1) # code.. - res = re.match('\\s+' + string, line) - if res is not None: - code = code + res.group(1) + if code is not None: + res = re.match('\\s+' + string, line) + if res is not None: + code = code + res.group(1) # assert res = re.match('\\s+ASSERT_EQUALS\\(\\"([^"]*)\\",', line) - if res is not None and len(code) > 10: + if res is not None and code is not None: node = {'testclass': testclass, 'functionName': functionName, 'code': code, 'expected': res.group(1)} self.nodes.append(node) - code = '' + code = None # close test file fin.close() diff --git a/tools/run_more_tests.sh b/tools/run_more_tests.sh new file mode 100755 index 000000000..46baac8eb --- /dev/null +++ b/tools/run_more_tests.sh @@ -0,0 +1,89 @@ +#!/bin/bash + +~/cppcheck/cppcheck -q . 2> 1.txt + + +# (!x) => (x==0) +sed -ri 's/([(&][ ]*)\!([a-z]+)([ ]*[&)])/\1\2==0\3/' *.cpp +~/cppcheck/cppcheck -q . 2> 2.txt && diff 1.txt 2.txt + +# (x==0) => (0==x) +sed -ri 's/([(&][ ]*)([a-z]+)[ ]*==[ ]*0([ ]*[&)])/\10==\2\3/' *.cpp +~/cppcheck/cppcheck -q . 2> 2.txt && diff 1.txt 2.txt + +# (0==x) => (!x) +sed -ri 's/([(&][ ]*)0[ ]*==[ ]*([a-z]+)([ ]*[&)])/\1!\2\3/' *.cpp +~/cppcheck/cppcheck -q . 2> 2.txt && diff 1.txt 2.txt + + + + +# if (x) => (x!=0) +sed -ri 's/(if[ ]*\([ ]*[a-z]+)([ ]*[&)])/\1!=0\2/' *.cpp +~/cppcheck/cppcheck -q . 2> 2.txt && diff 1.txt 2.txt + +# while (x) => (x!=0) +sed -ri 's/(while[ ]*\([ ]*[a-z]+)([ ]*[&)])/\1!=0\2/' *.cpp +~/cppcheck/cppcheck -q . 2> 2.txt && diff 1.txt 2.txt + +# (x!=0) => (0!=x) +sed -ri 's/([(&][ ]*)([a-z]+)[ ]*!=[ ]*0([ ]*[&)])/\10!=\2\3/' *.cpp +~/cppcheck/cppcheck -q . 2> 2.txt && diff 1.txt 2.txt + +# (0!=x) => (x) +sed -ri 's/([(&][ ]*)0[ ]*!=[ ]*([a-z]+[ ]*[&)])/\1\2/' *.cpp +~/cppcheck/cppcheck -q . 2> 2.txt && diff 1.txt 2.txt + + +# (x < 0) => (0 > x) +sed -ri 's/([(&][ ]*)([a-z]+)[ ]*<[ ]*(\-?[0-9]+)([ ]*[&)])/\1\3>\2\4/' *.cpp +~/cppcheck/cppcheck -q . 2> 2.txt && diff 1.txt 2.txt + +# (x <= 0) => (0 >= x) +sed -ri 's/([(&][ ]*)([a-z]+)[ ]*<=[ ]*(\-?[0-9]+)([ ]*[&)])/\1\3>=\2\4/' *.cpp +~/cppcheck/cppcheck -q . 2> 2.txt && diff 1.txt 2.txt + +# (x > 0) => (0 < x) +sed -ri 's/([(&][ ]*)([a-z]+)[ ]*<=[ ]*(\-?[0-9]+)([ ]*[&)])/\1\3>=\2\4/' *.cpp +~/cppcheck/cppcheck -q . 2> 2.txt && diff 1.txt 2.txt + +# (x >= 0) => (0 <= x) +sed -ri 's/([(&][ ]*)([a-z]+)[ ]*<=[ ]*(\-?[0-9]+)([ ]*[&)])/\1\3>=\2\4/' *.cpp +~/cppcheck/cppcheck -q . 2> 2.txt && diff 1.txt 2.txt + +# (x == 123) => (123 == x) +sed -ri 's/([(&][ ]*)([a-z]+)[ ]*==[ ]*(\-?[0-9]+)([ ]*[&)])/\1\3==\2\4/' *.cpp +~/cppcheck/cppcheck -q . 2> 2.txt && diff 1.txt 2.txt + +# (x != 123) => (123 != x) +sed -ri 's/([(&][ ]*)([a-z]+)[ ]*\!=[ ]*(\-?[0-9]+)([ ]*[&)])/\1\3!=\2\4/' *.cpp +~/cppcheck/cppcheck -q . 2> 2.txt && diff 1.txt 2.txt + + + +# (0 < x) => (x > 0) +sed -ri 's/([(&][ ]*)(\-?[0-9]+)[ ]*<[ ]*([a-z]+)([ ]*[&)])/\1\3>\2\4/' *.cpp +~/cppcheck/cppcheck -q . 2> 2.txt && diff 1.txt 2.txt + +# (0 <= x) => (x >= 0) +sed -ri 's/([(&][ ]*)(\-?[0-9]+)[ ]*<=[ ]*([a-z]+)([ ]*[&)])/\1\3>=\2\4/' *.cpp +~/cppcheck/cppcheck -q . 2> 2.txt && diff 1.txt 2.txt + +# (0 > x) => (x < 0) +sed -ri 's/([(&][ ]*)(\-?[0-9]+)[ ]*<=[ ]*([a-z]+)([ ]*[&)])/\1\3>=\2\4/' *.cpp +~/cppcheck/cppcheck -q . 2> 2.txt && diff 1.txt 2.txt + +# (0 >= x) => (x <= 0) +sed -ri 's/([(&][ ]*)(\-?[0-9]+)[ ]*<=[ ]*([a-z]+)([ ]*[&)])/\1\3>=\2\4/' *.cpp +~/cppcheck/cppcheck -q . 2> 2.txt && diff 1.txt 2.txt + +# (123 == x) => (x == 123) +sed -ri 's/([(&][ ]*)(\-?[0-9]+)[ ]*==[ ]*([a-z]+)([ ]*[&)])/\1\3==\2\4/' *.cpp +~/cppcheck/cppcheck -q . 2> 2.txt && diff 1.txt 2.txt + +# (123 != x) => (x <= 123) +sed -ri 's/([(&][ ]*)(\-?[0-9]+)[ ]*\!=[ ]*([a-z]+)([ ]*[&)])/\1\3!=\2\4/' *.cpp +~/cppcheck/cppcheck -q . 2> 2.txt && diff 1.txt 2.txt + + +