Skip to content

Commit 098f0bf

Browse files
author
Pete Johns
committed
Fixed cppcheck-opensource#2526 (Make TODO_ASSERT_EQUALS take three arguments (value, to_be, as_is)?...
Removed replaced EXPECTED with... WANTED (to-be): The future expected value. CURRENT (as-is): Documenting how cppcheck behaves now. This removes the need for an ASSERT_EQUALS but enforces the check for every TODO_ASSERT_EQUALS.
1 parent 8b6694f commit 098f0bf

16 files changed

Lines changed: 368 additions & 285 deletions

test/testbufferoverrun.cpp

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -561,8 +561,8 @@ class TestBufferOverrun : public TestFixture
561561
" char str[5];\n"
562562
" memclr( 10, str ); // ERROR\n"
563563
"}\n");
564-
TODO_ASSERT_EQUALS("[test.cpp:9] -> [test.cpp:3]: (possible error) Array index out of bounds\n", errout.str());
565-
ASSERT_EQUALS("", errout.str()); // current result
564+
TODO_ASSERT_EQUALS("[test.cpp:9] -> [test.cpp:3]: (possible error) Array index out of bounds\n",
565+
"", errout.str());
566566

567567
// This is not an error
568568
check("static void memclr( char *data, int size )\n"
@@ -610,8 +610,8 @@ class TestBufferOverrun : public TestFixture
610610
"{\n"
611611
" memclr(abc->str);\n"
612612
"}\n");
613-
ASSERT_EQUALS("", errout.str());
614-
TODO_ASSERT_EQUALS("[test.cpp:13] -> [test.cpp:8]: (possible error) Array index out of bounds\n", errout.str());
613+
TODO_ASSERT_EQUALS("[test.cpp:13] -> [test.cpp:8]: (possible error) Array index out of bounds\n",
614+
"", errout.str());
615615
}
616616

617617

@@ -795,8 +795,7 @@ class TestBufferOverrun : public TestFixture
795795
" i+=1;\n"
796796
" }\n"
797797
"}\n");
798-
ASSERT_EQUALS("", errout.str()); // Catch changes
799-
TODO_ASSERT_EQUALS("[test.cpp:6]: (error) Buffer overrun\n", errout.str());
798+
TODO_ASSERT_EQUALS("[test.cpp:6]: (error) Buffer overrun\n", "", errout.str());
800799
}
801800

802801
void array_index_19()
@@ -1001,8 +1000,7 @@ class TestBufferOverrun : public TestFixture
10011000
" int *ip = &i[1];\n"
10021001
" ip[-10] = 1;\n"
10031002
"}\n");
1004-
ASSERT_EQUALS("", errout.str());
1005-
TODO_ASSERT_EQUALS("[test.cpp:5]: (error) Array ip[-10] out of bounds\n", errout.str());
1003+
TODO_ASSERT_EQUALS("[test.cpp:5]: (error) Array ip[-10] out of bounds\n", "", errout.str());
10061004
}
10071005

10081006
void array_index_29()
@@ -1015,8 +1013,7 @@ class TestBufferOverrun : public TestFixture
10151013
" int *ii = &i[-5];"
10161014
" ii[10] = 0;"
10171015
"}\n");
1018-
ASSERT_EQUALS("", errout.str());
1019-
TODO_ASSERT_EQUALS("[test.cpp:6]: (error) Array ii[10] out of bounds\n", errout.str());
1016+
TODO_ASSERT_EQUALS("[test.cpp:6]: (error) Array ii[10] out of bounds\n", "", errout.str());
10201017
}
10211018

10221019
void array_index_30()
@@ -1133,7 +1130,7 @@ class TestBufferOverrun : public TestFixture
11331130
" char a[10][10][10];\n"
11341131
" a[2*3][4*3][2] = 'a';\n"
11351132
"}\n");
1136-
TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Array 'a[10][10][10]' index a[6][12][2] out of bounds\n", errout.str());
1133+
TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Array 'a[10][10][10]' index a[6][12][2] out of bounds\n", "", errout.str());
11371134

