Skip to content

Commit 1b6da06

Browse files
committed
C++: RangeAnalysis tests for bool conversions
1 parent 5ffc959 commit 1b6da06

File tree

6 files changed

+131
-2
lines changed

6 files changed

+131
-2
lines changed

cpp/ql/test/library-tests/rangeanalysis/SimpleRangeAnalysis/lowerBound.expected

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,25 @@
563563
| test.c:601:7:601:7 | n | -32768 |
564564
| test.c:601:22:601:22 | n | -32767 |
565565
| test.c:602:9:602:9 | n | -32766 |
566+
| test.c:605:7:605:7 | n | -32768 |
567+
| test.c:606:5:606:5 | n | 0 |
568+
| test.c:606:10:606:10 | n | 0 |
569+
| test.c:606:14:606:14 | n | 0 |
570+
| test.c:607:6:607:6 | n | 0 |
571+
| test.c:607:10:607:10 | n | 0 |
572+
| test.c:607:14:607:14 | n | 0 |
573+
| test.c:618:7:618:8 | ss | -32768 |
574+
| test.c:619:9:619:10 | ss | 0 |
575+
| test.c:622:7:622:8 | ss | -32768 |
576+
| test.c:623:9:623:10 | ss | -32768 |
577+
| test.c:626:14:626:15 | us | 0 |
578+
| test.c:627:9:627:10 | us | 0 |
579+
| test.c:630:14:630:15 | us | 0 |
580+
| test.c:631:9:631:10 | us | 0 |
581+
| test.c:634:7:634:8 | ss | -32768 |
582+
| test.c:635:9:635:10 | ss | -32768 |
583+
| test.c:638:7:638:8 | ss | -32768 |
584+
| test.c:639:9:639:10 | ss | -1 |
566585
| test.cpp:10:7:10:7 | b | -2147483648 |
567586
| test.cpp:11:5:11:5 | x | -2147483648 |
568587
| test.cpp:13:10:13:10 | x | -2147483648 |
@@ -616,3 +635,16 @@
616635
| test.cpp:97:10:97:10 | i | -2147483648 |
617636
| test.cpp:97:22:97:22 | i | -2147483648 |
618637
| test.cpp:98:5:98:5 | i | -2147483648 |
638+
| test.cpp:105:7:105:7 | n | -32768 |
639+
| test.cpp:108:7:108:7 | n | 0 |
640+
| test.cpp:109:5:109:5 | n | 0 |
641+
| test.cpp:111:5:111:5 | n | 0 |
642+
| test.cpp:114:8:114:8 | n | 0 |
643+
| test.cpp:115:5:115:5 | n | 0 |
644+
| test.cpp:117:5:117:5 | n | 0 |
645+
| test.cpp:120:3:120:3 | n | 0 |
646+
| test.cpp:120:8:120:8 | n | 0 |
647+
| test.cpp:120:12:120:12 | n | 0 |
648+
| test.cpp:121:4:121:4 | n | 0 |
649+
| test.cpp:121:8:121:8 | n | 0 |
650+
| test.cpp:121:12:121:12 | n | 0 |

cpp/ql/test/library-tests/rangeanalysis/SimpleRangeAnalysis/ternaryLower.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@
1313
| test.c:386:10:386:21 | ... ? ... : ... | 100.0 | 100.0 | 5.0 |
1414
| test.c:387:10:387:38 | ... ? ... : ... | 0.0 | 100.0 | 5.0 |
1515
| test.c:394:20:394:36 | ... ? ... : ... | 0.0 | 0.0 | 100.0 |
16+
| test.c:606:5:606:14 | ... ? ... : ... | 0.0 | 0.0 | 0.0 |
17+
| test.c:607:5:607:14 | ... ? ... : ... | 0.0 | 0.0 | 0.0 |
18+
| test.cpp:120:3:120:12 | ... ? ... : ... | 0.0 | 0.0 | 0.0 |
19+
| test.cpp:121:3:121:12 | ... ? ... : ... | 0.0 | 0.0 | 0.0 |

cpp/ql/test/library-tests/rangeanalysis/SimpleRangeAnalysis/ternaryUpper.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@
1313
| test.c:386:10:386:21 | ... ? ... : ... | 4.294967295E9 | 4.294967295E9 | 5.0 |
1414
| test.c:387:10:387:38 | ... ? ... : ... | 255.0 | 4.294967295E9 | 5.0 |
1515
| test.c:394:20:394:36 | ... ? ... : ... | 100.0 | 99.0 | 100.0 |
16+
| test.c:606:5:606:14 | ... ? ... : ... | 32767.0 | 32767.0 | 32767.0 |
17+
| test.c:607:5:607:14 | ... ? ... : ... | 32767.0 | 32767.0 | 32767.0 |
18+
| test.cpp:120:3:120:12 | ... ? ... : ... | 32767.0 | 32767.0 | 32767.0 |
19+
| test.cpp:121:3:121:12 | ... ? ... : ... | 32767.0 | 32767.0 | 32767.0 |

cpp/ql/test/library-tests/rangeanalysis/SimpleRangeAnalysis/test.c

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ int notequal_type_endpoint(unsigned n) {
549549
}
550550

