Asterisk - The Open Source Telephony Project GIT-master-b023714
Loading...
Searching...
No Matches
Data Structures | Macros | Functions | Variables
test_data_buffer.c File Reference

Data Buffer API Unit Tests. More...

#include "asterisk.h"
#include "asterisk/module.h"
#include "asterisk/test.h"
#include "asterisk/data_buffer.h"
Include dependency graph for test_data_buffer.c:

Go to the source code of this file.

Data Structures

struct  mock_payload
 

Macros

#define BUFFER_MAX_NOMINAL   10
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
static void ast_data_buffer_free_wrapper (struct ast_data_buffer *buffer)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
 AST_TEST_DEFINE (buffer_create)
 
 AST_TEST_DEFINE (buffer_nominal)
 
 AST_TEST_DEFINE (buffer_put)
 
 AST_TEST_DEFINE (buffer_resize)
 
static int load_module (void)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Data buffer API test module" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
 
static const struct ast_module_infoast_module_info = &__mod_info
 

Detailed Description

Data Buffer API Unit Tests.

Author
Ben Ford bford.nosp@m.@dig.nosp@m.ium.c.nosp@m.om

Definition in file test_data_buffer.c.

Macro Definition Documentation

◆ BUFFER_MAX_NOMINAL

#define BUFFER_MAX_NOMINAL   10

Definition at line 38 of file test_data_buffer.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 345 of file test_data_buffer.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 345 of file test_data_buffer.c.

◆ ast_data_buffer_free_wrapper()

static void ast_data_buffer_free_wrapper ( struct ast_data_buffer buffer)
static

Definition at line 46 of file test_data_buffer.c.

47{
48 if (!buffer) {
49 return;
50 }
51
53}
void ast_data_buffer_free(struct ast_data_buffer *buffer)
Free a data buffer (and all held data payloads)

References ast_data_buffer_free().

Referenced by AST_TEST_DEFINE(), AST_TEST_DEFINE(), AST_TEST_DEFINE(), and AST_TEST_DEFINE().

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 345 of file test_data_buffer.c.

◆ AST_TEST_DEFINE() [1/4]

AST_TEST_DEFINE ( buffer_create  )

Definition at line 55 of file test_data_buffer.c.

56{
58
59 switch (cmd) {
60 case TEST_INIT:
61 info->name = "buffer_create";
62 info->category = "/main/data_buffer/";
63 info->summary = "buffer create unit test";
64 info->description =
65 "Test that creating a data buffer results in a buffer with the expected values";
66 return AST_TEST_NOT_RUN;
67 case TEST_EXECUTE:
68 break;
69 }
70
72
73 ast_test_validate(test, buffer != NULL,
74 "Failed to create buffer with valid arguments");
75 ast_test_validate(test, ast_data_buffer_count(buffer) == 0,
76 "Newly created buffer does not have the expected payload count");
77 ast_test_validate(test, ast_data_buffer_max(buffer) == BUFFER_MAX_NOMINAL,
78 "Newly created buffer does not have the expected max size");
79
80 return AST_TEST_PASS;
81}
void ast_free_ptr(void *ptr)
free() wrapper
Definition astmm.c:1739
struct ast_data_buffer * ast_data_buffer_alloc(ast_data_buffer_free_callback free_fn, size_t size)
Allocate a data buffer.
size_t ast_data_buffer_count(const struct ast_data_buffer *buffer)
Return the number of payloads in a data buffer.
size_t ast_data_buffer_max(const struct ast_data_buffer *buffer)
Return the maximum number of payloads a data buffer can hold.
#define NULL
Definition resample.c:96
Data buffer containing fixed number of data payloads.
Definition data_buffer.c:59
@ TEST_INIT
Definition test.h:200
@ TEST_EXECUTE
Definition test.h:201
@ AST_TEST_PASS
Definition test.h:195
@ AST_TEST_NOT_RUN
Definition test.h:194
static void ast_data_buffer_free_wrapper(struct ast_data_buffer *buffer)
#define BUFFER_MAX_NOMINAL
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition utils.h:978

References ast_data_buffer_alloc(), ast_data_buffer_count(), ast_data_buffer_free_wrapper(), ast_data_buffer_max(), ast_free_ptr(), AST_TEST_NOT_RUN, AST_TEST_PASS, BUFFER_MAX_NOMINAL, NULL, RAII_VAR, TEST_EXECUTE, and TEST_INIT.

