Debian Codegraph @

regressions/ck_pr/validate/ck_pr_unary.c

raw
1 /*
2 * Copyright 2011 David Joseph.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <ck_pr.h>
31
32 #define REPEAT 2000000
33
34 #define TEST_UNARY(K, S, M, T, P, D, H) \
35 static void \
36 test_##K##_##S(M *target) \
37 { \
38 *target = *target P 1; \
39 \
40 return; \
41 } \
42 static void \
43 test_##K##_##S##_zero(M *target, bool *zero) \
44 { \
45 *zero = *target == H; \
46 *target = *target P 1; \
47 \
48 return; \
49 } \
50 static void \
51 run_test_##K##_##S(bool use_zero) \
52 { \
53 int i; \
54 T x = 1, y = 1; \
55 bool zero_x = false, zero_y = false; \
56 \
57 use_zero ? puts("***TESTING ck_pr_"#K"_"#S"_zero***") \
58 : puts("***TESTING ck_pr_"#K"_"#S"***"); \
59 for (i = 0; i < REPEAT; ++i) { \
60 if (use_zero) { \
61 test_##K##_##S##_zero(&x, &zero_x); \
62 ck_pr_##K##_##S##_zero(&y, &zero_y); \
63 } \
64 else { \
65 test_##K##_##S(&x); \
66 ck_pr_##K##_##S(&y); \
67 } \
68 \
69 if (x != y || zero_x != zero_y) { \
70 printf("Serial(%"#D") and ck_pr(%"#D")" \
71 #K"_"#S" do not match.\n" \
72 "FAILURE.\n", \
73 x, y); \
74 \
75 return; \
76 } \
77 \
78 if (zero_x) \
79 printf("Variables are zero at iteration %d\n", i); \
80 } \
81 \
82 \
83 printf("\tserial_"#K"_"#S": %"#D"\n" \
84 "\tck_pr_"#K"_"#S": %"#D"\n" \
85 "SUCCESS.\n", \
86 x, y); \
87 \
88 return; \
89 }
90
91 #define GENERATE_TEST(K, P, Y, Z) \
92 TEST_UNARY(K, int, int, int, P, d, Y) \
93 TEST_UNARY(K, uint, unsigned int, unsigned int, P, u, Z) \
94 static void \
95 run_test_##K(void) \
96 { \
97 run_test_##K##_int(false); \
98 run_test_##K##_int(true); \
99 run_test_##K##_uint(false); \
100 run_test_##K##_uint(true); \
101 }
102
103 GENERATE_TEST(inc, +, -1, UINT_MAX)
104 GENERATE_TEST(dec, -, 1, 1)
105
106 #undef GENERATE_TEST
107 #undef TEST_UNARY
108
109 int
110 main(void)
111 {
112 run_test_inc();
113 run_test_dec();
114
115 return (0);
116 }
117