Debian Codegraph @

lib/util/test_overflow.c

raw
1 /// @file
2 /// @brief unit test for overflow.h
3
4 #include <assert.h>
5 #include <limits.h>
6 #include <stdbool.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9
10 // were we asked to test the fallback C code?
11 #ifdef SUPPRESS_BUILTINS
12 #ifdef __GNUC__
13 #undef __GNUC__
14 #endif
15 #ifdef __clang__
16 #undef __clang__
17 #endif
18 #endif
19
20 #include <util/overflow.h>
21
22 /// a test case
23 typedef struct {
24 int operand1; ///< left-hand side
25 int operand2; ///< right-hand side
26 bool should_overflow; ///< should addition be expected to overflow?
27 } testcase_t;
28
29 /// cases to run
30 static const testcase_t TESTS[] = {
31 {.operand1 = 0, .operand2 = 0},
32 {.operand1 = 0, .operand2 = 1},
33 {.operand1 = 0, .operand2 = -1},
34 {.operand1 = 0, .operand2 = 2},
35 {.operand1 = 0, .operand2 = -2},
36 {.operand1 = 0, .operand2 = INT_MAX},
37 {.operand1 = 0, .operand2 = INT_MIN},
38 {.operand1 = 0, .operand2 = INT_MAX - 1},
39 {.operand1 = 0, .operand2 = INT_MIN + 1},
40
41 {.operand1 = 1, .operand2 = 0},
42 {.operand1 = 1, .operand2 = 1},
43 {.operand1 = 1, .operand2 = -1},
44 {.operand1 = 1, .operand2 = 2},
45 {.operand1 = 1, .operand2 = -2},
46 {.operand1 = 1, .operand2 = INT_MAX, .should_overflow = true},
47 {.operand1 = 1, .operand2 = INT_MIN},
48 {.operand1 = 1, .operand2 = INT_MAX - 1},
49 {.operand1 = 1, .operand2 = INT_MIN + 1},
50
51 {.operand1 = -1, .operand2 = 0},
52 {.operand1 = -1, .operand2 = 1},
53 {.operand1 = -1, .operand2 = -1},
54 {.operand1 = -1, .operand2 = 2},
55 {.operand1 = -1, .operand2 = -2},
56 {.operand1 = -1, .operand2 = INT_MAX},
57 {.operand1 = -1, .operand2 = INT_MIN, .should_overflow = true},
58 {.operand1 = -1, .operand2 = INT_MAX - 1},
59 {.operand1 = -1, .operand2 = INT_MIN + 1},
60
61 {.operand1 = 2, .operand2 = 0},
62 {.operand1 = 2, .operand2 = 1},
63 {.operand1 = 2, .operand2 = -1},
64 {.operand1 = 2, .operand2 = 2},
65 {.operand1 = 2, .operand2 = -2},
66 {.operand1 = 2, .operand2 = INT_MAX, .should_overflow = true},
67 {.operand1 = 2, .operand2 = INT_MIN},
68 {.operand1 = 2, .operand2 = INT_MAX - 1, .should_overflow = true},
69 {.operand1 = 2, .operand2 = INT_MIN + 1},
70
71 {.operand1 = -2, .operand2 = 0},
72 {.operand1 = -2, .operand2 = 1},
73 {.operand1 = -2, .operand2 = -1},
74 {.operand1 = -2, .operand2 = 2},
75 {.operand1 = -2, .operand2 = -2},
76 {.operand1 = -2, .operand2 = INT_MAX},
77 {.operand1 = -2, .operand2 = INT_MIN, .should_overflow = true},
78 {.operand1 = -2, .operand2 = INT_MAX - 1},
79 {.operand1 = -2, .operand2 = INT_MIN + 1, .should_overflow = true},
80
81 {.operand1 = INT_MAX, .operand2 = 0},
82 {.operand1 = INT_MAX, .operand2 = 1, .should_overflow = true},
83 {.operand1 = INT_MAX, .operand2 = -1},
84 {.operand1 = INT_MAX, .operand2 = 2, .should_overflow = true},
85 {.operand1 = INT_MAX, .operand2 = -2},
86 {.operand1 = INT_MAX, .operand2 = INT_MAX, .should_overflow = true},
87 {.operand1 = INT_MAX, .operand2 = INT_MIN},
88 {.operand1 = INT_MAX, .operand2 = INT_MAX - 1, .should_overflow = true},
89 {.operand1 = INT_MAX, .operand2 = INT_MIN + 1},
90
91 {.operand1 = INT_MIN, .operand2 = 0},
92 {.operand1 = INT_MIN, .operand2 = 1},
93 {.operand1 = INT_MIN, .operand2 = -1, .should_overflow = true},
94 {.operand1 = INT_MIN, .operand2 = 2},
95 {.operand1 = INT_MIN, .operand2 = -2, .should_overflow = true},
96 {.operand1 = INT_MIN, .operand2 = INT_MAX},
97 {.operand1 = INT_MIN, .operand2 = INT_MIN, .should_overflow = true},
98 {.operand1 = INT_MIN, .operand2 = INT_MAX - 1},
99 {.operand1 = INT_MIN, .operand2 = INT_MIN + 1, .should_overflow = true},
100
101 {.operand1 = INT_MAX - 1, .operand2 = 0},
102 {.operand1 = INT_MAX - 1, .operand2 = 1},
103 {.operand1 = INT_MAX - 1, .operand2 = -1},
104 {.operand1 = INT_MAX - 1, .operand2 = 2, .should_overflow = true},
105 {.operand1 = INT_MAX - 1, .operand2 = -2},
106 {.operand1 = INT_MAX - 1, .operand2 = INT_MAX, .should_overflow = true},
107 {.operand1 = INT_MAX - 1, .operand2 = INT_MIN},
108 {.operand1 = INT_MAX - 1, .operand2 = INT_MAX - 1, .should_overflow = true},
109 {.operand1 = INT_MAX - 1, .operand2 = INT_MIN + 1},
110
111 {.operand1 = INT_MIN + 1, .operand2 = 0},
112 {.operand1 = INT_MIN + 1, .operand2 = 1},
113 {.operand1 = INT_MIN + 1, .operand2 = -1},
114 {.operand1 = INT_MIN + 1, .operand2 = 2},
115 {.operand1 = INT_MIN + 1, .operand2 = -2, .should_overflow = true},
116 {.operand1 = INT_MIN + 1, .operand2 = INT_MAX},
117 {.operand1 = INT_MIN + 1, .operand2 = INT_MIN, .should_overflow = true},
118 {.operand1 = INT_MIN + 1, .operand2 = INT_MAX - 1},
119 {.operand1 = INT_MIN + 1, .operand2 = INT_MIN + 1, .should_overflow = true},
120 };
121
122 int main(void) {
123
124 for (size_t i = 0; i < sizeof(TESTS) / sizeof(TESTS[0]); ++i) {
125
126 const int op1 = TESTS[i].operand1;
127 const int op2 = TESTS[i].operand2;
128
129 int answer;
130 const bool did_overflow = sadd_overflow(op1, op2, &answer);
131
132 const bool should_overflow = TESTS[i].should_overflow;
133 if (did_overflow != should_overflow) {
134 fprintf(stderr, "%d + %d should%s have overflowed and it did%s\n", op1,
135 op2, should_overflow ? "" : " not", did_overflow ? "" : " not");
136 return EXIT_FAILURE;
137 }
138
139 if (!did_overflow && op1 + op2 != answer) {
140 fprintf(stderr, "%d + %d is %d but result was %d\n", op1, op2, op1 + op2,
141 answer);
142 return EXIT_FAILURE;
143 }
144 }
145
146 return EXIT_SUCCESS;
147 }