◆ AST_TEST_DEFINE() [2/4]

AST_TEST_DEFINE ( buffer_nominal  )

Definition at line 217 of file test_data_buffer.c.

218{
220 RAII_VAR(struct mock_payload *, removed_payload, NULL, ast_free_ptr);
221 struct mock_payload *payload;
222 struct mock_payload *fetched_payload;
223 int ret;
224 int i;
225
226 switch (cmd) {
227 case TEST_INIT:
228 info->name = "buffer_nominal";
229 info->category = "/main/data_buffer/";
230 info->summary = "buffer nominal unit test";
231 info->description =
232 "Tests the normal usage of a data buffer to ensure the expected payloads "
233 "are present after multiple insertions";
234 return AST_TEST_NOT_RUN;
235 case TEST_EXECUTE:
236 break;
237 }
238
240
241 ast_test_validate(test, buffer != NULL,
242 "Failed to create buffer with valid arguments");
243
244 for (i = 1; i <= BUFFER_MAX_NOMINAL; i++) {
245 payload = ast_calloc(1, sizeof(*payload));
246
247 ast_test_validate(test, payload != NULL,
248 "Failed to allocate memory for payload %d", i);
249
250 ret = ast_data_buffer_put(buffer, i, payload);
251 if (ret) {
252 ast_free(payload);
253 }
254
255 ast_test_validate(test, ret == 0,
256 "Failed to add payload %d to buffer", i);
257 }
258
259 ast_test_validate(test, ast_data_buffer_count(buffer) == BUFFER_MAX_NOMINAL,
260 "Buffer does not have the expected count after adding payloads");
261
262 for (i = 1; i <= BUFFER_MAX_NOMINAL; i++) {
263 fetched_payload = (struct mock_payload *)ast_data_buffer_get(buffer, i);
264
265 ast_test_validate(test, fetched_payload != NULL,
266 "Failed to get payload at position %d during first loop", i);
267 }
268
269 for (i = 1; i <= BUFFER_MAX_NOMINAL; i++) {
270 payload = ast_calloc(1, sizeof(*payload));
271
272 ast_test_validate(test, payload != NULL,
273 "Failed to allocate memory for payload %d", i + BUFFER_MAX_NOMINAL);
274
275 payload->id = i;
276 ret = ast_data_buffer_put(buffer, i + BUFFER_MAX_NOMINAL, payload);
277 if (ret) {
278 ast_free(payload);
279 }
280
281 ast_test_validate(test, ret == 0,
282 "Failed to add payload %d to buffer", i + BUFFER_MAX_NOMINAL);
283 }
284
285 ast_test_validate(test, ast_data_buffer_count(buffer) == BUFFER_MAX_NOMINAL,
286 "Buffer does not have the expected count after replacing payloads");
287
288 for (i = 1; i <= BUFFER_MAX_NOMINAL; i++) {
289 fetched_payload = (struct mock_payload *)ast_data_buffer_get(buffer, i);
290
291 ast_test_validate(test, fetched_payload == NULL,
292 "Got an unexpected payload at position %d", i);
293
294 fetched_payload = (struct mock_payload *)ast_data_buffer_get(buffer, i + BUFFER_MAX_NOMINAL);
295
296 ast_test_validate(test, fetched_payload != NULL,
297 "Failed to get payload at position %d during second loop", i + BUFFER_MAX_NOMINAL);
298 }
299
300 removed_payload = (struct mock_payload *)ast_data_buffer_remove_head(buffer);
301
302 ast_test_validate(test, removed_payload != NULL,
303 "Failed to get the payload at the HEAD of the buffer");
304
305 ast_test_validate(test, ast_data_buffer_count(buffer) == BUFFER_MAX_NOMINAL - 1,
306 "Removing payload from HEAD of buffer did not decrease buffer size");
307
308 ast_test_validate(test, removed_payload->id == 1,
309 "Removing payload from HEAD of buffer did not return expected payload");
310
311 ast_free(removed_payload);
312
313 removed_payload = (struct mock_payload *)ast_data_buffer_remove(buffer, BUFFER_MAX_NOMINAL * 2);
314
315 ast_test_validate(test, removed_payload != NULL,
316 "Failed to get payload at position %d from buffer", BUFFER_MAX_NOMINAL * 2);
317
318 ast_test_validate(test, ast_data_buffer_count(buffer) == BUFFER_MAX_NOMINAL - 2,
319 "Removing payload from buffer did not decrease buffer size");
320
321 ast_test_validate(test, removed_payload->id == BUFFER_MAX_NOMINAL,
322 "Removing payload from buffer did not return expected payload");
323
324 return AST_TEST_PASS;
325}
#define ast_free(a)
Definition astmm.h:180
#define ast_calloc(num, len)
A wrapper for calloc()
Definition astmm.h:202
void * ast_data_buffer_get(const struct ast_data_buffer *buffer, size_t pos)
Retrieve a data payload from the data buffer.
void * ast_data_buffer_remove_head(struct ast_data_buffer *buffer)
Remove the first payload from the data buffer.
int ast_data_buffer_put(struct ast_data_buffer *buffer, size_t pos, void *payload)
Place a data payload at a position in the data buffer.
void * ast_data_buffer_remove(struct ast_data_buffer *buffer, size_t pos)
Remove a data payload from the data buffer.

