Thu, 04 Jul 2019 22:32:03 +0200
adds ucx_array_set()
olaf@9 | 1 | /* |
universe@103 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@103 | 3 | * |
universe@334 | 4 | * Copyright 2019 Mike Becker, Olaf Wintermann All rights reserved. |
universe@103 | 5 | * |
universe@103 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@103 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@103 | 8 | * |
universe@103 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@103 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@103 | 11 | * |
universe@103 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@103 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@103 | 14 | * documentation and/or other materials provided with the distribution. |
universe@103 | 15 | * |
universe@103 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@103 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@103 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@103 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@103 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@103 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@103 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@103 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@103 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@103 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@103 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
olaf@9 | 27 | */ |
olaf@9 | 28 | |
universe@334 | 29 | #include "array_tests.h" |
universe@251 | 30 | #include <ucx/utils.h> |
olaf@9 | 31 | |
universe@334 | 32 | UCX_TEST(test_ucx_array_free) { |
universe@334 | 33 | UcxArray array = ucx_array_new(16, sizeof(int)); |
universe@334 | 34 | |
universe@334 | 35 | UCX_TEST_BEGIN |
universe@334 | 36 | ucx_array_free(&array); |
universe@334 | 37 | UCX_TEST_ASSERT(array.data == NULL, "data pointer not NULL after free"); |
universe@334 | 38 | UCX_TEST_ASSERT(array.size == 0, "size not zero after free"); |
universe@334 | 39 | UCX_TEST_ASSERT(array.capacity == 0, "capacity not zero after free"); |
universe@334 | 40 | UCX_TEST_ASSERT(array.allocator == ucx_default_allocator(), |
universe@334 | 41 | "allocator corrupted during free"); |
universe@334 | 42 | UCX_TEST_END |
universe@334 | 43 | } |
universe@334 | 44 | |
universe@334 | 45 | UCX_TEST(test_ucx_array_new) { |
universe@334 | 46 | UcxArray array = ucx_array_new(16, 47); |
universe@334 | 47 | |
universe@334 | 48 | UCX_TEST_BEGIN |
universe@334 | 49 | UCX_TEST_ASSERT(array.data, "no memory allocated"); |
universe@334 | 50 | UCX_TEST_ASSERT(array.size == 0, "size not initially zero"); |
universe@334 | 51 | UCX_TEST_ASSERT(array.capacity == 16, "capacity not as requested"); |
universe@334 | 52 | UCX_TEST_ASSERT(array.elemsize == 47, "element size not as requested"); |
universe@334 | 53 | UCX_TEST_ASSERT(array.allocator == ucx_default_allocator(), |
universe@334 | 54 | "array not using the default allocator"); |
universe@334 | 55 | UCX_TEST_END |
universe@334 | 56 | ucx_array_free(&array); |
universe@334 | 57 | } |
universe@334 | 58 | |
universe@334 | 59 | UCX_TEST(test_ucx_array_append) { |
universe@334 | 60 | UcxArray array = ucx_array_new(16, sizeof(int)); |
universe@334 | 61 | |
universe@334 | 62 | int x = 42; |
universe@334 | 63 | ucx_array_append(&array, &x); |
universe@33 | 64 | UCX_TEST_BEGIN |
universe@27 | 65 | |
universe@334 | 66 | UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 42, "failed"); |
universe@27 | 67 | |
universe@334 | 68 | x = 13; |
universe@334 | 69 | ucx_array_append(&array, &x); |
universe@27 | 70 | |
universe@334 | 71 | UCX_TEST_ASSERT(array.size == 2, "incorrect size after append"); |
universe@334 | 72 | UCX_TEST_ASSERT(ucx_array_at_int(array, 1) == 13, "failed"); |
universe@334 | 73 | UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 42, |
universe@334 | 74 | "append corrupted previously inserted data"); |
universe@334 | 75 | |
universe@334 | 76 | ucx_array_append(&array, NULL); |
universe@334 | 77 | |
universe@334 | 78 | UCX_TEST_ASSERT(array.size == 3, "incorrect size after NULL append"); |
universe@334 | 79 | UCX_TEST_ASSERT(ucx_array_at_int(array, 2) == 0, "element is not zeroed"); |
universe@334 | 80 | UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 42, |
universe@334 | 81 | "NULL append corrupted previously inserted data"); |
universe@334 | 82 | UCX_TEST_ASSERT(ucx_array_at_int(array, 1) == 13, |
universe@334 | 83 | "NULL append corrupted previously inserted data"); |
universe@334 | 84 | |
universe@33 | 85 | UCX_TEST_END |
universe@27 | 86 | |
universe@334 | 87 | ucx_array_free(&array); |
universe@24 | 88 | } |
universe@24 | 89 | |
universe@334 | 90 | UCX_TEST(test_ucx_array_prepend) { |
universe@334 | 91 | UcxArray array = ucx_array_new(16, sizeof(int)); |
universe@334 | 92 | |
universe@334 | 93 | int x = 42; |
universe@334 | 94 | ucx_array_prepend(&array, &x); |
universe@33 | 95 | UCX_TEST_BEGIN |
universe@27 | 96 | |
universe@334 | 97 | UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 42, "failed"); |
universe@334 | 98 | |
universe@334 | 99 | x = 13; |
universe@334 | 100 | ucx_array_prepend(&array, &x); |
universe@334 | 101 | |
universe@334 | 102 | UCX_TEST_ASSERT(array.size == 2, "incorrect size after prepend"); |
universe@334 | 103 | UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 13, "failed"); |
universe@334 | 104 | UCX_TEST_ASSERT(ucx_array_at_int(array, 1) == 42, |
universe@334 | 105 | "prepend corrupted previously inserted data"); |
universe@334 | 106 | |
universe@334 | 107 | ucx_array_prepend(&array, NULL); |
universe@334 | 108 | |
universe@334 | 109 | UCX_TEST_ASSERT(array.size == 3, "incorrect size after NULL prepend"); |
universe@334 | 110 | UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 0, "element is not zeroed"); |
universe@334 | 111 | UCX_TEST_ASSERT(ucx_array_at_int(array, 1) == 13, |
universe@334 | 112 | "NULL prepend corrupted previously inserted data"); |
universe@334 | 113 | UCX_TEST_ASSERT(ucx_array_at_int(array, 2) == 42, |
universe@334 | 114 | "NULL prepend corrupted previously inserted data"); |
universe@27 | 115 | |
universe@33 | 116 | UCX_TEST_END |
universe@334 | 117 | |
universe@334 | 118 | ucx_array_free(&array); |
universe@18 | 119 | } |
universe@18 | 120 | |
universe@337 | 121 | UCX_TEST(test_ucx_array_set) { |
universe@337 | 122 | UcxArray array = ucx_array_new(16, sizeof(int)); |
universe@337 | 123 | |
universe@337 | 124 | int x = 42; |
universe@337 | 125 | |
universe@337 | 126 | UCX_TEST_BEGIN |
universe@337 | 127 | |
universe@337 | 128 | ucx_array_set(&array, 7, &x); |
universe@337 | 129 | UCX_TEST_ASSERT(ucx_array_at_int(array, 7) == 42, "failed"); |
universe@337 | 130 | UCX_TEST_ASSERT(array.size >= 8, "array not resized on set"); |
universe@337 | 131 | UCX_TEST_ASSERT(array.capacity == 16, "capacity changed unnecessarily"); |
universe@337 | 132 | |
universe@337 | 133 | x = 13; |
universe@337 | 134 | ucx_array_set(&array, 27, &x); |
universe@337 | 135 | |
universe@337 | 136 | UCX_TEST_ASSERT(ucx_array_at_int(array, 27) == 13, "failed"); |
universe@337 | 137 | UCX_TEST_ASSERT(array.size == 28, "array not resized on set"); |
universe@337 | 138 | UCX_TEST_ASSERT(array.capacity == 28, "capacity not grown"); |
universe@337 | 139 | |
universe@337 | 140 | ucx_array_set(&array, 7, NULL); |
universe@337 | 141 | |
universe@337 | 142 | UCX_TEST_ASSERT(ucx_array_at_int(array, 7) == 0, "not zeroed on NULL set"); |
universe@337 | 143 | |
universe@337 | 144 | UCX_TEST_END |
universe@337 | 145 | |
universe@337 | 146 | ucx_array_free(&array); |
universe@337 | 147 | } |
universe@337 | 148 | |
universe@334 | 149 | UCX_TEST(test_ucx_array_equals) { |
universe@334 | 150 | UcxArray a1 = ucx_array_new(16, sizeof(int)); |
universe@334 | 151 | UcxArray a2 = ucx_array_new(16, sizeof(int)); |
universe@334 | 152 | UcxArray a3 = ucx_array_new(16, sizeof(long int)); |
universe@334 | 153 | UcxArray a4 = ucx_array_new(16, sizeof(int)); |
universe@27 | 154 | |
universe@334 | 155 | a1.size = 5; |
universe@334 | 156 | ucx_array_at_int(a1, 0) = 47; |
universe@334 | 157 | ucx_array_at_int(a1, 1) = 11; |
universe@334 | 158 | ucx_array_at_int(a1, 2) = 0; |
universe@334 | 159 | ucx_array_at_int(a1, 3) = 8; |
universe@334 | 160 | ucx_array_at_int(a1, 4) = 15; |
universe@334 | 161 | a2.size = 5; |
universe@334 | 162 | ucx_array_at_int(a2, 0) = 47; |
universe@334 | 163 | ucx_array_at_int(a2, 1) = 11; |
universe@334 | 164 | ucx_array_at_int(a2, 2) = 0; |
universe@334 | 165 | ucx_array_at_int(a2, 3) = 8; |
universe@334 | 166 | ucx_array_at_int(a2, 4) = 15; |
universe@334 | 167 | a3.size = 5; |
universe@334 | 168 | ucx_array_at_longint(a3, 0) = 47; |
universe@334 | 169 | ucx_array_at_longint(a3, 1) = 11; |
universe@334 | 170 | ucx_array_at_longint(a3, 2) = 0; |
universe@334 | 171 | ucx_array_at_longint(a3, 3) = 8; |
universe@334 | 172 | ucx_array_at_longint(a3, 4) = 15; |
universe@334 | 173 | a4.size = 5; |
universe@334 | 174 | ucx_array_at_int(a4, 0) = 47; |
universe@334 | 175 | ucx_array_at_int(a4, 1) = 11; |
universe@334 | 176 | ucx_array_at_int(a4, 2) = -6; |
universe@334 | 177 | ucx_array_at_int(a4, 3) = 8; |
universe@334 | 178 | ucx_array_at_int(a4, 4) = 15; |
universe@27 | 179 | |
universe@123 | 180 | UCX_TEST_BEGIN |
universe@123 | 181 | |
universe@336 | 182 | UCX_TEST_ASSERT(ucx_array_equals(a1, a2, ucx_cmp_int, NULL), "failed"); |
universe@336 | 183 | UCX_TEST_ASSERT(!ucx_array_equals(a1, a4, ucx_cmp_int, NULL), "failed"); |
universe@336 | 184 | UCX_TEST_ASSERT(!ucx_array_equals(a4, a1, ucx_cmp_int, NULL), "failed"); |
universe@336 | 185 | UCX_TEST_ASSERT(!ucx_array_equals(a1, a3, ucx_cmp_int, NULL), |
universe@336 | 186 | "comparing arrays of different element size shall fail"); |
universe@336 | 187 | UCX_TEST_ASSERT(!ucx_array_equals(a3, a1, ucx_cmp_int, NULL), |
universe@336 | 188 | "comparing arrays of different element size shall fail"); |
universe@334 | 189 | |
universe@336 | 190 | UCX_TEST_ASSERT(ucx_array_equals(a1, a2, NULL, NULL), |
universe@334 | 191 | "compare using memcmp() failed"); |
universe@336 | 192 | UCX_TEST_ASSERT(!ucx_array_equals(a1, a4, NULL, NULL), |
universe@334 | 193 | "compare using memcmp() failed"); |
universe@27 | 194 | |
universe@33 | 195 | UCX_TEST_END |
universe@334 | 196 | ucx_array_free(&a1); |
universe@334 | 197 | ucx_array_free(&a2); |
universe@334 | 198 | ucx_array_free(&a3); |
universe@334 | 199 | ucx_array_free(&a4); |
olaf@9 | 200 | } |
olaf@11 | 201 | |
universe@334 | 202 | UCX_TEST(test_ucx_array_concat) { |
universe@334 | 203 | UcxArray a1 = ucx_array_new(16, sizeof(int)); |
universe@334 | 204 | UcxArray a2 = ucx_array_new(16, sizeof(int)); |
universe@334 | 205 | |
universe@334 | 206 | a1.size = 2; |
universe@334 | 207 | ucx_array_at_int(a1, 0) = 47; |
universe@334 | 208 | ucx_array_at_int(a1, 1) = 11; |
universe@334 | 209 | a2.size = 3; |
universe@334 | 210 | ucx_array_at_int(a2, 0) = 0; |
universe@334 | 211 | ucx_array_at_int(a2, 1) = 8; |
universe@334 | 212 | ucx_array_at_int(a2, 2) = 15; |
universe@27 | 213 | |
universe@123 | 214 | UCX_TEST_BEGIN |
universe@123 | 215 | |
universe@334 | 216 | UCX_TEST_ASSERT(!ucx_array_concat(&a1, &a2), "failed"); |
universe@334 | 217 | UCX_TEST_ASSERT(a1.size == 5, "failed"); |
universe@334 | 218 | UCX_TEST_ASSERT(ucx_array_at_int(a1, 0) == 47, "failed"); |
universe@334 | 219 | UCX_TEST_ASSERT(ucx_array_at_int(a1, 1) == 11, "failed"); |
universe@334 | 220 | UCX_TEST_ASSERT(ucx_array_at_int(a1, 2) == 0, "failed"); |
universe@334 | 221 | UCX_TEST_ASSERT(ucx_array_at_int(a1, 3) == 8, "failed"); |
universe@334 | 222 | UCX_TEST_ASSERT(ucx_array_at_int(a1, 4) == 15, "failed"); |
universe@27 | 223 | |
universe@334 | 224 | a1.elemsize *= 2; |
universe@334 | 225 | UCX_TEST_ASSERT(ucx_array_concat(&a1, &a2), |
universe@334 | 226 | "arrays of different element size must not be concatenated"); |
universe@334 | 227 | UCX_TEST_ASSERT(a1.size == 5, |
universe@334 | 228 | "arrays of different element size must not be concatenated"); |
universe@27 | 229 | |
universe@33 | 230 | UCX_TEST_END |
universe@334 | 231 | ucx_array_free(&a1); |
universe@334 | 232 | ucx_array_free(&a2); |
universe@27 | 233 | } |
universe@27 | 234 | |
universe@334 | 235 | UCX_TEST(test_ucx_array_at) { |
universe@334 | 236 | UcxArray array = ucx_array_new(16, sizeof(int)); |
universe@334 | 237 | |
universe@334 | 238 | int x = 42; |
universe@334 | 239 | ucx_array_append(&array, &x); |
universe@334 | 240 | x = 13; |
universe@334 | 241 | ucx_array_append(&array, &x); |
universe@334 | 242 | x = 5; |
universe@334 | 243 | ucx_array_append(&array, &x); |
universe@27 | 244 | |
universe@123 | 245 | UCX_TEST_BEGIN |
universe@123 | 246 | |
universe@334 | 247 | UCX_TEST_ASSERT(ucx_array_at_int(array, 1) == 13, "failed"); |
universe@334 | 248 | ucx_array_at_int(array, 1) = 80; |
universe@334 | 249 | UCX_TEST_ASSERT(ucx_array_at_int(array, 1) == 80, "assignment failed"); |
universe@27 | 250 | |
universe@334 | 251 | |
universe@334 | 252 | UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 42, "corrupted data"); |
universe@334 | 253 | UCX_TEST_ASSERT(ucx_array_at_int(array, 2) == 5, "corrupted data"); |
universe@27 | 254 | |
universe@33 | 255 | UCX_TEST_END |
universe@334 | 256 | |
universe@334 | 257 | ucx_array_free(&array); |
universe@27 | 258 | } |
universe@27 | 259 | |
universe@334 | 260 | UCX_TEST(test_ucx_array_find) { |
universe@334 | 261 | UcxArray array = ucx_array_new(16, sizeof(int)); |
universe@334 | 262 | |
universe@334 | 263 | array.size = 5; |
universe@334 | 264 | ucx_array_at_int(array, 0) = 47; |
universe@334 | 265 | ucx_array_at_int(array, 1) = 11; |
universe@334 | 266 | ucx_array_at_int(array, 2) = 0; |
universe@334 | 267 | ucx_array_at_int(array, 3) = 8; |
universe@334 | 268 | ucx_array_at_int(array, 4) = 15; |
universe@334 | 269 | |
universe@334 | 270 | int x = 8; |
universe@334 | 271 | int y = 90; |
universe@27 | 272 | |
universe@123 | 273 | UCX_TEST_BEGIN |
universe@123 | 274 | |
universe@334 | 275 | UCX_TEST_ASSERT(ucx_array_find(array,(void*)&x,ucx_cmp_int,NULL) == 3, |
universe@334 | 276 | "doesn't find element"); |
universe@334 | 277 | UCX_TEST_ASSERT(ucx_array_find(array,(void*)&y,ucx_cmp_int,NULL) == 5, |
universe@334 | 278 | "finds non-existing element"); |
universe@27 | 279 | |
universe@334 | 280 | UCX_TEST_ASSERT(ucx_array_find(array,(void*)&x,NULL,NULL) == 3, |
universe@334 | 281 | "failed using memcmp()"); |
universe@334 | 282 | UCX_TEST_ASSERT(ucx_array_find(array,(void*)&y,NULL,NULL) == 5, |
universe@334 | 283 | "failed using memcmp()"); |
universe@27 | 284 | |
universe@33 | 285 | UCX_TEST_END |
universe@334 | 286 | ucx_array_free(&array); |
universe@27 | 287 | } |
universe@27 | 288 | |
universe@334 | 289 | UCX_TEST(test_ucx_array_contains) { |
universe@334 | 290 | UcxArray array = ucx_array_new(16, sizeof(int)); |
universe@334 | 291 | |
universe@334 | 292 | array.size = 5; |
universe@334 | 293 | ucx_array_at_int(array, 0) = 47; |
universe@334 | 294 | ucx_array_at_int(array, 1) = 11; |
universe@334 | 295 | ucx_array_at_int(array, 2) = 0; |
universe@334 | 296 | ucx_array_at_int(array, 3) = 8; |
universe@334 | 297 | ucx_array_at_int(array, 4) = 15; |
universe@334 | 298 | |
universe@334 | 299 | int x = 8; |
universe@334 | 300 | int y = 90; |
universe@123 | 301 | |
universe@123 | 302 | UCX_TEST_BEGIN |
universe@123 | 303 | |
universe@334 | 304 | UCX_TEST_ASSERT(ucx_array_contains(array,(void*)&x,ucx_cmp_int,NULL), |
universe@334 | 305 | "false negative"); |
universe@334 | 306 | UCX_TEST_ASSERT(!ucx_array_contains(array,(void*)&y,ucx_cmp_int,NULL), |
universe@334 | 307 | "false positive"); |
universe@123 | 308 | |
universe@334 | 309 | UCX_TEST_ASSERT(ucx_array_contains(array,(void*)&x,NULL,NULL), |
universe@334 | 310 | "false negative using memcmp()"); |
universe@334 | 311 | UCX_TEST_ASSERT(!ucx_array_contains(array,(void*)&y,NULL,NULL), |
universe@334 | 312 | "false positive using memcmp()"); |
universe@123 | 313 | |
universe@123 | 314 | UCX_TEST_END |
universe@334 | 315 | ucx_array_free(&array); |
universe@123 | 316 | } |
universe@123 | 317 | |
universe@334 | 318 | UCX_TEST(test_ucx_array_remove) { |
universe@334 | 319 | UcxArray array = ucx_array_new(16, sizeof(int)); |
universe@334 | 320 | |
universe@334 | 321 | array.size = 5; |
universe@334 | 322 | ucx_array_at_int(array, 0) = 47; |
universe@334 | 323 | ucx_array_at_int(array, 1) = 11; |
universe@334 | 324 | ucx_array_at_int(array, 2) = 0; |
universe@334 | 325 | ucx_array_at_int(array, 3) = 8; |
universe@334 | 326 | ucx_array_at_int(array, 4) = 15; |
universe@334 | 327 | |
universe@334 | 328 | UCX_TEST_BEGIN |
universe@334 | 329 | |
universe@334 | 330 | ucx_array_remove(&array, 2); |
universe@334 | 331 | UCX_TEST_ASSERT( |
universe@334 | 332 | ucx_array_at_int(array, 0) == 47 && |
universe@334 | 333 | ucx_array_at_int(array, 1) == 11 && |
universe@334 | 334 | ucx_array_at_int(array, 2) == 8 && |
universe@334 | 335 | ucx_array_at_int(array, 3) == 15, |
universe@334 | 336 | "wrong contents after remove"); |
universe@334 | 337 | UCX_TEST_ASSERT(array.size == 4, "wrong size after remove"); |
universe@334 | 338 | |
universe@334 | 339 | ucx_array_remove_fast(&array, 1); |
universe@334 | 340 | UCX_TEST_ASSERT( |
universe@334 | 341 | ucx_array_at_int(array, 0) == 47 && |
universe@334 | 342 | ucx_array_at_int(array, 1) == 15 && |
universe@334 | 343 | ucx_array_at_int(array, 2) == 8, |
universe@334 | 344 | "wrong contents after fast remove"); |
universe@334 | 345 | UCX_TEST_ASSERT(array.size == 3, "wrong size after fast remove"); |
universe@334 | 346 | |
universe@334 | 347 | UCX_TEST_END |
universe@334 | 348 | ucx_array_free(&array); |
universe@334 | 349 | } |
universe@334 | 350 | |
universe@334 | 351 | UCX_TEST(test_ucx_array_clone) { |
universe@334 | 352 | |
universe@334 | 353 | UcxArray array = ucx_array_new(16, sizeof(int)); |
universe@334 | 354 | |
universe@334 | 355 | array.size = 5; |
universe@334 | 356 | ucx_array_at_int(array, 0) = 47; |
universe@334 | 357 | ucx_array_at_int(array, 1) = 11; |
universe@334 | 358 | ucx_array_at_int(array, 2) = 0; |
universe@334 | 359 | ucx_array_at_int(array, 3) = 8; |
universe@334 | 360 | ucx_array_at_int(array, 4) = 15; |
universe@334 | 361 | |
universe@334 | 362 | UcxArray copy = ucx_array_clone(array); |
universe@334 | 363 | UCX_TEST_BEGIN |
universe@334 | 364 | |
universe@334 | 365 | UCX_TEST_ASSERT(array.data != copy.data, "no true copy"); |
universe@334 | 366 | UCX_TEST_ASSERT(array.size == copy.size, "size mismatch"); |
universe@334 | 367 | UCX_TEST_ASSERT(array.capacity == copy.capacity, "capacity mismatch"); |
universe@334 | 368 | UCX_TEST_ASSERT(array.elemsize == copy.elemsize, "element size mismatch"); |
universe@334 | 369 | UCX_TEST_ASSERT(array.allocator == copy.allocator, "allocator mismatch"); |
universe@336 | 370 | UCX_TEST_ASSERT(ucx_array_equals(array, copy, ucx_cmp_int, NULL), "failed"); |
universe@334 | 371 | |
universe@334 | 372 | UCX_TEST_END |
universe@334 | 373 | |
universe@334 | 374 | ucx_array_free(&array); |
universe@334 | 375 | ucx_array_free(©); |
universe@334 | 376 | } |
universe@334 | 377 | |
universe@334 | 378 | UCX_TEST(test_ucx_array_sort) { |
universe@334 | 379 | UcxArray array = ucx_array_new(16, sizeof(int)); |
universe@334 | 380 | array.size = 5; |
universe@334 | 381 | ucx_array_at_int(array, 0) = 47; |
universe@334 | 382 | ucx_array_at_int(array, 1) = 11; |
universe@334 | 383 | ucx_array_at_int(array, 2) = 0; |
universe@334 | 384 | ucx_array_at_int(array, 3) = 8; |
universe@334 | 385 | ucx_array_at_int(array, 4) = 15; |
universe@334 | 386 | |
universe@334 | 387 | UcxArray expected = ucx_array_new(16, sizeof(int)); |
universe@334 | 388 | expected.size = 5; |
universe@334 | 389 | ucx_array_at_int(expected, 0) = 0; |
universe@334 | 390 | ucx_array_at_int(expected, 1) = 8; |
universe@334 | 391 | ucx_array_at_int(expected, 2) = 11; |
universe@334 | 392 | ucx_array_at_int(expected, 3) = 15; |
universe@334 | 393 | ucx_array_at_int(expected, 4) = 47; |
universe@334 | 394 | |
universe@334 | 395 | |
universe@334 | 396 | UCX_TEST_BEGIN |
universe@334 | 397 | void* original_ptr = array.data; |
universe@336 | 398 | ucx_array_sort(array, ucx_cmp_int, NULL); |
universe@336 | 399 | UCX_TEST_ASSERT(ucx_array_equals(array, expected, NULL, NULL), "failed"); |
universe@334 | 400 | UCX_TEST_ASSERT(array.size == 5, "size corrupted"); |
universe@334 | 401 | UCX_TEST_ASSERT(array.data == original_ptr, "shall not reallocate"); |
universe@336 | 402 | |
universe@336 | 403 | ucx_array_reserve(&array, 32); |
universe@336 | 404 | ucx_array_reserve(&expected, 32); |
universe@336 | 405 | array.size = expected.size = 32; |
universe@336 | 406 | for (size_t i = 0 ; i < 32 ; i++) { |
universe@336 | 407 | ucx_array_at_int(array, i) = ((i%2==0)?-1:1) * ((int) i); |
universe@336 | 408 | ucx_array_at_int(expected, i) = (-30+2*i) - (i > 15 ? 1 : 0); |
universe@336 | 409 | } |
universe@336 | 410 | |
universe@336 | 411 | ucx_array_sort(array, ucx_cmp_int, NULL); |
universe@336 | 412 | UCX_TEST_ASSERT(ucx_array_equals(array, expected, NULL, NULL), |
universe@336 | 413 | "failed for bigger arrays"); |
universe@334 | 414 | UCX_TEST_END |
universe@334 | 415 | |
universe@334 | 416 | ucx_array_free(&expected); |
universe@334 | 417 | ucx_array_free(&array); |
universe@334 | 418 | } |
universe@334 | 419 | |
universe@334 | 420 | UCX_TEST(test_ucx_array_autogrow) { |
universe@334 | 421 | UcxArray array = ucx_array_new(4, sizeof(int)); |
universe@334 | 422 | array.size = 3; |
universe@334 | 423 | ucx_array_at_int(array, 0) = 47; |
universe@334 | 424 | ucx_array_at_int(array, 1) = 11; |
universe@334 | 425 | int x = 5; |
universe@123 | 426 | |
universe@123 | 427 | UCX_TEST_BEGIN |
universe@334 | 428 | |
universe@334 | 429 | void* oldptr = array.data; |
universe@123 | 430 | |
universe@334 | 431 | ucx_array_append(&array, &x); |
universe@334 | 432 | UCX_TEST_ASSERT(array.capacity == 4 && array.data == oldptr, |
universe@334 | 433 | "array should not grow too early"); |
universe@334 | 434 | ucx_array_append(&array, &x); |
universe@334 | 435 | UCX_TEST_ASSERT(array.capacity == 8, "array did not grow"); |
universe@334 | 436 | UCX_TEST_ASSERT(array.size == 5, "incorrect size after grow"); |
universe@334 | 437 | UCX_TEST_ASSERT(ucx_array_at_int(array, 3) == 5 && |
universe@334 | 438 | ucx_array_at_int(array, 3) == 5, "corrupt data"); |
universe@172 | 439 | |
universe@123 | 440 | UCX_TEST_END |
universe@334 | 441 | ucx_array_free(&array); |
universe@123 | 442 | } |
universe@123 | 443 | |
universe@334 | 444 | UCX_TEST(test_ucx_array_shrink) { |
universe@334 | 445 | UcxArray array = ucx_array_new(16, sizeof(int)); |
universe@334 | 446 | array.size = 4; |
universe@90 | 447 | |
universe@123 | 448 | UCX_TEST_BEGIN |
universe@334 | 449 | UCX_TEST_ASSERT(!ucx_array_shrink(&array), "failed"); |
universe@334 | 450 | UCX_TEST_ASSERT(array.capacity == 4, "incorrect capacity after shrink"); |
universe@334 | 451 | UCX_TEST_END |
universe@334 | 452 | ucx_array_free(&array); |
universe@334 | 453 | } |
universe@334 | 454 | |
universe@334 | 455 | UCX_TEST(test_ucx_array_resize) { |
universe@334 | 456 | UcxArray array = ucx_array_new(16, sizeof(int)); |
universe@334 | 457 | array.size = 8; |
universe@123 | 458 | |
universe@334 | 459 | UCX_TEST_BEGIN |
universe@334 | 460 | |
universe@334 | 461 | UCX_TEST_ASSERT(!ucx_array_resize(&array, 32), "failed"); |
universe@334 | 462 | UCX_TEST_ASSERT(array.capacity == 32, "incorrect capacity after resize"); |
universe@334 | 463 | UCX_TEST_ASSERT(array.size == 8, "incorrect size after resize"); |
universe@334 | 464 | |
universe@334 | 465 | UCX_TEST_ASSERT(!ucx_array_resize(&array, 4), "failed"); |
universe@334 | 466 | UCX_TEST_ASSERT(array.capacity == 4, "incorrect capacity after resize"); |
universe@334 | 467 | UCX_TEST_ASSERT(array.size == 4, "incorrect size after resize"); |
universe@90 | 468 | |
universe@90 | 469 | UCX_TEST_END |
universe@334 | 470 | ucx_array_free(&array); |
universe@90 | 471 | } |
universe@90 | 472 | |
universe@334 | 473 | UCX_TEST(test_ucx_array_reserve) { |
universe@334 | 474 | UcxArray array = ucx_array_new(16, sizeof(int)); |
universe@27 | 475 | |
universe@123 | 476 | UCX_TEST_BEGIN |
universe@334 | 477 | |
universe@334 | 478 | UCX_TEST_ASSERT(!ucx_array_reserve(&array, 4), "failed"); |
universe@334 | 479 | UCX_TEST_ASSERT(array.capacity == 16, "reserve shall not shrink"); |
universe@334 | 480 | |
universe@334 | 481 | UCX_TEST_ASSERT(!ucx_array_resize(&array, 32), "failed"); |
universe@334 | 482 | UCX_TEST_ASSERT(array.capacity == 32, "incorrect capacity after reserve"); |
olaf@162 | 483 | |
universe@33 | 484 | UCX_TEST_END |
universe@334 | 485 | ucx_array_free(&array); |
universe@27 | 486 | } |