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) 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/fifo.h> |
| 39 | #include <vppinfra/random.h> |
| 40 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 41 | typedef struct |
| 42 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 43 | int a, b, c; |
| 44 | } A; |
| 45 | |
| 46 | always_inline void |
| 47 | A_set (A * a, int k) |
| 48 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 49 | a->a = 1 * k; |
| 50 | a->b = 2 * k; |
| 51 | a->c = 3 * k; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | always_inline int |
| 55 | A_is_valid (A * a, int k) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 56 | { |
| 57 | return a->a == 1 * k && a->b == 2 * k && a->c == 3 * k; |
| 58 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 59 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 60 | int |
| 61 | test_fifo_main (unformat_input_t * input) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 62 | { |
| 63 | u32 n_added, n_removed, i, j, n_iter, seed, verbose; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 64 | A *as = 0, *a; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 65 | |
| 66 | n_iter = 1000; |
| 67 | seed = random_default_seed (); |
| 68 | verbose = 0; |
| 69 | |
| 70 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 71 | { |
| 72 | if (unformat (input, "iter %d", &n_iter)) |
| 73 | ; |
| 74 | else if (unformat (input, "seed %d", &seed)) |
| 75 | ; |
| 76 | else if (unformat (input, "verbose %=", &verbose, 1)) |
| 77 | ; |
| 78 | else |
| 79 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 80 | clib_warning ("unknown input `%U'\n", format_unformat_error, input); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 81 | return 1; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | if (verbose) |
| 86 | clib_warning ("iter %d seed %d\n", n_iter, seed); |
| 87 | |
| 88 | n_added = n_removed = 0; |
| 89 | for (i = 0; i < n_iter; i++) |
| 90 | { |
| 91 | if (clib_fifo_elts (as) > 0 && (random_u32 (&seed) & 1)) |
| 92 | { |
| 93 | A tmp; |
| 94 | clib_fifo_sub1 (as, tmp); |
| 95 | ASSERT (A_is_valid (&tmp, n_removed)); |
| 96 | n_removed++; |
| 97 | } |
| 98 | else |
| 99 | { |
| 100 | clib_fifo_add2 (as, a); |
| 101 | A_set (a, n_added); |
| 102 | n_added++; |
| 103 | } |
| 104 | |
| 105 | ASSERT (clib_fifo_elts (as) == n_added - n_removed); |
| 106 | |
| 107 | j = 0; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 108 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 109 | clib_fifo_foreach (a, as, { |
| 110 | ASSERT (A_is_valid (a, n_removed + j)); |
| 111 | j++; |
| 112 | }); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 113 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 114 | |
| 115 | ASSERT (j == clib_fifo_elts (as)); |
| 116 | } |
| 117 | |
| 118 | clib_fifo_free (as); |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | #ifdef CLIB_UNIX |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 124 | int |
| 125 | main (int argc, char *argv[]) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 126 | { |
| 127 | unformat_input_t i; |
| 128 | int r; |
| 129 | |
| 130 | unformat_init_command_line (&i, argv); |
| 131 | r = test_fifo_main (&i); |
| 132 | unformat_free (&i); |
| 133 | |
| 134 | return r; |
| 135 | } |
| 136 | #endif |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 137 | |
| 138 | /* |
| 139 | * fd.io coding-style-patch-verification: ON |
| 140 | * |
| 141 | * Local Variables: |
| 142 | * eval: (c-set-style "gnu") |
| 143 | * End: |
| 144 | */ |