551551
if (!n) {
552-
out(n); // 0 .. 0
552+
out(n); // 0 .. 0 [BUG: upper bound is deduced to be 2^32-1]
553553
} else {
554554
out(n); // 1 .. [BUG: lower bound is deduced to be 0]
555555
}
@@ -574,7 +574,7 @@ void notequal_refinement(short n) {
574574
if (n) {
575575
out(n); // 1 .. [BUG: lower bound is deduced to be 0]
576576
} else {
577-
out(n); // 0 .. 0
577+
out(n); // 0 .. 0 [BUG: upper bound is deduced to be 32767]
578578
}
579579

580580
while (n != 0) {
@@ -601,4 +601,41 @@ void notequal_variations(short n, float f) {
601601
if (n != -32768 && n != -32767) {
602602
out(n); // -32766 ..
603603
}
604+
605+
if (n >= 0) {
606+
n ? n : n; // ? 1.. : 0..0 [BUG: no useful bounds]
607+
!n ? n : n; // ? 0..0 : 1.. [BUG: no useful bounds]
608+
}
609+
}
610+
611+
void two_bounds_from_one_test(short ss, unsigned short us) {
612+
// These tests demonstrate how the range analysis is often able to deduce
613+
// both an upper bound and a lower bound even when there is only one
614+
// inequality in the source. For example `signedInt < 4U` establishes that
615+
// `signedInt >= 0` since if `signedInt` were negative then it would be
616+
// greater than 4 in the unsigned comparison.
617+
618+
if (ss < sizeof(int)) { // Lower bound added in `linearBoundFromGuard`
619+
out(ss); // 0 .. 3
620+
}
621+
622+
if (ss < 0x8001) { // Lower bound removed in `getDefLowerBounds`
623+
out(ss); // -32768 .. 32767
624+
}
625+
626+
if ((short)us >= 0) {
627+
out(us); // 0 .. 32767
628+
}
629+
630+
if ((short)us >= -1) {
631+
out(us); // 0 .. 65535
632+
}
633+
634+
if (ss >= sizeof(int)) { // test is true for negative numbers
635+
out(ss); // -32768 .. 32767
636+
}
637+
638+
if (ss + 1 < sizeof(int)) {
639+
out(ss); // -1 .. 2
640+
}
604641
}

cpp/ql/test/library-tests/rangeanalysis/SimpleRangeAnalysis/test.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,23 @@ int ref_to_number(int &i, const int &ci, int &aliased) {
100100

101101
return 0;
102102
}
103+
104+
void notequal_refinement(short n) {
105+
if (n < 0)
106+
return;
107+
108+
if (n) {
109+
n; // 1 .. [BUG: lower bound is deduced to be 0]
110+
} else {
111+
n; // 0 .. 0 [BUG: upper bound is deduced to be 32767]
112+
}
113+
114+
if (!n) {
115+
n; // 0 .. 0 [BUG: upper bound is deduced to be 32767]
116+
} else {
117+
n; // 1 .. [BUG: lower bound is deduced to be 0]
118+
}
119+
120+
n ? n : n; // ? 1.. : 0..0 [BUG: no useful bounds]
121+
!n ? n : n; // ? 0..0 : 1.. [BUG: no useful bounds]
122+
}

cpp/ql/test/library-tests/rangeanalysis/SimpleRangeAnalysis/upperBound.expected

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,25 @@
563563
| test.c:601:7:601:7 | n | 32767 |
564564
| test.c:601:22:601:22 | n | 32767 |
565565
| test.c:602:9:602:9 | n | 32767 |
566+
| test.c:605:7:605:7 | n | 32767 |
567+
| test.c:606:5:606:5 | n | 32767 |
568+
| test.c:606:10:606:10 | n | 32767 |
569+
| test.c:606:14:606:14 | n | 32767 |
570+
| test.c:607:6:607:6 | n | 32767 |
571+
| test.c:607:10:607:10 | n | 32767 |
572+
| test.c:607:14:607:14 | n | 32767 |
573+
| test.c:618:7:618:8 | ss | 32767 |
574+
| test.c:619:9:619:10 | ss | 3 |
575+
| test.c:622:7:622:8 | ss | 32767 |
576+
| test.c:623:9:623:10 | ss | 32767 |
577+
| test.c:626:14:626:15 | us | 65535 |
578+
| test.c:627:9:627:10 | us | 32767 |
579+
| test.c:630:14:630:15 | us | 65535 |
580+
| test.c:631:9:631:10 | us | 65535 |
581+
| test.c:634:7:634:8 | ss | 32767 |
582+
| test.c:635:9:635:10 | ss | 32767 |
583+
| test.c:638:7:638:8 | ss | 32767 |
584+
| test.c:639:9:639:10 | ss | 2 |
566585
| test.cpp:10:7:10:7 | b | 2147483647 |
567586
| test.cpp:11:5:11:5 | x | 2147483647 |
568587
| test.cpp:13:10:13:10 | x | 2147483647 |
@@ -616,3 +635,16 @@
616635
| test.cpp:97:10:97:10 | i | 65535 |
617636
| test.cpp:97:22:97:22 | i | 32767 |
618637
| test.cpp:98:5:98:5 | i | 32767 |
638+
| test.cpp:105:7:105:7 | n | 32767 |
639+
| test.cpp:108:7:108:7 | n | 32767 |
640+
| test.cpp:109:5:109:5 | n | 32767 |
641+
| test.cpp:111:5:111:5 | n | 32767 |
642+
| test.cpp:114:8:114:8 | n | 32767 |
643+
| test.cpp:115:5:115:5 | n | 32767 |
644+
| test.cpp:117:5:117:5 | n | 32767 |
645+
| test.cpp:120:3:120:3 | n | 32767 |
646+
| test.cpp:120:8:120:8 | n | 32767 |
647+
| test.cpp:120:12:120:12 | n | 32767 |
648+
| test.cpp:121:4:121:4 | n | 32767 |
649+
| test.cpp:121:8:121:8 | n | 32767 |
650+
| test.cpp:121:12:121:12 | n | 32767 |

0 commit comments

Comments
 (0)