1.1 --- a/test/array_tests.c Sat Aug 10 09:47:59 2019 +0200 1.2 +++ b/test/array_tests.c Sat Aug 10 11:12:49 2019 +0200 1.3 @@ -56,35 +56,138 @@ 1.4 ucx_array_destroy(&array); 1.5 } 1.6 1.7 -UCX_TEST(test_ucx_array_append) { 1.8 +UCX_TEST(test_ucx_array_append_from) { 1.9 UcxArray array = ucx_array_new(16, sizeof(int)); 1.10 int *elements; 1.11 1.12 int x = 42; 1.13 - ucx_array_append(&array, &x); 1.14 + ucx_array_append_from(&array, &x, 1); 1.15 UCX_TEST_BEGIN 1.16 1.17 elements = array.data; 1.18 UCX_TEST_ASSERT(elements[0] == 42, "failed"); 1.19 1.20 - x = 13; 1.21 - ucx_array_append(&array, &x); 1.22 + int y[2] = {13, 37}; 1.23 + ucx_array_append_from(&array, y, 2); 1.24 1.25 elements = array.data; 1.26 - UCX_TEST_ASSERT(array.size == 2, "incorrect size after append"); 1.27 + UCX_TEST_ASSERT(array.size == 3, "incorrect size after append"); 1.28 UCX_TEST_ASSERT(elements[1] == 13, "failed"); 1.29 + UCX_TEST_ASSERT(elements[2] == 37, "failed"); 1.30 UCX_TEST_ASSERT(elements[0] == 42, 1.31 "append corrupted previously inserted data"); 1.32 1.33 - ucx_array_append(&array, NULL); 1.34 + ucx_array_append_from(&array, NULL, 2); 1.35 1.36 elements = array.data; 1.37 - UCX_TEST_ASSERT(array.size == 3, "incorrect size after NULL append"); 1.38 - UCX_TEST_ASSERT(elements[2] == 0, "element is not zeroed"); 1.39 + UCX_TEST_ASSERT(array.size == 5, "incorrect size after NULL append"); 1.40 + UCX_TEST_ASSERT(elements[3] == 0, "element is not zeroed"); 1.41 + UCX_TEST_ASSERT(elements[4] == 0, "element is not zeroed"); 1.42 UCX_TEST_ASSERT(elements[0] == 42, 1.43 "NULL append corrupted previously inserted data"); 1.44 UCX_TEST_ASSERT(elements[1] == 13, 1.45 "NULL append corrupted previously inserted data"); 1.46 + UCX_TEST_ASSERT(elements[2] == 37, 1.47 + "NULL append corrupted previously inserted data"); 1.48 + 1.49 + UCX_TEST_END 1.50 + 1.51 + ucx_array_destroy(&array); 1.52 +} 1.53 + 1.54 +UCX_TEST(test_ucx_array_prepend_from) { 1.55 + int *elems; 1.56 + UcxArray array = ucx_array_new(16, sizeof(int)); 1.57 + 1.58 + int x = 42; 1.59 + ucx_array_prepend_from(&array, &x, 1); 1.60 + UCX_TEST_BEGIN 1.61 + 1.62 + elems = array.data; 1.63 + UCX_TEST_ASSERT(elems[0] == 42, "failed"); 1.64 + 1.65 + int y[2] = {13, 37}; 1.66 + ucx_array_prepend_from(&array, y, 2); 1.67 + 1.68 + elems = array.data; 1.69 + UCX_TEST_ASSERT(array.size == 3, "incorrect size after prepend"); 1.70 + UCX_TEST_ASSERT(elems[0] == 13, "failed"); 1.71 + UCX_TEST_ASSERT(elems[1] == 37, "failed"); 1.72 + UCX_TEST_ASSERT(elems[2] == 42, 1.73 + "prepend corrupted previously inserted data"); 1.74 + 1.75 + ucx_array_prepend_from(&array, NULL, 2); 1.76 + 1.77 + elems = array.data; 1.78 + UCX_TEST_ASSERT(array.size == 5, "incorrect size after NULL prepend"); 1.79 + UCX_TEST_ASSERT(elems[0] == 0, "element is not zeroed"); 1.80 + UCX_TEST_ASSERT(elems[1] == 0, "element is not zeroed"); 1.81 + UCX_TEST_ASSERT(elems[2] == 13, 1.82 + "NULL prepend corrupted previously inserted data"); 1.83 + UCX_TEST_ASSERT(elems[3] == 37, 1.84 + "NULL prepend corrupted previously inserted data"); 1.85 + UCX_TEST_ASSERT(elems[4] == 42, 1.86 + "NULL prepend corrupted previously inserted data"); 1.87 + 1.88 + UCX_TEST_END 1.89 + 1.90 + ucx_array_destroy(&array); 1.91 +} 1.92 + 1.93 +UCX_TEST(test_ucx_array_set_from) { 1.94 + int *elems; 1.95 + UcxArray array = ucx_array_new(16, sizeof(int)); 1.96 + 1.97 + int x = 42; 1.98 + 1.99 + UCX_TEST_BEGIN 1.100 + 1.101 + ucx_array_set_from(&array, 7, &x, 1); 1.102 + 1.103 + elems = array.data; 1.104 + UCX_TEST_ASSERT(elems[7] == 42, "failed"); 1.105 + UCX_TEST_ASSERT(array.size >= 8, "array not resized on set"); 1.106 + UCX_TEST_ASSERT(array.capacity == 16, "capacity changed unnecessarily"); 1.107 + 1.108 + int y[2] = {13, 37}; 1.109 + ucx_array_set_from(&array, 27, y, 2); 1.110 + 1.111 + elems = array.data; 1.112 + UCX_TEST_ASSERT(elems[27] == 13, "failed"); 1.113 + UCX_TEST_ASSERT(elems[28] == 37, "failed"); 1.114 + UCX_TEST_ASSERT(array.size == 29, "array not resized on set"); 1.115 + UCX_TEST_ASSERT(array.capacity == 32, "capacity not grown"); 1.116 + 1.117 + ucx_array_set_from(&array, 7, NULL, 2); 1.118 + 1.119 + elems = array.data; 1.120 + UCX_TEST_ASSERT(elems[7] == 0, "not zeroed on NULL set"); 1.121 + UCX_TEST_ASSERT(elems[8] == 0, "not zeroed on NULL set"); 1.122 + 1.123 + UCX_TEST_END 1.124 + 1.125 + ucx_array_destroy(&array); 1.126 +} 1.127 + 1.128 +UCX_TEST(test_ucx_array_append) { 1.129 + UcxArray array = ucx_array_new(16, sizeof(int)); 1.130 + int *elements; 1.131 + 1.132 + ucx_array_append(&array, 42); 1.133 + UCX_TEST_BEGIN 1.134 + 1.135 + elements = array.data; 1.136 + UCX_TEST_ASSERT(elements[0] == 42, "failed"); 1.137 + 1.138 + ucx_array_append(&array, 13); 1.139 + ucx_array_append(&array, 37); 1.140 + 1.141 + elements = array.data; 1.142 + UCX_TEST_ASSERT(array.size == 3, "incorrect size after append"); 1.143 + UCX_TEST_ASSERT(elements[1] == 13, "failed"); 1.144 + UCX_TEST_ASSERT(elements[2] == 37, "failed"); 1.145 + UCX_TEST_ASSERT(elements[0] == 42, 1.146 + "append corrupted previously inserted data"); 1.147 1.148 UCX_TEST_END 1.149 1.150 @@ -95,32 +198,22 @@ 1.151 int *elems; 1.152 UcxArray array = ucx_array_new(16, sizeof(int)); 1.153 1.154 - int x = 42; 1.155 - ucx_array_prepend(&array, &x); 1.156 + ucx_array_prepend(&array, 42); 1.157 UCX_TEST_BEGIN 1.158 1.159 elems = array.data; 1.160 UCX_TEST_ASSERT(elems[0] == 42, "failed"); 1.161 1.162 - x = 13; 1.163 - ucx_array_prepend(&array, &x); 1.164 + ucx_array_prepend(&array, 37); 1.165 + ucx_array_prepend(&array, 13); 1.166 1.167 elems = array.data; 1.168 - UCX_TEST_ASSERT(array.size == 2, "incorrect size after prepend"); 1.169 + UCX_TEST_ASSERT(array.size == 3, "incorrect size after prepend"); 1.170 UCX_TEST_ASSERT(elems[0] == 13, "failed"); 1.171 - UCX_TEST_ASSERT(elems[1] == 42, 1.172 + UCX_TEST_ASSERT(elems[1] == 37, "failed"); 1.173 + UCX_TEST_ASSERT(elems[2] == 42, 1.174 "prepend corrupted previously inserted data"); 1.175 1.176 - ucx_array_prepend(&array, NULL); 1.177 - 1.178 - elems = array.data; 1.179 - UCX_TEST_ASSERT(array.size == 3, "incorrect size after NULL prepend"); 1.180 - UCX_TEST_ASSERT(elems[0] == 0, "element is not zeroed"); 1.181 - UCX_TEST_ASSERT(elems[1] == 13, 1.182 - "NULL prepend corrupted previously inserted data"); 1.183 - UCX_TEST_ASSERT(elems[2] == 42, 1.184 - "NULL prepend corrupted previously inserted data"); 1.185 - 1.186 UCX_TEST_END 1.187 1.188 ucx_array_destroy(&array); 1.189 @@ -129,32 +222,25 @@ 1.190 UCX_TEST(test_ucx_array_set) { 1.191 int *elems; 1.192 UcxArray array = ucx_array_new(16, sizeof(int)); 1.193 - 1.194 - 1.195 - int x = 42; 1.196 1.197 UCX_TEST_BEGIN 1.198 1.199 - ucx_array_set(&array, 7, &x); 1.200 + ucx_array_set(&array, 7, 42); 1.201 1.202 elems = array.data; 1.203 UCX_TEST_ASSERT(elems[7] == 42, "failed"); 1.204 - UCX_TEST_ASSERT(array.size >= 8, "array not resized on set"); 1.205 + UCX_TEST_ASSERT(array.size == 8, "array not resized on set"); 1.206 UCX_TEST_ASSERT(array.capacity == 16, "capacity changed unnecessarily"); 1.207 1.208 - x = 13; 1.209 - ucx_array_set(&array, 27, &x); 1.210 + ucx_array_set(&array, 27, 13); 1.211 + ucx_array_set(&array, 28, 37); 1.212 1.213 elems = array.data; 1.214 UCX_TEST_ASSERT(elems[27] == 13, "failed"); 1.215 - UCX_TEST_ASSERT(array.size == 28, "array not resized on set"); 1.216 - UCX_TEST_ASSERT(array.capacity == 28, "capacity not grown"); 1.217 - 1.218 - ucx_array_set(&array, 7, NULL); 1.219 - 1.220 - elems = array.data; 1.221 - UCX_TEST_ASSERT(elems[7] == 0, "not zeroed on NULL set"); 1.222 - 1.223 + UCX_TEST_ASSERT(elems[28] == 37, "failed"); 1.224 + UCX_TEST_ASSERT(array.size == 29, "array not resized on set"); 1.225 + UCX_TEST_ASSERT(array.capacity == 32, "capacity not grown"); 1.226 + 1.227 UCX_TEST_END 1.228 1.229 ucx_array_destroy(&array); 1.230 @@ -260,12 +346,8 @@ 1.231 UCX_TEST(test_ucx_array_at) { 1.232 UcxArray array = ucx_array_new(16, sizeof(int)); 1.233 1.234 - int x = 42; 1.235 - ucx_array_append(&array, &x); 1.236 - x = 13; 1.237 - ucx_array_append(&array, &x); 1.238 - x = 5; 1.239 - ucx_array_append(&array, &x); 1.240 + int x[3] = {42, 13, 5}; 1.241 + ucx_array_append_from(&array, x, 3); 1.242 1.243 UCX_TEST_BEGIN 1.244 1.245 @@ -484,10 +566,10 @@ 1.246 1.247 void* oldptr = array.data; 1.248 1.249 - ucx_array_append(&array, &x); 1.250 + ucx_array_append(&array, 5); 1.251 UCX_TEST_ASSERT(array.capacity == 4 && array.data == oldptr, 1.252 "array should not grow too early"); 1.253 - ucx_array_append(&array, &x); 1.254 + ucx_array_append(&array, 5); 1.255 elems = array.data; 1.256 UCX_TEST_ASSERT(array.capacity == 8, "array did not grow"); 1.257 UCX_TEST_ASSERT(array.size == 5, "incorrect size after grow");