blob: bed8673e7104bca9d0189d5f801ff4a3336ef47b [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) 2005 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/hash.h>
40#include <vppinfra/random.h>
41#include <vppinfra/random_isaac.h>
42
43static int verbose;
44#define if_verbose(format,args...) \
45 if (verbose) { clib_warning(format, ## args); }
46
Dave Barachc3799992016-08-15 11:12:27 -040047int
48test_isaac_main (unformat_input_t * input)
Ed Warnickecb9cada2015-12-08 15:45:58 -070049{
50 uword n_iterations, seed;
51 uword i, repeat_count;
Dave Barachc3799992016-08-15 11:12:27 -040052 uword *hash = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070053 uword print;
54 isaac_t ctx;
Dave Barachc3799992016-08-15 11:12:27 -040055 uword results[ISAAC_SIZE] = { 0 };
Ed Warnickecb9cada2015-12-08 15:45:58 -070056 uword n_results;
Dave Barachc3799992016-08-15 11:12:27 -040057
Ed Warnickecb9cada2015-12-08 15:45:58 -070058 n_iterations = 1000;
59 seed = 0;
60 print = 1 << 24;
61
62 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
63 {
64 if (0 == unformat (input, "iter %d", &n_iterations)
65 && 0 == unformat (input, "print %d", &print)
66 && 0 == unformat (input, "seed %d", &seed))
67 clib_error ("unknown input `%U'", format_unformat_error, input);
68 }
69
Dave Barachc3799992016-08-15 11:12:27 -040070 if (!seed)
Ed Warnickecb9cada2015-12-08 15:45:58 -070071 seed = random_default_seed ();
72
73 results[0] = seed;
74
75 if (n_iterations == 0)
76 n_iterations = ~0;
77
Dave Barachc3799992016-08-15 11:12:27 -040078 if_verbose ("%d iterations, seed %d\n", n_iterations, seed);
Ed Warnickecb9cada2015-12-08 15:45:58 -070079
80 repeat_count = 0;
81 isaac_init (&ctx, results);
82 isaac (&ctx, results);
83 n_results = 0;
84 for (i = 0; i < n_iterations; i++)
85 {
86 uword r = results[n_results++];
87
Dave Barachc3799992016-08-15 11:12:27 -040088 if (!hash)
Ed Warnickecb9cada2015-12-08 15:45:58 -070089 hash = hash_create (0, /* value bytes */ 0);
90
91 if (hash_get (hash, r))
92 goto repeat;
93
94 hash_set1 (hash, r);
95
96 if (n_results >= ARRAY_LEN (results))
97 {
98 isaac (&ctx, results);
99 n_results = 0;
100 }
101
102 if (verbose && 0 == (i & (print - 1)))
103 fformat (stderr, "0x%08x iterations %d repeats\n", i, repeat_count);
104
105 if (hash_elts (hash) > 0x100000)
106 hash_free (hash);
107
108 continue;
109
110 repeat:
111 fformat (stderr, "repeat found at iteration %d/%d\n", i, n_iterations);
112 repeat_count++;
113 continue;
114 }
115
116 return repeat_count > 0 ? 1 : 0;
117}
118
119#ifdef CLIB_UNIX
Dave Barachc3799992016-08-15 11:12:27 -0400120int
121main (int argc, char *argv[])
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122{
123 unformat_input_t i;
124 int ret;
125
Damjan Marion4dffd1c2018-09-03 12:30:36 +0200126 clib_mem_init (0, 64ULL << 20);
127
Ed Warnickecb9cada2015-12-08 15:45:58 -0700128 verbose = (argc > 1);
129 unformat_init_command_line (&i, argv);
130 ret = test_isaac_main (&i);
131 unformat_free (&i);
132
133 return ret;
134}
135#endif /* CLIB_UNIX */
136
Dave Barachc3799992016-08-15 11:12:27 -0400137
138/*
139 * fd.io coding-style-patch-verification: ON
140 *
141 * Local Variables:
142 * eval: (c-set-style "gnu")
143 * End:
144 */