Debian Codegraph @

regressions/ck_pr/validate/ck_pr_btx.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 <stdbool.h>
31 #include <ck_pr.h>
32
33 #include "../../common.h"
34 #define REPEAT 2000000
35
36 #define TEST_BTX(K, S, M, T, L, P, D, R) \
37 static bool \
38 test_##K##_##S(M *target, int offset) \
39 { \
40 T previous; \
41 const L change = R (0x01 << offset); \
42 \
43 previous = (T)*target; \
44 *target = previous P change; \
45 return ((previous >> offset) & 0x01); \
46 } \
47 static void \
48 run_test_##K##_##S(void) \
49 { \
50 int i, offset, m; \
51 bool serial_t, ck_pr_t; \
52 T x = 65535, y = 65535; \
53 \
54 common_srand((unsigned int)getpid()); \
55 m = sizeof(T) * 8; \
56 \
57 puts("***TESTING ck_pr_"#K"_"#S"***"); \
58 for (i = 0; i < REPEAT; ++i) { \
59 offset = common_rand() % m; \
60 serial_t = test_##K##_##S(&x, offset); \
61 ck_pr_t = ck_pr_##K##_##S(&y, offset); \
62 \
63 if (serial_t != ck_pr_t || x != y ) { \
64 printf("Serial(%"#D") and ck_pr(%"#D")" \
65 #K"_"#S " do not match.\n" \
66 "FAILURE.\n", \
67 serial_t, ck_pr_t); \
68 \
69 return; \
70 } \
71 } \
72 printf("\tserial_"#K"_"#S": %"#D"\n" \
73 "\tck_pr_"#K"_"#S": %"#D"\n" \
74 "SUCCESS.\n", \
75 x, y); \
76 \
77 return; \
78 }
79
80 #define TEST_BTX_S(K, S, T, P, D, R) TEST_BTX(K, S, T, T, T, P, D, R)
81
82 #define GENERATE_TEST(K, P, R) \
83 TEST_BTX_S(K, int, int, P, d, R) \
84 TEST_BTX_S(K, uint, unsigned int, P, u, R) \
85 static void \
86 run_test_##K(void) \
87 { \
88 run_test_##K##_int(); \
89 run_test_##K##_uint(); \
90 \
91 return; \
92 }
93
94 GENERATE_TEST(btc, ^, 0+)
95 GENERATE_TEST(btr, &, ~)
96 GENERATE_TEST(bts, |, 0+)
97
98 #undef GENERATE_TEST
99 #undef TEST_BTX_S
100 #undef TEST_BTX
101
102 int
103 main(void)
104 {
105 run_test_btc();
106 run_test_btr();
107 run_test_bts();
108
109 return (0);
110 }
111
112