References ast_calloc, ast_data_buffer_alloc(), ast_data_buffer_count(), ast_data_buffer_free_wrapper(), ast_data_buffer_get(), ast_data_buffer_put(), ast_data_buffer_remove(), ast_data_buffer_remove_head(), ast_free, ast_free_ptr(), AST_TEST_NOT_RUN, AST_TEST_PASS, BUFFER_MAX_NOMINAL, mock_payload::id, NULL, RAII_VAR, TEST_EXECUTE, and TEST_INIT.

◆ AST_TEST_DEFINE() [3/4]

AST_TEST_DEFINE ( buffer_put  )

Definition at line 83 of file test_data_buffer.c.

84{
86 struct mock_payload *payload;
87 struct mock_payload *fetched_payload;
88 int ret;
89
90 switch (cmd) {
91 case TEST_INIT:
92 info->name = "buffer_put";
93 info->category = "/main/data_buffer/";
94 info->summary = "buffer put unit test";
95 info->description =
96 "Test that putting payloads in the buffer yields the expected results";
97 return AST_TEST_NOT_RUN;
98 case TEST_EXECUTE:
99 break;
100 }
101
103
104 ast_test_validate(test, buffer != NULL,
105 "Failed to create buffer with valid arguments");
106 ast_test_validate(test, ast_data_buffer_count(buffer) == 0,
107 "Newly created buffer is not empty");
108
109 payload = ast_calloc(1, sizeof(*payload));
110
111 ast_test_validate(test, payload != NULL,
112 "Failed to allocate memory for first payload");
113
114 payload->id = 2;
115 ret = ast_data_buffer_put(buffer, 2, payload);
116
117 ast_test_validate(test, ret == 0,
118 "Adding a payload to an empty buffer did not return the expected value");
119 ast_test_validate(test, ast_data_buffer_count(buffer) == 1,
120 "Adding a payload to an empty buffer did not update count to the expected value");
121
122 fetched_payload = (struct mock_payload *)ast_data_buffer_get(buffer, 2);
123
124 ast_test_validate(test, fetched_payload != NULL,
125 "Failed to get only payload from buffer given valid arguments");
126
127 ast_data_buffer_put(buffer, 2, payload);
128
129 ast_test_validate(test, ast_data_buffer_count(buffer) == 1,
130 "Adding a payload that is already in the buffer should not do anything");
131
132 payload = ast_calloc(1, sizeof(*payload));
133
134 ast_test_validate(test, payload != NULL,
135 "Failed to allocate memory for second payload");
136
137 payload->id = 1;
138 ast_data_buffer_put(buffer, 1, payload);
139 fetched_payload = ast_data_buffer_get(buffer, 1);
140
141 ast_test_validate(test, fetched_payload != NULL,
142 "Failed to get a payload from buffer given valid arguments");
143 ast_test_validate(test, ast_data_buffer_count(buffer) == 2,
144 "Buffer does not have the expected count after removing a payload");
145 ast_test_validate(test, fetched_payload->id == 1,
146 "Did not get the expected payload from the buffer");
147
148 payload = ast_calloc(1, sizeof(*payload));
149
150 ast_test_validate(test, payload != NULL,
151 "Failed to allocate memory for third payload");
152
153 payload->id = 3;
154 ret = ast_data_buffer_put(buffer, 3, payload);
155
156 ast_test_validate(test, ret == 0,
157 "Failed to replace a payload in the buffer");
158 ast_test_validate(test, ast_data_buffer_count(buffer) <= 2,
159 "Buffer count exceeded the max");
160
161 fetched_payload = (struct mock_payload *)ast_data_buffer_get(buffer, 3);
162
163 ast_test_validate(test, fetched_payload != NULL,
164 "Failed to get a payload from buffer at position 3 given valid arguments");
165 ast_test_validate(test, fetched_payload->id == 3,
166 "Did not get the expected payload at position 3 from the buffer");
167
168 fetched_payload = (struct mock_payload *)ast_data_buffer_get(buffer, 2);
169
170 ast_test_validate(test, fetched_payload != NULL,
171 "Failed to get a payload from buffer at position 2 given valid arguments");
172 ast_test_validate(test, fetched_payload->id == 2,
173 "Did not get the expected payload at position 2 from the buffer");
174
175 return AST_TEST_PASS;
176}

