Debian Codegraph @

regressions/ck_pr/validate/ck_pr_btr.c

raw
1 /*
2 * Copyright 2009 Samy Al Bahra.
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 <inttypes.h>
28 #include <stdbool.h>
29 #include <stdio.h>
30 #include <stdint.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33
34 #include <ck_pr.h>
35
36 #include "../../common.h"
37 #ifndef R_REPEAT
38 #define R_REPEAT 200000
39 #endif
40
41 /*
42 * Bit selector.
43 */
44 #define BM(v, b) (((v) >> (b)) & 1)
45
46 #define CK_PR_BTR_T(w, v) \
47 { \
48 unsigned int j; \
49 uint##w##_t r = v, c = v; \
50 bool t; \
51 for (j = 0; j < (w); j++) { \
52 c &= (uint##w##_t)-1 ^ (1 << j); \
53 t = ck_pr_btr_##w(&r, j); \
54 if ((t && !BM(v, j)) || (r != c)) { \
55 printf("FAIL [%" PRIx##w ":%u != %" PRIx##w ":%u]\n", r, j, c, j); \
56 exit(EXIT_FAILURE); \
57 } \
58 } \
59 }
60
61 #define CK_PR_BTR_B(w) \
62 { \
63 uint##w##_t o; \
64 unsigned int i; \
65 printf("ck_pr_btr_" #w ": "); \
66 for (i = 0; i < R_REPEAT; i++) { \
67 o = (uint##w##_t)common_rand(); \
68 CK_PR_BTR_T(w, o); \
69 } \
70 printf(" SUCCESS\n"); \
71 }
72
73 int
74 main(void)
75 {
76
77 common_srand((unsigned int)getpid());
78
79 #ifdef CK_F_PR_BTR_64
80 CK_PR_BTR_B(64);
81 #endif
82
83 #ifdef CK_F_PR_BTR_32
84 CK_PR_BTR_B(32);
85 #endif
86
87 #ifdef CK_F_PR_BTR_16
88 CK_PR_BTR_B(16);
89 #endif
90
91 #ifdef CK_F_PR_BTR_8
92 CK_PR_BTR_B(8);
93 #endif
94
95 return (0);
96 }
97