[Asterisk-code-review] vector: Additional enhancements and fixes (asterisk[master])
George Joseph
asteriskteam at digium.com
Tue May 5 20:34:06 CDT 2015
George Joseph has uploaded a new change for review.
https://gerrit.asterisk.org/375
Change subject: vector: Additional enhancements and fixes
......................................................................
vector: Additional enhancements and fixes
After using the new vector stuff for real I found...
A bug in AST_VECTOR_INSERT_AT that could cause a seg fault.
The callbacks needed to be closer to ao2_callback in behavior
WRT to CMP_MATCH and CMP_STOP behavior and the ability to return
a vector of matched entries.
I also added a new macro to test.h that acts like ast_test_validate
but also accepts a return code variable and a cleanup label. As well
as printing the error, it sets the rc variable to AST_TEST_FAIL and
does a goto to the specified label on error. I had a local version
of this in test_vector so I just moved it.
ASTERISK-25045
Change-Id: I05e5e47fd02f61964be13b7e8942bab5d61b29cc
---
M include/asterisk/test.h
M include/asterisk/vector.h
M tests/test_vector.c
3 files changed, 335 insertions(+), 162 deletions(-)
git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/75/375/1
diff --git a/include/asterisk/test.h b/include/asterisk/test.h
index 2ea8332..69f8837 100644
--- a/include/asterisk/test.h
+++ b/include/asterisk/test.h
@@ -389,6 +389,28 @@
} \
} while(0)
+/*!
+ * \brief Check a test condition, report error and goto cleanup label if failed.
+ *
+ * \since 13.4.0
+ *
+ * This macro evaluates \a condition. If the condition evaluates to true (non-zero),
+ * nothing happens. If it evaluates to false (zero), then the failure is printed
+ * using \ref ast_test_status_update, the variable \a rc_variable is set to AST_TEST_FAIL,
+ * and a goto to \a cleanup_label is executed.
+ *
+ * \param test Currently executing test
+ * \param condition Boolean condition to check.
+ * \param rc_variable Variable to receive AST_TEST_FAIL.
+ * \param cleanup_label The label to go to on failure.
+ */
+#define ast_test_validate_cleanup(test, condition, rc_variable, cleanup_label) ({ \
+ if (!(condition)) { \
+ ast_test_status_update((test), "%s: %s\n", "Condition failed", #condition); \
+ rc_variable = AST_TEST_FAIL; \
+ goto cleanup_label; \
+ } \
+})
#endif /* TEST_FRAMEWORK */
#endif /* _AST_TEST_H */
diff --git a/include/asterisk/vector.h b/include/asterisk/vector.h
index 23db8a5..1328afb 100644
--- a/include/asterisk/vector.h
+++ b/include/asterisk/vector.h
@@ -123,6 +123,22 @@
} while (0)
/*!
+ * \brief Deallocates this vector pointer.
+ *
+ * If any code to free the elements of this vector need to be run, that should
+ * be done prior to this call.
+ *
+ * \param vec Pointer to a malloc'd vector structure.
+ */
+#define AST_VECTOR_PTR_FREE(vec) do { \
+ ast_free((vec)->elems); \
+ (vec)->elems = NULL; \
+ (vec)->max = 0; \
+ (vec)->current = 0; \
+ ast_free(vec); \
+} while (0)
+
+/*!
* \brief Deallocates this locked vector
*
* If any code to free the elements of this vector need to be run, that should
@@ -133,6 +149,19 @@
#define AST_VECTOR_RW_FREE(vec) do { \
AST_VECTOR_FREE(vec); \
ast_rwlock_destroy(&(vec)->lock); \
+} while(0)
+
+/*!
+ * \brief Deallocates this locked vector pointer.
+ *
+ * If any code to free the elements of this vector need to be run, that should
+ * be done prior to this call.
+ *
+ * \param vec Pointer to a malloc'd vector structure.
+ */
+#define AST_VECTOR_RW_PTR_FREE(vec) do { \
+ ast_rwlock_destroy(&(vec)->lock); \
+ AST_VECTOR_PTR_FREE(vec); \
} while(0)
/*!
@@ -238,8 +267,10 @@
break; \
} \
} \
- __move = ((vec)->current - 1) * sizeof(typeof((vec)->elems[0])); \
- memmove(&(vec)->elems[(idx) + 1], &(vec)->elems[(idx)], __move); \
+ if ((vec)->current > 0) { \
+ __move = ((vec)->current - 1) * sizeof(typeof((vec)->elems[0])); \
+ memmove(&(vec)->elems[(idx) + 1], &(vec)->elems[(idx)], __move); \
+ } \
(vec)->elems[(idx)] = (elem); \
(vec)->current++; \
} while (0); \
@@ -441,25 +472,118 @@
})
/*!
- * \brief Execute a callback on every element in a vector
+ * \brief Default callback for AST_VECTOR_CALLBACK()
+ *
+ * \param elem Element to compare against
+ * \param value Value to compare with the vector element.
+ *
+ * \return CMP_MATCH always.
+ */
+#define AST_VECTOR_MATCH_ALL(element) (CMP_MATCH)
+
+
+/*!
+ * \brief Execute a callback on every element in a vector returning the last matched
*
* \param vec Vector to operate on.
* \param callback A callback that takes at least 1 argument (the element)
* plus number of optional arguments
*
- * \return the number of elements visited before the end of the vector
- * was reached or CMP_STOP was returned.
+ * \return the last element matched before CMP_STOP was returned
+ * or the end of the vector was reached
*/
#define AST_VECTOR_CALLBACK(vec, callback, ...) ({ \
size_t idx; \
+ typeof((vec)->elems[0]) res = NULL; \
for (idx = 0; idx < (vec)->current; idx++) { \
int rc = callback((vec)->elems[idx], ##__VA_ARGS__); \
- if (rc == CMP_STOP) { \
- idx++; \
+ if (rc & CMP_MATCH) { \
+ res = (vec)->elems[idx]; \
+ }\
+ if (rc & CMP_STOP) { \
break; \
}\
} \
- idx; \
+ res; \
+})
+
+/*!
+ * \brief Execute a callback on every element in a vector returning the matching
+ * elements in a new vector
+ *
+ * \param vec Vector to operate on.
+ * \param callback A callback that takes at least 1 argument (the element)
+ * plus number of optional arguments
+ *
+ * \return a vector containing the elements matched before CMP_STOP was returned
+ * or the end of the vector was reached. The vector may be empty and could be NULL
+ * if there was not enough memory to allocate it's control structure.
+ *
+ * \warning The returned vector must have AST_VECTOR_PTR_FREE()
+ * called on it after you've finished with it.
+ *
+ * \note The type of the returned vector must be traceable to the original vector.
+ *
+ * The following will resut in "error: assignment from incompatible pointer type"
+ * because these declare 2 different structures.
+ *
+ * AST_VECTOR(, char *) vector_1;
+ * AST_VECTOR(, char *) *vector_2;
+ *
+ * vector_2 = AST_VECTOR_CALLBACK_MULTIPLE(&vector_1, callback);
+ *
+ * Either of the following will work because you're using the type of the first
+ * to declare the second:
+ *
+ * AST_VECTOR(mytype, char *) vector_1;
+ * struct mytype *vector_2 = NULL;
+ *
+ * vector_2 = AST_VECTOR_CALLBACK_MULTIPLE(&vector_1, callback);
+ *
+ * AST_VECTOR(, char *) vector_1;
+ * typeof(vector_1) *vector_2 = NULL;
+ *
+ * vector_2 = AST_VECTOR_CALLBACK_MULTIPLE(&vector_1, callback);
+ *
+ */
+#define AST_VECTOR_CALLBACK_MULTIPLE(vec, callback, ...) ({ \
+ size_t idx; \
+ typeof((vec)) new_vec; \
+ do { \
+ new_vec = ast_malloc(sizeof(*new_vec)); \
+ if (!new_vec) { \
+ break; \
+ } \
+ if (AST_VECTOR_INIT(new_vec, AST_VECTOR_SIZE((vec))) != 0) { \
+ ast_free(new_vec); \
+ new_vec = NULL; \
+ break; \
+ } \
+ for (idx = 0; idx < (vec)->current; idx++) { \
+ int rc = callback((vec)->elems[idx], ##__VA_ARGS__); \
+ if (rc & CMP_MATCH) { \
+ AST_VECTOR_APPEND(new_vec, (vec)->elems[idx]); \
+ } \
+ if (rc & CMP_STOP) { \
+ break; \
+ }\
+ } \
+ } while(0); \
+ new_vec; \
+})
+
+/*!
+ * \brief Execute a callback on every element in a vector disregarding callback return
+ *
+ * \param vec Vector to operate on.
+ * \param callback A callback that takes at least 1 argument (the element)
+ * plus number of optional arguments
+ */
+#define AST_VECTOR_CALLBACK_VOID(vec, callback, ...) ({ \
+ size_t idx; \
+ for (idx = 0; idx < (vec)->current; idx++) { \
+ callback((vec)->elems[idx], ##__VA_ARGS__); \
+ } \
})
/*!
diff --git a/tests/test_vector.c b/tests/test_vector.c
index eae1881..92f40c2 100644
--- a/tests/test_vector.c
+++ b/tests/test_vector.c
@@ -42,14 +42,6 @@
#include "asterisk/module.h"
#include "asterisk/vector.h"
-#define test_validate_cleanup(condition) ({ \
- if (!(condition)) { \
- ast_test_status_update((test), "%s: %s\n", "Condition failed", #condition); \
- rc = AST_TEST_FAIL; \
- goto cleanup; \
- } \
-})
-
static int cleanup_count;
static void cleanup(char *element)
@@ -79,28 +71,28 @@
}
ast_test_validate(test, AST_VECTOR_INIT(&sv1, 3) == 0);
- test_validate_cleanup(sv1.max == 3);
- test_validate_cleanup(AST_VECTOR_SIZE(&sv1) == 0);
+ ast_test_validate_cleanup(test, sv1.max == 3, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_SIZE(&sv1) == 0, rc, cleanup);
- test_validate_cleanup(AST_VECTOR_APPEND(&sv1, AAA) == 0);
- test_validate_cleanup(AST_VECTOR_APPEND(&sv1, BBB) == 0);
- test_validate_cleanup(AST_VECTOR_APPEND(&sv1, CCC) == 0);
- test_validate_cleanup(AST_VECTOR_SIZE(&sv1) == 3);
- test_validate_cleanup(sv1.max == 3);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 0) == AAA);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 1) == BBB);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 2) == CCC);
+ ast_test_validate_cleanup(test, AST_VECTOR_APPEND(&sv1, AAA) == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_APPEND(&sv1, BBB) == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_APPEND(&sv1, CCC) == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_SIZE(&sv1) == 3, rc, cleanup);
+ ast_test_validate_cleanup(test, sv1.max == 3, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 0) == AAA, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 1) == BBB, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 2) == CCC, rc, cleanup);
- test_validate_cleanup(AST_VECTOR_INSERT_AT(&sv1, 1, ZZZ) == 0);
- test_validate_cleanup(sv1.max >= 4);
- test_validate_cleanup(AST_VECTOR_SIZE(&sv1) == 4);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 0) == AAA);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 1) == ZZZ);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 2) == BBB);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 3) == CCC);
+ ast_test_validate_cleanup(test, AST_VECTOR_INSERT_AT(&sv1, 1, ZZZ) == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, sv1.max >= 4, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_SIZE(&sv1) == 4, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 0) == AAA, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 1) == ZZZ, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 2) == BBB, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 3) == CCC, rc, cleanup);
- test_validate_cleanup(*(char **)AST_VECTOR_GET_CMP(&sv1, "AAA", 0 == strcmp) == AAA);
- test_validate_cleanup(*(char **)AST_VECTOR_GET_CMP(&sv1, "ZZZ", 0 == strcmp) == ZZZ);
+ ast_test_validate_cleanup(test, *(char **)AST_VECTOR_GET_CMP(&sv1, "AAA", 0 == strcmp) == AAA, rc, cleanup);
+ ast_test_validate_cleanup(test, *(char **)AST_VECTOR_GET_CMP(&sv1, "ZZZ", 0 == strcmp) == ZZZ, rc, cleanup);
AST_VECTOR_FREE(&sv1);
ast_test_validate(test, sv1.elems == NULL);
@@ -108,76 +100,95 @@
ast_test_validate(test, sv1.max == 0);
ast_test_validate(test, AST_VECTOR_INIT(&sv1, 0) == 0);
- test_validate_cleanup(sv1.max == 0);
- test_validate_cleanup(AST_VECTOR_SIZE(&sv1) == 0);
+ ast_test_validate_cleanup(test, sv1.max == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_SIZE(&sv1) == 0, rc, cleanup);
- test_validate_cleanup(AST_VECTOR_APPEND(&sv1, AAA) == 0);
- test_validate_cleanup(AST_VECTOR_APPEND(&sv1, BBB) == 0);
- test_validate_cleanup(AST_VECTOR_APPEND(&sv1, CCC) == 0);
- test_validate_cleanup(sv1.max >= 3);
- test_validate_cleanup(AST_VECTOR_SIZE(&sv1) == 3);
+ ast_test_validate_cleanup(test, AST_VECTOR_APPEND(&sv1, AAA) == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_APPEND(&sv1, BBB) == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_APPEND(&sv1, CCC) == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, sv1.max >= 3, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_SIZE(&sv1) == 3, rc, cleanup);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 0) == AAA);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 1) == BBB);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 2) == CCC);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 0) == AAA, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 1) == BBB, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 2) == CCC, rc, cleanup);
/* Overwrite index 1 */
- test_validate_cleanup(AST_VECTOR_REPLACE(&sv1, 1, ZZZ) == 0);
- test_validate_cleanup(AST_VECTOR_SIZE(&sv1) == 3);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 0) == AAA);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 1) == ZZZ);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 2) == CCC);
+ ast_test_validate_cleanup(test, AST_VECTOR_REPLACE(&sv1, 1, ZZZ) == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_SIZE(&sv1) == 3, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 0) == AAA, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 1) == ZZZ, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 2) == CCC, rc, cleanup);
/* Remove index 0 and bring the last entry into it's empty slot */
- test_validate_cleanup(AST_VECTOR_REMOVE_UNORDERED(&sv1, 0) == AAA);
- test_validate_cleanup(AST_VECTOR_SIZE(&sv1) == 2);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 0) == CCC);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 1) == ZZZ);
+ ast_test_validate_cleanup(test, AST_VECTOR_REMOVE_UNORDERED(&sv1, 0) == AAA, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_SIZE(&sv1) == 2, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 0) == CCC, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 1) == ZZZ, rc, cleanup);
/* Replace 0 and 2 leaving 1 alone */
- test_validate_cleanup(AST_VECTOR_REPLACE(&sv1, 0, AAA) == 0);
- test_validate_cleanup(AST_VECTOR_REPLACE(&sv1, 2, CCC) == 0);
- test_validate_cleanup(AST_VECTOR_SIZE(&sv1) == 3);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 0) == AAA);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 1) == ZZZ);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 2) == CCC);
+ ast_test_validate_cleanup(test, AST_VECTOR_REPLACE(&sv1, 0, AAA) == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_REPLACE(&sv1, 2, CCC) == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_SIZE(&sv1) == 3, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 0) == AAA, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 1) == ZZZ, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 2) == CCC, rc, cleanup);
/* Remove 1 and compact preserving order */
- test_validate_cleanup(AST_VECTOR_REMOVE_ORDERED(&sv1, 1) == ZZZ);
- test_validate_cleanup(AST_VECTOR_SIZE(&sv1) == 2);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 0) == AAA);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 1) == CCC);
+ ast_test_validate_cleanup(test, AST_VECTOR_REMOVE_ORDERED(&sv1, 1) == ZZZ, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_SIZE(&sv1) == 2, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 0) == AAA, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 1) == CCC, rc, cleanup);
/* Equivalent of APPEND */
- test_validate_cleanup(AST_VECTOR_REPLACE(&sv1, 2, ZZZ) == 0);
- test_validate_cleanup(AST_VECTOR_SIZE(&sv1) == 3);
+ ast_test_validate_cleanup(test, AST_VECTOR_REPLACE(&sv1, 2, ZZZ) == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_SIZE(&sv1) == 3, rc, cleanup);
/* This should fail because comparison is by pointer */
- test_validate_cleanup(AST_VECTOR_REMOVE_ELEM_ORDERED(&sv1, "ZZZ", cleanup) != 0);
+ ast_test_validate_cleanup(test, AST_VECTOR_REMOVE_ELEM_ORDERED(&sv1, "ZZZ", cleanup) != 0, rc, cleanup);
/* This should work because we passing in the specific object to be removed */
cleanup_count = 0;
- test_validate_cleanup(AST_VECTOR_REMOVE_ELEM_ORDERED(&sv1, ZZZ, cleanup) == 0);
- test_validate_cleanup(AST_VECTOR_SIZE(&sv1) == 2);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 0) == AAA);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 1) == CCC);
- test_validate_cleanup(cleanup_count == 1);
+ ast_test_validate_cleanup(test, AST_VECTOR_REMOVE_ELEM_ORDERED(&sv1, ZZZ, cleanup) == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_SIZE(&sv1) == 2, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 0) == AAA, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 1) == CCC, rc, cleanup);
+ ast_test_validate_cleanup(test, cleanup_count == 1, rc, cleanup);
/* If we want a comparison by value, we need to pass in a comparison
* function. The comparison looks weird but that's what it takes.
*/
cleanup_count = 0;
- test_validate_cleanup(AST_VECTOR_REMOVE_CMP_ORDERED(&sv1, "AAA", 0 == strcmp, cleanup) == 0);
- test_validate_cleanup(AST_VECTOR_SIZE(&sv1) == 1);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 0) == CCC);
- test_validate_cleanup(cleanup_count == 1);
+ ast_test_validate_cleanup(test, AST_VECTOR_REMOVE_CMP_ORDERED(&sv1, "AAA", 0 == strcmp, cleanup) == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_SIZE(&sv1) == 1, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 0) == CCC, rc, cleanup);
+ ast_test_validate_cleanup(test, cleanup_count == 1, rc, cleanup);
/* This element is gone so we shouldn't be able to find it or delete it again. */
- test_validate_cleanup(AST_VECTOR_GET_CMP(&sv1, "AAA", 0 == strcmp) == NULL);
- test_validate_cleanup(AST_VECTOR_REMOVE_CMP_ORDERED(&sv1, "AAA", 0 == strcmp, cleanup) != 0);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET_CMP(&sv1, "AAA", 0 == strcmp) == NULL, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_REMOVE_CMP_ORDERED(&sv1, "AAA", 0 == strcmp, cleanup) != 0, rc, cleanup);
/* CCC should still be there though */
- test_validate_cleanup(*(char **)AST_VECTOR_GET_CMP(&sv1, "CCC", 0 == strcmp) == CCC);
+ ast_test_validate_cleanup(test, *(char **)AST_VECTOR_GET_CMP(&sv1, "CCC", 0 == strcmp) == CCC, rc, cleanup);
+
+ /* Make sure we can insert at beginning and end and that growing works */
+ ast_test_validate_cleanup(test, AST_VECTOR_INSERT_AT(&sv1, 0, ZZZ) == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 0) == ZZZ, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 1) == CCC, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_INSERT_AT(&sv1, AST_VECTOR_SIZE(&sv1), BBB) == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 2) == BBB, rc, cleanup);
+
+ AST_VECTOR_FREE(&sv1);
+ ast_test_validate(test, AST_VECTOR_INIT(&sv1, 0) == 0);
+ ast_test_validate_cleanup(test, AST_VECTOR_INSERT_AT(&sv1, 0, ZZZ) == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 0) == ZZZ, rc, cleanup);
+
+ AST_VECTOR_FREE(&sv1);
+ ast_test_validate(test, AST_VECTOR_INIT(&sv1, 0) == 0);
+ ast_test_validate_cleanup(test, AST_VECTOR_INSERT_AT(&sv1, AST_VECTOR_SIZE(&sv1), ZZZ) == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 0) == ZZZ, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_INSERT_AT(&sv1, AST_VECTOR_SIZE(&sv1), CCC) == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 1) == CCC, rc, cleanup);
cleanup:
AST_VECTOR_FREE(&sv1);
@@ -211,28 +222,28 @@
}
ast_test_validate(test, AST_VECTOR_INIT(&sv1, 3) == 0);
- test_validate_cleanup(sv1.max == 3);
- test_validate_cleanup(AST_VECTOR_SIZE(&sv1) == 0);
+ ast_test_validate_cleanup(test, sv1.max == 3, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_SIZE(&sv1) == 0, rc, cleanup);
- test_validate_cleanup(AST_VECTOR_APPEND(&sv1, AAA) == 0);
- test_validate_cleanup(AST_VECTOR_APPEND(&sv1, BBB) == 0);
- test_validate_cleanup(AST_VECTOR_APPEND(&sv1, CCC) == 0);
- test_validate_cleanup(AST_VECTOR_SIZE(&sv1) == 3);
- test_validate_cleanup(sv1.max == 3);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 0) == AAA);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 1) == BBB);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 2) == CCC);
+ ast_test_validate_cleanup(test, AST_VECTOR_APPEND(&sv1, AAA) == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_APPEND(&sv1, BBB) == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_APPEND(&sv1, CCC) == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_SIZE(&sv1) == 3, rc, cleanup);
+ ast_test_validate_cleanup(test, sv1.max == 3, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 0) == AAA, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 1) == BBB, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 2) == CCC, rc, cleanup);
- test_validate_cleanup(AST_VECTOR_INSERT_AT(&sv1, 1, ZZZ) == 0);
- test_validate_cleanup(sv1.max >= 4);
- test_validate_cleanup(AST_VECTOR_SIZE(&sv1) == 4);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 0) == AAA);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 1) == ZZZ);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 2) == BBB);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 3) == CCC);
+ ast_test_validate_cleanup(test, AST_VECTOR_INSERT_AT(&sv1, 1, ZZZ) == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, sv1.max >= 4, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_SIZE(&sv1) == 4, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 0) == AAA, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 1) == ZZZ, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 2) == BBB, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 3) == CCC, rc, cleanup);
- test_validate_cleanup(*(int *)AST_VECTOR_GET_CMP(&sv1, AAA, AST_VECTOR_ELEM_DEFAULT_CMP) == AAA);
- test_validate_cleanup(*(int *)AST_VECTOR_GET_CMP(&sv1, ZZZ, AST_VECTOR_ELEM_DEFAULT_CMP) == ZZZ);
+ ast_test_validate_cleanup(test, *(int *)AST_VECTOR_GET_CMP(&sv1, AAA, AST_VECTOR_ELEM_DEFAULT_CMP) == AAA, rc, cleanup);
+ ast_test_validate_cleanup(test, *(int *)AST_VECTOR_GET_CMP(&sv1, ZZZ, AST_VECTOR_ELEM_DEFAULT_CMP) == ZZZ, rc, cleanup);
AST_VECTOR_FREE(&sv1);
ast_test_validate(test, sv1.elems == NULL);
@@ -240,73 +251,73 @@
ast_test_validate(test, sv1.max == 0);
ast_test_validate(test, AST_VECTOR_INIT(&sv1, 0) == 0);
- test_validate_cleanup(sv1.max == 0);
- test_validate_cleanup(AST_VECTOR_SIZE(&sv1) == 0);
+ ast_test_validate_cleanup(test, sv1.max == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_SIZE(&sv1) == 0, rc, cleanup);
- test_validate_cleanup(AST_VECTOR_APPEND(&sv1, AAA) == 0);
- test_validate_cleanup(AST_VECTOR_APPEND(&sv1, BBB) == 0);
- test_validate_cleanup(AST_VECTOR_APPEND(&sv1, CCC) == 0);
- test_validate_cleanup(sv1.max >= 3);
- test_validate_cleanup(AST_VECTOR_SIZE(&sv1) == 3);
+ ast_test_validate_cleanup(test, AST_VECTOR_APPEND(&sv1, AAA) == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_APPEND(&sv1, BBB) == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_APPEND(&sv1, CCC) == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, sv1.max >= 3, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_SIZE(&sv1) == 3, rc, cleanup);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 0) == AAA);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 1) == BBB);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 2) == CCC);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 0) == AAA, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 1) == BBB, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 2) == CCC, rc, cleanup);
/* Overwrite index 1 */
- test_validate_cleanup(AST_VECTOR_REPLACE(&sv1, 1, ZZZ) == 0);
- test_validate_cleanup(AST_VECTOR_SIZE(&sv1) == 3);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 0) == AAA);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 1) == ZZZ);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 2) == CCC);
+ ast_test_validate_cleanup(test, AST_VECTOR_REPLACE(&sv1, 1, ZZZ) == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_SIZE(&sv1) == 3, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 0) == AAA, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 1) == ZZZ, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 2) == CCC, rc, cleanup);
/* Remove index 0 and bring the last entry into it's empty slot */
- test_validate_cleanup(AST_VECTOR_REMOVE_UNORDERED(&sv1, 0) == 1);
- test_validate_cleanup(AST_VECTOR_SIZE(&sv1) == 2);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 0) == CCC);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 1) == ZZZ);
+ ast_test_validate_cleanup(test, AST_VECTOR_REMOVE_UNORDERED(&sv1, 0) == 1, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_SIZE(&sv1) == 2, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 0) == CCC, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 1) == ZZZ, rc, cleanup);
/* Replace 0 and 2 leaving 1 alone */
- test_validate_cleanup(AST_VECTOR_REPLACE(&sv1, 0, AAA) == 0);
- test_validate_cleanup(AST_VECTOR_REPLACE(&sv1, 2, CCC) == 0);
- test_validate_cleanup(AST_VECTOR_SIZE(&sv1) == 3);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 0) == AAA);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 1) == ZZZ);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 2) == CCC);
+ ast_test_validate_cleanup(test, AST_VECTOR_REPLACE(&sv1, 0, AAA) == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_REPLACE(&sv1, 2, CCC) == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_SIZE(&sv1) == 3, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 0) == AAA, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 1) == ZZZ, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 2) == CCC, rc, cleanup);
/* Remove 1 and compact preserving order */
- test_validate_cleanup(AST_VECTOR_REMOVE_ORDERED(&sv1, 1) == ZZZ);
- test_validate_cleanup(AST_VECTOR_SIZE(&sv1) == 2);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 0) == AAA);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 1) == CCC);
+ ast_test_validate_cleanup(test, AST_VECTOR_REMOVE_ORDERED(&sv1, 1) == ZZZ, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_SIZE(&sv1) == 2, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 0) == AAA, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 1) == CCC, rc, cleanup);
/* Equivalent of APPEND */
- test_validate_cleanup(AST_VECTOR_REPLACE(&sv1, 2, ZZZ) == 0);
- test_validate_cleanup(AST_VECTOR_SIZE(&sv1) == 3);
+ ast_test_validate_cleanup(test, AST_VECTOR_REPLACE(&sv1, 2, ZZZ) == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_SIZE(&sv1) == 3, rc, cleanup);
/* This should work because we passing in the specific object to be removed */
cleanup_count = 0;
- test_validate_cleanup(AST_VECTOR_REMOVE_ELEM_ORDERED(&sv1, ZZZ, cleanup_int) == 0);
- test_validate_cleanup(AST_VECTOR_SIZE(&sv1) == 2);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 0) == AAA);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 1) == CCC);
- test_validate_cleanup(cleanup_count == 1);
+ ast_test_validate_cleanup(test, AST_VECTOR_REMOVE_ELEM_ORDERED(&sv1, ZZZ, cleanup_int) == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_SIZE(&sv1) == 2, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 0) == AAA, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 1) == CCC, rc, cleanup);
+ ast_test_validate_cleanup(test, cleanup_count == 1, rc, cleanup);
/* If we want a comparison by value, we need to pass in a comparison
* function.
*/
cleanup_count = 0;
- test_validate_cleanup(AST_VECTOR_REMOVE_CMP_ORDERED(&sv1, AAA, AST_VECTOR_ELEM_DEFAULT_CMP, cleanup_int) == 0);
- test_validate_cleanup(AST_VECTOR_SIZE(&sv1) == 1);
- test_validate_cleanup(AST_VECTOR_GET(&sv1, 0) == CCC);
- test_validate_cleanup(cleanup_count == 1);
+ ast_test_validate_cleanup(test, AST_VECTOR_REMOVE_CMP_ORDERED(&sv1, AAA, AST_VECTOR_ELEM_DEFAULT_CMP, cleanup_int) == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_SIZE(&sv1) == 1, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(&sv1, 0) == CCC, rc, cleanup);
+ ast_test_validate_cleanup(test, cleanup_count == 1, rc, cleanup);
/* This element is gone so we shouldn't be able to find it or delete it again. */
- test_validate_cleanup(AST_VECTOR_GET_CMP(&sv1, AAA, AST_VECTOR_ELEM_DEFAULT_CMP) == NULL);
- test_validate_cleanup(AST_VECTOR_REMOVE_CMP_ORDERED(&sv1, AAA, AST_VECTOR_ELEM_DEFAULT_CMP, cleanup_int) != 0);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET_CMP(&sv1, AAA, AST_VECTOR_ELEM_DEFAULT_CMP) == NULL, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_REMOVE_CMP_ORDERED(&sv1, AAA, AST_VECTOR_ELEM_DEFAULT_CMP, cleanup_int) != 0, rc, cleanup);
/* CCC should still be there though */
- test_validate_cleanup(*(int *)AST_VECTOR_GET_CMP(&sv1, CCC, AST_VECTOR_ELEM_DEFAULT_CMP) == CCC);
+ ast_test_validate_cleanup(test, *(int *)AST_VECTOR_GET_CMP(&sv1, CCC, AST_VECTOR_ELEM_DEFAULT_CMP) == CCC, rc, cleanup);
cleanup:
AST_VECTOR_FREE(&sv1);
@@ -316,18 +327,23 @@
static int cb(void *obj, void *arg, void *data)
{
- return strcmp(arg, "ARG") == 0 ? 0 : CMP_STOP;
+ return strcmp(arg, "ARG") == 0 ? CMP_MATCH : CMP_STOP;
}
static int cb_first(void *obj, void *arg, void *data)
{
- return data == arg ? CMP_STOP : 0;
+ return data == arg ? CMP_MATCH | CMP_STOP : 0;
}
AST_TEST_DEFINE(callbacks)
{
AST_VECTOR(, char *) sv1;
+ typeof(sv1) *sv2 = NULL;
+
int rc = AST_TEST_PASS;
+ char *AAA = "AAA";
+ char *BBB = "BBB";
+ char *CCC = "CCC";
switch (cmd) {
case TEST_INIT:
@@ -342,16 +358,27 @@
AST_VECTOR_INIT(&sv1, 32);
- AST_VECTOR_APPEND(&sv1, "AAA");
- AST_VECTOR_APPEND(&sv1, "BBB");
- AST_VECTOR_APPEND(&sv1, "CCC");
+ AST_VECTOR_APPEND(&sv1, AAA);
+ AST_VECTOR_APPEND(&sv1, BBB);
+ AST_VECTOR_APPEND(&sv1, CCC);
- test_validate_cleanup(AST_VECTOR_CALLBACK(&sv1, cb, "ARG", test) == 3);
+ ast_test_validate_cleanup(test, AST_VECTOR_CALLBACK(&sv1, cb, "ARG", test) == CCC, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_CALLBACK(&sv1, cb_first, test, test) == AAA, rc, cleanup);
- test_validate_cleanup(AST_VECTOR_CALLBACK(&sv1, cb_first, test, test) == 1);
+ sv2 = AST_VECTOR_CALLBACK_MULTIPLE(&sv1, cb, "ARG", test);
+ ast_test_validate_cleanup(test, AST_VECTOR_SIZE(sv2) == 3, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(sv2, 0) == AAA, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(sv2, 1) == BBB, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(sv2, 2) == CCC, rc, cleanup);
+
+ AST_VECTOR_PTR_FREE(sv2);
+ sv2 = AST_VECTOR_CALLBACK_MULTIPLE(&sv1, cb_first, test, test);
+ ast_test_validate_cleanup(test, AST_VECTOR_SIZE(sv2) == 1, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_GET(sv2, 0) == AAA, rc, cleanup);
cleanup:
AST_VECTOR_FREE(&sv1);
+ AST_VECTOR_PTR_FREE(sv2);
return rc;
}
@@ -379,25 +406,25 @@
AST_VECTOR_RW_INIT(&sv1, 0);
- test_validate_cleanup(AST_VECTOR_RW_RDLOCK(&sv1) == 0);
- test_validate_cleanup(AST_VECTOR_RW_UNLOCK(&sv1) == 0);
- test_validate_cleanup(AST_VECTOR_RW_WRLOCK(&sv1) == 0);
- test_validate_cleanup(AST_VECTOR_RW_UNLOCK(&sv1) == 0);
+ ast_test_validate_cleanup(test, AST_VECTOR_RW_RDLOCK(&sv1) == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_RW_UNLOCK(&sv1) == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_RW_WRLOCK(&sv1) == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_RW_UNLOCK(&sv1) == 0, rc, cleanup);
- test_validate_cleanup(AST_VECTOR_RW_RDLOCK_TRY(&sv1) == 0);
- test_validate_cleanup(AST_VECTOR_RW_WRLOCK_TRY(&sv1) != 0);
- test_validate_cleanup(AST_VECTOR_RW_UNLOCK(&sv1) == 0);
- test_validate_cleanup(AST_VECTOR_RW_WRLOCK_TRY(&sv1) == 0);
- test_validate_cleanup(AST_VECTOR_RW_UNLOCK(&sv1) == 0);
+ ast_test_validate_cleanup(test, AST_VECTOR_RW_RDLOCK_TRY(&sv1) == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_RW_WRLOCK_TRY(&sv1) != 0, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_RW_UNLOCK(&sv1) == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_RW_WRLOCK_TRY(&sv1) == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_RW_UNLOCK(&sv1) == 0, rc, cleanup);
ts.tv_nsec = 0;
ts.tv_sec = 2;
- test_validate_cleanup(AST_VECTOR_RW_RDLOCK_TIMED(&sv1, &ts) == 0);
- test_validate_cleanup(AST_VECTOR_RW_WRLOCK_TIMED(&sv1, &ts) != 0);
- test_validate_cleanup(AST_VECTOR_RW_UNLOCK(&sv1) == 0);
- test_validate_cleanup(AST_VECTOR_RW_WRLOCK_TIMED(&sv1, &ts) == 0);
- test_validate_cleanup(AST_VECTOR_RW_UNLOCK(&sv1) == 0);
+ ast_test_validate_cleanup(test, AST_VECTOR_RW_RDLOCK_TIMED(&sv1, &ts) == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_RW_WRLOCK_TIMED(&sv1, &ts) != 0, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_RW_UNLOCK(&sv1) == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_RW_WRLOCK_TIMED(&sv1, &ts) == 0, rc, cleanup);
+ ast_test_validate_cleanup(test, AST_VECTOR_RW_UNLOCK(&sv1) == 0, rc, cleanup);
cleanup:
AST_VECTOR_RW_FREE(&sv1);
--
To view, visit https://gerrit.asterisk.org/375
To unsubscribe, visit https://gerrit.asterisk.org/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I05e5e47fd02f61964be13b7e8942bab5d61b29cc
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Owner: George Joseph <george.joseph at fairview5.com>
More information about the asterisk-code-review
mailing list