References ast_calloc, ast_data_buffer_alloc(), ast_data_buffer_count(), ast_data_buffer_free_wrapper(), ast_data_buffer_get(), ast_data_buffer_put(), ast_free_ptr(), AST_TEST_NOT_RUN, AST_TEST_PASS, mock_payload::id, NULL, RAII_VAR, TEST_EXECUTE, and TEST_INIT.

◆ AST_TEST_DEFINE() [4/4]

AST_TEST_DEFINE ( buffer_resize  )

Definition at line 178 of file test_data_buffer.c.

179{
181
182 switch (cmd) {
183 case TEST_INIT:
184 info->name = "buffer_resize";
185 info->category = "/main/data_buffer/";
186 info->summary = "buffer resize unit test";
187 info->description =
188 "Tests resizing a data buffer to make sure it has the expected outcome";
189 return AST_TEST_NOT_RUN;
190 case TEST_EXECUTE:
191 break;
192 }
193
195
196 ast_test_validate(test, buffer != NULL,
197 "Failed to create buffer with valid arguments");
198
200
201 ast_test_validate(test, ast_data_buffer_max(buffer) == BUFFER_MAX_NOMINAL,
202 "Trying to resize buffer to same size should not change its max size");
203
205
206 ast_test_validate(test, ast_data_buffer_max(buffer) == BUFFER_MAX_NOMINAL + 2,
207 "Increasing buffer size did not return the expected max");
208
209 ast_data_buffer_resize(buffer, 1);
210
211 ast_test_validate(test, ast_data_buffer_max(buffer) == 1,
212 "Decreasing buffer size did not return the expected max");
213
214 return AST_TEST_PASS;
215}
void ast_data_buffer_resize(struct ast_data_buffer *buffer, size_t size)
Resize a data buffer.

References ast_data_buffer_alloc(), ast_data_buffer_free_wrapper(), ast_data_buffer_max(), ast_data_buffer_resize(), ast_free_ptr(), AST_TEST_NOT_RUN, AST_TEST_PASS, BUFFER_MAX_NOMINAL, NULL, RAII_VAR, TEST_EXECUTE, and TEST_INIT.

◆ load_module()

static int load_module ( void  )
static

Definition at line 336 of file test_data_buffer.c.

337{
338 AST_TEST_REGISTER(buffer_create);
339 AST_TEST_REGISTER(buffer_put);
340 AST_TEST_REGISTER(buffer_resize);
341 AST_TEST_REGISTER(buffer_nominal);
343}
@ AST_MODULE_LOAD_SUCCESS
Definition module.h:70
#define AST_TEST_REGISTER(cb)
Definition test.h:127

References AST_MODULE_LOAD_SUCCESS, and AST_TEST_REGISTER.

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 327 of file test_data_buffer.c.

328{
329 AST_TEST_UNREGISTER(buffer_create);
330 AST_TEST_UNREGISTER(buffer_put);
331 AST_TEST_UNREGISTER(buffer_resize);
332 AST_TEST_UNREGISTER(buffer_nominal);
333 return 0;
334}
#define AST_TEST_UNREGISTER(cb)
Definition test.h:128

References AST_TEST_UNREGISTER.

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Data buffer API test module" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
static

Definition at line 345 of file test_data_buffer.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 345 of file test_data_buffer.c.