Update Red Hat Copyright Notices
[nbdkit.git] / common / include / test-random.c
blobb111e0abca91091dd4e519290a41b0359065923b
1 /* nbdkit
2 * Copyright Red Hat
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * * Neither the name of Red Hat nor the names of its contributors may be
16 * used to endorse or promote products derived from this software without
17 * specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #include <config.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <stdint.h>
38 #include <inttypes.h>
40 #include "array-size.h"
41 #include "random.h"
43 /* This works by comparing the result to some known test vectors. It
44 * should produce the same result on all architectures, platforms and
45 * compilers.
48 #define LEN 16
50 struct {
51 uint64_t seed;
52 uint64_t vector[LEN];
53 } tests[] = {
54 { 0, {
55 UINT64_C (0x99ec5f36cb75f2b4),
56 UINT64_C (0xbf6e1f784956452a),
57 UINT64_C (0x1a5f849d4933e6e0),
58 UINT64_C (0x6aa594f1262d2d2c),
59 UINT64_C (0xbba5ad4a1f842e59),
60 UINT64_C (0xffef8375d9ebcaca),
61 UINT64_C (0x6c160deed2f54c98),
62 UINT64_C (0x8920ad648fc30a3f),
63 UINT64_C (0xdb032c0ba7539731),
64 UINT64_C (0xeb3a475a3e749a3d),
65 UINT64_C (0x1d42993fa43f2a54),
66 UINT64_C (0x11361bf526a14bb5),
67 UINT64_C (0x1b4f07a5ab3d8e9c),
68 UINT64_C (0xa7a3257f6986db7f),
69 UINT64_C (0x7efdaa95605dfc9c),
70 UINT64_C (0x4bde97c0a78eaab8),
71 } },
72 { 1, {
73 UINT64_C (0xb3f2af6d0fc710c5),
74 UINT64_C (0x853b559647364cea),
75 UINT64_C (0x92f89756082a4514),
76 UINT64_C (0x642e1c7bc266a3a7),
77 UINT64_C (0xb27a48e29a233673),
78 UINT64_C (0x24c123126ffda722),
79 UINT64_C (0x123004ef8df510e6),
80 UINT64_C (0x61954dcc47b1e89d),
81 UINT64_C (0xddfdb48ab9ed4a21),
82 UINT64_C (0x8d3cdb8c3aa5b1d0),
83 UINT64_C (0xeebd114bd87226d1),
84 UINT64_C (0xf50c3ff1e7d7e8a6),
85 UINT64_C (0xeeca3115e23bc8f1),
86 UINT64_C (0xab49ed3db4c66435),
87 UINT64_C (0x99953c6c57808dd7),
88 UINT64_C (0xe3fa941b05219325),
89 } },
90 { 2, {
91 UINT64_C (0x1a28690da8a8d057),
92 UINT64_C (0xb9bb8042daedd58a),
93 UINT64_C (0x2f1829af001ef205),
94 UINT64_C (0xbf733e63d139683d),
95 UINT64_C (0xafa78247c6a82034),
96 UINT64_C (0x3c69a1b6d15cf0d0),
97 UINT64_C (0xa5a9fdd18948c400),
98 UINT64_C (0x3813d2654a981e91),
99 UINT64_C (0x9be35597c9c97bfa),
100 UINT64_C (0xbfc5e80fd0b75f32),
101 UINT64_C (0xbee02daaac716557),
102 UINT64_C (0x5afed6f12b594dbe),
103 UINT64_C (0xae346b9196e12cc7),
104 UINT64_C (0xf5f45afc1af068ed),
105 UINT64_C (0xff75eccacfb37519),
106 UINT64_C (0x1adca5a0b2e766c5),
107 } },
108 { 3, {
109 UINT64_C (0xb0cdabdae5668cc0),
110 UINT64_C (0xa3fd1dea5e1864ee),
111 UINT64_C (0x37e00afb3229fd51),
112 UINT64_C (0x88b1b58b236f3bea),
113 UINT64_C (0x6cb24c8fb224980a),
114 UINT64_C (0x6646287ee2a98083),
115 UINT64_C (0x35cd8bb5e1fa7256),
116 UINT64_C (0xb72fe6e16b6fb4e6),
117 UINT64_C (0xf1397a9f1db4f5d9),
118 UINT64_C (0x31f25047faa8e5d4),
119 UINT64_C (0xec616a6e46e96dec),
120 UINT64_C (0xae0c5e0f7b5d1449),
121 UINT64_C (0xa517e799c5c6e32f),
122 UINT64_C (0xc1276908f843b688),
123 UINT64_C (0xaf7e924d738d87ec),
124 UINT64_C (0x1c3f3ba863d5c7d1),
125 } },
126 { 4, {
127 UINT64_C (0x437057a4eb7c3a13),
128 UINT64_C (0xe95a0d7fd8c1832c),
129 UINT64_C (0x71807ff81a0c627e),
130 UINT64_C (0xfa40f34634632cd2),
131 UINT64_C (0x39cf61fc694b95b7),
132 UINT64_C (0x9ca3d6e037621a02),
133 UINT64_C (0x7be965236729c7d3),
134 UINT64_C (0xb95fba07afa980ac),
135 UINT64_C (0x91424978ab94232),
136 UINT64_C (0x565eb8170fdae341),
137 UINT64_C (0x0744508beb95a6bb),
138 UINT64_C (0xf2426b33aa0a601d),
139 UINT64_C (0x7ddc1fcd0bfec893),
140 UINT64_C (0x9e09fedd4af1ff3d),
141 UINT64_C (0xbe77c1bed02132e7),
142 UINT64_C (0x61e4f6e3e88d34d4),
143 } },
144 { UINT64_MAX, {
145 UINT64_C (0x8f5520d52a7ead08),
146 UINT64_C (0xc476a018caa1802d),
147 UINT64_C (0x81de31c0d260469e),
148 UINT64_C (0xbf658d7e065f3c2f),
149 UINT64_C (0x913593fda1bca32a),
150 UINT64_C (0xbb535e93941ba525),
151 UINT64_C (0x5ecda415c3c6dfde),
152 UINT64_C (0xc487398fc9de9ae2),
153 UINT64_C (0xa06746dbb57c4d62),
154 UINT64_C (0x9d414196fdf05c8a),
155 UINT64_C (0x41cf1af9a178c669),
156 UINT64_C (0x0b3b3a95e78839f9),
157 UINT64_C (0x7aaab30444aefc7e),
158 UINT64_C (0x7b251ec961f341b1),
159 UINT64_C (0x30ed32acf367205f),
160 UINT64_C (0xc6ca62fc772728b0),
161 } },
165 main (void)
167 size_t i, j;
168 uint64_t r;
169 unsigned errors = 0;
171 for (i = 0; i < ARRAY_SIZE (tests); ++i) {
172 struct random_state state;
174 printf ("seed: %" PRIu64 "\n", tests[i].seed);
175 xsrandom (tests[i].seed, &state);
176 for (j = 0; j < LEN; ++j) {
177 r = xrandom (&state);
178 if (tests[i].vector[j] != r) {
179 printf ("\texpected: 0x%" PRIx64 "\tactual: 0x%" PRIx64 "\n",
180 tests[i].vector[j], r);
181 errors++;
186 if (errors > 0) {
187 fprintf (stderr, "random vector does not match expected\n");
188 exit (EXIT_FAILURE);
191 printf ("test successful\n");
192 exit (EXIT_SUCCESS);