blob: 49759eacb971da26d7577975d40d32f697c583a1 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15/*
16 Copyright (c) 2001, 2002, 2003 Eliot Dresselhaus
17
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:
25
26 The above copyright notice and this permission notice shall be
27 included in all copies or substantial portions of the Software.
28
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.
36*/
37
38#include <vppinfra/format.h>
39#include <vppinfra/bitmap.h>
40
Dave Barachc3799992016-08-15 11:12:27 -040041static u32 known_random_sequence[] = {
42 0x00000000, 0x3c6ef35f, 0x47502932, 0xd1ccf6e9,
43 0xaaf95334, 0x6252e503, 0x9f2ec686, 0x57fe6c2d,
44 0xa3d95fa8, 0x81fdbee7, 0x94f0af1a, 0xcbf633b1,
Ed Warnickecb9cada2015-12-08 15:45:58 -070045};
46
47
Dave Barachc3799992016-08-15 11:12:27 -040048int
49test_random_main (unformat_input_t * input)
Ed Warnickecb9cada2015-12-08 15:45:58 -070050{
51 uword n_iterations;
52 uword i, repeat_count;
Dave Barachc3799992016-08-15 11:12:27 -040053 uword *bitmap = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070054 uword print;
55 u32 seed;
56 u32 *seedp = &seed;
Florin Corase10e3722016-04-13 00:05:27 +020057
Ed Warnickecb9cada2015-12-08 15:45:58 -070058 /* first, check known sequence from Numerical Recipes in C, 2nd ed.
59 page 284 */
60 seed = known_random_sequence[0];
Dave Barachc3799992016-08-15 11:12:27 -040061 for (i = 0; i < ARRAY_LEN (known_random_sequence) - 1; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -070062 {
63 u32 rv;
64 rv = random_u32 (seedp);
Dave Barachc3799992016-08-15 11:12:27 -040065 if (rv != known_random_sequence[i + 1])
66 {
67 fformat (stderr, "known sequence check FAILS at index %d", i + 1);
68 break;
69 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070070 }
71
72 clib_warning ("known sequence check passes");
73
74 n_iterations = 1000;
75 seed = 0;
76 print = 1 << 24;
77
78 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
79 {
80 if (0 == unformat (input, "iter %d", &n_iterations)
81 && 0 == unformat (input, "print %d", &print)
82 && 0 == unformat (input, "seed %d", &seed))
83 clib_error ("unknown input `%U'", format_unformat_error, input);
84 }
85
Dave Barachc3799992016-08-15 11:12:27 -040086 if (!seed)
Ed Warnickecb9cada2015-12-08 15:45:58 -070087 seed = random_default_seed ();
88
89 if (n_iterations == 0)
90 n_iterations = random_u32_max ();
91
92 clib_warning ("%d iterations, seed %d\n", n_iterations, seed);
93
94 repeat_count = 0;
95 for (i = 0; i < n_iterations; i++)
96 {
97 uword r = random_u32 (&seed);
98 uword b, ri, rj;
99
100 ri = r / BITS (bitmap[0]);
101 rj = (uword) 1 << (r % BITS (bitmap[0]));
102
103 vec_validate (bitmap, ri);
104 b = bitmap[ri];
105
106 if (b & rj)
107 goto repeat;
108 b |= rj;
109 bitmap[ri] = b;
110
111 if (0 == (i & (print - 1)))
112 fformat (stderr, "0x%08x iterations %d repeats\n", i, repeat_count);
113 continue;
114
115 repeat:
116 fformat (stderr, "repeat found at iteration %d/%d\n", i, n_iterations);
117 repeat_count++;
118 continue;
119 }
120
121 return 0;
122}
123
124#ifdef CLIB_UNIX
Dave Barachc3799992016-08-15 11:12:27 -0400125int
126main (int argc, char *argv[])
Ed Warnickecb9cada2015-12-08 15:45:58 -0700127{
128 unformat_input_t i;
129 int ret;
130
Dave Barachc3799992016-08-15 11:12:27 -0400131 clib_mem_init (0, 3ULL << 30);
Florin Corase10e3722016-04-13 00:05:27 +0200132
Ed Warnickecb9cada2015-12-08 15:45:58 -0700133 unformat_init_command_line (&i, argv);
134 ret = test_random_main (&i);
135 unformat_free (&i);
136
137 return ret;
138}
139#endif /* CLIB_UNIX */
140
Dave Barachc3799992016-08-15 11:12:27 -0400141
142/*
143 * fd.io coding-style-patch-verification: ON
144 *
145 * Local Variables:
146 * eval: (c-set-style "gnu")
147 * End:
148 */