quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

unit3211.c (5296B)


      1 /***************************************************************************
      2  *                                  _   _ ____  _
      3  *  Project                     ___| | | |  _ \| |
      4  *                             / __| | | | |_) | |
      5  *                            | (__| |_| |  _ <| |___
      6  *                             \___|\___/|_| \_\_____|
      7  *
      8  * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
      9  *
     10  * This software is licensed as described in the file COPYING, which
     11  * you should have received as part of this distribution. The terms
     12  * are also available at https://curl.se/docs/copyright.html.
     13  *
     14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
     15  * copies of the Software, and permit persons to whom the Software is
     16  * furnished to do so, under the terms of the COPYING file.
     17  *
     18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
     19  * KIND, either express or implied.
     20  *
     21  * SPDX-License-Identifier: curl
     22  *
     23  ***************************************************************************/
     24 #include "unitcheck.h"
     25 
     26 #include "urldata.h"
     27 #include "uint-bset.h"
     28 #include "curl_trc.h"
     29 
     30 static void check_set(const char *name, unsigned int capacity,
     31                       const unsigned int *s, size_t slen)
     32 {
     33   struct uint_bset bset;
     34   size_t i, j;
     35   unsigned int n, c;
     36 
     37   curl_mfprintf(stderr, "test %s, capacity=%u, %zu numbers\n",
     38                 name, capacity, slen);
     39   Curl_uint_bset_init(&bset);
     40   fail_unless(!Curl_uint_bset_resize(&bset, capacity), "bset resize failed");
     41   c = Curl_uint_bset_capacity(&bset);
     42   fail_unless(c == (((capacity + 63) / 64) * 64), "wrong capacity");
     43 
     44   Curl_uint_bset_clear(&bset);
     45   c = Curl_uint_bset_count(&bset);
     46   fail_unless(c == 0, "set count is not 0");
     47 
     48   for(i = 0; i < slen; ++i) { /* add all */
     49     fail_unless(Curl_uint_bset_add(&bset, s[i]), "failed to add");
     50     for(j = i + 1; j < slen; ++j)
     51       fail_unless(!Curl_uint_bset_contains(&bset, s[j]), "unexpectedly found");
     52   }
     53 
     54   for(i = 0; i < slen; ++i) { /* all present */
     55     fail_unless(Curl_uint_bset_contains(&bset, s[i]), "failed presence check");
     56   }
     57 
     58   /* iterator over all numbers */
     59   fail_unless(Curl_uint_bset_first(&bset, &n), "first failed");
     60   fail_unless(n == s[0], "first not correct number");
     61   for(i = 1; i < slen; ++i) {
     62     fail_unless(Curl_uint_bset_next(&bset, n, &n), "next failed");
     63     if(n != s[i]) {
     64       curl_mfprintf(stderr, "expected next to be %u, not %u\n", s[i], n);
     65       fail_unless(n == s[i], "next not correct number");
     66     }
     67   }
     68 
     69   /* Adding capacity number does not work (0 - capacity-1) */
     70   c = Curl_uint_bset_capacity(&bset);
     71   fail_unless(!Curl_uint_bset_add(&bset, c), "add out of range worked");
     72   /* The count it correct */
     73   c = Curl_uint_bset_count(&bset);
     74   fail_unless(c == slen, "set count is wrong");
     75 
     76   for(i = 0; i < slen; i += 2) { /* remove every 2nd */
     77     Curl_uint_bset_remove(&bset, s[i]);
     78     fail_unless(!Curl_uint_bset_contains(&bset, s[i]), "unexpectedly found");
     79   }
     80   for(i = 1; i < slen; i += 2) { /* others still there */
     81     fail_unless(Curl_uint_bset_contains(&bset, s[i]), "unexpectedly gone");
     82   }
     83   /* The count is half */
     84   c = Curl_uint_bset_count(&bset);
     85   fail_unless(c == slen/2, "set count is wrong");
     86 
     87   Curl_uint_bset_clear(&bset);
     88   c = Curl_uint_bset_count(&bset);
     89   fail_unless(c == 0, "set count is not 0");
     90   for(i = 0; i < slen; i++) { /* none present any longer */
     91     fail_unless(!Curl_uint_bset_contains(&bset, s[i]), "unexpectedly there");
     92   }
     93 
     94   for(i = 0; i < slen; ++i) { /* add all again */
     95     fail_unless(Curl_uint_bset_add(&bset, s[i]), "failed to add");
     96   }
     97 
     98   fail_unless(!Curl_uint_bset_resize(&bset, capacity * 2),
     99               "resize double failed");
    100   for(i = 0; i < slen; i++) { /* all still present after resize */
    101     fail_unless(Curl_uint_bset_contains(&bset, s[i]), "unexpectedly lost");
    102   }
    103 
    104   fail_unless(!Curl_uint_bset_resize(&bset, capacity), "resize back failed");
    105   for(i = 0; i < slen; i++)  /* all still present after resize back */
    106     fail_unless(Curl_uint_bset_contains(&bset, s[i]), "unexpectedly lost");
    107 
    108   fail_unless(!Curl_uint_bset_resize(&bset, capacity/2), "resize half failed");
    109   /* halved the size, what numbers remain in set? */
    110   c = Curl_uint_bset_capacity(&bset);
    111   n = 0;
    112   for(i = 0; i < slen; ++i) {
    113     if(s[i] < c)
    114       ++n;
    115   }
    116   fail_unless(n == Curl_uint_bset_count(&bset), "set count(halved) wrong");
    117   for(i = 0; i < n; i++)  /* still present after resize half */
    118     fail_unless(Curl_uint_bset_contains(&bset, s[i]), "unexpectedly lost");
    119 
    120   Curl_uint_bset_destroy(&bset);
    121 }
    122 
    123 static CURLcode test_unit3211(char *arg)
    124 {
    125   UNITTEST_BEGIN_SIMPLE
    126 
    127   static const unsigned int s1[] = {  /* spread numbers, some at slot edges */
    128     0, 1, 4, 17, 63, 64, 65, 66,
    129     90, 99,
    130   };
    131   static const unsigned int s2[] = { /* set with all bits in slot1 set */
    132     64, 65, 66, 67, 68, 69, 70, 71,
    133     72, 73, 74, 75, 76, 77, 78, 79,
    134     80, 81, 82, 83, 84, 85, 86, 87,
    135     88, 89, 90, 91, 92, 93, 94, 95,
    136     96, 97, 98, 99, 100, 101, 102, 103,
    137     104, 105, 106, 107, 108, 109, 110, 111,
    138     112, 113, 114, 115, 116, 117, 118, 119,
    139     120, 121, 122, 123, 124, 125, 126, 127,
    140   };
    141 
    142   check_set("s1", 100, s1, CURL_ARRAYSIZE(s1));
    143   check_set("s2", 1000, s2, CURL_ARRAYSIZE(s2));
    144 
    145   UNITTEST_END_SIMPLE
    146 }