dpdk: Add support for Mellanox ConnectX-4 devices
[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
48 test_isaac_main (unformat_input_t * input)
49 {
50   uword n_iterations, seed;
51   uword i, repeat_count;
52   uword *hash = 0;
53   uword print;
54   isaac_t ctx;
55   uword results[ISAAC_SIZE] = { 0 };
56   uword n_results;
57
58   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
70   if (!seed)
71     seed = random_default_seed ();
72
73   results[0] = seed;
74
75   if (n_iterations == 0)
76     n_iterations = ~0;
77
78   if_verbose ("%d iterations, seed %d\n", n_iterations, seed);
79
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
88       if (!hash)
89         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
120 int
121 main (int argc, char *argv[])
122 {
123   unformat_input_t i;
124   int ret;
125
126   verbose = (argc > 1);
127   unformat_init_command_line (&i, argv);
128   ret = test_isaac_main (&i);
129   unformat_free (&i);
130
131   return ret;
132 }
133 #endif /* CLIB_UNIX */
134
135
136 /*
137  * fd.io coding-style-patch-verification: ON
138  *
139  * Local Variables:
140  * eval: (c-set-style "gnu")
141  * End:
142  */