basic vector test cases
+/* XXX TODO
+ - test split()
+ - test join()
+ - test (or just delete; it's unused) bitvector_set()
+ - test build_vector_internal()
+*/
+
+//#define ENABLE_MSG_DEBUG
#include <runtime.h>
#include <stdlib.h>
@@ -37,6 +45,55 @@ boolean basic_test(heap h)
msg = "double peek or pop fail";
goto fail;
}
+
+ /* resize test */
+ deallocate_vector(v);
+ v = allocate_vector(h, 1);
+ int n = 1 << 20;
+ for (long i = 0; i < n; i++) {
+ void * prev = v->contents;
+ vector_set(v, i, (void *)i);
+ if (vector_length(v) != i + 1) {
+ msg = "resize: wrong vector length";
+ goto fail;
+ }
+
+ /* Attempt to detect resize and check content */
+ if (prev != v->contents || i == n - 1) {
+ msg_debug("resize detected at index %d\n", i);
+ for (long j = 0; j < i + 1; j++) {
+ if (vector_get(v, j) != (void *)j) {
+ msg = "resize: content mismatch";
+ goto fail;
+ }
+ }
+ }
+ }
+
+ /* foreach - assuming it's meant to always be in order */
+ long i = 0;
+ void *x;
+ vector_foreach(v, x) {
+ if ((long)x != i) {
+ msg = "foreach: content mismatch";
+ goto fail;
+ }
+ i++;
+ }
+
+ if (i != n) {
+ msg = "foreach: iterations doesn't match length";
+ goto fail;
+ }
+
+ deallocate_vector(v);
+ v = allocate_vector(h, 1);
+ vector_foreach(v, x) {
+ msg = "foreach: body run on zero len";
+ goto fail;
+ }
+
+ deallocate_vector(v);
return true;
fail:
deallocate_vector(v);
@@ -51,9 +108,9 @@ int main(int argc, char **argv)
if (!basic_test(h))
goto fail;
- msg_debug("test passed\n");
+ msg_debug("vector test passed\n");
exit(EXIT_SUCCESS);
fail:
- msg_err("test failed\n");
+ msg_err("vector test failed\n");
exit(EXIT_FAILURE);
}
