45392bc35ebe0e05d89abe2c111f0d24d4430c46
[vpp.git] / vppinfra / vppinfra / test_fifo.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/fifo.h>
39 #include <vppinfra/random.h>
40
41 typedef struct
42 {
43   int a, b, c;
44 } A;
45
46 always_inline void
47 A_set (A * a, int k)
48 {
49   a->a = 1 * k;
50   a->b = 2 * k;
51   a->c = 3 * k;
52 }
53
54 always_inline int
55 A_is_valid (A * a, int k)
56 {
57   return a->a == 1 * k && a->b == 2 * k && a->c == 3 * k;
58 }
59
60 int
61 test_fifo_main (unformat_input_t * input)
62 {
63   u32 n_added, n_removed, i, j, n_iter, seed, verbose;
64   A *as = 0, *a;
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         {
80           clib_warning ("unknown input `%U'\n", format_unformat_error, input);
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;
108       /* *INDENT-OFF* */
109       clib_fifo_foreach (a, as, {
110         ASSERT (A_is_valid (a, n_removed + j));
111         j++;
112       });
113       /* *INDENT-ON* */
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
124 int
125 main (int argc, char *argv[])
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
137
138 /*
139  * fd.io coding-style-patch-verification: ON
140  *
141  * Local Variables:
142  * eval: (c-set-style "gnu")
143  * End:
144  */