diff options
Diffstat (limited to 'lib/test_overflow.c')
-rw-r--r-- | lib/test_overflow.c | 54 |
1 files changed, 39 insertions, 15 deletions
diff --git a/lib/test_overflow.c b/lib/test_overflow.c index fa87e283b149..501ed86205c4 100644 --- a/lib/test_overflow.c +++ b/lib/test_overflow.c | |||
@@ -220,30 +220,38 @@ DEFINE_TEST_ARRAY(s64) = { | |||
220 | pr_warn("expected "fmt" "sym" "fmt \ | 220 | pr_warn("expected "fmt" "sym" "fmt \ |
221 | " to%s overflow (type %s)\n", \ | 221 | " to%s overflow (type %s)\n", \ |
222 | a, b, of ? "" : " not", #t); \ | 222 | a, b, of ? "" : " not", #t); \ |
223 | err = 1; \ | ||
223 | } \ | 224 | } \ |
224 | if (_r != r) { \ | 225 | if (_r != r) { \ |
225 | pr_warn("expected "fmt" "sym" "fmt" == " \ | 226 | pr_warn("expected "fmt" "sym" "fmt" == " \ |
226 | fmt", got "fmt" (type %s)\n", \ | 227 | fmt", got "fmt" (type %s)\n", \ |
227 | a, b, r, _r, #t); \ | 228 | a, b, r, _r, #t); \ |
229 | err = 1; \ | ||
228 | } \ | 230 | } \ |
229 | } while (0) | 231 | } while (0) |
230 | 232 | ||
231 | #define DEFINE_TEST_FUNC(t, fmt) \ | 233 | #define DEFINE_TEST_FUNC(t, fmt) \ |
232 | static void __init do_test_ ## t(const struct test_ ## t *p) \ | 234 | static int __init do_test_ ## t(const struct test_ ## t *p) \ |
233 | { \ | 235 | { \ |
236 | int err = 0; \ | ||
237 | \ | ||
234 | check_one_op(t, fmt, add, "+", p->a, p->b, p->sum, p->s_of); \ | 238 | check_one_op(t, fmt, add, "+", p->a, p->b, p->sum, p->s_of); \ |
235 | check_one_op(t, fmt, add, "+", p->b, p->a, p->sum, p->s_of); \ | 239 | check_one_op(t, fmt, add, "+", p->b, p->a, p->sum, p->s_of); \ |
236 | check_one_op(t, fmt, sub, "-", p->a, p->b, p->diff, p->d_of); \ | 240 | check_one_op(t, fmt, sub, "-", p->a, p->b, p->diff, p->d_of); \ |
237 | check_one_op(t, fmt, mul, "*", p->a, p->b, p->prod, p->p_of); \ | 241 | check_one_op(t, fmt, mul, "*", p->a, p->b, p->prod, p->p_of); \ |
238 | check_one_op(t, fmt, mul, "*", p->b, p->a, p->prod, p->p_of); \ | 242 | check_one_op(t, fmt, mul, "*", p->b, p->a, p->prod, p->p_of); \ |
243 | \ | ||
244 | return err; \ | ||
239 | } \ | 245 | } \ |
240 | \ | 246 | \ |
241 | static void __init test_ ## t ## _overflow(void) { \ | 247 | static int __init test_ ## t ## _overflow(void) { \ |
248 | int err = 0; \ | ||
242 | unsigned i; \ | 249 | unsigned i; \ |
243 | \ | 250 | \ |
244 | pr_info("%-3s: %zu tests\n", #t, ARRAY_SIZE(t ## _tests)); \ | 251 | pr_info("%-3s: %zu tests\n", #t, ARRAY_SIZE(t ## _tests)); \ |
245 | for (i = 0; i < ARRAY_SIZE(t ## _tests); ++i) \ | 252 | for (i = 0; i < ARRAY_SIZE(t ## _tests); ++i) \ |
246 | do_test_ ## t(&t ## _tests[i]); \ | 253 | err |= do_test_ ## t(&t ## _tests[i]); \ |
254 | return err; \ | ||
247 | } | 255 | } |
248 | 256 | ||
249 | DEFINE_TEST_FUNC(u8, "%d"); | 257 | DEFINE_TEST_FUNC(u8, "%d"); |
@@ -257,27 +265,43 @@ DEFINE_TEST_FUNC(u64, "%llu"); | |||
257 | DEFINE_TEST_FUNC(s64, "%lld"); | 265 | DEFINE_TEST_FUNC(s64, "%lld"); |
258 | #endif | 266 | #endif |
259 | 267 | ||
260 | static int __init test_overflow(void) | 268 | static int __init test_overflow_calculation(void) |
261 | { | 269 | { |
262 | test_u8_overflow(); | 270 | int err = 0; |
263 | test_s8_overflow(); | 271 | |
264 | test_u16_overflow(); | 272 | err |= test_u8_overflow(); |
265 | test_s16_overflow(); | 273 | err |= test_s8_overflow(); |
266 | test_u32_overflow(); | 274 | err |= test_u16_overflow(); |
267 | test_s32_overflow(); | 275 | err |= test_s16_overflow(); |
276 | err |= test_u32_overflow(); | ||
277 | err |= test_s32_overflow(); | ||
268 | #if BITS_PER_LONG == 64 | 278 | #if BITS_PER_LONG == 64 |
269 | test_u64_overflow(); | 279 | err |= test_u64_overflow(); |
270 | test_s64_overflow(); | 280 | err |= test_s64_overflow(); |
271 | #endif | 281 | #endif |
272 | 282 | ||
273 | pr_info("done\n"); | 283 | return err; |
284 | } | ||
285 | |||
286 | static int __init test_module_init(void) | ||
287 | { | ||
288 | int err = 0; | ||
289 | |||
290 | err |= test_overflow_calculation(); | ||
291 | |||
292 | if (err) { | ||
293 | pr_warn("FAIL!\n"); | ||
294 | err = -EINVAL; | ||
295 | } else { | ||
296 | pr_info("all tests passed\n"); | ||
297 | } | ||
274 | 298 | ||
275 | return 0; | 299 | return err; |
276 | } | 300 | } |
277 | 301 | ||
278 | static void __exit test_module_exit(void) | 302 | static void __exit test_module_exit(void) |
279 | { } | 303 | { } |
280 | 304 | ||
281 | module_init(test_overflow); | 305 | module_init(test_module_init); |
282 | module_exit(test_module_exit); | 306 | module_exit(test_module_exit); |
283 | MODULE_LICENSE("Dual MIT/GPL"); | 307 | MODULE_LICENSE("Dual MIT/GPL"); |