Debian Codegraph @

tests/overflow.c

raw
1 /* Copyright (C) 2008 Vincent Penquerc'h.
2 This file is part of the Kate codec library.
3 Written by Vincent Penquerc'h.
4
5 Use, distribution and reproduction of this library is governed
6 by a BSD style source license included with this source in the
7 file 'COPYING'. Please read these terms before distributing. */
8
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include "kate/kate.h"
14 #include "../src/kate_internal.h"
15
16 #define CHKOVF(test,f) \
17 do { \
18 size_t res; \
19 int ret=f(test.x,test.y,&res); \
20 if (test.ret!=ret) { \
21 fprintf(stderr,"Failure: " #f " %zu %zu returned %d, expected %d\n",test.x,test.y,ret,test.ret); \
22 exit(1); \
23 } \
24 if (!ret && test.res!=res) { \
25 fprintf(stderr,"Failure: " #f " %zu %zu yields %zu, expected %zu\n",test.x,test.y,res,test.res); \
26 exit(1); \
27 } \
28 } while(0)
29
30 #define CHKADDOVF(test) CHKOVF(test,kate_check_add_overflow)
31 #define CHKMULOVF(test) CHKOVF(test,kate_check_mul_overflow)
32 #define CHKMULGOVF(test) CHKOVF(test,kate_check_mul_overflow_generic)
33
34 static const struct {
35 size_t x,y;
36 int ret;
37 size_t res;
38 } add_tests[]={
39 {0,0,0,0},
40 {0,(size_t)-1,0,(size_t)-1},
41 {1,(size_t)-1,KATE_E_LIMIT,0},
42 {2,(size_t)-2,KATE_E_LIMIT,0},
43 {((size_t)-1)/2,((size_t)-1)/2+1,0,(size_t)-1},
44 };
45
46 static const struct {
47 size_t x,y;
48 int ret;
49 size_t res;
50 } mul_tests[]={
51 {0,0,0,0},
52 {0,(size_t)-1,0,0},
53 {(size_t)-1,0,0,0},
54 {(size_t)-1,(size_t)-1,KATE_E_LIMIT,0},
55 {(size_t)-1,1,0,(size_t)-1},
56 {1,(size_t)-1,0,(size_t)-1},
57 {((size_t)-1)/2,2,0,((size_t)-1)&~1},
58 {((size_t)-1)/2+1,2,KATE_E_LIMIT,0},
59 };
60
61 int main()
62 {
63 size_t n;
64 for (n=0;n<sizeof(add_tests)/sizeof(add_tests[0]);++n) {
65 CHKADDOVF(add_tests[n]);
66 }
67 for (n=0;n<sizeof(mul_tests)/sizeof(mul_tests[0]);++n) {
68 CHKMULOVF(mul_tests[n]);
69 CHKMULGOVF(mul_tests[n]);
70 }
71 return 0;
72 }
73