Initial commit of vpp code.
[vpp.git] / vppinfra / vppinfra / test_random_isaac.c
1 /*
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
43 static int verbose;
44 #define if_verbose(format,args...) \
45   if (verbose) { clib_warning(format, ## args); }
46
47 int test_isaac_main (unformat_input_t * input)
48 {
49   uword n_iterations, seed;
50   uword i, repeat_count;
51   uword * hash = 0;
52   uword print;
53   isaac_t ctx;
54   uword results[ISAAC_SIZE] = {0};
55   uword n_results;
56   
57   n_iterations = 1000;
58   seed = 0;
59   print = 1 << 24;
60
61   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
62     {
63       if (0 == unformat (input, "iter %d", &n_iterations)
64           && 0 == unformat (input, "print %d", &print)
65           && 0 == unformat (input, "seed %d", &seed))
66         clib_error ("unknown input `%U'", format_unformat_error, input);
67     }
68
69   if (! seed)
70     seed = random_default_seed ();
71
72   results[0] = seed;
73
74   if (n_iterations == 0)
75     n_iterations = ~0;
76
77   if_verbose   ("%d iterations, seed %d\n", n_iterations, seed);
78
79   repeat_count = 0;
80   isaac_init (&ctx, results);
81   isaac (&ctx, results);
82   n_results = 0;
83   for (i = 0; i < n_iterations; i++)
84     {
85       uword r = results[n_results++];
86
87       if (! hash)
88         hash = hash_create (0, /* value bytes */ 0);
89
90       if (hash_get (hash, r))
91         goto repeat;
92
93       hash_set1 (hash, r);
94
95       if (n_results >= ARRAY_LEN (results))
96         {
97           isaac (&ctx, results);
98           n_results = 0;
99         }
100
101       if (verbose && 0 == (i & (print - 1)))
102         fformat (stderr, "0x%08x iterations %d repeats\n", i, repeat_count);
103
104       if (hash_elts (hash) > 0x100000)
105         hash_free (hash);
106
107       continue;
108
109     repeat:
110       fformat (stderr, "repeat found at iteration  %d/%d\n", i, n_iterations);
111       repeat_count++;
112       continue;
113     }
114
115   return repeat_count > 0 ? 1 : 0;
116 }
117
118 #ifdef CLIB_UNIX
119 int main (int argc, char * argv[])
120 {
121   unformat_input_t i;
122   int ret;
123
124   verbose = (argc > 1);
125   unformat_init_command_line (&i, argv);
126   ret = test_isaac_main (&i);
127   unformat_free (&i);
128
129   return ret;
130 }
131 #endif /* CLIB_UNIX */
132