Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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) 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 Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 41 | static u32 known_random_sequence[] = { |
| 42 | 0x00000000, 0x3c6ef35f, 0x47502932, 0xd1ccf6e9, |
| 43 | 0xaaf95334, 0x6252e503, 0x9f2ec686, 0x57fe6c2d, |
| 44 | 0xa3d95fa8, 0x81fdbee7, 0x94f0af1a, 0xcbf633b1, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 48 | int |
| 49 | test_random_main (unformat_input_t * input) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 50 | { |
| 51 | uword n_iterations; |
| 52 | uword i, repeat_count; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 53 | uword *bitmap = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 54 | uword print; |
| 55 | u32 seed; |
| 56 | u32 *seedp = &seed; |
Florin Coras | e10e372 | 2016-04-13 00:05:27 +0200 | [diff] [blame] | 57 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 58 | /* first, check known sequence from Numerical Recipes in C, 2nd ed. |
| 59 | page 284 */ |
| 60 | seed = known_random_sequence[0]; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 61 | for (i = 0; i < ARRAY_LEN (known_random_sequence) - 1; i++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 62 | { |
| 63 | u32 rv; |
| 64 | rv = random_u32 (seedp); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 65 | if (rv != known_random_sequence[i + 1]) |
| 66 | { |
| 67 | fformat (stderr, "known sequence check FAILS at index %d", i + 1); |
| 68 | break; |
| 69 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 70 | } |
| 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 Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 86 | if (!seed) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 87 | 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 Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 125 | int |
| 126 | main (int argc, char *argv[]) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 127 | { |
| 128 | unformat_input_t i; |
| 129 | int ret; |
| 130 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 131 | clib_mem_init (0, 3ULL << 30); |
Florin Coras | e10e372 | 2016-04-13 00:05:27 +0200 | [diff] [blame] | 132 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 133 | 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 Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 141 | |
| 142 | /* |
| 143 | * fd.io coding-style-patch-verification: ON |
| 144 | * |
| 145 | * Local Variables: |
| 146 | * eval: (c-set-style "gnu") |
| 147 | * End: |
| 148 | */ |