test/array_tests.c

branch
feature/array
changeset 342
8f0a3c00d1d2
parent 337
f695ae118460
child 344
320b962afaf9
     1.1 --- a/test/array_tests.c	Thu Jul 11 10:11:43 2019 +0200
     1.2 +++ b/test/array_tests.c	Tue Aug 06 16:26:46 2019 +0200
     1.3 @@ -58,28 +58,32 @@
     1.4  
     1.5  UCX_TEST(test_ucx_array_append) {
     1.6      UcxArray array = ucx_array_new(16, sizeof(int));
     1.7 +    int *elements;
     1.8      
     1.9      int x = 42;
    1.10      ucx_array_append(&array, &x);
    1.11      UCX_TEST_BEGIN
    1.12      
    1.13 -    UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 42, "failed");
    1.14 +    elements = array.data;
    1.15 +    UCX_TEST_ASSERT(elements[0] == 42, "failed");
    1.16      
    1.17      x = 13;
    1.18      ucx_array_append(&array, &x);
    1.19      
    1.20 +    elements = array.data;
    1.21      UCX_TEST_ASSERT(array.size == 2, "incorrect size after append");
    1.22 -    UCX_TEST_ASSERT(ucx_array_at_int(array, 1) == 13, "failed");
    1.23 -    UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 42,
    1.24 +    UCX_TEST_ASSERT(elements[1] == 13, "failed");
    1.25 +    UCX_TEST_ASSERT(elements[0] == 42,
    1.26              "append corrupted previously inserted data");
    1.27      
    1.28      ucx_array_append(&array, NULL);
    1.29      
    1.30 +    elements = array.data;
    1.31      UCX_TEST_ASSERT(array.size == 3, "incorrect size after NULL append");
    1.32 -    UCX_TEST_ASSERT(ucx_array_at_int(array, 2) == 0, "element is not zeroed");
    1.33 -    UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 42,
    1.34 +    UCX_TEST_ASSERT(elements[2] == 0, "element is not zeroed");
    1.35 +    UCX_TEST_ASSERT(elements[0] == 42,
    1.36              "NULL append corrupted previously inserted data");
    1.37 -    UCX_TEST_ASSERT(ucx_array_at_int(array, 1) == 13,
    1.38 +    UCX_TEST_ASSERT(elements[1] == 13,
    1.39              "NULL append corrupted previously inserted data");
    1.40      
    1.41      UCX_TEST_END
    1.42 @@ -88,29 +92,33 @@
    1.43  }
    1.44  
    1.45  UCX_TEST(test_ucx_array_prepend) {
    1.46 +    int *elems;
    1.47      UcxArray array = ucx_array_new(16, sizeof(int));
    1.48      
    1.49      int x = 42;
    1.50      ucx_array_prepend(&array, &x);
    1.51      UCX_TEST_BEGIN
    1.52      
    1.53 -    UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 42, "failed");
    1.54 +    elems = array.data;
    1.55 +    UCX_TEST_ASSERT(elems[0] == 42, "failed");
    1.56      
    1.57      x = 13;
    1.58      ucx_array_prepend(&array, &x);
    1.59      
    1.60 +    elems = array.data;
    1.61      UCX_TEST_ASSERT(array.size == 2, "incorrect size after prepend");
    1.62 -    UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 13, "failed");
    1.63 -    UCX_TEST_ASSERT(ucx_array_at_int(array, 1) == 42,
    1.64 +    UCX_TEST_ASSERT(elems[0] == 13, "failed");
    1.65 +    UCX_TEST_ASSERT(elems[1] == 42,
    1.66              "prepend corrupted previously inserted data");
    1.67      
    1.68      ucx_array_prepend(&array, NULL);
    1.69      
    1.70 +    elems = array.data;
    1.71      UCX_TEST_ASSERT(array.size == 3, "incorrect size after NULL prepend");
    1.72 -    UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 0, "element is not zeroed");
    1.73 -    UCX_TEST_ASSERT(ucx_array_at_int(array, 1) == 13,
    1.74 +    UCX_TEST_ASSERT(elems[0] == 0, "element is not zeroed");
    1.75 +    UCX_TEST_ASSERT(elems[1] == 13,
    1.76              "NULL prepend corrupted previously inserted data");
    1.77 -    UCX_TEST_ASSERT(ucx_array_at_int(array, 2) == 42,
    1.78 +    UCX_TEST_ASSERT(elems[2] == 42,
    1.79              "NULL prepend corrupted previously inserted data");
    1.80      
    1.81      UCX_TEST_END
    1.82 @@ -119,27 +127,33 @@
    1.83  }
    1.84  
    1.85  UCX_TEST(test_ucx_array_set) {
    1.86 +    int *elems;
    1.87      UcxArray array = ucx_array_new(16, sizeof(int));
    1.88      
    1.89 +    
    1.90      int x = 42;
    1.91  
    1.92      UCX_TEST_BEGIN
    1.93  
    1.94      ucx_array_set(&array, 7, &x);
    1.95 -    UCX_TEST_ASSERT(ucx_array_at_int(array, 7) == 42, "failed");
    1.96 +    
    1.97 +    elems = array.data;
    1.98 +    UCX_TEST_ASSERT(elems[7] == 42, "failed");
    1.99      UCX_TEST_ASSERT(array.size >= 8, "array not resized on set");
   1.100      UCX_TEST_ASSERT(array.capacity == 16, "capacity changed unnecessarily");
   1.101      
   1.102      x = 13;
   1.103      ucx_array_set(&array, 27, &x);
   1.104      
   1.105 -    UCX_TEST_ASSERT(ucx_array_at_int(array, 27) == 13, "failed");
   1.106 +    elems = array.data;
   1.107 +    UCX_TEST_ASSERT(elems[27] == 13, "failed");
   1.108      UCX_TEST_ASSERT(array.size == 28, "array not resized on set");
   1.109      UCX_TEST_ASSERT(array.capacity == 28, "capacity not grown");
   1.110      
   1.111      ucx_array_set(&array, 7, NULL);
   1.112      
   1.113 -    UCX_TEST_ASSERT(ucx_array_at_int(array, 7) == 0, "not zeroed on NULL set");
   1.114 +    elems = array.data;
   1.115 +    UCX_TEST_ASSERT(elems[7] == 0, "not zeroed on NULL set");
   1.116      
   1.117      UCX_TEST_END
   1.118      
   1.119 @@ -152,30 +166,37 @@
   1.120      UcxArray a3 = ucx_array_new(16, sizeof(long int));
   1.121      UcxArray a4 = ucx_array_new(16, sizeof(int));
   1.122      
   1.123 +    int *intelems;
   1.124 +    long int *longintelems;
   1.125 +    
   1.126      a1.size = 5;
   1.127 -    ucx_array_at_int(a1, 0) = 47;
   1.128 -    ucx_array_at_int(a1, 1) = 11;
   1.129 -    ucx_array_at_int(a1, 2) = 0;
   1.130 -    ucx_array_at_int(a1, 3) = 8;
   1.131 -    ucx_array_at_int(a1, 4) = 15;
   1.132 +    intelems = a1.data;
   1.133 +    intelems[0] = 47;
   1.134 +    intelems[1] = 11;
   1.135 +    intelems[2] = 0;
   1.136 +    intelems[3] = 8;
   1.137 +    intelems[4] = 15;
   1.138      a2.size = 5;
   1.139 -    ucx_array_at_int(a2, 0) = 47;
   1.140 -    ucx_array_at_int(a2, 1) = 11;
   1.141 -    ucx_array_at_int(a2, 2) = 0;
   1.142 -    ucx_array_at_int(a2, 3) = 8;
   1.143 -    ucx_array_at_int(a2, 4) = 15;
   1.144 +    intelems = a2.data;
   1.145 +    intelems[0] = 47;
   1.146 +    intelems[1] = 11;
   1.147 +    intelems[2] = 0;
   1.148 +    intelems[3] = 8;
   1.149 +    intelems[4] = 15;
   1.150      a3.size = 5;
   1.151 -    ucx_array_at_longint(a3, 0) = 47;
   1.152 -    ucx_array_at_longint(a3, 1) = 11;
   1.153 -    ucx_array_at_longint(a3, 2) = 0;
   1.154 -    ucx_array_at_longint(a3, 3) = 8;
   1.155 -    ucx_array_at_longint(a3, 4) = 15;
   1.156 +    longintelems = a3.data;
   1.157 +    longintelems[0] = 47;
   1.158 +    longintelems[1] = 11;
   1.159 +    longintelems[2] = 0;
   1.160 +    longintelems[3] = 8;
   1.161 +    longintelems[4] = 15;
   1.162      a4.size = 5;
   1.163 -    ucx_array_at_int(a4, 0) = 47;
   1.164 -    ucx_array_at_int(a4, 1) = 11;
   1.165 -    ucx_array_at_int(a4, 2) = -6;
   1.166 -    ucx_array_at_int(a4, 3) = 8;
   1.167 -    ucx_array_at_int(a4, 4) = 15;
   1.168 +    intelems = a4.data;
   1.169 +    intelems[0] = 47;
   1.170 +    intelems[1] = 11;
   1.171 +    intelems[2] = -6;
   1.172 +    intelems[3] = 8;
   1.173 +    intelems[4] = 15;
   1.174      
   1.175      UCX_TEST_BEGIN
   1.176      
   1.177 @@ -202,24 +223,28 @@
   1.178  UCX_TEST(test_ucx_array_concat) {
   1.179      UcxArray a1 = ucx_array_new(16, sizeof(int));
   1.180      UcxArray a2 = ucx_array_new(16, sizeof(int));
   1.181 +    int *elems;
   1.182      
   1.183      a1.size = 2;
   1.184 -    ucx_array_at_int(a1, 0) = 47;
   1.185 -    ucx_array_at_int(a1, 1) = 11;
   1.186 +    elems = a1.data;
   1.187 +    elems[0] = 47;
   1.188 +    elems[1] = 11;
   1.189      a2.size = 3;
   1.190 -    ucx_array_at_int(a2, 0) = 0;
   1.191 -    ucx_array_at_int(a2, 1) = 8;
   1.192 -    ucx_array_at_int(a2, 2) = 15;
   1.193 +    elems = a2.data;
   1.194 +    elems[0] = 0;
   1.195 +    elems[1] = 8;
   1.196 +    elems[2] = 15;
   1.197      
   1.198      UCX_TEST_BEGIN
   1.199      
   1.200      UCX_TEST_ASSERT(!ucx_array_concat(&a1, &a2), "failed");
   1.201      UCX_TEST_ASSERT(a1.size == 5, "failed");
   1.202 -    UCX_TEST_ASSERT(ucx_array_at_int(a1, 0) == 47, "failed");
   1.203 -    UCX_TEST_ASSERT(ucx_array_at_int(a1, 1) == 11, "failed");
   1.204 -    UCX_TEST_ASSERT(ucx_array_at_int(a1, 2) == 0, "failed");
   1.205 -    UCX_TEST_ASSERT(ucx_array_at_int(a1, 3) == 8, "failed");
   1.206 -    UCX_TEST_ASSERT(ucx_array_at_int(a1, 4) == 15, "failed");
   1.207 +    elems = a1.data;
   1.208 +    UCX_TEST_ASSERT(elems[0] == 47, "failed");
   1.209 +    UCX_TEST_ASSERT(elems[1] == 11, "failed");
   1.210 +    UCX_TEST_ASSERT(elems[2] == 0, "failed");
   1.211 +    UCX_TEST_ASSERT(elems[3] == 8, "failed");
   1.212 +    UCX_TEST_ASSERT(elems[4] == 15, "failed");
   1.213      
   1.214      a1.elemsize *= 2;
   1.215      UCX_TEST_ASSERT(ucx_array_concat(&a1, &a2),
   1.216 @@ -232,40 +257,17 @@
   1.217      ucx_array_free(&a2);    
   1.218  }
   1.219  
   1.220 -UCX_TEST(test_ucx_array_at) {
   1.221 -    UcxArray array = ucx_array_new(16, sizeof(int));
   1.222 -    
   1.223 -    int x = 42;
   1.224 -    ucx_array_append(&array, &x);
   1.225 -    x = 13;
   1.226 -    ucx_array_append(&array, &x);
   1.227 -    x = 5;
   1.228 -    ucx_array_append(&array, &x);
   1.229 -    
   1.230 -    UCX_TEST_BEGIN
   1.231 -    
   1.232 -    UCX_TEST_ASSERT(ucx_array_at_int(array, 1) == 13, "failed");
   1.233 -    ucx_array_at_int(array, 1) = 80;
   1.234 -    UCX_TEST_ASSERT(ucx_array_at_int(array, 1) == 80, "assignment failed");
   1.235 -    
   1.236 -    
   1.237 -    UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 42, "corrupted data");
   1.238 -    UCX_TEST_ASSERT(ucx_array_at_int(array, 2) == 5, "corrupted data");
   1.239 -    
   1.240 -    UCX_TEST_END
   1.241 -    
   1.242 -    ucx_array_free(&array);
   1.243 -}
   1.244 -
   1.245  UCX_TEST(test_ucx_array_find) {
   1.246      UcxArray array = ucx_array_new(16, sizeof(int));
   1.247 +    int *elems;
   1.248      
   1.249      array.size = 5;
   1.250 -    ucx_array_at_int(array, 0) = 47;
   1.251 -    ucx_array_at_int(array, 1) = 11;
   1.252 -    ucx_array_at_int(array, 2) = 0;
   1.253 -    ucx_array_at_int(array, 3) = 8;
   1.254 -    ucx_array_at_int(array, 4) = 15;
   1.255 +    elems = array.data;
   1.256 +    elems[0] = 47;
   1.257 +    elems[1] = 11;
   1.258 +    elems[2] = 0;
   1.259 +    elems[3] = 8;
   1.260 +    elems[4] = 15;
   1.261      
   1.262      int x = 8;
   1.263      int y = 90;
   1.264 @@ -288,13 +290,15 @@
   1.265  
   1.266  UCX_TEST(test_ucx_array_contains) {
   1.267      UcxArray array = ucx_array_new(16, sizeof(int));
   1.268 +    int *elems;
   1.269      
   1.270      array.size = 5;
   1.271 -    ucx_array_at_int(array, 0) = 47;
   1.272 -    ucx_array_at_int(array, 1) = 11;
   1.273 -    ucx_array_at_int(array, 2) = 0;
   1.274 -    ucx_array_at_int(array, 3) = 8;
   1.275 -    ucx_array_at_int(array, 4) = 15;
   1.276 +    elems = array.data;
   1.277 +    elems[0] = 47;
   1.278 +    elems[1] = 11;
   1.279 +    elems[2] = 0;
   1.280 +    elems[3] = 8;
   1.281 +    elems[4] = 15;
   1.282      
   1.283      int x = 8;
   1.284      int y = 90;
   1.285 @@ -317,30 +321,34 @@
   1.286  
   1.287  UCX_TEST(test_ucx_array_remove) {
   1.288      UcxArray array = ucx_array_new(16, sizeof(int));
   1.289 +    int *elems;
   1.290      
   1.291      array.size = 5;
   1.292 -    ucx_array_at_int(array, 0) = 47;
   1.293 -    ucx_array_at_int(array, 1) = 11;
   1.294 -    ucx_array_at_int(array, 2) = 0;
   1.295 -    ucx_array_at_int(array, 3) = 8;
   1.296 -    ucx_array_at_int(array, 4) = 15;
   1.297 +    elems = array.data;
   1.298 +    elems[0] = 47;
   1.299 +    elems[1] = 11;
   1.300 +    elems[2] = 0;
   1.301 +    elems[3] = 8;
   1.302 +    elems[4] = 15;
   1.303          
   1.304      UCX_TEST_BEGIN
   1.305      
   1.306      ucx_array_remove(&array, 2);
   1.307 +    elems = array.data;
   1.308      UCX_TEST_ASSERT(
   1.309 -            ucx_array_at_int(array, 0) == 47 &&
   1.310 -            ucx_array_at_int(array, 1) == 11 &&
   1.311 -            ucx_array_at_int(array, 2) == 8 &&
   1.312 -            ucx_array_at_int(array, 3) == 15,
   1.313 +            elems[0] == 47 &&
   1.314 +            elems[1] == 11 &&
   1.315 +            elems[2] == 8 &&
   1.316 +            elems[3] == 15,
   1.317              "wrong contents after remove");
   1.318      UCX_TEST_ASSERT(array.size == 4, "wrong size after remove");
   1.319      
   1.320      ucx_array_remove_fast(&array, 1);
   1.321 +    elems = array.data;
   1.322      UCX_TEST_ASSERT(
   1.323 -            ucx_array_at_int(array, 0) == 47 &&
   1.324 -            ucx_array_at_int(array, 1) == 15 &&
   1.325 -            ucx_array_at_int(array, 2) == 8,
   1.326 +            elems[0] == 47 &&
   1.327 +            elems[1] == 15 &&
   1.328 +            elems[2] == 8,
   1.329              "wrong contents after fast remove");
   1.330      UCX_TEST_ASSERT(array.size == 3, "wrong size after fast remove");
   1.331      
   1.332 @@ -349,15 +357,16 @@
   1.333  }
   1.334  
   1.335  UCX_TEST(test_ucx_array_clone) {
   1.336 -   
   1.337      UcxArray array = ucx_array_new(16, sizeof(int));
   1.338 +    int *elems;
   1.339      
   1.340      array.size = 5;
   1.341 -    ucx_array_at_int(array, 0) = 47;
   1.342 -    ucx_array_at_int(array, 1) = 11;
   1.343 -    ucx_array_at_int(array, 2) = 0;
   1.344 -    ucx_array_at_int(array, 3) = 8;
   1.345 -    ucx_array_at_int(array, 4) = 15;
   1.346 +    elems = array.data;
   1.347 +    elems[0] = 47;
   1.348 +    elems[1] = 11;
   1.349 +    elems[2] = 0;
   1.350 +    elems[3] = 8;
   1.351 +    elems[4] = 15;
   1.352      
   1.353      UcxArray copy = ucx_array_clone(array);
   1.354      UCX_TEST_BEGIN
   1.355 @@ -375,22 +384,39 @@
   1.356      ucx_array_free(&copy);
   1.357  }
   1.358  
   1.359 +static int ucx_cmp_int_reverse(const void* x, const void* y, void* data) {
   1.360 +    return -ucx_cmp_int(x,y,data);
   1.361 +}
   1.362 +
   1.363  UCX_TEST(test_ucx_array_sort) {
   1.364 -    UcxArray array = ucx_array_new(16, sizeof(int));
   1.365 +    int *elems;
   1.366 +
   1.367 +    UcxArray array = ucx_array_new(16, sizeof(int));    
   1.368      array.size = 5;
   1.369 -    ucx_array_at_int(array, 0) = 47;
   1.370 -    ucx_array_at_int(array, 1) = 11;
   1.371 -    ucx_array_at_int(array, 2) = 0;
   1.372 -    ucx_array_at_int(array, 3) = 8;
   1.373 -    ucx_array_at_int(array, 4) = 15;
   1.374 +    elems = array.data;
   1.375 +    elems[0] = 47;
   1.376 +    elems[1] = 11;
   1.377 +    elems[2] = 0;
   1.378 +    elems[3] = 8;
   1.379 +    elems[4] = 15;
   1.380      
   1.381      UcxArray expected = ucx_array_new(16, sizeof(int));
   1.382      expected.size = 5;
   1.383 -    ucx_array_at_int(expected, 0) = 0;
   1.384 -    ucx_array_at_int(expected, 1) = 8;
   1.385 -    ucx_array_at_int(expected, 2) = 11;
   1.386 -    ucx_array_at_int(expected, 3) = 15;
   1.387 -    ucx_array_at_int(expected, 4) = 47;
   1.388 +    elems = expected.data;
   1.389 +    elems[0] = 0;
   1.390 +    elems[1] = 8;
   1.391 +    elems[2] = 11;
   1.392 +    elems[3] = 15;
   1.393 +    elems[4] = 47;
   1.394 +    
   1.395 +    UcxArray expectedrev = ucx_array_new(16, sizeof(int));
   1.396 +    expectedrev.size = 5;
   1.397 +    elems = expectedrev.data;
   1.398 +    elems[0] = 47;
   1.399 +    elems[1] = 15;
   1.400 +    elems[2] = 11;
   1.401 +    elems[3] = 8;
   1.402 +    elems[4] = 0;
   1.403      
   1.404  
   1.405      UCX_TEST_BEGIN
   1.406 @@ -399,16 +425,20 @@
   1.407      UCX_TEST_ASSERT(ucx_array_equals(array, expected, NULL, NULL), "failed");
   1.408      UCX_TEST_ASSERT(array.size == 5, "size corrupted");
   1.409      UCX_TEST_ASSERT(array.data == original_ptr, "shall not reallocate");
   1.410 -                
   1.411 +    
   1.412 +    ucx_array_sort(array, ucx_cmp_int_reverse, NULL);
   1.413 +    UCX_TEST_ASSERT(ucx_array_equals(array, expectedrev, NULL, NULL), "failed");
   1.414 +
   1.415      ucx_array_reserve(&array, 32);
   1.416      ucx_array_reserve(&expected, 32);
   1.417      array.size = expected.size = 32;
   1.418      for (size_t i = 0 ; i < 32 ; i++) {
   1.419 -        ucx_array_at_int(array, i) = ((i%2==0)?-1:1) * ((int) i);
   1.420 -        ucx_array_at_int(expected, i) = (-30+2*i) - (i > 15 ? 1 : 0);
   1.421 +        ((int*)array.data)[i]= ((i%2==0)?-1:1) * ((int) i);
   1.422 +        ((int*)expected.data)[i] = (-30+2*i) - (i > 15 ? 1 : 0);
   1.423      }
   1.424      
   1.425 -    ucx_array_sort(array, ucx_cmp_int, NULL);
   1.426 +    /* dummy third argument to trigger a possible fallback for qsort_s */
   1.427 +    ucx_array_sort(array, ucx_cmp_int, array.data);
   1.428      UCX_TEST_ASSERT(ucx_array_equals(array, expected, NULL, NULL),
   1.429              "failed for bigger arrays");
   1.430      UCX_TEST_END
   1.431 @@ -418,10 +448,12 @@
   1.432  }
   1.433  
   1.434  UCX_TEST(test_ucx_array_autogrow) {
   1.435 +    int *elems;
   1.436      UcxArray array = ucx_array_new(4, sizeof(int));
   1.437      array.size = 3;
   1.438 -    ucx_array_at_int(array, 0) = 47;
   1.439 -    ucx_array_at_int(array, 1) = 11;
   1.440 +    elems = array.data;
   1.441 +    elems[0] = 47;
   1.442 +    elems[1] = 11;
   1.443      int x = 5;
   1.444      
   1.445      UCX_TEST_BEGIN
   1.446 @@ -432,10 +464,10 @@
   1.447      UCX_TEST_ASSERT(array.capacity == 4 && array.data == oldptr,
   1.448              "array should not grow too early");
   1.449      ucx_array_append(&array, &x);
   1.450 +    elems = array.data;
   1.451      UCX_TEST_ASSERT(array.capacity == 8, "array did not grow");
   1.452      UCX_TEST_ASSERT(array.size == 5, "incorrect size after grow");
   1.453 -    UCX_TEST_ASSERT(ucx_array_at_int(array, 3) == 5 &&
   1.454 -        ucx_array_at_int(array, 3) == 5, "corrupt data");
   1.455 +    UCX_TEST_ASSERT(elems[3] == 5 && elems[4] == 5, "corrupt data");
   1.456      
   1.457      UCX_TEST_END
   1.458      ucx_array_free(&array);

mercurial