Initial commit of vpp code.
[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   int a, b, c;
43 } A;
44
45 always_inline void
46 A_set (A * a, int k)
47 {
48   a->a = 1*k;
49   a->b = 2*k;
50   a->c = 3*k;
51 }
52
53 always_inline int
54 A_is_valid (A * a, int k)
55 { return a->a == 1*k && a->b == 2*k && a->c == 3*k; }
56
57 int test_fifo_main (unformat_input_t * input)
58 {
59   u32 n_added, n_removed, i, j, n_iter, seed, verbose;
60   A * as = 0, * a;
61
62   n_iter = 1000;
63   seed = random_default_seed ();
64   verbose = 0;
65
66   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
67     {
68       if (unformat (input, "iter %d", &n_iter))
69         ;
70       else if (unformat (input, "seed %d", &seed))
71         ;
72       else if (unformat (input, "verbose %=", &verbose, 1))
73         ;
74       else
75         {
76           clib_warning ("unknown input `%U'\n",
77                         format_unformat_error, input);
78           return 1;
79         }
80     }
81
82   if (verbose)
83     clib_warning ("iter %d seed %d\n", n_iter, seed);
84
85   n_added = n_removed = 0;
86   for (i = 0; i < n_iter; i++)
87     {
88       if (clib_fifo_elts (as) > 0 && (random_u32 (&seed) & 1))
89         {
90           A tmp;
91           clib_fifo_sub1 (as, tmp);
92           ASSERT (A_is_valid (&tmp, n_removed));
93           n_removed++;
94         }
95       else
96         {
97           clib_fifo_add2 (as, a);
98           A_set (a, n_added);
99           n_added++;
100         }
101
102       ASSERT (clib_fifo_elts (as) == n_added - n_removed);
103
104       j = 0;
105       clib_fifo_foreach (a, as, {
106         ASSERT (A_is_valid (a, n_removed + j));
107         j++;
108       });
109
110       ASSERT (j == clib_fifo_elts (as));
111     }
112
113   clib_fifo_free (as);
114
115   return 0;
116 }
117
118 #ifdef CLIB_UNIX
119 int main (int argc, char * argv [])
120 {
121   unformat_input_t i;
122   int r;
123
124   unformat_init_command_line (&i, argv);
125   r = test_fifo_main (&i);
126   unformat_free (&i);
127
128   return r;
129 }
130 #endif