11381135
check("void f()\n"
11391136
"{\n"
@@ -1201,8 +1198,7 @@ class TestBufferOverrun : public TestFixture
12011198
" };\n"
12021199
" }\n"
12031200
"}\n");
1204-
ASSERT_EQUALS("", errout.str());
1205-
TODO_ASSERT_EQUALS("[test.cpp:12]: (error) Array index out of bounds\n", errout.str());
1201+
TODO_ASSERT_EQUALS("[test.cpp:12]: (error) Array index out of bounds\n", "", errout.str());
12061202
}
12071203

12081204
void array_index_calculation()
@@ -1295,8 +1291,7 @@ class TestBufferOverrun : public TestFixture
12951291
" val[i+1] = val[i];\n"
12961292
" }\n"
12971293
"}\n");
1298-
ASSERT_EQUALS("", errout.str()); // catch changes
1299-
TODO_ASSERT_EQUALS("[test.cpp:5]: (error) Array 'val[5]' index -1 out of bounds\n", errout.str());
1294+
TODO_ASSERT_EQUALS("[test.cpp:5]: (error) Array 'val[5]' index -1 out of bounds\n", "", errout.str());
13001295
}
13011296

13021297

@@ -1562,8 +1557,7 @@ class TestBufferOverrun : public TestFixture
15621557
" char s[3];\n"
15631558
" f1(s,3);\n"
15641559
"}\n");
1565-
TODO_ASSERT_EQUALS("[test.cpp:8] -> [test.cpp:3]: (error) Buffer access out-of-bounds\n", errout.str());
1566-
ASSERT_EQUALS("", errout.str());
1560+
TODO_ASSERT_EQUALS("[test.cpp:8] -> [test.cpp:3]: (error) Buffer access out-of-bounds\n", "", errout.str());
15671561

15681562
check("void f1(char *s,int size)\n"
15691563
"{\n"
@@ -1999,8 +1993,7 @@ class TestBufferOverrun : public TestFixture
19991993
" char buf[3];\n"
20001994
" sprintf(buf, \"%s\", condition ? \"11\" : \"222\");\n"
20011995
"}\n");
2002-
ASSERT_EQUALS("", errout.str()); // catch changes
2003-
TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Buffer access out-of-bounds\n", errout.str());
1996+
TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Buffer access out-of-bounds\n", "", errout.str());
20041997
}
20051998

20061999
void sprintf7()
@@ -2224,8 +2217,7 @@ class TestBufferOverrun : public TestFixture
22242217
" char a[5], b[50];\n"
22252218
" memchr(a, b, 10);\n"
22262219
"}\n");
2227-
TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Buffer access out-of-bounds\n", errout.str());
2228-
ASSERT_EQUALS("", errout.str());
2220+
TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Buffer access out-of-bounds\n", "", errout.str());
22292221
}
22302222

22312223
// ticket #2121 - buffer access out of bounds when using uint32_t
@@ -2466,7 +2458,7 @@ class TestBufferOverrun : public TestFixture
24662458
ASSERT_EQUALS(9, CheckBufferOverrun::countSprintfLength("%08ld", intAsParameter));
24672459
ASSERT_EQUALS(6, CheckBufferOverrun::countSprintfLength("%.2d", intAsParameter));
24682460
ASSERT_EQUALS(9, CheckBufferOverrun::countSprintfLength("%08.2d", intAsParameter));
2469-
TODO_ASSERT_EQUALS(5, CheckBufferOverrun::countSprintfLength("%x", intAsParameter));
2461+
TODO_ASSERT_EQUALS(5, 2, CheckBufferOverrun::countSprintfLength("%x", intAsParameter));
24702462
ASSERT_EQUALS(5, CheckBufferOverrun::countSprintfLength("%4x", intAsParameter));
24712463
ASSERT_EQUALS(6, CheckBufferOverrun::countSprintfLength("%5x", intAsParameter));
24722464
ASSERT_EQUALS(5, CheckBufferOverrun::countSprintfLength("%.4x", intAsParameter));
@@ -2478,17 +2470,17 @@ class TestBufferOverrun : public TestFixture
24782470
Token floatTok(0);
24792471
floatTok.str("1.12345f");
24802472
floatAsParameter.push_back(&floatTok);
2481-
TODO_ASSERT_EQUALS(5, CheckBufferOverrun::countSprintfLength("%.2f", floatAsParameter));
2473+
TODO_ASSERT_EQUALS(5, 3, CheckBufferOverrun::countSprintfLength("%.2f", floatAsParameter));
24822474
ASSERT_EQUALS(9, CheckBufferOverrun::countSprintfLength("%8.2f", floatAsParameter));
2483-
TODO_ASSERT_EQUALS(5, CheckBufferOverrun::countSprintfLength("%2.2f", floatAsParameter));
2475+
TODO_ASSERT_EQUALS(5, 3, CheckBufferOverrun::countSprintfLength("%2.2f", floatAsParameter));
24842476

