2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 Copyright (c) 2001, 2002, 2003 Eliot Dresselhaus
18 Permission is hereby granted, free of charge, to any person obtaining
19 a copy of this software and associated documentation files (the
20 "Software"), to deal in the Software without restriction, including
21 without limitation the rights to use, copy, modify, merge, publish,
22 distribute, sublicense, and/or sell copies of the Software, and to
23 permit persons to whom the Software is furnished to do so, subject to
24 the following conditions:
26 The above copyright notice and this permission notice shall be
27 included in all copies or substantial portions of the Software.
29 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 #include <vppinfra/format.h>
39 #include <vppinfra/bitmap.h>
41 static u32 outcome_frequencies[] = {
53 vec_validate (values, 5);
55 for (i = 0; i < 6; i++)
56 values[i] = (u64) outcome_frequencies[i];
58 d = clib_chisquare (values);
67 fformat (stdout, "chisquare OK...\n");
72 fformat (stdout, "chisquare BAD, d = %.3f\n", d);
77 static u32 known_random_sequence[] = {
78 0x00000000, 0x3c6ef35f, 0x47502932, 0xd1ccf6e9,
79 0xaaf95334, 0x6252e503, 0x9f2ec686, 0x57fe6c2d,
80 0xa3d95fa8, 0x81fdbee7, 0x94f0af1a, 0xcbf633b1,
85 test_random_main (unformat_input_t * input)
88 uword i, repeat_count;
96 /* first, check known sequence from Numerical Recipes in C, 2nd ed.
98 seed = known_random_sequence[0];
99 for (i = 0; i < ARRAY_LEN (known_random_sequence) - 1; i++)
102 rv = random_u32 (seedp);
103 if (rv != known_random_sequence[i + 1])
105 fformat (stderr, "known sequence check FAILS at index %d", i + 1);
110 clib_warning ("known sequence check passes");
116 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
118 if (0 == unformat (input, "iter %d", &n_iterations)
119 && 0 == unformat (input, "print %d", &print)
120 && 0 == unformat (input, "seed %d", &seed))
121 clib_error ("unknown input `%U'", format_unformat_error, input);
125 seed = random_default_seed ();
127 if (n_iterations == 0)
128 n_iterations = random_u32_max ();
130 clib_warning ("%d iterations, seed %d\n", n_iterations, seed);
133 for (i = 0; i < n_iterations; i++)
135 uword r = random_u32 (&seed);
138 ri = r / BITS (bitmap[0]);
139 rj = (uword) 1 << (r % BITS (bitmap[0]));
141 vec_validate (bitmap, ri);
149 if (0 == (i & (print - 1)))
150 fformat (stderr, "0x%08x iterations %d repeats\n", i, repeat_count);
154 fformat (stderr, "repeat found at iteration %d/%d\n", i, n_iterations);
159 if (test_chisquare ())
162 /* Simple randomness tests based on X2 stats */
163 vec_validate (counts, 255);
165 for (i = 0; i < 1000000; i++)
168 u32 r = random_u32 (&seed);
170 random_index = r & 0xFF;
172 counts[random_index]++;
175 d = clib_chisquare (counts);
177 fformat (stdout, "%d random octets, chisquare stat d = %.3f\n", i, d);
186 main (int argc, char *argv[])
191 clib_mem_init (0, 3ULL << 30);
193 unformat_init_command_line (&i, argv);
194 ret = test_random_main (&i);
199 #endif /* CLIB_UNIX */
203 * fd.io coding-style-patch-verification: ON
206 * eval: (c-set-style "gnu")