The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
atomic_queue_test.c
Go to the documentation of this file.
1/*
2 * atomic_queue_test.c Tests for atomic queues
3 *
4 * Version: $Id: 1325db7eb7977919432cf2e47f0a279a550b6249 $
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 *
20 * @copyright 2016 Alan DeKok (aland@freeradius.org)
21 */
22
23RCSID("$Id: 1325db7eb7977919432cf2e47f0a279a550b6249 $")
24
25#include <freeradius-devel/io/atomic_queue.h>
26#include <freeradius-devel/util/debug.h>
27#include <string.h>
28#include <sys/time.h>
29
30#ifdef HAVE_GETOPT_H
31# include <getopt.h>
32#endif
33
34#define OFFSET (1024)
35
36static int debug_lvl = 0;
37
38
39/**********************************************************************/
40typedef struct request_s request_t;
41void request_verify(UNUSED char const *file, UNUSED int line, UNUSED request_t *request);
42
43void request_verify(UNUSED char const *file, UNUSED int line, UNUSED request_t *request)
44{
45}
46/**********************************************************************/
47
48
49static NEVER_RETURNS void usage(void)
50{
51 fprintf(stderr, "usage: atomic_queue_test [OPTS]\n");
52 fprintf(stderr, " -s size set queue size.\n");
53 fprintf(stderr, " -x Debugging mode.\n");
54
55 fr_exit_now(EXIT_SUCCESS);
56}
57
58int main(int argc, char *argv[])
59{
60 int c, i, ret = 0;
61 int size;
62 intptr_t val;
63 void *data;
65 TALLOC_CTX *autofree = talloc_autofree_context();
66
67 size = 4;
68
69 while ((c = getopt(argc, argv, "hs:tx")) != -1) switch (c) {
70 case 's':
71 size = atoi(optarg);
72 break;
73
74 case 'x':
75 debug_lvl++;
76 break;
77
78 case 'h':
79 default:
80 usage();
81 }
82#if 0
83 argc -= (optind - 1);
84 argv += (optind - 1);
85#endif
86
88
89#ifndef NDEBUG
90 if (debug_lvl) {
91 printf("Start\n");
93
94 if (debug_lvl > 1) printf("Filling with %d\n", size);
95 }
96
97#endif
98
99
100 for (i = 0; i < size; i++) {
101 val = i + OFFSET;
102 data = (void *) val;
103
105 fprintf(stderr, "Failed pushing at %d\n", i);
106 fr_exit_now(EXIT_FAILURE);
107 }
108
109#ifndef NDEBUG
110 if (debug_lvl > 1) {
111 printf("iteration %d\n", i);
112 fr_atomic_queue_debug(stdout, aq);
113 }
114#endif
115 }
116
117 val = size + OFFSET;
118 data = (void *) val;
119
120 /*
121 * Queue is full. No more pushes are allowed.
122 */
124 fprintf(stderr, "Pushed an entry past the end of the queue.");
125 fr_exit_now(EXIT_FAILURE);
126 }
127
128#ifndef NDEBUG
129 if (debug_lvl) {
130 printf("Full\n");
131 fr_atomic_queue_debug(stdout, aq);
132
133 if (debug_lvl > 1) printf("Emptying\n");
134 }
135#endif
136
137 /*
138 * And now pop them all.
139 */
140 for (i = 0; i < size; i++) {
141 if (!fr_atomic_queue_pop(aq, &data)) {
142 fprintf(stderr, "Failed popping at %d\n", i);
143 fr_exit_now(EXIT_FAILURE);
144 }
145
146 val = (intptr_t) data;
147 if (val != (i + OFFSET)) {
148 fprintf(stderr, "Pop expected %d, got %d\n",
149 i + OFFSET, (int) val);
150 fr_exit_now(EXIT_FAILURE);
151 }
152
153#ifndef NDEBUG
154 if (debug_lvl > 1) {
155 printf("iteration %d\n", i);
156 fr_atomic_queue_debug(stdout, aq);
157 }
158#endif
159 }
160
161 /*
162 * Queue is empty. No more pops are allowed.
163 */
164 if (fr_atomic_queue_pop(aq, &data)) {
165 fprintf(stderr, "Popped an entry past the end of the queue.");
166 fr_exit_now(EXIT_FAILURE);
167 }
168
169#ifndef NDEBUG
170 if (debug_lvl) {
171 printf("Empty\n");
172 fr_atomic_queue_debug(stdout, aq);
173 }
174#endif
175
176 return ret;
177}
178
int const char * file
Definition acutest.h:702
int const char int line
Definition acutest.h:702
bool fr_atomic_queue_pop(fr_atomic_queue_t *aq, void **p_data)
Pop a pointer from the atomic queue.
void fr_atomic_queue_debug(FILE *fp, fr_atomic_queue_t *aq)
Dump an atomic queue.
fr_atomic_queue_t * fr_atomic_queue_alloc(TALLOC_CTX *ctx, size_t size)
Create fixed-size atomic queue.
bool fr_atomic_queue_push(fr_atomic_queue_t *aq, void *data)
Push a pointer into the atomic queue.
Structure to hold the atomic queue.
int main(int argc, char *argv[])
#define OFFSET
static NEVER_RETURNS void usage(void)
void request_verify(UNUSED char const *file, UNUSED int line, UNUSED request_t *request)
static int debug_lvl
static TALLOC_CTX * autofree
Definition fuzzer.c:44
#define RCSID(id)
Definition build.h:506
#define NEVER_RETURNS
Should be placed before the function return type.
Definition build.h:334
#define UNUSED
Definition build.h:336
static fr_atomic_queue_t ** aq
#define fr_exit_now(_x)
Exit without calling atexit() handlers, producing a log message in debug builds.
Definition debug.h:236
#define talloc_autofree_context
The original function is deprecated, so replace it with our version.
Definition talloc.h:48
static fr_slen_t data
Definition value.h:1340