24852477
std::list<const Token*> floatAsParameter2;
24862478
Token floatTok2(0);
24872479
floatTok2.str("100.12345f");
24882480
floatAsParameter2.push_back(&floatTok2);
2489-
TODO_ASSERT_EQUALS(7, CheckBufferOverrun::countSprintfLength("%2.2f", floatAsParameter2));
2490-
TODO_ASSERT_EQUALS(7, CheckBufferOverrun::countSprintfLength("%.2f", floatAsParameter));
2491-
TODO_ASSERT_EQUALS(7, CheckBufferOverrun::countSprintfLength("%4.2f", floatAsParameter));
2481+
TODO_ASSERT_EQUALS(7, 3, CheckBufferOverrun::countSprintfLength("%2.2f", floatAsParameter2));
2482+
TODO_ASSERT_EQUALS(7, 3, CheckBufferOverrun::countSprintfLength("%.2f", floatAsParameter));
2483+
TODO_ASSERT_EQUALS(7, 5, CheckBufferOverrun::countSprintfLength("%4.2f", floatAsParameter));
24922484

24932485
std::list<const Token*> multipleParams;
24942486
multipleParams.push_back(&strTok);

test/testclass.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,7 +1498,8 @@ class TestClass : public TestFixture
14981498
"public:\n"
14991499
" ~B() { int a; }\n"
15001500
"};\n");
1501-
TODO_ASSERT_EQUALS("[test.cpp:7]: (error) Class A which is inherited by class B does not have a virtual destructor\n", errout.str());
1501+
TODO_ASSERT_EQUALS("[test.cpp:7]: (error) Class A which is inherited by class B does not have a virtual destructor\n",
1502+
"", errout.str());
15021503
}
15031504

15041505
void virtualDestructorTemplate()
@@ -4303,7 +4304,8 @@ class TestClass : public TestFixture
43034304
"std::vector<std::string> m_strVec;\n"
43044305
"};\n"
43054306
);
4306-
TODO_ASSERT_EQUALS("[test.cpp:4]: (information) Technically the member function 'A::strGetSize' can be const.\n", errout.str());
4307+
TODO_ASSERT_EQUALS("[test.cpp:4]: (information) Technically the member function 'A::strGetSize' can be const.\n",
4308+
"", errout.str());
43074309
}
43084310

43094311
void const26() // ticket #1847
@@ -4625,8 +4627,8 @@ class TestClass : public TestFixture
46254627
"}\n"
46264628
"using namespace N;\n"
46274629
"int Base::getResourceName() { return var; }\n");
4628-
ASSERT_EQUALS("", errout.str());
4629-
TODO_ASSERT_EQUALS("[test.cpp:11] -> [test.cpp:6]: (information) Technically the member function 'N::Base::getResourceName' can be const.\n", errout.str());
4630+
TODO_ASSERT_EQUALS("[test.cpp:11] -> [test.cpp:6]: (information) Technically the member function 'N::Base::getResourceName' can be const.\n",
4631+
"", errout.str());
46304632
}
46314633

46324634
void const36() // ticket #2003
@@ -5076,7 +5078,8 @@ class TestClass : public TestFixture
50765078
" A(){}\n"
50775079
" unsigned int GetVecSize() {return m_v.size();}\n"
50785080
"}");
5079-
TODO_ASSERT_EQUALS("[test.cpp:7]: (information) Technically the member function 'A::GetVecSize' can be const.\n", errout.str());
5081+
TODO_ASSERT_EQUALS("[test.cpp:7]: (information) Technically the member function 'A::GetVecSize' can be const.\n",
5082+
"", errout.str());
50805083
}
50815084

