Newer
Older
{
struct ao2_container *container;
container = NULL;
switch (type) {
case TEST_CONTAINER_LIST:
container = ao2_t_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, options,
test_sort_cb, test_cmp_cb, "test");
break;
case TEST_CONTAINER_HASH:
container = ao2_t_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, options, 5,
test_hash_cb, test_sort_cb, test_cmp_cb, "test");
break;
case TEST_CONTAINER_RBTREE:
container = ao2_t_container_alloc_rbtree(AO2_ALLOC_OPT_LOCK_MUTEX, options,
test_sort_cb, test_cmp_cb, "test");
break;
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
}
return container;
}
/*!
* \internal
* \brief Insert the given test vector into the given container.
* \since 12.0.0
*
* \note The given test vector must not have any duplicates.
*
* \param container Container to insert the test vector.
* \param destroy_counter What to increment when the object is destroyed.
* \param vector Test vector to insert.
* \param count Number of objects in the vector.
* \param prefix Test output prefix string.
* \param test Test output controller.
*
* \retval 0 on success.
* \retval -1 on error.
*/
static int insert_test_vector(struct ao2_container *container, int *destroy_counter, const int *vector, int count, const char *prefix, struct ast_test *test)
{
int idx;
struct test_obj *obj;
for (idx = 0; idx < count; ++idx) {
obj = ao2_alloc(sizeof(struct test_obj), test_obj_destructor);
if (!obj) {
ast_test_status_update(test, "%s: ao2_alloc failed.\n", prefix);
return -1;
}
if (destroy_counter) {
/* This object ultimately needs to be destroyed. */
++*destroy_counter;
}
obj->destructor_count = destroy_counter;
obj->i = vector[idx];
ao2_link(container, obj);
ao2_t_ref(obj, -1, "test");
if (ao2_container_check(container, 0)) {
ast_test_status_update(test, "%s: Container integrity check failed linking vector[%d]:%d\n",
prefix, idx, vector[idx]);
return -1;
}
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
if (ao2_container_count(container) != idx + 1) {
ast_test_status_update(test,
"%s: Unexpected container count. Expected:%d Got:%d\n",
prefix, idx + 1, ao2_container_count(container));
return -1;
}
}
return 0;
}
/*!
* \internal
* \brief Insert duplicates of number into the given container.
* \since 12.0.0
*
* \note The given container must not already have the number in it.
*
* \param container Container to insert the duplicates.
* \param destroy_counter What to increment when the object is destroyed.
* \param number Number of object to duplicate.
* \param prefix Test output prefix string.
* \param test Test output controller.
*
* \retval 0 on success.
* \retval -1 on error.
*/
static int insert_test_duplicates(struct ao2_container *container, int *destroy_counter, int number, const char *prefix, struct ast_test *test)
{
int count;
struct test_obj *obj;
struct test_obj *obj_dup;
/* Check if object already exists in the container. */
obj = ao2_find(container, &number, OBJ_KEY);
if (obj) {
ast_test_status_update(test, "%s: Object %d already exists.\n", prefix, number);
ao2_t_ref(obj, -1, "test");
return -1;
}
/* Add three duplicate keyed objects. */
obj_dup = NULL;
for (count = 0; count < 4; ++count) {
obj = ao2_alloc(sizeof(struct test_obj), test_obj_destructor);
if (!obj) {
ast_test_status_update(test, "%s: ao2_alloc failed.\n", prefix);
if (obj_dup) {
ao2_t_ref(obj_dup, -1, "test");
}
return -1;
}
if (destroy_counter) {
/* This object ultimately needs to be destroyed. */
++*destroy_counter;
}
obj->destructor_count = destroy_counter;
obj->i = number;
obj->dup_number = count;
ao2_link(container, obj);
if (count == 2) {
/* Duplicate this object. */
obj_dup = obj;
} else {
ao2_t_ref(obj, -1, "test");
}
if (ao2_container_check(container, 0)) {
ast_test_status_update(test, "%s: Container integrity check failed linking num:%d dup:%d\n",
prefix, number, count);
if (obj_dup) {
ao2_t_ref(obj_dup, -1, "test");
}
return -1;
}
}
/* Add the duplicate object. */
ao2_link(container, obj_dup);
ao2_t_ref(obj_dup, -1, "test");
if (ao2_container_check(container, 0)) {
ast_test_status_update(test, "%s: Container integrity check failed linking obj_dup\n",
prefix);
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
return -1;
}
return 0;
}
/*!
* \internal
* \brief Iterate over the container and compare the objects with the given vector.
* \since 12.0.0
*
* \param res Passed in enum ast_test_result_state.
* \param container Container to iterate.
* \param flags Flags controlling the iteration.
* \param vector Expected vector to find.
* \param count Number of objects in the vector.
* \param prefix Test output prefix string.
* \param test Test output controller.
*
* \return enum ast_test_result_state
*/
static int test_ao2_iteration(int res, struct ao2_container *container,
enum ao2_iterator_flags flags,
const int *vector, int count, const char *prefix, struct ast_test *test)
{
struct ao2_iterator iter;
struct test_obj *obj;
int idx;
if (ao2_container_count(container) != count) {
ast_test_status_update(test, "%s: Container count doesn't match vector count.\n",
prefix);
res = AST_TEST_FAIL;
}
iter = ao2_iterator_init(container, flags);
/* Check iterated objects against the given vector. */
for (idx = 0; idx < count; ++idx) {
obj = ao2_iterator_next(&iter);
if (!obj) {
ast_test_status_update(test, "%s: Too few objects found.\n", prefix);
res = AST_TEST_FAIL;
break;
}
if (vector[idx] != obj->i) {
ast_test_status_update(test, "%s: Object %d != vector[%d] %d.\n",
prefix, obj->i, idx, vector[idx]);
res = AST_TEST_FAIL;
}
ao2_ref(obj, -1); /* remove ref from iterator */
}
obj = ao2_iterator_next(&iter);
if (obj) {
ast_test_status_update(test, "%s: Too many objects found. Object %d\n",
prefix, obj->i);
ao2_ref(obj, -1); /* remove ref from iterator */
res = AST_TEST_FAIL;
}
ao2_iterator_destroy(&iter);
return res;
}
/*!
* \internal
* \brief Run an ao2_callback() and compare the returned vector with the given vector.
* \since 12.0.0
*
* \param res Passed in enum ast_test_result_state.
* \param container Container to traverse.
* \param flags Callback flags controlling the traversal.
* \param cmp_fn Compare function to select objects.
* \param arg Optional argument.
* \param vector Expected vector to find.
* \param count Number of objects in the vector.
* \param prefix Test output prefix string.
* \param test Test output controller.
*
* \return enum ast_test_result_state
*/
static int test_ao2_callback_traversal(int res, struct ao2_container *container,
enum search_flags flags, ao2_callback_fn *cmp_fn, void *arg,
const int *vector, int count, const char *prefix, struct ast_test *test)
{
struct ao2_iterator *mult_iter;
struct test_obj *obj;
int idx;
mult_iter = ao2_callback(container, flags | OBJ_MULTIPLE, cmp_fn, arg);
if (!mult_iter) {
ast_test_status_update(test, "%s: Did not return iterator.\n", prefix);
return AST_TEST_FAIL;
}
/* Check matching objects against the given vector. */
for (idx = 0; idx < count; ++idx) {
obj = ao2_iterator_next(mult_iter);
if (!obj) {
ast_test_status_update(test, "%s: Too few objects found.\n", prefix);
res = AST_TEST_FAIL;
break;
}
if (vector[idx] != obj->i) {
ast_test_status_update(test, "%s: Object %d != vector[%d] %d.\n",
prefix, obj->i, idx, vector[idx]);
res = AST_TEST_FAIL;
}
ao2_ref(obj, -1); /* remove ref from iterator */
}
obj = ao2_iterator_next(mult_iter);
if (obj) {
ast_test_status_update(test, "%s: Too many objects found. Object %d\n",
prefix, obj->i);
ao2_ref(obj, -1); /* remove ref from iterator */
res = AST_TEST_FAIL;
}
ao2_iterator_destroy(mult_iter);
return res;
}
/*!
* \internal
* \brief Run an ao2_find() for duplicates and compare the returned vector with the given vector.
* \since 12.0.0
*
* \param res Passed in enum ast_test_result_state.
* \param container Container to traverse.
* \param flags Callback flags controlling the traversal.
* \param number Number of object to find all duplicates.
* \param vector Expected vector to find.
* \param count Number of objects in the vector.
* \param prefix Test output prefix string.
* \param test Test output controller.
*
* \return enum ast_test_result_state
*/
static int test_expected_duplicates(int res, struct ao2_container *container,
enum search_flags flags, int number,
const int *vector, int count, const char *prefix, struct ast_test *test)
{
struct ao2_iterator *mult_iter;
struct test_obj *obj;
int idx;
mult_iter = ao2_find(container, &number, flags | OBJ_MULTIPLE | OBJ_KEY);
if (!mult_iter) {
ast_test_status_update(test, "%s: Did not return iterator.\n", prefix);
return AST_TEST_FAIL;
}
/* Check matching objects against the given vector. */
for (idx = 0; idx < count; ++idx) {
obj = ao2_iterator_next(mult_iter);
if (!obj) {
ast_test_status_update(test, "%s: Too few objects found.\n", prefix);
res = AST_TEST_FAIL;
break;
}
if (number != obj->i) {
ast_test_status_update(test, "%s: Object %d != %d.\n",
prefix, obj->i, number);
res = AST_TEST_FAIL;
}
if (vector[idx] != obj->dup_number) {
ast_test_status_update(test, "%s: Object dup id %d != vector[%d] %d.\n",
prefix, obj->dup_number, idx, vector[idx]);
res = AST_TEST_FAIL;
}
ao2_ref(obj, -1); /* remove ref from iterator */
}
obj = ao2_iterator_next(mult_iter);
if (obj) {
ast_test_status_update(test,
"%s: Too many objects found. Object %d, dup id %d\n",
prefix, obj->i, obj->dup_number);
ao2_ref(obj, -1); /* remove ref from iterator */
res = AST_TEST_FAIL;
}
ao2_iterator_destroy(mult_iter);
return res;
}
/*!
* \internal
* \brief Test nonsorted container traversal.
* \since 12.0.0
*
* \param res Passed in enum ast_test_result_state.
* \param tst_num Test number.
* \param type Container type to test.
* \param test Test output controller.
*
* \return enum ast_test_result_state
*/
static int test_traversal_nonsorted(int res, int tst_num, enum test_container_type type, struct ast_test *test)
{
struct ao2_container *c1;
struct ao2_container *c2 = NULL;
int partial;
int destructor_count = 0;
/*! Container object insertion vector. */
static const int test_initial[] = {
1, 0, 2, 6, 4, 7, 5, 3, 9, 8
};
/*! Container object insertion vector reversed. */
static const int test_reverse[] = {
8, 9, 3, 5, 7, 4, 6, 2, 0, 1
};
static const int test_list_partial_forward[] = {
6, 7, 5
};
static const int test_list_partial_backward[] = {
5, 7, 6
};
/* The hash orders assume that there are 5 buckets. */
static const int test_hash_end_forward[] = {
0, 5, 1, 6, 2, 7, 3, 8, 4, 9
};
static const int test_hash_end_backward[] = {
9, 4, 8, 3, 7, 2, 6, 1, 5, 0
};
static const int test_hash_begin_forward[] = {
5, 0, 6, 1, 7, 2, 8, 3, 9, 4
};
static const int test_hash_begin_backward[] = {
4, 9, 3, 8, 2, 7, 1, 6, 0, 5
};
static const int test_hash_partial_forward[] = {
5, 6, 7
};
static const int test_hash_partial_backward[] = {
7, 6, 5
};
ast_test_status_update(test, "Test %d, %s containers.\n",
tst_num, test_container2str(type));
/* Create container that inserts objects at the end. */
c1 = test_make_nonsorted(type, 0);
if (!c1) {
ast_test_status_update(test, "Container c1 creation failed.\n");
res = AST_TEST_FAIL;
goto test_cleanup;
}
if (insert_test_vector(c1, &destructor_count, test_initial, ARRAY_LEN(test_initial), "c1", test)) {
res = AST_TEST_FAIL;
goto test_cleanup;
}
/* Create container that inserts objects at the beginning. */
c2 = test_make_nonsorted(type, AO2_CONTAINER_ALLOC_OPT_INSERT_BEGIN);
if (!c2) {
ast_test_status_update(test, "Container c2 creation failed.\n");
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
res = AST_TEST_FAIL;
goto test_cleanup;
}
if (insert_test_vector(c2, &destructor_count, test_initial, ARRAY_LEN(test_initial), "c2", test)) {
res = AST_TEST_FAIL;
goto test_cleanup;
}
/* Check container iteration directions */
switch (type) {
case TEST_CONTAINER_LIST:
res = test_ao2_iteration(res, c1, 0,
test_initial, ARRAY_LEN(test_initial),
"Iteration (ascending, insert end)", test);
res = test_ao2_iteration(res, c1, AO2_ITERATOR_DESCENDING,
test_reverse, ARRAY_LEN(test_reverse),
"Iteration (descending, insert end)", test);
res = test_ao2_iteration(res, c2, 0,
test_reverse, ARRAY_LEN(test_reverse),
"Iteration (ascending, insert begin)", test);
res = test_ao2_iteration(res, c2, AO2_ITERATOR_DESCENDING,
test_initial, ARRAY_LEN(test_initial),
"Iteration (descending, insert begin)", test);
break;
case TEST_CONTAINER_HASH:
res = test_ao2_iteration(res, c1, 0,
test_hash_end_forward, ARRAY_LEN(test_hash_end_forward),
"Iteration (ascending, insert end)", test);
res = test_ao2_iteration(res, c1, AO2_ITERATOR_DESCENDING,
test_hash_end_backward, ARRAY_LEN(test_hash_end_backward),
"Iteration (descending, insert end)", test);
res = test_ao2_iteration(res, c2, 0,
test_hash_begin_forward, ARRAY_LEN(test_hash_begin_forward),
"Iteration (ascending, insert begin)", test);
res = test_ao2_iteration(res, c2, AO2_ITERATOR_DESCENDING,
test_hash_begin_backward, ARRAY_LEN(test_hash_begin_backward),
"Iteration (descending, insert begin)", test);
break;
case TEST_CONTAINER_RBTREE:
break;
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
}
/* Check container traversal directions */
switch (type) {
case TEST_CONTAINER_LIST:
res = test_ao2_callback_traversal(res, c1, OBJ_ORDER_ASCENDING, NULL, NULL,
test_initial, ARRAY_LEN(test_initial),
"Traversal (ascending, insert end)", test);
res = test_ao2_callback_traversal(res, c1, OBJ_ORDER_DESCENDING, NULL, NULL,
test_reverse, ARRAY_LEN(test_reverse),
"Traversal (descending, insert end)", test);
res = test_ao2_callback_traversal(res, c2, OBJ_ORDER_ASCENDING, NULL, NULL,
test_reverse, ARRAY_LEN(test_reverse),
"Traversal (ascending, insert begin)", test);
res = test_ao2_callback_traversal(res, c2, OBJ_ORDER_DESCENDING, NULL, NULL,
test_initial, ARRAY_LEN(test_initial),
"Traversal (descending, insert begin)", test);
break;
case TEST_CONTAINER_HASH:
res = test_ao2_callback_traversal(res, c1, OBJ_ORDER_ASCENDING, NULL, NULL,
test_hash_end_forward, ARRAY_LEN(test_hash_end_forward),
"Traversal (ascending, insert end)", test);
res = test_ao2_callback_traversal(res, c1, OBJ_ORDER_DESCENDING, NULL, NULL,
test_hash_end_backward, ARRAY_LEN(test_hash_end_backward),
"Traversal (descending, insert end)", test);
res = test_ao2_callback_traversal(res, c2, OBJ_ORDER_ASCENDING, NULL, NULL,
test_hash_begin_forward, ARRAY_LEN(test_hash_begin_forward),
"Traversal (ascending, insert begin)", test);
res = test_ao2_callback_traversal(res, c2, OBJ_ORDER_DESCENDING, NULL, NULL,
test_hash_begin_backward, ARRAY_LEN(test_hash_begin_backward),
"Traversal (descending, insert begin)", test);
break;
case TEST_CONTAINER_RBTREE:
break;
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
}
/* Check traversal with OBJ_PARTIAL_KEY search range. */
partial = 6;
partial_key_match_range = 1;
switch (type) {
case TEST_CONTAINER_LIST:
res = test_ao2_callback_traversal(res, c1, OBJ_PARTIAL_KEY | OBJ_ORDER_ASCENDING,
test_cmp_cb, &partial,
test_list_partial_forward, ARRAY_LEN(test_list_partial_forward),
"Traversal OBJ_PARTIAL_KEY (ascending)", test);
res = test_ao2_callback_traversal(res, c1, OBJ_PARTIAL_KEY | OBJ_ORDER_DESCENDING,
test_cmp_cb, &partial,
test_list_partial_backward, ARRAY_LEN(test_list_partial_backward),
"Traversal OBJ_PARTIAL_KEY (descending)", test);
break;
case TEST_CONTAINER_HASH:
res = test_ao2_callback_traversal(res, c1, OBJ_PARTIAL_KEY | OBJ_ORDER_ASCENDING,
test_cmp_cb, &partial,
test_hash_partial_forward, ARRAY_LEN(test_hash_partial_forward),
"Traversal OBJ_PARTIAL_KEY (ascending)", test);
res = test_ao2_callback_traversal(res, c1, OBJ_PARTIAL_KEY | OBJ_ORDER_DESCENDING,
test_cmp_cb, &partial,
test_hash_partial_backward, ARRAY_LEN(test_hash_partial_backward),
"Traversal OBJ_PARTIAL_KEY (descending)", test);
break;
case TEST_CONTAINER_RBTREE:
break;
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
}
test_cleanup:
/* destroy containers */
if (c1) {
ao2_t_ref(c1, -1, "bye c1");
}
if (c2) {
ao2_t_ref(c2, -1, "bye c2");
}
if (destructor_count > 0) {
ast_test_status_update(test,
"all destructors were not called, destructor count is %d\n",
destructor_count);
res = AST_TEST_FAIL;
} else if (destructor_count < 0) {
ast_test_status_update(test,
"Destructor was called too many times, destructor count is %d\n",
destructor_count);
res = AST_TEST_FAIL;
}
return res;
}
/*!
* \internal
* \brief Test sorted container traversal.
* \since 12.0.0
*
* \param res Passed in enum ast_test_result_state.
* \param tst_num Test number.
* \param type Container type to test.
* \param test Test output controller.
*
* \return enum ast_test_result_state
*/
static int test_traversal_sorted(int res, int tst_num, enum test_container_type type, struct ast_test *test)
{
struct ao2_container *c1;
struct ao2_container *c2 = NULL;
int partial;
int destructor_count = 0;
int duplicate_number = 100;
/*! Container object insertion vector. */
static const int test_initial[] = {
1, 0, 2, 6, 4, 7, 5, 3, 9, 8
};
/*! Container forward traversal/iteration. */
static const int test_forward[] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
};
/*! Container backward traversal/iteration. */
static const int test_backward[] = {
9, 8, 7, 6, 5, 4, 3, 2, 1, 0
};
static const int test_partial_forward[] = {
5, 6, 7
};
static const int test_partial_backward[] = {
7, 6, 5
};
/* The hash orders assume that there are 5 buckets. */
static const int test_hash_forward[] = {
0, 5, 1, 6, 2, 7, 3, 8, 4, 9
};
static const int test_hash_backward[] = {
9, 4, 8, 3, 7, 2, 6, 1, 5, 0
};
static const int test_hash_partial_forward[] = {
5, 6, 7
};
static const int test_hash_partial_backward[] = {
7, 6, 5
};
/* Duplicate identifier order */
static const int test_dup_allow_forward[] = {
0, 1, 2, 3, 2
};
static const int test_dup_allow_backward[] = {
2, 3, 2, 1, 0
};
static const int test_dup_reject[] = {
0
};
static const int test_dup_obj_reject_forward[] = {
0, 1, 2, 3
};
static const int test_dup_obj_reject_backward[] = {
3, 2, 1, 0
};
static const int test_dup_replace[] = {
2
};
ast_test_status_update(test, "Test %d, %s containers.\n",
tst_num, test_container2str(type));
/* Create container that inserts duplicate objects after matching objects. */
c1 = test_make_sorted(type, AO2_CONTAINER_ALLOC_OPT_DUPS_ALLOW);
if (!c1) {
ast_test_status_update(test, "Container c1 creation failed.\n");
res = AST_TEST_FAIL;
goto test_cleanup;
}
if (insert_test_vector(c1, &destructor_count, test_initial, ARRAY_LEN(test_initial), "c1(DUPS_ALLOW)", test)) {
res = AST_TEST_FAIL;
goto test_cleanup;
}
/* Create container that inserts duplicate objects before matching objects. */
c2 = test_make_sorted(type, AO2_CONTAINER_ALLOC_OPT_INSERT_BEGIN | AO2_CONTAINER_ALLOC_OPT_DUPS_ALLOW);
if (!c2) {
ast_test_status_update(test, "Container c2 creation failed.\n");
res = AST_TEST_FAIL;
goto test_cleanup;
}
if (insert_test_vector(c2, &destructor_count, test_initial, ARRAY_LEN(test_initial), "c2(DUPS_ALLOW)", test)) {
res = AST_TEST_FAIL;
goto test_cleanup;
}
#if defined(TEST_CONTAINER_DEBUG_DUMP)
ao2_container_dump(c1, 0, "c1(DUPS_ALLOW)", (void *) test, (ao2_prnt_fn *) ast_test_debug, test_prnt_obj);
ao2_container_stats(c1, 0, "c1(DUPS_ALLOW)", (void *) test, (ao2_prnt_fn *) ast_test_debug);
ao2_container_dump(c2, 0, "c2(DUPS_ALLOW)", (void *) test, (ao2_prnt_fn *) ast_test_debug, test_prnt_obj);
ao2_container_stats(c2, 0, "c2(DUPS_ALLOW)", (void *) test, (ao2_prnt_fn *) ast_test_debug);
#endif /* defined(TEST_CONTAINER_DEBUG_DUMP) */
/* Check container iteration directions */
switch (type) {
case TEST_CONTAINER_RBTREE:
case TEST_CONTAINER_LIST:
res = test_ao2_iteration(res, c1, 0,
test_forward, ARRAY_LEN(test_forward),
"Iteration (ascending)", test);
res = test_ao2_iteration(res, c1, AO2_ITERATOR_DESCENDING,
test_backward, ARRAY_LEN(test_backward),
"Iteration (descending)", test);
break;
case TEST_CONTAINER_HASH:
res = test_ao2_iteration(res, c1, 0,
test_hash_forward, ARRAY_LEN(test_hash_forward),
"Iteration (ascending)", test);
res = test_ao2_iteration(res, c1, AO2_ITERATOR_DESCENDING,
test_hash_backward, ARRAY_LEN(test_hash_backward),
"Iteration (descending)", test);
break;
}
/* Check container traversal directions */
switch (type) {
case TEST_CONTAINER_RBTREE:
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
case TEST_CONTAINER_LIST:
res = test_ao2_callback_traversal(res, c1, OBJ_ORDER_ASCENDING, NULL, NULL,
test_forward, ARRAY_LEN(test_forward),
"Traversal (ascending)", test);
res = test_ao2_callback_traversal(res, c1, OBJ_ORDER_DESCENDING, NULL, NULL,
test_backward, ARRAY_LEN(test_backward),
"Traversal (descending)", test);
break;
case TEST_CONTAINER_HASH:
res = test_ao2_callback_traversal(res, c1, OBJ_ORDER_ASCENDING, NULL, NULL,
test_hash_forward, ARRAY_LEN(test_hash_forward),
"Traversal (ascending, insert end)", test);
res = test_ao2_callback_traversal(res, c1, OBJ_ORDER_DESCENDING, NULL, NULL,
test_hash_backward, ARRAY_LEN(test_hash_backward),
"Traversal (descending)", test);
break;
}
/* Check traversal with OBJ_PARTIAL_KEY search range. */
partial = 6;
partial_key_match_range = 1;
switch (type) {
case TEST_CONTAINER_RBTREE:
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
case TEST_CONTAINER_LIST:
res = test_ao2_callback_traversal(res, c1, OBJ_PARTIAL_KEY | OBJ_ORDER_ASCENDING,
test_cmp_cb, &partial,
test_partial_forward, ARRAY_LEN(test_partial_forward),
"Traversal OBJ_PARTIAL_KEY (ascending)", test);
res = test_ao2_callback_traversal(res, c1, OBJ_PARTIAL_KEY | OBJ_ORDER_DESCENDING,
test_cmp_cb, &partial,
test_partial_backward, ARRAY_LEN(test_partial_backward),
"Traversal OBJ_PARTIAL_KEY (descending)", test);
break;
case TEST_CONTAINER_HASH:
res = test_ao2_callback_traversal(res, c1, OBJ_PARTIAL_KEY | OBJ_ORDER_ASCENDING,
test_cmp_cb, &partial,
test_hash_partial_forward, ARRAY_LEN(test_hash_partial_forward),
"Traversal OBJ_PARTIAL_KEY (ascending)", test);
res = test_ao2_callback_traversal(res, c1, OBJ_PARTIAL_KEY | OBJ_ORDER_DESCENDING,
test_cmp_cb, &partial,
test_hash_partial_backward, ARRAY_LEN(test_hash_partial_backward),
"Traversal OBJ_PARTIAL_KEY (descending)", test);
break;
}
/* Add duplicates to initial containers that allow duplicates */
if (insert_test_duplicates(c1, &destructor_count, duplicate_number, "c1(DUPS_ALLOW)", test)) {
res = AST_TEST_FAIL;
goto test_cleanup;
}
if (insert_test_duplicates(c2, &destructor_count, duplicate_number, "c2(DUPS_ALLOW)", test)) {
res = AST_TEST_FAIL;
goto test_cleanup;
}
#if defined(TEST_CONTAINER_DEBUG_DUMP)
ao2_container_dump(c1, 0, "c1(DUPS_ALLOW) w/ dups", (void *) test, (ao2_prnt_fn *) ast_test_debug, test_prnt_obj);
ao2_container_stats(c1, 0, "c1(DUPS_ALLOW) w/ dups", (void *) test, (ao2_prnt_fn *) ast_test_debug);
ao2_container_dump(c2, 0, "c2(DUPS_ALLOW) w/ dups", (void *) test, (ao2_prnt_fn *) ast_test_debug, test_prnt_obj);
ao2_container_stats(c2, 0, "c2(DUPS_ALLOW) w/ dups", (void *) test, (ao2_prnt_fn *) ast_test_debug);
#endif /* defined(TEST_CONTAINER_DEBUG_DUMP) */
/* Check duplicates in containers that allow duplicates. */
res = test_expected_duplicates(res, c1, OBJ_ORDER_ASCENDING, duplicate_number,
test_dup_allow_forward, ARRAY_LEN(test_dup_allow_forward),
"Duplicates (ascending, DUPS_ALLOW)", test);
res = test_expected_duplicates(res, c1, OBJ_ORDER_DESCENDING, duplicate_number,
test_dup_allow_backward, ARRAY_LEN(test_dup_allow_backward),
"Duplicates (descending, DUPS_ALLOW)", test);
ao2_t_ref(c1, -1, "bye c1");
c1 = NULL;
ao2_t_ref(c2, -1, "bye c2");
c2 = NULL;
/* Create containers that reject duplicate keyed objects. */
c1 = test_make_sorted(type, AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT);
if (!c1) {
ast_test_status_update(test, "Container c1 creation failed.\n");
res = AST_TEST_FAIL;
goto test_cleanup;
}
if (insert_test_vector(c1, &destructor_count, test_initial, ARRAY_LEN(test_initial), "c1(DUPS_REJECT)", test)) {
res = AST_TEST_FAIL;
goto test_cleanup;
}
if (insert_test_duplicates(c1, &destructor_count, duplicate_number, "c1(DUPS_REJECT)", test)) {
res = AST_TEST_FAIL;
goto test_cleanup;
}
c2 = test_make_sorted(type, AO2_CONTAINER_ALLOC_OPT_INSERT_BEGIN | AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT);
if (!c2) {
ast_test_status_update(test, "Container c2 creation failed.\n");
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
res = AST_TEST_FAIL;
goto test_cleanup;
}
if (insert_test_vector(c2, &destructor_count, test_initial, ARRAY_LEN(test_initial), "c2(DUPS_REJECT)", test)) {
res = AST_TEST_FAIL;
goto test_cleanup;
}
if (insert_test_duplicates(c2, &destructor_count, duplicate_number, "c2(DUPS_REJECT)", test)) {
res = AST_TEST_FAIL;
goto test_cleanup;
}
/* Check duplicates in containers that reject duplicate keyed objects. */
res = test_expected_duplicates(res, c1, OBJ_ORDER_ASCENDING, duplicate_number,
test_dup_reject, ARRAY_LEN(test_dup_reject),
"Duplicates (ascending, DUPS_REJECT)", test);
res = test_expected_duplicates(res, c1, OBJ_ORDER_DESCENDING, duplicate_number,
test_dup_reject, ARRAY_LEN(test_dup_reject),
"Duplicates (descending, DUPS_REJECT)", test);
ao2_t_ref(c1, -1, "bye c1");
c1 = NULL;
ao2_t_ref(c2, -1, "bye c2");
c2 = NULL;
/* Create containers that reject duplicate objects. */
c1 = test_make_sorted(type, AO2_CONTAINER_ALLOC_OPT_DUPS_OBJ_REJECT);
if (!c1) {
ast_test_status_update(test, "Container c1 creation failed.\n");
res = AST_TEST_FAIL;
goto test_cleanup;
}
if (insert_test_vector(c1, &destructor_count, test_initial, ARRAY_LEN(test_initial), "c1(DUPS_OBJ_REJECT)", test)) {
res = AST_TEST_FAIL;
goto test_cleanup;
}
if (insert_test_duplicates(c1, &destructor_count, duplicate_number, "c1(DUPS_OBJ_REJECT)", test)) {
res = AST_TEST_FAIL;
goto test_cleanup;
}
c2 = test_make_sorted(type, AO2_CONTAINER_ALLOC_OPT_INSERT_BEGIN | AO2_CONTAINER_ALLOC_OPT_DUPS_OBJ_REJECT);
if (!c2) {
ast_test_status_update(test, "Container c2 creation failed.\n");
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
res = AST_TEST_FAIL;
goto test_cleanup;
}
if (insert_test_vector(c2, &destructor_count, test_initial, ARRAY_LEN(test_initial), "c2(DUPS_OBJ_REJECT)", test)) {
res = AST_TEST_FAIL;
goto test_cleanup;
}
if (insert_test_duplicates(c2, &destructor_count, duplicate_number, "c2(DUPS_OBJ_REJECT)", test)) {
res = AST_TEST_FAIL;
goto test_cleanup;
}
/* Check duplicates in containers that reject duplicate objects. */
res = test_expected_duplicates(res, c1, OBJ_ORDER_ASCENDING, duplicate_number,
test_dup_obj_reject_forward, ARRAY_LEN(test_dup_obj_reject_forward),
"Duplicates (ascending, DUPS_OBJ_REJECT)", test);
res = test_expected_duplicates(res, c1, OBJ_ORDER_DESCENDING, duplicate_number,
test_dup_obj_reject_backward, ARRAY_LEN(test_dup_obj_reject_backward),
"Duplicates (descending, DUPS_OBJ_REJECT)", test);
ao2_t_ref(c1, -1, "bye c1");
c1 = NULL;
ao2_t_ref(c2, -1, "bye c2");
c2 = NULL;
/* Create container that replaces duplicate keyed objects. */
c1 = test_make_sorted(type, AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE);
if (!c1) {
ast_test_status_update(test, "Container c1 creation failed.\n");
res = AST_TEST_FAIL;
goto test_cleanup;
}
if (insert_test_vector(c1, &destructor_count, test_initial, ARRAY_LEN(test_initial), "c1(DUPS_REJECT)", test)) {
res = AST_TEST_FAIL;
goto test_cleanup;
}
if (insert_test_duplicates(c1, &destructor_count, duplicate_number, "c1(DUPS_REJECT)", test)) {
res = AST_TEST_FAIL;
goto test_cleanup;
}
c2 = test_make_sorted(type, AO2_CONTAINER_ALLOC_OPT_INSERT_BEGIN | AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE);
if (!c2) {
ast_test_status_update(test, "Container c2 creation failed.\n");
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
res = AST_TEST_FAIL;
goto test_cleanup;
}
if (insert_test_vector(c2, &destructor_count, test_initial, ARRAY_LEN(test_initial), "c2(DUPS_REPLACE)", test)) {
res = AST_TEST_FAIL;
goto test_cleanup;
}
if (insert_test_duplicates(c2, &destructor_count, duplicate_number, "c2(DUPS_REPLACE)", test)) {
res = AST_TEST_FAIL;
goto test_cleanup;
}
/* Check duplicates in containers that replaces duplicate keyed objects. */
res = test_expected_duplicates(res, c1, OBJ_ORDER_ASCENDING, duplicate_number,
test_dup_replace, ARRAY_LEN(test_dup_replace),
"Duplicates (ascending, DUPS_REPLACE)", test);
res = test_expected_duplicates(res, c1, OBJ_ORDER_DESCENDING, duplicate_number,
test_dup_replace, ARRAY_LEN(test_dup_replace),
"Duplicates (descending, DUPS_REPLACE)", test);
test_cleanup:
/* destroy containers */
if (c1) {
ao2_t_ref(c1, -1, "bye c1");
}
if (c2) {
ao2_t_ref(c2, -1, "bye c2");
}
if (destructor_count > 0) {
ast_test_status_update(test,
"all destructors were not called, destructor count is %d\n",
destructor_count);
res = AST_TEST_FAIL;
} else if (destructor_count < 0) {
ast_test_status_update(test,
"Destructor was called too many times, destructor count is %d\n",
destructor_count);
res = AST_TEST_FAIL;
}
return res;
}
AST_TEST_DEFINE(astobj2_test_4)
{
int res = AST_TEST_PASS;
switch (cmd) {
case TEST_INIT:
info->name = "astobj2_test4";
info->category = "/main/astobj2/";
info->summary = "Test container traversal/iteration";
info->description =
"This test is to see if the container traversal/iteration works "
"as intended for each supported container type.";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
res = test_traversal_nonsorted(res, 1, TEST_CONTAINER_LIST, test);
res = test_traversal_nonsorted(res, 2, TEST_CONTAINER_HASH, test);
res = test_traversal_sorted(res, 3, TEST_CONTAINER_LIST, test);
res = test_traversal_sorted(res, 4, TEST_CONTAINER_HASH, test);
res = test_traversal_sorted(res, 5, TEST_CONTAINER_RBTREE, test);
return res;
}
static enum ast_test_result_state test_performance(struct ast_test *test,
enum test_container_type type, unsigned int copt)
{
George Joseph
committed
/*!
* \brief The number of objects inserted and searched for in the container under test.
*/
#define OBJS 73
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
int res = AST_TEST_PASS;
struct ao2_container *c1 = NULL;
struct test_obj *tobj[OBJS];
struct test_obj *tobj2;
int i;
switch (type) {
case TEST_CONTAINER_HASH:
c1 = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, copt, 17,
test_hash_cb, test_sort_cb, test_cmp_cb);
break;
case TEST_CONTAINER_LIST:
c1 = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, copt,
test_sort_cb, test_cmp_cb);
break;
case TEST_CONTAINER_RBTREE:
c1 = ao2_container_alloc_rbtree(AO2_ALLOC_OPT_LOCK_MUTEX, copt,
test_sort_cb, test_cmp_cb);
break;
}
for (i = 0; i < OBJS; i++) {
tobj[i] = NULL;
}
if (!c1) {
ast_test_status_update(test, "Container c1 creation failed.\n");
res = AST_TEST_FAIL;
goto test_cleanup;
}
for (i = 0; i < OBJS; i++) {
tobj[i] = ao2_alloc(sizeof(struct test_obj), test_obj_destructor);
if (!tobj[i]) {
ast_test_status_update(test, "test object creation failed.\n");
res = AST_TEST_FAIL;
goto test_cleanup;
}
tobj[i]->i = i;
ao2_link(c1, tobj[i]);
}
for (i = 0; i < OBJS; i++) {
if ((!(tobj2 = ao2_find(c1, &i, OBJ_KEY)))) {
ast_test_status_update(test, "Should have found object %d in container.\n", i);
res = AST_TEST_FAIL;
goto test_cleanup;
}
ao2_ref(tobj2, -1);
tobj2 = NULL;
}
test_cleanup:
for (i = 0; i < OBJS ; i++) {
ao2_cleanup(tobj[i]);
}
ao2_cleanup(c1);
return res;
}
static enum ast_test_result_state testloop(struct ast_test *test,
George Joseph
committed
enum test_container_type type, int copt, int iterations)
{
int res = AST_TEST_PASS;
int i;
struct timeval start;
start = ast_tvnow();
George Joseph
committed
for (i = 1 ; i <= iterations && res == AST_TEST_PASS ; i++) {