Debian Codegraph @

regressions/ck_pr/validate/ck_pr_btc.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_BTC_T(w, v) \
47 { \
48 unsigned int j; \
49 uint##w##_t r = v; \
50 bool t; \
51 for (j = 0; j < (w); j++) { \
52 t = ck_pr_btc_##w(&r, j); \
53 if ((t && !BM(v, j)) || ((BM(v, j) + BM(r, j)) != 1)) { \
54 printf("FAIL [%" PRIx##w ":%u]\n", r, j); \
55 exit(EXIT_FAILURE); \
56 } \
57 } \
58 }
59
60 #define CK_PR_BTC_B(w) \
61 { \
62 uint##w##_t o; \
63 unsigned int i; \
64 printf("ck_pr_btc_" #w ": "); \
65 for (i = 0; i < R_REPEAT; i++) { \
66 o = (uint##w##_t)common_rand(); \
67 CK_PR_BTC_T(w, o); \
68 } \
69 printf(" SUCCESS\n"); \
70 }
71
72 int
73 main(void)
74 {
75
76 common_srand((unsigned int)getpid());
77
78 #ifdef CK_F_PR_BTC_64
79 CK_PR_BTC_B(64);
80 #endif
81
82 #ifdef CK_F_PR_BTC_32
83 CK_PR_BTC_B(32);
84 #endif
85
86 #ifdef CK_F_PR_BTC_16
87 CK_PR_BTC_B(16);
88 #endif
89
90 #ifdef CK_F_PR_BTC_8
91 CK_PR_BTC_B(8);
92 #endif
93
94 return (0);
95 }
96