50825085
void constVirtualFunc()

test/testdivision.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ class TestDivision : public TestFixture
7474
" unsigned int uvar = 2;\n"
7575
" return ivar / uvar;\n"
7676
"}\n");
77-
TODO_ASSERT_EQUALS("[test.cpp:5]: (style) Division with signed and unsigned operators\n", errout.str());
78-
ASSERT_EQUALS("", errout.str());
77+
TODO_ASSERT_EQUALS("[test.cpp:5]: (style) Division with signed and unsigned operators\n",
78+
"", errout.str());
7979
}
8080

8181
void division2()
@@ -86,8 +86,8 @@ class TestDivision : public TestFixture
8686
" unsigned int uvar = 2;\n"
8787
" return uvar / ivar;\n"
8888
"}\n");
89-
TODO_ASSERT_EQUALS("[test.cpp:5]: (style) Division with signed and unsigned operators\n", errout.str());
90-
ASSERT_EQUALS("", errout.str());
89+
TODO_ASSERT_EQUALS("[test.cpp:5]: (style) Division with signed and unsigned operators\n",
90+
"", errout.str());
9191
}
9292

9393
void division4()
@@ -166,8 +166,8 @@ class TestDivision : public TestFixture
166166
" unsigned int c = a / b;\n"
167167
" }\n"
168168
"}\n", true);
169-
ASSERT_EQUALS("", errout.str());
170-
TODO_ASSERT_EQUALS("unsigned division", errout.str());
169+
TODO_ASSERT_EQUALS("unsigned division",
170+
"", errout.str());
171171

172172
check("void a(int i) { }\n"
173173
"int foo( unsigned int sz )\n"
@@ -186,17 +186,17 @@ class TestDivision : public TestFixture
186186
" unsigned long uvar = 2;\n"
187187
" return ivar / uvar;\n"
188188
"}\n");
189-
ASSERT_EQUALS("", errout.str());
190-
TODO_ASSERT_EQUALS("unsigned division", errout.str());
189+
TODO_ASSERT_EQUALS("unsigned division",
190+
"", errout.str());
191191

192192
check("void f()\n"
193193
"{\n"
194194
" int ivar = -2;\n"
195195
" unsigned long long uvar = 2;\n"
196196
" return ivar / uvar;\n"
197197
"}\n");
198-
ASSERT_EQUALS("", errout.str());
199-
TODO_ASSERT_EQUALS("unsigned division", errout.str());
198+
TODO_ASSERT_EQUALS("unsigned division",
199+
"", errout.str());
200200
}
201201
};
202202

test/testmemleak.cpp

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,8 @@ class TestMemleakInFunction : public TestFixture
403403
ASSERT_EQUALS(";;dealloc;", getcode("char *s; free(reinterpret_cast<void *>(s));", "s"));
404404
ASSERT_EQUALS(";;dealloc;", getcode("char *s; delete s;", "s"));
405405
ASSERT_EQUALS(";;dealloc;", getcode("char *s; delete (s);", "s"));
406-
ASSERT_EQUALS(";;;", getcode("char *s; delete (void *)(s);", "s")); // current result..
407-
TODO_ASSERT_EQUALS(";;dealloc;", getcode("char *s; delete (void *)(s);", "s")); // ..wanted result
406+
TODO_ASSERT_EQUALS(";;dealloc;",
407+
";;;", getcode("char *s; delete (void *)(s);", "s"));
408408
ASSERT_EQUALS(";;dealloc;", getcode("char *s; delete [] s;", "s"));
409409
ASSERT_EQUALS(";;dealloc;", getcode("char *s; delete [] (s);", "s"));
410410
ASSERT_EQUALS(";;dealloc;", getcode("void *p; foo(fclose(p));", "p"));
@@ -453,7 +453,8 @@ class TestMemleakInFunction : public TestFixture
453453

