test/array_tests.c

branch
feature/array
changeset 354
7fd13b9f8f60
parent 353
135ce35d5108
child 355
d315a068235a
--- a/test/array_tests.c	Sat Aug 10 09:47:59 2019 +0200
+++ b/test/array_tests.c	Sat Aug 10 11:12:49 2019 +0200
@@ -56,35 +56,138 @@
     ucx_array_destroy(&array);
 }
 
-UCX_TEST(test_ucx_array_append) {
+UCX_TEST(test_ucx_array_append_from) {
     UcxArray array = ucx_array_new(16, sizeof(int));
     int *elements;
     
     int x = 42;
-    ucx_array_append(&array, &x);
+    ucx_array_append_from(&array, &x, 1);
     UCX_TEST_BEGIN
     
     elements = array.data;
     UCX_TEST_ASSERT(elements[0] == 42, "failed");
     
-    x = 13;
-    ucx_array_append(&array, &x);
+    int y[2] = {13, 37};
+    ucx_array_append_from(&array, y, 2);
     
     elements = array.data;
-    UCX_TEST_ASSERT(array.size == 2, "incorrect size after append");
+    UCX_TEST_ASSERT(array.size == 3, "incorrect size after append");
     UCX_TEST_ASSERT(elements[1] == 13, "failed");
+    UCX_TEST_ASSERT(elements[2] == 37, "failed");
     UCX_TEST_ASSERT(elements[0] == 42,
             "append corrupted previously inserted data");
     
-    ucx_array_append(&array, NULL);
+    ucx_array_append_from(&array, NULL, 2);
     
     elements = array.data;
-    UCX_TEST_ASSERT(array.size == 3, "incorrect size after NULL append");
-    UCX_TEST_ASSERT(elements[2] == 0, "element is not zeroed");
+    UCX_TEST_ASSERT(array.size == 5, "incorrect size after NULL append");
+    UCX_TEST_ASSERT(elements[3] == 0, "element is not zeroed");
+    UCX_TEST_ASSERT(elements[4] == 0, "element is not zeroed");
     UCX_TEST_ASSERT(elements[0] == 42,
             "NULL append corrupted previously inserted data");
     UCX_TEST_ASSERT(elements[1] == 13,
             "NULL append corrupted previously inserted data");
+    UCX_TEST_ASSERT(elements[2] == 37,
+            "NULL append corrupted previously inserted data");
+    
+    UCX_TEST_END
+    
+    ucx_array_destroy(&array);
+}
+
+UCX_TEST(test_ucx_array_prepend_from) {
+    int *elems;
+    UcxArray array = ucx_array_new(16, sizeof(int));
+    
+    int x = 42;
+    ucx_array_prepend_from(&array, &x, 1);
+    UCX_TEST_BEGIN
+    
+    elems = array.data;
+    UCX_TEST_ASSERT(elems[0] == 42, "failed");
+    
+    int y[2] = {13, 37};
+    ucx_array_prepend_from(&array, y, 2);
+    
+    elems = array.data;
+    UCX_TEST_ASSERT(array.size == 3, "incorrect size after prepend");
+    UCX_TEST_ASSERT(elems[0] == 13, "failed");
+    UCX_TEST_ASSERT(elems[1] == 37, "failed");
+    UCX_TEST_ASSERT(elems[2] == 42,
+            "prepend corrupted previously inserted data");
+    
+    ucx_array_prepend_from(&array, NULL, 2);
+    
+    elems = array.data;
+    UCX_TEST_ASSERT(array.size == 5, "incorrect size after NULL prepend");
+    UCX_TEST_ASSERT(elems[0] == 0, "element is not zeroed");
+    UCX_TEST_ASSERT(elems[1] == 0, "element is not zeroed");
+    UCX_TEST_ASSERT(elems[2] == 13,
+            "NULL prepend corrupted previously inserted data");
+    UCX_TEST_ASSERT(elems[3] == 37,
+            "NULL prepend corrupted previously inserted data");
+    UCX_TEST_ASSERT(elems[4] == 42,
+            "NULL prepend corrupted previously inserted data");
+    
+    UCX_TEST_END
+    
+    ucx_array_destroy(&array);
+}
+
+UCX_TEST(test_ucx_array_set_from) {
+    int *elems;
+    UcxArray array = ucx_array_new(16, sizeof(int));
+    
+    int x = 42;
+
+    UCX_TEST_BEGIN
+
+    ucx_array_set_from(&array, 7, &x, 1);
+    
+    elems = array.data;
+    UCX_TEST_ASSERT(elems[7] == 42, "failed");
+    UCX_TEST_ASSERT(array.size >= 8, "array not resized on set");
+    UCX_TEST_ASSERT(array.capacity == 16, "capacity changed unnecessarily");
+    
+    int y[2] = {13, 37};
+    ucx_array_set_from(&array, 27, y, 2);
+    
+    elems = array.data;
+    UCX_TEST_ASSERT(elems[27] == 13, "failed");
+    UCX_TEST_ASSERT(elems[28] == 37, "failed");
+    UCX_TEST_ASSERT(array.size == 29, "array not resized on set");
+    UCX_TEST_ASSERT(array.capacity == 32, "capacity not grown");
+    
+    ucx_array_set_from(&array, 7, NULL, 2);
+    
+    elems = array.data;
+    UCX_TEST_ASSERT(elems[7] == 0, "not zeroed on NULL set");
+    UCX_TEST_ASSERT(elems[8] == 0, "not zeroed on NULL set");
+    
+    UCX_TEST_END
+    
+    ucx_array_destroy(&array);
+}
+
+UCX_TEST(test_ucx_array_append) {
+    UcxArray array = ucx_array_new(16, sizeof(int));
+    int *elements;
+    
+    ucx_array_append(&array, 42);
+    UCX_TEST_BEGIN
+    
+    elements = array.data;
+    UCX_TEST_ASSERT(elements[0] == 42, "failed");
+    
+    ucx_array_append(&array, 13);
+    ucx_array_append(&array, 37);
+    
+    elements = array.data;
+    UCX_TEST_ASSERT(array.size == 3, "incorrect size after append");
+    UCX_TEST_ASSERT(elements[1] == 13, "failed");
+    UCX_TEST_ASSERT(elements[2] == 37, "failed");
+    UCX_TEST_ASSERT(elements[0] == 42,
+            "append corrupted previously inserted data");
     
     UCX_TEST_END
     
@@ -95,32 +198,22 @@
     int *elems;
     UcxArray array = ucx_array_new(16, sizeof(int));
     
-    int x = 42;
-    ucx_array_prepend(&array, &x);
+    ucx_array_prepend(&array, 42);
     UCX_TEST_BEGIN
     
     elems = array.data;
     UCX_TEST_ASSERT(elems[0] == 42, "failed");
     
-    x = 13;
-    ucx_array_prepend(&array, &x);
+    ucx_array_prepend(&array, 37);
+    ucx_array_prepend(&array, 13);
     
     elems = array.data;
-    UCX_TEST_ASSERT(array.size == 2, "incorrect size after prepend");
+    UCX_TEST_ASSERT(array.size == 3, "incorrect size after prepend");
     UCX_TEST_ASSERT(elems[0] == 13, "failed");
-    UCX_TEST_ASSERT(elems[1] == 42,
+    UCX_TEST_ASSERT(elems[1] == 37, "failed");
+    UCX_TEST_ASSERT(elems[2] == 42,
             "prepend corrupted previously inserted data");
     
-    ucx_array_prepend(&array, NULL);
-    
-    elems = array.data;
-    UCX_TEST_ASSERT(array.size == 3, "incorrect size after NULL prepend");
-    UCX_TEST_ASSERT(elems[0] == 0, "element is not zeroed");
-    UCX_TEST_ASSERT(elems[1] == 13,
-            "NULL prepend corrupted previously inserted data");
-    UCX_TEST_ASSERT(elems[2] == 42,
-            "NULL prepend corrupted previously inserted data");
-    
     UCX_TEST_END
     
     ucx_array_destroy(&array);
@@ -129,32 +222,25 @@
 UCX_TEST(test_ucx_array_set) {
     int *elems;
     UcxArray array = ucx_array_new(16, sizeof(int));
-    
-    
-    int x = 42;
 
     UCX_TEST_BEGIN
 
-    ucx_array_set(&array, 7, &x);
+    ucx_array_set(&array, 7, 42);
     
     elems = array.data;
     UCX_TEST_ASSERT(elems[7] == 42, "failed");
-    UCX_TEST_ASSERT(array.size >= 8, "array not resized on set");
+    UCX_TEST_ASSERT(array.size == 8, "array not resized on set");
     UCX_TEST_ASSERT(array.capacity == 16, "capacity changed unnecessarily");
     
-    x = 13;
-    ucx_array_set(&array, 27, &x);
+    ucx_array_set(&array, 27, 13);
+    ucx_array_set(&array, 28, 37);
     
     elems = array.data;
     UCX_TEST_ASSERT(elems[27] == 13, "failed");
-    UCX_TEST_ASSERT(array.size == 28, "array not resized on set");
-    UCX_TEST_ASSERT(array.capacity == 28, "capacity not grown");
-    
-    ucx_array_set(&array, 7, NULL);
-    
-    elems = array.data;
-    UCX_TEST_ASSERT(elems[7] == 0, "not zeroed on NULL set");
-    
+    UCX_TEST_ASSERT(elems[28] == 37, "failed");
+    UCX_TEST_ASSERT(array.size == 29, "array not resized on set");
+    UCX_TEST_ASSERT(array.capacity == 32, "capacity not grown");
+        
     UCX_TEST_END
     
     ucx_array_destroy(&array);
@@ -260,12 +346,8 @@
 UCX_TEST(test_ucx_array_at) {
     UcxArray array = ucx_array_new(16, sizeof(int));
     
-    int x = 42;
-    ucx_array_append(&array, &x);
-    x = 13;
-    ucx_array_append(&array, &x);
-    x = 5;
-    ucx_array_append(&array, &x);
+    int x[3] = {42, 13, 5};
+    ucx_array_append_from(&array, x, 3);
     
     UCX_TEST_BEGIN
     
@@ -484,10 +566,10 @@
 
     void* oldptr = array.data;
     
-    ucx_array_append(&array, &x);
+    ucx_array_append(&array, 5);
     UCX_TEST_ASSERT(array.capacity == 4 && array.data == oldptr,
             "array should not grow too early");
-    ucx_array_append(&array, &x);
+    ucx_array_append(&array, 5);
     elems = array.data;
     UCX_TEST_ASSERT(array.capacity == 8, "array did not grow");
     UCX_TEST_ASSERT(array.size == 5, "incorrect size after grow");

mercurial