38 #define BUFFER_MAX_NOMINAL 10
46 static void ast_data_buffer_free_wrapper(
struct ast_data_buffer *buffer)
61 info->name =
"buffer_create";
62 info->category =
"/main/data_buffer/";
63 info->summary =
"buffer create unit test";
65 "Test that creating a data buffer results in a buffer with the expected values";
66 return AST_TEST_NOT_RUN;
73 ast_test_validate(
test, buffer != NULL,
74 "Failed to create buffer with valid arguments");
76 "Newly created buffer does not have the expected payload count");
78 "Newly created buffer does not have the expected max size");
92 info->name =
"buffer_put";
93 info->category =
"/main/data_buffer/";
94 info->summary =
"buffer put unit test";
96 "Test that putting payloads in the buffer yields the expected results";
97 return AST_TEST_NOT_RUN;
104 ast_test_validate(
test, buffer != NULL,
105 "Failed to create buffer with valid arguments");
107 "Newly created buffer is not empty");
111 ast_test_validate(
test, payload != NULL,
112 "Failed to allocate memory for first payload");
117 ast_test_validate(
test, ret == 0,
118 "Adding a payload to an empty buffer did not return the expected value");
120 "Adding a payload to an empty buffer did not update count to the expected value");
124 ast_test_validate(
test, fetched_payload != NULL,
125 "Failed to get only payload from buffer given valid arguments");
130 "Adding a payload that is already in the buffer should not do anything");
134 ast_test_validate(
test, payload != NULL,
135 "Failed to allocate memory for second payload");
141 ast_test_validate(
test, fetched_payload != NULL,
142 "Failed to get a payload from buffer given valid arguments");
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");
150 ast_test_validate(
test, payload != NULL,
151 "Failed to allocate memory for third payload");
156 ast_test_validate(
test, ret == 0,
157 "Failed to replace a payload in the buffer");
159 "Buffer count exceeded the max");
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");
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");
175 return AST_TEST_PASS;
184 info->name =
"buffer_resize";
185 info->category =
"/main/data_buffer/";
186 info->summary =
"buffer resize unit test";
188 "Tests resizing a data buffer to make sure it has the expected outcome";
189 return AST_TEST_NOT_RUN;
196 ast_test_validate(
test, buffer != NULL,
197 "Failed to create buffer with valid arguments");
202 "Trying to resize buffer to same size should not change its max size");
207 "Increasing buffer size did not return the expected max");
212 "Decreasing buffer size did not return the expected max");
214 return AST_TEST_PASS;
228 info->name =
"buffer_nominal";
229 info->category =
"/main/data_buffer/";
230 info->summary =
"buffer nominal unit test";
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;
241 ast_test_validate(
test, buffer != NULL,
242 "Failed to create buffer with valid arguments");
244 for (i = 1; i <= BUFFER_MAX_NOMINAL; i++) {
247 ast_test_validate(
test, payload != NULL,
248 "Failed to allocate memory for payload %d", i);
255 ast_test_validate(
test, ret == 0,
256 "Failed to add payload %d to buffer", i);
260 "Buffer does not have the expected count after adding payloads");
262 for (i = 1; i <= BUFFER_MAX_NOMINAL; i++) {
265 ast_test_validate(
test, fetched_payload != NULL,
266 "Failed to get payload at position %d during first loop", i);
269 for (i = 1; i <= BUFFER_MAX_NOMINAL; i++) {
272 ast_test_validate(
test, payload != NULL,
273 "Failed to allocate memory for payload %d", i + BUFFER_MAX_NOMINAL);
281 ast_test_validate(
test, ret == 0,
282 "Failed to add payload %d to buffer", i + BUFFER_MAX_NOMINAL);
286 "Buffer does not have the expected count after replacing payloads");
288 for (i = 1; i <= BUFFER_MAX_NOMINAL; i++) {
291 ast_test_validate(
test, fetched_payload == NULL,
292 "Got an unexpected payload at position %d", i);
296 ast_test_validate(
test, fetched_payload != NULL,
297 "Failed to get payload at position %d during second loop", i + BUFFER_MAX_NOMINAL);
302 ast_test_validate(
test, removed_payload != NULL,
303 "Failed to get the payload at the HEAD of the buffer");
306 "Removing payload from HEAD of buffer did not decrease buffer size");
308 ast_test_validate(
test, removed_payload->id == 1,
309 "Removing payload from HEAD of buffer did not return expected payload");
311 ast_free(removed_payload);
315 ast_test_validate(
test, removed_payload != NULL,
316 "Failed to get payload at position %d from buffer", BUFFER_MAX_NOMINAL * 2);
319 "Removing payload from buffer did not decrease buffer size");
321 ast_test_validate(
test, removed_payload->id == BUFFER_MAX_NOMINAL,
322 "Removing payload from buffer did not return expected payload");
324 return AST_TEST_PASS;
327 static int unload_module(
void)
329 AST_TEST_UNREGISTER(buffer_create);
330 AST_TEST_UNREGISTER(buffer_put);
331 AST_TEST_UNREGISTER(buffer_resize);
332 AST_TEST_UNREGISTER(buffer_nominal);
336 static int load_module(
void)
338 AST_TEST_REGISTER(buffer_create);
339 AST_TEST_REGISTER(buffer_put);
340 AST_TEST_REGISTER(buffer_resize);
341 AST_TEST_REGISTER(buffer_nominal);
Asterisk main include file. File version handling, generic pbx functions.
Data buffer containing fixed number of data payloads.
struct ast_data_buffer * ast_data_buffer_alloc(ast_data_buffer_free_callback free_fn, size_t size)
Allocate a data buffer.
void * ast_data_buffer_remove(struct ast_data_buffer *buffer, size_t pos)
Remove a data payload from the data buffer.
void ast_free_ptr(void *ptr)
free() wrapper
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_get(const struct ast_data_buffer *buffer, size_t pos)
Retrieve a data payload from the data buffer.
#define ast_calloc(num, len)
A wrapper for calloc()
size_t ast_data_buffer_count(const struct ast_data_buffer *buffer)
Return the number of payloads in a data buffer.
#define AST_TEST_DEFINE(hdr)
void ast_data_buffer_resize(struct ast_data_buffer *buffer, size_t size)
Resize a data buffer.
#define ASTERISK_GPL_KEY
The text the key() function should return.
Asterisk module definitions.
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
size_t ast_data_buffer_max(const struct ast_data_buffer *buffer)
Return the maximum number of payloads a data buffer can hold.
void ast_data_buffer_free(struct ast_data_buffer *buffer)
Free a data buffer (and all held data payloads)
void * ast_data_buffer_remove_head(struct ast_data_buffer *buffer)
Remove the first payload from the data buffer.