@@ -47,7 +47,7 @@ class TestCondition : public TestFixture {
4747
4848 TEST_CASE (assignAndCompare); // assignment and comparison don't match
4949 TEST_CASE (mismatchingBitAnd); // overlapping bitmasks
50- TEST_CASE (compare ); // mismatching LHS/RHS in comparison
50+ TEST_CASE (comparison ); // CheckCondition::comparison test cases
5151 TEST_CASE (multicompare); // mismatching comparisons
5252 TEST_CASE (duplicateIf); // duplicate conditions in if and else-if
5353
@@ -318,30 +318,64 @@ class TestCondition : public TestFixture {
318318 ASSERT_EQUALS (" " , errout.str ());
319319 }
320320
321- void compare () {
322- check (" void foo(int x)\n "
323- " {\n "
324- " if ((x & 4) == 3);\n "
325- " }" );
326- ASSERT_EQUALS (" [test.cpp:3]: (style) Expression '(X & 0x4) == 0x3' is always false.\n " , errout.str ());
327-
328- check (" void foo(int x)\n "
329- " {\n "
330- " if ((x | 4) == 3);\n "
331- " }" );
332- ASSERT_EQUALS (" [test.cpp:3]: (style) Expression '(X | 0x4) == 0x3' is always false.\n " , errout.str ());
333-
334- check (" void foo(int x)\n "
335- " {\n "
336- " if ((x | 4) != 3);\n "
337- " }" );
338- ASSERT_EQUALS (" [test.cpp:3]: (style) Expression '(X | 0x4) != 0x3' is always true.\n " , errout.str ());
339-
340- check (" void foo(int x)\n "
341- " {\n "
342- " if ((x & y & 4 & z ) == 3);\n "
343- " }" );
344- ASSERT_EQUALS (" [test.cpp:3]: (style) Expression '(X & 0x4) == 0x3' is always false.\n " , errout.str ());
321+ void comparison () {
322+ // CheckCondition::comparison test cases
323+ // '=='
324+ check (" void f(int a) {\n assert( (a & 0x07) == 8U );\n }" );
325+ ASSERT_EQUALS (" [test.cpp:2]: (style) Expression '(X & 0x7) == 0x8' is always false.\n " ,errout.str ());
326+ check (" void f(int a) {\n assert( (a & b & 4 & c ) == 3 );\n }" );
327+ ASSERT_EQUALS (" [test.cpp:2]: (style) Expression '(X & 0x4) == 0x3' is always false.\n " , errout.str ());
328+ check (" void f(int a) {\n assert( (a | 0x07) == 8U );\n }" );
329+ ASSERT_EQUALS (" [test.cpp:2]: (style) Expression '(X | 0x7) == 0x8' is always false.\n " ,errout.str ());
330+ check (" void f(int a) {\n assert( (a & 0x07) == 7U );\n }" );
331+ ASSERT_EQUALS (" " , errout.str ());
332+ check (" void f(int a) {\n assert( (a | 0x01) == -15 );\n }" );
333+ ASSERT_EQUALS (" " , errout.str ());
334+ // '!='
335+ check (" void f(int a) {\n assert( (a & 0x07) != 8U );\n }" );
336+ ASSERT_EQUALS (" [test.cpp:2]: (style) Expression '(X & 0x7) != 0x8' is always true.\n " ,errout.str ());
337+ check (" void f(int a) {\n assert( (a | 0x07) != 8U );\n }" );
338+ ASSERT_EQUALS (" [test.cpp:2]: (style) Expression '(X | 0x7) != 0x8' is always true.\n " ,errout.str ());
339+ check (" void f(int a) {\n assert( (a & 0x07) != 7U );\n }" );
340+ ASSERT_EQUALS (" " , errout.str ());
341+ check (" void f(int a) {\n assert( (a | 0x07) != 7U );\n }" );
342+ ASSERT_EQUALS (" " , errout.str ());
343+ // '>='
344+ check (" void f(int a) {\n assert( (a & 0x07) >= 8U );\n }" );
345+ ASSERT_EQUALS (" [test.cpp:2]: (style) Expression '(X & 0x7) >= 0x8' is always false.\n " ,errout.str ());
346+ check (" void f(unsigned int a) {\n assert( (a | 0x7) >= 7U );\n }" );
347+ ASSERT_EQUALS (" [test.cpp:2]: (style) Expression '(X | 0x7) >= 0x7' is always true.\n " ,errout.str ());
348+ check (" void f(int a) {\n assert( (a & 0x07) >= 7U );\n }" );
349+ ASSERT_EQUALS (" " ,errout.str ());
350+ check (" void f(int a) {\n assert( (a | 0x07) >= 8U );\n }" );
351+ ASSERT_EQUALS (" " ,errout.str ()); // correct for negative 'a'
352+ // '>'
353+ check (" void f(int a) {\n assert( (a & 0x07) > 7U );\n }" );
354+ ASSERT_EQUALS (" [test.cpp:2]: (style) Expression '(X & 0x7) > 0x7' is always false.\n " ,errout.str ());
355+ check (" void f(unsigned int a) {\n assert( (a | 0x7) > 6U );\n }" );
356+ ASSERT_EQUALS (" [test.cpp:2]: (style) Expression '(X | 0x7) > 0x6' is always true.\n " ,errout.str ());
357+ check (" void f(int a) {\n assert( (a & 0x07) > 6U );\n }" );
358+ ASSERT_EQUALS (" " ,errout.str ());
359+ check (" void f(int a) {\n assert( (a | 0x07) > 7U );\n }" );
360+ ASSERT_EQUALS (" " ,errout.str ()); // correct for negative 'a'
361+ // '<='
362+ check (" void f(int a) {\n assert( (a & 0x07) <= 7U );\n }" );
363+ ASSERT_EQUALS (" [test.cpp:2]: (style) Expression '(X & 0x7) <= 0x7' is always true.\n " ,errout.str ());
364+ check (" void f(unsigned int a) {\n assert( (a | 0x08) <= 7U );\n }" );
365+ ASSERT_EQUALS (" [test.cpp:2]: (style) Expression '(X | 0x8) <= 0x7' is always false.\n " ,errout.str ());
366+ check (" void f(int a) {\n assert( (a & 0x07) <= 6U );\n }" );
367+ ASSERT_EQUALS (" " ,errout.str ());
368+ check (" void f(int a) {\n assert( (a | 0x08) <= 7U );\n }" );
369+ ASSERT_EQUALS (" " ,errout.str ()); // correct for negative 'a'
370+ // '<'
371+ check (" void f(int a) {\n assert( (a & 0x07) < 8U );\n }" );
372+ ASSERT_EQUALS (" [test.cpp:2]: (style) Expression '(X & 0x7) < 0x8' is always true.\n " ,errout.str ());
373+ check (" void f(unsigned int a) {\n assert( (a | 0x07) < 7U );\n }" );
374+ ASSERT_EQUALS (" [test.cpp:2]: (style) Expression '(X | 0x7) < 0x7' is always false.\n " ,errout.str ());
375+ check (" void f(int a) {\n assert( (a & 0x07) < 3U );\n }" );
376+ ASSERT_EQUALS (" " ,errout.str ());
377+ check (" void f(int a) {\n assert( (a | 0x07) < 7U );\n }" );
378+ ASSERT_EQUALS (" " ,errout.str ()); // correct for negative 'a'
345379 }
346380
347381 void multicompare () {
@@ -592,75 +626,6 @@ class TestCondition : public TestFixture {
592626 " }\n "
593627 " }" );
594628 ASSERT_EQUALS (" " , errout.str ());
595-
596- // Various bitmask comparison checks
597- // #7428 false negative: condition '(a&7)>7U' is always false
598- // #7522 false positive: condition '(X|7)>=6' is correct
599-
600- check (" void f() {\n "
601- " assert( (a & 0x07) == 8U );\n " // statement always false
602- " }" );
603- ASSERT_EQUALS (" [test.cpp:2]: (style) Expression '(X & 0x7) == 0x8' is always false.\n " ,
604- errout.str ());
605-
606- check (" void f() {\n "
607- " assert( (a & 0x07) == 7U );\n " // statement correct
608- " assert( (a & 0x07) == 6U );\n " // statement correct
609- " }" );
610- ASSERT_EQUALS (" " , errout.str ());
611-
612- check (" void f() {\n "
613- " assert( (a | 0x07) == 8U );\n " // statement always false
614- " }" );
615- ASSERT_EQUALS (" [test.cpp:2]: (style) Expression '(X | 0x7) == 0x8' is always false.\n " ,
616- errout.str ());
617-
618- check (" void f() {\n "
619- " assert( (a | 0x07) == 7U );\n " // statement correct
620- " assert( (a | 0x07) == 23U );\n " // statement correct
621- " }" );
622- ASSERT_EQUALS (" " , errout.str ());
623-
624- check (" void f() {\n "
625- " assert( (a & 0x07) != 8U );\n " // statement always true
626- " }" );
627- ASSERT_EQUALS (" [test.cpp:2]: (style) Expression '(X & 0x7) != 0x8' is always true.\n " ,
628- errout.str ());
629-
630- check (" void f() {\n "
631- " assert( (a & 0x07) != 7U );\n " // statement correct
632- " assert( (a & 0x07) != 0U );\n " // statement correct
633- " }" );
634- ASSERT_EQUALS (" " , errout.str ());
635-
636- check (" void f() {\n "
637- " assert( (a | 0x07) != 8U );\n " // statement always true
638- " }" );
639- ASSERT_EQUALS (" [test.cpp:2]: (style) Expression '(X | 0x7) != 0x8' is always true.\n " ,
640- errout.str ());
641-
642- check (" void f() {\n "
643- " assert( (a | 0x07) != 7U );\n " // statement correct
644- " }" );
645- ASSERT_EQUALS (" " , errout.str ());
646-
647- // TRAC #7428 false negative: condition '(X&7)>7' is always false
648- check (" void f() {\n "
649- " assert( (a & 0x07) > 7U );\n " // statement always false
650- " }" );
651- ASSERT_EQUALS (" [test.cpp:2]: (style) Expression '(X & 0x7) > 0x7' is always false.\n " ,
652- errout.str ());
653-
654- check (" void f() {\n "
655- " assert( (a & 0x07) > 6U );\n " // statement correct
656- " }" );
657- ASSERT_EQUALS (" " , errout.str ());
658-
659- // TRAC #7522 false positive: condition '(X|7)>=6' is correct (X can be negative)
660- check (" void f() {\n "
661- " assert( (a | 0x07) >= 6U );\n " // statement correct (X can be negative)
662- " }" );
663- ASSERT_EQUALS (" " ,errout.str ());
664629 }
665630
666631
0 commit comments