454454
// Since we don't check how the return value is used we must bail out
455455
ASSERT_EQUALS("", getcode("char *s; int ret = asprintf(&s, \"xyz\");", "s"));
456-
TODO_ASSERT_EQUALS(";;alloc;", getcode("char *s; int ret; ret=asprintf(&s, \"xyz\"); if (ret==-1) return;", "s"));
456+
TODO_ASSERT_EQUALS(";;alloc;",
457+
"", getcode("char *s; int ret; ret=asprintf(&s, \"xyz\"); if (ret==-1) return;", "s"));
457458

458459
// use..
459460
ASSERT_EQUALS(";;use;", getcode("char *s; a(s);", "s"));
@@ -695,10 +696,8 @@ class TestMemleakInFunction : public TestFixture
695696
ASSERT_EQUALS("; use ;", simplifycode("; while1 { if { dealloc ; return ; } if { if { continue ; } } }"));
696697

697698
// scope..
698-
// current result - ok
699-
ASSERT_EQUALS("; assign ; dealloc ; if alloc ; }", simplifycode("; assign ; { dealloc ; if alloc ; } }"));
700-
// wanted result - better
701-
TODO_ASSERT_EQUALS("; assign ; if alloc ; }", simplifycode("; assign ; { dealloc ; if alloc ; } }"));
699+
TODO_ASSERT_EQUALS("; assign ; if alloc ; }",
700+
"; assign ; dealloc ; if alloc ; }", simplifycode("; assign ; { dealloc ; if alloc ; } }"));
702701

703702
// callfunc..
704703
ASSERT_EQUALS("; callfunc ; }", simplifycode(";callfunc;}"));
@@ -713,8 +712,10 @@ class TestMemleakInFunction : public TestFixture
713712
ASSERT_EQUALS(";", simplifycode("; if { alloc; exit; }"));
714713
ASSERT_EQUALS("; alloc ;", simplifycode("; alloc ; if { use; exit; }"));
715714
ASSERT_EQUALS("; alloc ;", simplifycode("; alloc ; if(!var) { exit; }"));
716-
TODO_ASSERT_EQUALS(";", simplifycode("; alloc ; if(var) { exit; }"));
717-
TODO_ASSERT_EQUALS(";\n; alloc ;", simplifycode("; alloc ; ifv { exit; }"));
715+
TODO_ASSERT_EQUALS(";",
716+
"; if(var) exit ;", simplifycode("; alloc ; if(var) { exit; }"));
717+
TODO_ASSERT_EQUALS(";\n; alloc ;",
718+
"; alloc ; ifv exit ;", simplifycode("; alloc ; ifv { exit; }"));
718719

719720
// try-catch
720721
ASSERT_EQUALS("; }", simplifycode("; try ; catch exit ; }"));
@@ -808,8 +809,8 @@ class TestMemleakInFunction : public TestFixture
808809
ASSERT_EQUALS(2, dofindleak(";alloc;\n if assign;\n dealloc;"));
809810

810811
// loop..
811-
TODO_ASSERT_EQUALS(1, dofindleak("; loop { alloc ; if break; dealloc ; }"));
812-
TODO_ASSERT_EQUALS(1, dofindleak("; loop { alloc ; if continue; dealloc ; }"));
812+
TODO_ASSERT_EQUALS(1, notfound, dofindleak("; loop { alloc ; if break; dealloc ; }"));
813+
TODO_ASSERT_EQUALS(1, notfound, dofindleak("; loop { alloc ; if continue; dealloc ; }"));
813814
ASSERT_EQUALS(notfound, dofindleak("; loop { alloc ; if break; } dealloc ;"));
814815
ASSERT_EQUALS(1, dofindleak("; loop alloc ;"));
815816
ASSERT_EQUALS(1, dofindleak("; loop alloc ; dealloc ;"));
@@ -1132,13 +1133,11 @@ class TestMemleakInFunction : public TestFixture
11321133
" }\n"
11331134
" delete [] x;\n"
11341135
"}\n", true);
1135-
TODO_ASSERT_EQUALS("[test.cpp:6]: (error) Memory leak: x\n", errout.str());
1136-
ASSERT_EQUALS("", errout.str());
1136+
TODO_ASSERT_EQUALS("[test.cpp:6]: (error) Memory leak: x\n",
1137+
"", errout.str());
11371138
}
11381139

