blob: 2d3cad33119c77f1efadb9e2bf7fa465cf0148d7 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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 Barachc3799992016-08-15 11:12:27 -040041typedef struct
42{
Ed Warnickecb9cada2015-12-08 15:45:58 -070043 int a, b, c;
44} A;
45
46always_inline void
47A_set (A * a, int k)
48{
Dave Barachc3799992016-08-15 11:12:27 -040049 a->a = 1 * k;
50 a->b = 2 * k;
51 a->c = 3 * k;
Ed Warnickecb9cada2015-12-08 15:45:58 -070052}
53
54always_inline int
55A_is_valid (A * a, int k)
Dave Barachc3799992016-08-15 11:12:27 -040056{
57 return a->a == 1 * k && a->b == 2 * k && a->c == 3 * k;
58}
Ed Warnickecb9cada2015-12-08 15:45:58 -070059
Dave Barachc3799992016-08-15 11:12:27 -040060int
61test_fifo_main (unformat_input_t * input)
Ed Warnickecb9cada2015-12-08 15:45:58 -070062{
63 u32 n_added, n_removed, i, j, n_iter, seed, verbose;
Dave Barachc3799992016-08-15 11:12:27 -040064 A *as = 0, *a;
Ed Warnickecb9cada2015-12-08 15:45:58 -070065
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 Barachc3799992016-08-15 11:12:27 -040080 clib_warning ("unknown input `%U'\n", format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -070081 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 clib_fifo_foreach (a, as, {
109 ASSERT (A_is_valid (a, n_removed + j));
110 j++;
111 });
112
113 ASSERT (j == clib_fifo_elts (as));
114 }
115
116 clib_fifo_free (as);
117
118 return 0;
119}
120
121#ifdef CLIB_UNIX
Dave Barachc3799992016-08-15 11:12:27 -0400122int
123main (int argc, char *argv[])
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124{
125 unformat_input_t i;
126 int r;
127
128 unformat_init_command_line (&i, argv);
129 r = test_fifo_main (&i);
130 unformat_free (&i);
131
132 return r;
133}
134#endif
Dave Barachc3799992016-08-15 11:12:27 -0400135
136/*
137 * fd.io coding-style-patch-verification: ON
138 *
139 * Local Variables:
140 * eval: (c-set-style "gnu")
141 * End:
142 */