43 const char *address_holder;
47 static const char LONG_STRING[] =
"A professional panoramic photograph of the majestic elephant bathing itself and its young by the shores of the raging Mississippi River";
66 info->name =
"string_field_test";
67 info->category =
"/main/utils/";
68 info->summary =
"Test stringfield operations";
70 "This tests the stringfield API";
71 return AST_TEST_NOT_RUN;
79 ast_test_status_update(
test,
"First things first. Let's see if we can actually allocate string fields\n");
82 ast_test_status_update(
test,
"Failure to initialize string fields. They are totally messed up\n");
85 ast_test_status_update(
test,
"All right! Successfully allocated! Now let's get down to business\n");
89 ast_test_status_update(
test,
"We're going to set some string fields and perform some checks\n");
94 ast_test_status_update(
test,
"First we're going to make sure that the strings are actually set to what we expect\n");
97 ast_test_status_update(
test,
"We were expecting test_struct.string1 to have 'elephant' but it has %s\n",
test_struct.string1);
100 ast_test_status_update(
test,
"test_struct.string1 appears to be all clear. It has '%s' and that's what we expect\n",
test_struct.string1);
104 ast_test_status_update(
test,
"We were expecting test_struct.string2 to have 'hippopotamus' but it has %s\n",
test_struct.string2);
107 ast_test_status_update(
test,
"test_struct.string2 appears to be all clear. It has '%s' and that's what we expect\n",
test_struct.string2);
110 ast_test_status_update(
test,
"Now let's make sure that our recorded capacities for these strings is what we expect\n");
113 ast_test_status_update(
test,
"string1 has allocation area of %hu but we expect %lu\n",
121 ast_test_status_update(
test,
"string2 has allocation area of %hu but we expect %lu\n",
128 ast_test_status_update(
test,
"Now we're going to shrink string1 and see if it's in the same place in memory\n");
134 ast_test_status_update(
test,
"string1 has the wrong value in it. We want 'rhino' but it has '%s'\n",
test_struct.string1);
137 ast_test_status_update(
test,
"string1 successfully was changed to '%s'\n",
test_struct.string1);
141 ast_test_status_update(
test,
"We shrunk string1, but it moved?!\n");
144 ast_test_status_update(
test,
"Shrinking string1 allowed it to stay in the same place in memory\n");
148 ast_test_status_update(
test,
"The allocation amount changed when we shrunk the string...\n");
151 ast_test_status_update(
test,
"Shrinking string1 did not change its allocation area (This is a good thing)\n");
154 ast_test_status_update(
test,
"Next, let's increase it a little but not all the way to its original size\n");
160 ast_test_status_update(
test,
"string1 has the wrong value in it. We want 'mammoth' but it has '%s'\n",
test_struct.string1);
163 ast_test_status_update(
test,
"string1 successfully was changed to '%s'\n",
test_struct.string1);
167 ast_test_status_update(
test,
"We expanded string1, but it moved?!\n");
170 ast_test_status_update(
test,
"Expanding string1 allowed it to stay in the same place in memory\n");
174 ast_test_status_update(
test,
"The allocation amount changed when we expanded the string...\n");
177 ast_test_status_update(
test,
"Expanding string1 did not change its allocation area (This is a good thing)\n");
180 ast_test_status_update(
test,
"Cool, now let's bring it back to its original size and see what happens\n");
185 ast_test_status_update(
test,
"string1 has the wrong value in it. We want 'elephant' but it has '%s'\n",
test_struct.string1);
188 ast_test_status_update(
test,
"string1 successfully changed to '%s'\n",
test_struct.string1);
192 ast_test_status_update(
test,
"We restored string1 to its original size, but it moved?!\n");
195 ast_test_status_update(
test,
"Restoring string1 did not cause it to move (This is a good thing)\n");
199 ast_test_status_update(
test,
"The allocation amount changed when we re-expanded the string...\n");
202 ast_test_status_update(
test,
"The allocation amount for string1 is still holding steady\n");
205 ast_test_status_update(
test,
"All right, now we're going to expand string 2. It should stay in place since it was the last string allocated in this pool\n");
210 if (strcmp(
test_struct.string2,
"hippopotamus face")) {
211 ast_test_status_update(
test,
"string2 has the wrong value. We want 'hippopotamus face' but it has '%s'\n",
test_struct.string2);
214 ast_test_status_update(
test,
"string2 successfully changed to '%s'\n",
test_struct.string2);
218 ast_test_status_update(
test,
"The allocation amount is incorrect for string2. We expect %lu but it has %d\n",
222 ast_test_status_update(
test,
"The allocation amount successfully increased for string2 when it grew\n");
226 ast_test_status_update(
test,
"string2 has moved, but it should not have since it had room to grow\n");
229 ast_test_status_update(
test,
"string2 stayed in place when it grew. Good job!\n");
232 ast_test_status_update(
test,
"Now we're going to set string1 to a very long string so that a new string field pool must be allocated\n");
238 ast_test_status_update(
test,
"We were expecting string1 to be '%s'\nbut it was actually '%s'\n", LONG_STRING,
test_struct.string1);
241 ast_test_status_update(
test,
"string1 successfully changed to '%s'\n",
test_struct.string1);
245 ast_test_status_update(
test,
"Uh oh, string1 didn't move when we set it to a long value\n");
248 ast_test_status_update(
test,
"Good. Setting string1 to a long value caused it to change addresses\n");
252 ast_test_status_update(
test,
"The string field allocation for string1 indicates a length of %hu instead of the expected %lu\n",
256 ast_test_status_update(
test,
"The stored allocation size of string1 is what we expect\n");
260 ast_test_status_update(
test,
"Now using a totally separate area of memory we're going to test a basic pool freeing scenario\n");
269 ast_string_field_set(&test_struct2, string1,
"Expanded first string to create new pool-----------------------------------------------------------------------------------------------------------------------------------------------------------------------");
271 ast_string_field_set(&test_struct2, string2,
"Expanded second string to create new pool----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
273 field_pool3 = test_struct2.__field_mgr_pool;
274 field_pool2 = test_struct2.__field_mgr_pool->
prev;
275 field_pool1 = test_struct2.__field_mgr_pool->
prev->
prev;
277 if(field_pool3->
prev != field_pool2 || field_pool2->
prev != field_pool1) {
278 ast_test_status_update(
test,
"Pools are not linked properly!\n");
281 ast_test_status_update(
test,
"Three different pools are linked as expected.\n");
285 if (test_struct2.string1 != __ast_string_field_empty || field_pool3->
prev != field_pool1) {
286 ast_test_status_update(
test,
"Things did not work out when removing the middle pool!\n");
289 ast_test_status_update(
test,
"After removing a pool the remaining two are linked as expected.\n");
294 return AST_TEST_PASS;
299 return AST_TEST_FAIL;
308 AST_STRING_FIELD_EXTENDED(string2);
313 enum ast_test_result_state res = AST_TEST_PASS;
321 info->name =
"string_field_aggregate_test";
322 info->category =
"/main/utils/";
323 info->summary =
"Test stringfield aggregate operations";
325 "This tests the structure comparison and copy macros of the stringfield API";
326 return AST_TEST_NOT_RUN;
333 ast_test_status_update(
test,
"Unable to allocate structure 1!\n");
341 ast_test_status_update(
test,
"Unable to allocate structure 2!\n");
349 ast_test_status_update(
test,
"Unable to allocate structure 3!\n");
357 ast_test_status_update(
test,
"Unable to allocate structure 4!\n");
368 ast_string_field_ptr_set_by_fields(inst2->__field_mgr_pool, inst2->__field_mgr, &inst2->string2,
"bar");
369 ast_string_field_ptr_set_by_fields(inst2->__field_mgr_pool, inst2->__field_mgr, &inst2->string1,
"foo");
373 ast_test_status_update(
test,
"Structure 3 embedded pool should not have a previous pool!\n");
380 if (inst3->__field_mgr.
embedded_pool != inst3->__field_mgr_pool) {
381 ast_test_status_update(
test,
"Structure 3 embedded pool should have been the current pool!\n");
387 ast_test_status_update(
test,
"Structure 3 embedded pool should not have a previous pool!\n");
392 ast_test_status_update(
test,
"Structures 3 embedded pool initialized successfully.\n");
395 ast_string_field_set(inst3, string2,
"baz 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890");
398 if (inst3->__field_mgr_pool == inst3->__field_mgr.
embedded_pool) {
399 ast_test_status_update(
test,
"Structure 3 embedded pool should not have been the current pool!\n");
405 ast_test_status_update(
test,
"Structure 3 embedded pool should be the current pool's previous!\n");
410 ast_test_status_update(
test,
"Structures 3 additional pool initialized successfully.\n");
414 ast_string_field_set(inst4, string2,
"baz 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890");
418 ast_test_status_update(
test,
"Structures 1/2 should be equal!\n");
422 ast_test_status_update(
test,
"Structures 1/2 are equal as expected.\n");
426 ast_test_status_update(
test,
"Structures 1/3 should be different!\n");
430 ast_test_status_update(
test,
"Structures 1/3 are different as expected.\n");
434 ast_test_status_update(
test,
"Structures 2/3 should be different!\n");
438 ast_test_status_update(
test,
"Structures 2/3 are different as expected.\n");
442 ast_test_status_update(
test,
"Structures 3/4 should be different!\n");
446 ast_test_status_update(
test,
"Structures 3/4 are different as expected.\n");
450 ast_test_status_update(
test,
"Copying from structure 3 to structure 1 failed!\n");
454 ast_test_status_update(
test,
"Copying from structure 3 to structure 1 succeeded!\n");
459 ast_test_status_update(
test,
"Structures 1/3 should be equal!\n");
463 ast_test_status_update(
test,
"Structures 1/3 are equal as expected.\n");
467 ast_test_status_update(
test,
"Structures 1/2 should be different!\n");
470 ast_test_status_update(
test,
"Structures 1/2 are different as expected.\n");
473 ast_test_status_update(
test,
"Reset but don't free.\n");
481 ast_test_status_update(
test,
"Structures 1/2 should be the same (empty)!\n");
484 ast_test_status_update(
test,
"Structures 1/2 are the same (empty) as expected.\n");
487 if (inst4->__field_mgr.
embedded_pool != inst4->__field_mgr_pool) {
488 ast_test_status_update(
test,
"Structure 4 embedded pool should have been the current pool!\n");
492 ast_test_status_update(
test,
"Structure 4 embedded pool is the current pool as expected.\n");
496 ast_test_status_update(
test,
"Structure 4 embedded pool should not have a previous pool!\n");
500 ast_test_status_update(
test,
"Structure 4 embedded pool does not have a previous as expected.\n");
516 static int unload_module(
void)
518 AST_TEST_UNREGISTER(string_field_aggregate_test);
519 AST_TEST_UNREGISTER(string_field_test);
523 static int load_module(
void)
525 AST_TEST_REGISTER(string_field_test);
526 AST_TEST_REGISTER(string_field_aggregate_test);
#define ast_string_field_init_extended(x, field)
Initialize an extended string field.
Asterisk main include file. File version handling, generic pbx functions.
struct ast_string_field_pool * embedded_pool
#define ast_calloc_with_stringfields(n, type, size)
Allocate a structure with embedded stringfields in a single allocation.
#define AST_DECLARE_STRING_FIELDS(field_list)
Declare the fields needed in a structure.
#define AST_STRING_FIELD_ALLOCATION(x)
Macro to provide access to the allocation field that lives immediately in front of a string field...
#define AST_STRING_FIELD_EXTENDED(name)
Declare an extended string field.
#define ast_string_field_init(x, size)
Initialize a field pool and fields.
#define AST_STRING_FIELD(name)
Declare a string field.
#define ast_string_fields_cmp(instance1, instance2)
Compare the string fields in two instances of the same structure.
#define ast_string_fields_copy(copy, orig)
Copy all string fields from one instance to another of the same structure.
Standard Command Line Interface.
struct ast_string_field_pool * prev
#define AST_TEST_DEFINE(hdr)
#define ASTERISK_GPL_KEY
The text the key() function should return.
Asterisk module definitions.
#define ast_string_field_free_memory(x)
free all memory - to be called before destroying the object
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.