11391140

1140-
1141-
11421141
void forwhile5()
11431142
{
11441143
check("void f(const char **a)\n"
@@ -1187,8 +1186,10 @@ class TestMemleakInFunction : public TestFixture
11871186
"\n"
11881187
" return a;\n"
11891188
"}\n");
1190-
TODO_ASSERT_EQUALS("[test.cpp:10]: (error) Memory leak: a\n", errout.str());
1191-
ASSERT_EQUALS("[test.cpp:8]: (error) Common realloc mistake: \'a\' nulled but not freed upon failure\n", errout.str());
1189+
TODO_ASSERT_EQUALS("[test.cpp:10]: (error) Memory leak: a\n",
1190+
1191+
"[test.cpp:8]: (error) Common realloc mistake: \'a\' nulled but not freed upon failure\n",
1192+
errout.str());
11921193
}
11931194

11941195

@@ -1478,8 +1479,8 @@ class TestMemleakInFunction : public TestFixture
14781479
" char *p = new char[100];\n"
14791480
" foo(p);\n"
14801481
"}\n");
1481-
TODO_ASSERT_EQUALS("[test.cpp:11]: (error) Memory leak: p\n", errout.str());
1482-
ASSERT_EQUALS("", errout.str());
1482+
TODO_ASSERT_EQUALS("[test.cpp:11]: (error) Memory leak: p\n",
1483+
"", errout.str());
14831484
}
14841485

14851486

@@ -1880,8 +1881,8 @@ class TestMemleakInFunction : public TestFixture
18801881
" char *p;\n"
18811882
" foo(&p);\n"
18821883
"}\n");
1883-
TODO_ASSERT_EQUALS(std::string("[test.cpp:11]: (error) Memory leak: p\n"), errout.str());
1884-
ASSERT_EQUALS("", errout.str());
1884+
TODO_ASSERT_EQUALS(std::string("[test.cpp:11]: (error) Memory leak: p\n"),
1885+
"", errout.str());
18851886

18861887
check("void foo(char **str)\n"
18871888
"{\n"
@@ -2083,8 +2084,10 @@ class TestMemleakInFunction : public TestFixture
20832084
" free(a);\n"
20842085
"}\n");
20852086

2086-
TODO_ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: a\n", errout.str());
2087-
ASSERT_EQUALS("[test.cpp:4]: (error) Common realloc mistake: \'a\' nulled but not freed upon failure\n", errout.str());
2087+
TODO_ASSERT_EQUALS("[test.cpp:5]: (error) Memory leak: a\n",
2088+
2089+
"[test.cpp:4]: (error) Common realloc mistake: \'a\' nulled but not freed upon failure\n",
2090+
errout.str());
20882091
}
20892092

20902093
void realloc5()
@@ -2296,8 +2299,8 @@ class TestMemleakInFunction : public TestFixture
22962299
" free(str);\n"
22972300
" char c = *str;\n"
22982301
"}\n");
2299-
TODO_ASSERT_EQUALS("[test.cpp:5]: (error) Dereferencing 'str' after it is deallocated / released\n", errout.str());
2300-
ASSERT_EQUALS("", errout.str());
2302+
TODO_ASSERT_EQUALS("[test.cpp:5]: (error) Dereferencing 'str' after it is deallocated / released\n",
2303+
"", errout.str());
23012304

23022305
check("void foo()\n"
23032306
"{\n"
@@ -4184,8 +4187,8 @@ class TestMemleakInClass : public TestFixture
41844187
"private:\n"
41854188
" char *s;\n"
41864189
"};\n");
4187-
ASSERT_EQUALS("", errout.str());
4188-
TODO_ASSERT_EQUALS("publicAllocation", errout.str());
4190+
TODO_ASSERT_EQUALS("publicAllocation",
4191+
"", errout.str());
41894192
}
41904193

41914194
void func2()

0 commit comments

Comments
 (0)