Thu, 04 Jul 2019 22:23:15 +0200
implements ucx_array_sort()
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@334 | 121 | UCX_TEST(test_ucx_array_equals) { |
universe@334 | 122 | UcxArray a1 = ucx_array_new(16, sizeof(int)); |
universe@334 | 123 | UcxArray a2 = ucx_array_new(16, sizeof(int)); |
universe@334 | 124 | UcxArray a3 = ucx_array_new(16, sizeof(long int)); |
universe@334 | 125 | UcxArray a4 = ucx_array_new(16, sizeof(int)); |
universe@27 | 126 | |
universe@334 | 127 | a1.size = 5; |
universe@334 | 128 | ucx_array_at_int(a1, 0) = 47; |
universe@334 | 129 | ucx_array_at_int(a1, 1) = 11; |
universe@334 | 130 | ucx_array_at_int(a1, 2) = 0; |
universe@334 | 131 | ucx_array_at_int(a1, 3) = 8; |
universe@334 | 132 | ucx_array_at_int(a1, 4) = 15; |
universe@334 | 133 | a2.size = 5; |
universe@334 | 134 | ucx_array_at_int(a2, 0) = 47; |
universe@334 | 135 | ucx_array_at_int(a2, 1) = 11; |
universe@334 | 136 | ucx_array_at_int(a2, 2) = 0; |
universe@334 | 137 | ucx_array_at_int(a2, 3) = 8; |
universe@334 | 138 | ucx_array_at_int(a2, 4) = 15; |
universe@334 | 139 | a3.size = 5; |
universe@334 | 140 | ucx_array_at_longint(a3, 0) = 47; |
universe@334 | 141 | ucx_array_at_longint(a3, 1) = 11; |
universe@334 | 142 | ucx_array_at_longint(a3, 2) = 0; |
universe@334 | 143 | ucx_array_at_longint(a3, 3) = 8; |
universe@334 | 144 | ucx_array_at_longint(a3, 4) = 15; |
universe@334 | 145 | a4.size = 5; |
universe@334 | 146 | ucx_array_at_int(a4, 0) = 47; |
universe@334 | 147 | ucx_array_at_int(a4, 1) = 11; |
universe@334 | 148 | ucx_array_at_int(a4, 2) = -6; |
universe@334 | 149 | ucx_array_at_int(a4, 3) = 8; |
universe@334 | 150 | ucx_array_at_int(a4, 4) = 15; |
universe@27 | 151 | |
universe@123 | 152 | UCX_TEST_BEGIN |
universe@123 | 153 | |
universe@336 | 154 | UCX_TEST_ASSERT(ucx_array_equals(a1, a2, ucx_cmp_int, NULL), "failed"); |
universe@336 | 155 | UCX_TEST_ASSERT(!ucx_array_equals(a1, a4, ucx_cmp_int, NULL), "failed"); |
universe@336 | 156 | UCX_TEST_ASSERT(!ucx_array_equals(a4, a1, ucx_cmp_int, NULL), "failed"); |
universe@336 | 157 | UCX_TEST_ASSERT(!ucx_array_equals(a1, a3, ucx_cmp_int, NULL), |
universe@336 | 158 | "comparing arrays of different element size shall fail"); |
universe@336 | 159 | UCX_TEST_ASSERT(!ucx_array_equals(a3, a1, ucx_cmp_int, NULL), |
universe@336 | 160 | "comparing arrays of different element size shall fail"); |
universe@334 | 161 | |
universe@336 | 162 | UCX_TEST_ASSERT(ucx_array_equals(a1, a2, NULL, NULL), |
universe@334 | 163 | "compare using memcmp() failed"); |
universe@336 | 164 | UCX_TEST_ASSERT(!ucx_array_equals(a1, a4, NULL, NULL), |
universe@334 | 165 | "compare using memcmp() failed"); |
universe@27 | 166 | |
universe@33 | 167 | UCX_TEST_END |
universe@334 | 168 | ucx_array_free(&a1); |
universe@334 | 169 | ucx_array_free(&a2); |
universe@334 | 170 | ucx_array_free(&a3); |
universe@334 | 171 | ucx_array_free(&a4); |
olaf@9 | 172 | } |
olaf@11 | 173 | |
universe@334 | 174 | UCX_TEST(test_ucx_array_concat) { |
universe@334 | 175 | UcxArray a1 = ucx_array_new(16, sizeof(int)); |
universe@334 | 176 | UcxArray a2 = ucx_array_new(16, sizeof(int)); |
universe@334 | 177 | |
universe@334 | 178 | a1.size = 2; |
universe@334 | 179 | ucx_array_at_int(a1, 0) = 47; |
universe@334 | 180 | ucx_array_at_int(a1, 1) = 11; |
universe@334 | 181 | a2.size = 3; |
universe@334 | 182 | ucx_array_at_int(a2, 0) = 0; |
universe@334 | 183 | ucx_array_at_int(a2, 1) = 8; |
universe@334 | 184 | ucx_array_at_int(a2, 2) = 15; |
universe@27 | 185 | |
universe@123 | 186 | UCX_TEST_BEGIN |
universe@123 | 187 | |
universe@334 | 188 | UCX_TEST_ASSERT(!ucx_array_concat(&a1, &a2), "failed"); |
universe@334 | 189 | UCX_TEST_ASSERT(a1.size == 5, "failed"); |
universe@334 | 190 | UCX_TEST_ASSERT(ucx_array_at_int(a1, 0) == 47, "failed"); |
universe@334 | 191 | UCX_TEST_ASSERT(ucx_array_at_int(a1, 1) == 11, "failed"); |
universe@334 | 192 | UCX_TEST_ASSERT(ucx_array_at_int(a1, 2) == 0, "failed"); |
universe@334 | 193 | UCX_TEST_ASSERT(ucx_array_at_int(a1, 3) == 8, "failed"); |
universe@334 | 194 | UCX_TEST_ASSERT(ucx_array_at_int(a1, 4) == 15, "failed"); |
universe@27 | 195 | |
universe@334 | 196 | a1.elemsize *= 2; |
universe@334 | 197 | UCX_TEST_ASSERT(ucx_array_concat(&a1, &a2), |
universe@334 | 198 | "arrays of different element size must not be concatenated"); |
universe@334 | 199 | UCX_TEST_ASSERT(a1.size == 5, |
universe@334 | 200 | "arrays of different element size must not be concatenated"); |
universe@27 | 201 | |
universe@33 | 202 | UCX_TEST_END |
universe@334 | 203 | ucx_array_free(&a1); |
universe@334 | 204 | ucx_array_free(&a2); |
universe@27 | 205 | } |
universe@27 | 206 | |
universe@334 | 207 | UCX_TEST(test_ucx_array_at) { |
universe@334 | 208 | UcxArray array = ucx_array_new(16, sizeof(int)); |
universe@334 | 209 | |
universe@334 | 210 | int x = 42; |
universe@334 | 211 | ucx_array_append(&array, &x); |
universe@334 | 212 | x = 13; |
universe@334 | 213 | ucx_array_append(&array, &x); |
universe@334 | 214 | x = 5; |
universe@334 | 215 | ucx_array_append(&array, &x); |
universe@27 | 216 | |
universe@123 | 217 | UCX_TEST_BEGIN |
universe@123 | 218 | |
universe@334 | 219 | UCX_TEST_ASSERT(ucx_array_at_int(array, 1) == 13, "failed"); |
universe@334 | 220 | ucx_array_at_int(array, 1) = 80; |
universe@334 | 221 | UCX_TEST_ASSERT(ucx_array_at_int(array, 1) == 80, "assignment failed"); |
universe@27 | 222 | |
universe@334 | 223 | |
universe@334 | 224 | UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 42, "corrupted data"); |
universe@334 | 225 | UCX_TEST_ASSERT(ucx_array_at_int(array, 2) == 5, "corrupted data"); |
universe@27 | 226 | |
universe@33 | 227 | UCX_TEST_END |
universe@334 | 228 | |
universe@334 | 229 | ucx_array_free(&array); |
universe@27 | 230 | } |
universe@27 | 231 | |
universe@334 | 232 | UCX_TEST(test_ucx_array_find) { |
universe@334 | 233 | UcxArray array = ucx_array_new(16, sizeof(int)); |
universe@334 | 234 | |
universe@334 | 235 | array.size = 5; |
universe@334 | 236 | ucx_array_at_int(array, 0) = 47; |
universe@334 | 237 | ucx_array_at_int(array, 1) = 11; |
universe@334 | 238 | ucx_array_at_int(array, 2) = 0; |
universe@334 | 239 | ucx_array_at_int(array, 3) = 8; |
universe@334 | 240 | ucx_array_at_int(array, 4) = 15; |
universe@334 | 241 | |
universe@334 | 242 | int x = 8; |
universe@334 | 243 | int y = 90; |
universe@27 | 244 | |
universe@123 | 245 | UCX_TEST_BEGIN |
universe@123 | 246 | |
universe@334 | 247 | UCX_TEST_ASSERT(ucx_array_find(array,(void*)&x,ucx_cmp_int,NULL) == 3, |
universe@334 | 248 | "doesn't find element"); |
universe@334 | 249 | UCX_TEST_ASSERT(ucx_array_find(array,(void*)&y,ucx_cmp_int,NULL) == 5, |
universe@334 | 250 | "finds non-existing element"); |
universe@27 | 251 | |
universe@334 | 252 | UCX_TEST_ASSERT(ucx_array_find(array,(void*)&x,NULL,NULL) == 3, |
universe@334 | 253 | "failed using memcmp()"); |
universe@334 | 254 | UCX_TEST_ASSERT(ucx_array_find(array,(void*)&y,NULL,NULL) == 5, |
universe@334 | 255 | "failed using memcmp()"); |
universe@27 | 256 | |
universe@33 | 257 | UCX_TEST_END |
universe@334 | 258 | ucx_array_free(&array); |
universe@27 | 259 | } |
universe@27 | 260 | |
universe@334 | 261 | UCX_TEST(test_ucx_array_contains) { |
universe@334 | 262 | UcxArray array = ucx_array_new(16, sizeof(int)); |
universe@334 | 263 | |
universe@334 | 264 | array.size = 5; |
universe@334 | 265 | ucx_array_at_int(array, 0) = 47; |
universe@334 | 266 | ucx_array_at_int(array, 1) = 11; |
universe@334 | 267 | ucx_array_at_int(array, 2) = 0; |
universe@334 | 268 | ucx_array_at_int(array, 3) = 8; |
universe@334 | 269 | ucx_array_at_int(array, 4) = 15; |
universe@334 | 270 | |
universe@334 | 271 | int x = 8; |
universe@334 | 272 | int y = 90; |
universe@123 | 273 | |
universe@123 | 274 | UCX_TEST_BEGIN |
universe@123 | 275 | |
universe@334 | 276 | UCX_TEST_ASSERT(ucx_array_contains(array,(void*)&x,ucx_cmp_int,NULL), |
universe@334 | 277 | "false negative"); |
universe@334 | 278 | UCX_TEST_ASSERT(!ucx_array_contains(array,(void*)&y,ucx_cmp_int,NULL), |
universe@334 | 279 | "false positive"); |
universe@123 | 280 | |
universe@334 | 281 | UCX_TEST_ASSERT(ucx_array_contains(array,(void*)&x,NULL,NULL), |
universe@334 | 282 | "false negative using memcmp()"); |
universe@334 | 283 | UCX_TEST_ASSERT(!ucx_array_contains(array,(void*)&y,NULL,NULL), |
universe@334 | 284 | "false positive using memcmp()"); |
universe@123 | 285 | |
universe@123 | 286 | UCX_TEST_END |
universe@334 | 287 | ucx_array_free(&array); |
universe@123 | 288 | } |
universe@123 | 289 | |
universe@334 | 290 | UCX_TEST(test_ucx_array_remove) { |
universe@334 | 291 | UcxArray array = ucx_array_new(16, sizeof(int)); |
universe@334 | 292 | |
universe@334 | 293 | array.size = 5; |
universe@334 | 294 | ucx_array_at_int(array, 0) = 47; |
universe@334 | 295 | ucx_array_at_int(array, 1) = 11; |
universe@334 | 296 | ucx_array_at_int(array, 2) = 0; |
universe@334 | 297 | ucx_array_at_int(array, 3) = 8; |
universe@334 | 298 | ucx_array_at_int(array, 4) = 15; |
universe@334 | 299 | |
universe@334 | 300 | UCX_TEST_BEGIN |
universe@334 | 301 | |
universe@334 | 302 | ucx_array_remove(&array, 2); |
universe@334 | 303 | UCX_TEST_ASSERT( |
universe@334 | 304 | ucx_array_at_int(array, 0) == 47 && |
universe@334 | 305 | ucx_array_at_int(array, 1) == 11 && |
universe@334 | 306 | ucx_array_at_int(array, 2) == 8 && |
universe@334 | 307 | ucx_array_at_int(array, 3) == 15, |
universe@334 | 308 | "wrong contents after remove"); |
universe@334 | 309 | UCX_TEST_ASSERT(array.size == 4, "wrong size after remove"); |
universe@334 | 310 | |
universe@334 | 311 | ucx_array_remove_fast(&array, 1); |
universe@334 | 312 | UCX_TEST_ASSERT( |
universe@334 | 313 | ucx_array_at_int(array, 0) == 47 && |
universe@334 | 314 | ucx_array_at_int(array, 1) == 15 && |
universe@334 | 315 | ucx_array_at_int(array, 2) == 8, |
universe@334 | 316 | "wrong contents after fast remove"); |
universe@334 | 317 | UCX_TEST_ASSERT(array.size == 3, "wrong size after fast remove"); |
universe@334 | 318 | |
universe@334 | 319 | UCX_TEST_END |
universe@334 | 320 | ucx_array_free(&array); |
universe@334 | 321 | } |
universe@334 | 322 | |
universe@334 | 323 | UCX_TEST(test_ucx_array_clone) { |
universe@334 | 324 | |
universe@334 | 325 | UcxArray array = ucx_array_new(16, sizeof(int)); |
universe@334 | 326 | |
universe@334 | 327 | array.size = 5; |
universe@334 | 328 | ucx_array_at_int(array, 0) = 47; |
universe@334 | 329 | ucx_array_at_int(array, 1) = 11; |
universe@334 | 330 | ucx_array_at_int(array, 2) = 0; |
universe@334 | 331 | ucx_array_at_int(array, 3) = 8; |
universe@334 | 332 | ucx_array_at_int(array, 4) = 15; |
universe@334 | 333 | |
universe@334 | 334 | UcxArray copy = ucx_array_clone(array); |
universe@334 | 335 | UCX_TEST_BEGIN |
universe@334 | 336 | |
universe@334 | 337 | UCX_TEST_ASSERT(array.data != copy.data, "no true copy"); |
universe@334 | 338 | UCX_TEST_ASSERT(array.size == copy.size, "size mismatch"); |
universe@334 | 339 | UCX_TEST_ASSERT(array.capacity == copy.capacity, "capacity mismatch"); |
universe@334 | 340 | UCX_TEST_ASSERT(array.elemsize == copy.elemsize, "element size mismatch"); |
universe@334 | 341 | UCX_TEST_ASSERT(array.allocator == copy.allocator, "allocator mismatch"); |
universe@336 | 342 | UCX_TEST_ASSERT(ucx_array_equals(array, copy, ucx_cmp_int, NULL), "failed"); |
universe@334 | 343 | |
universe@334 | 344 | UCX_TEST_END |
universe@334 | 345 | |
universe@334 | 346 | ucx_array_free(&array); |
universe@334 | 347 | ucx_array_free(©); |
universe@334 | 348 | } |
universe@334 | 349 | |
universe@334 | 350 | UCX_TEST(test_ucx_array_sort) { |
universe@334 | 351 | UcxArray array = ucx_array_new(16, sizeof(int)); |
universe@334 | 352 | array.size = 5; |
universe@334 | 353 | ucx_array_at_int(array, 0) = 47; |
universe@334 | 354 | ucx_array_at_int(array, 1) = 11; |
universe@334 | 355 | ucx_array_at_int(array, 2) = 0; |
universe@334 | 356 | ucx_array_at_int(array, 3) = 8; |
universe@334 | 357 | ucx_array_at_int(array, 4) = 15; |
universe@334 | 358 | |
universe@334 | 359 | UcxArray expected = ucx_array_new(16, sizeof(int)); |
universe@334 | 360 | expected.size = 5; |
universe@334 | 361 | ucx_array_at_int(expected, 0) = 0; |
universe@334 | 362 | ucx_array_at_int(expected, 1) = 8; |
universe@334 | 363 | ucx_array_at_int(expected, 2) = 11; |
universe@334 | 364 | ucx_array_at_int(expected, 3) = 15; |
universe@334 | 365 | ucx_array_at_int(expected, 4) = 47; |
universe@334 | 366 | |
universe@334 | 367 | |
universe@334 | 368 | UCX_TEST_BEGIN |
universe@334 | 369 | void* original_ptr = array.data; |
universe@336 | 370 | ucx_array_sort(array, ucx_cmp_int, NULL); |
universe@336 | 371 | UCX_TEST_ASSERT(ucx_array_equals(array, expected, NULL, NULL), "failed"); |
universe@334 | 372 | UCX_TEST_ASSERT(array.size == 5, "size corrupted"); |
universe@334 | 373 | UCX_TEST_ASSERT(array.data == original_ptr, "shall not reallocate"); |
universe@336 | 374 | |
universe@336 | 375 | ucx_array_reserve(&array, 32); |
universe@336 | 376 | ucx_array_reserve(&expected, 32); |
universe@336 | 377 | array.size = expected.size = 32; |
universe@336 | 378 | for (size_t i = 0 ; i < 32 ; i++) { |
universe@336 | 379 | ucx_array_at_int(array, i) = ((i%2==0)?-1:1) * ((int) i); |
universe@336 | 380 | ucx_array_at_int(expected, i) = (-30+2*i) - (i > 15 ? 1 : 0); |
universe@336 | 381 | } |
universe@336 | 382 | |
universe@336 | 383 | ucx_array_sort(array, ucx_cmp_int, NULL); |
universe@336 | 384 | UCX_TEST_ASSERT(ucx_array_equals(array, expected, NULL, NULL), |
universe@336 | 385 | "failed for bigger arrays"); |
universe@334 | 386 | UCX_TEST_END |
universe@334 | 387 | |
universe@334 | 388 | ucx_array_free(&expected); |
universe@334 | 389 | ucx_array_free(&array); |
universe@334 | 390 | } |
universe@334 | 391 | |
universe@334 | 392 | UCX_TEST(test_ucx_array_autogrow) { |
universe@334 | 393 | UcxArray array = ucx_array_new(4, sizeof(int)); |
universe@334 | 394 | array.size = 3; |
universe@334 | 395 | ucx_array_at_int(array, 0) = 47; |
universe@334 | 396 | ucx_array_at_int(array, 1) = 11; |
universe@334 | 397 | int x = 5; |
universe@123 | 398 | |
universe@123 | 399 | UCX_TEST_BEGIN |
universe@334 | 400 | |
universe@334 | 401 | void* oldptr = array.data; |
universe@123 | 402 | |
universe@334 | 403 | ucx_array_append(&array, &x); |
universe@334 | 404 | UCX_TEST_ASSERT(array.capacity == 4 && array.data == oldptr, |
universe@334 | 405 | "array should not grow too early"); |
universe@334 | 406 | ucx_array_append(&array, &x); |
universe@334 | 407 | UCX_TEST_ASSERT(array.capacity == 8, "array did not grow"); |
universe@334 | 408 | UCX_TEST_ASSERT(array.size == 5, "incorrect size after grow"); |
universe@334 | 409 | UCX_TEST_ASSERT(ucx_array_at_int(array, 3) == 5 && |
universe@334 | 410 | ucx_array_at_int(array, 3) == 5, "corrupt data"); |
universe@172 | 411 | |
universe@123 | 412 | UCX_TEST_END |
universe@334 | 413 | ucx_array_free(&array); |
universe@123 | 414 | } |
universe@123 | 415 | |
universe@334 | 416 | UCX_TEST(test_ucx_array_shrink) { |
universe@334 | 417 | UcxArray array = ucx_array_new(16, sizeof(int)); |
universe@334 | 418 | array.size = 4; |
universe@90 | 419 | |
universe@123 | 420 | UCX_TEST_BEGIN |
universe@334 | 421 | UCX_TEST_ASSERT(!ucx_array_shrink(&array), "failed"); |
universe@334 | 422 | UCX_TEST_ASSERT(array.capacity == 4, "incorrect capacity after shrink"); |
universe@334 | 423 | UCX_TEST_END |
universe@334 | 424 | ucx_array_free(&array); |
universe@334 | 425 | } |
universe@334 | 426 | |
universe@334 | 427 | UCX_TEST(test_ucx_array_resize) { |
universe@334 | 428 | UcxArray array = ucx_array_new(16, sizeof(int)); |
universe@334 | 429 | array.size = 8; |
universe@123 | 430 | |
universe@334 | 431 | UCX_TEST_BEGIN |
universe@334 | 432 | |
universe@334 | 433 | UCX_TEST_ASSERT(!ucx_array_resize(&array, 32), "failed"); |
universe@334 | 434 | UCX_TEST_ASSERT(array.capacity == 32, "incorrect capacity after resize"); |
universe@334 | 435 | UCX_TEST_ASSERT(array.size == 8, "incorrect size after resize"); |
universe@334 | 436 | |
universe@334 | 437 | UCX_TEST_ASSERT(!ucx_array_resize(&array, 4), "failed"); |
universe@334 | 438 | UCX_TEST_ASSERT(array.capacity == 4, "incorrect capacity after resize"); |
universe@334 | 439 | UCX_TEST_ASSERT(array.size == 4, "incorrect size after resize"); |
universe@90 | 440 | |
universe@90 | 441 | UCX_TEST_END |
universe@334 | 442 | ucx_array_free(&array); |
universe@90 | 443 | } |
universe@90 | 444 | |
universe@334 | 445 | UCX_TEST(test_ucx_array_reserve) { |
universe@334 | 446 | UcxArray array = ucx_array_new(16, sizeof(int)); |
universe@27 | 447 | |
universe@123 | 448 | UCX_TEST_BEGIN |
universe@334 | 449 | |
universe@334 | 450 | UCX_TEST_ASSERT(!ucx_array_reserve(&array, 4), "failed"); |
universe@334 | 451 | UCX_TEST_ASSERT(array.capacity == 16, "reserve shall not shrink"); |
universe@334 | 452 | |
universe@334 | 453 | UCX_TEST_ASSERT(!ucx_array_resize(&array, 32), "failed"); |
universe@334 | 454 | UCX_TEST_ASSERT(array.capacity == 32, "incorrect capacity after reserve"); |
olaf@162 | 455 | |
universe@33 | 456 | UCX_TEST_END |
universe@334 | 457 | ucx_array_free(&array); |
universe@27 | 458 | } |