quickjs-tart

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

unit3213.c (4134B)


      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-spbset.h"
     28 #include "curl_trc.h"
     29 #include "unitprotos.h"
     30 
     31 static void check_spbset(const char *name, const unsigned int *s, size_t slen)
     32 {
     33   struct uint_spbset bset;
     34   size_t i, j;
     35   unsigned int n, c;
     36 
     37   curl_mfprintf(stderr, "test %s, %zu numbers\n", name, slen);
     38 
     39   Curl_uint_spbset_init(&bset);
     40 
     41   Curl_uint_spbset_clear(&bset);
     42   c = Curl_uint_spbset_count(&bset);
     43   fail_unless(c == 0, "set count is not 0");
     44 
     45   for(i = 0; i < slen; ++i) { /* add all */
     46     fail_unless(Curl_uint_spbset_add(&bset, s[i]), "failed to add");
     47     for(j = i + 1; j < slen; ++j)
     48       fail_unless(!Curl_uint_spbset_contains(&bset, s[j]),
     49                   "unexpectedly found");
     50   }
     51 
     52   for(i = 0; i < slen; ++i) { /* all present */
     53     fail_unless(Curl_uint_spbset_contains(&bset, s[i]),
     54                 "failed presence check");
     55   }
     56 
     57   /* iterator over all numbers */
     58   fail_unless(Curl_uint_spbset_first(&bset, &n), "first failed");
     59   fail_unless(n == s[0], "first not correct number");
     60   for(i = 1; i < slen; ++i) {
     61     fail_unless(Curl_uint_spbset_next(&bset, n, &n), "next failed");
     62     if(n != s[i]) {
     63       curl_mfprintf(stderr, "expected next to be %u, not %u\n", s[i], n);
     64       fail_unless(n == s[i], "next not correct number");
     65     }
     66   }
     67 
     68   for(i = 0; i < slen; i += 2) { /* remove every 2nd */
     69     Curl_uint_spbset_remove(&bset, s[i]);
     70     fail_unless(!Curl_uint_spbset_contains(&bset, s[i]), "unexpectedly found");
     71   }
     72   for(i = 1; i < slen; i += 2) { /* others still there */
     73     fail_unless(Curl_uint_spbset_contains(&bset, s[i]), "unexpectedly gone");
     74   }
     75   /* The count is half */
     76   c = Curl_uint_spbset_count(&bset);
     77   fail_unless(c == slen/2, "set count is wrong");
     78 
     79   Curl_uint_spbset_clear(&bset);
     80   c = Curl_uint_spbset_count(&bset);
     81   fail_unless(c == 0, "set count is not 0");
     82   for(i = 0; i < slen; i++) { /* none present any longer */
     83     fail_unless(!Curl_uint_spbset_contains(&bset, s[i]), "unexpectedly there");
     84   }
     85 
     86   for(i = 0; i < slen; ++i) { /* add all again */
     87     fail_unless(Curl_uint_spbset_add(&bset, s[i]), "failed to add");
     88   }
     89 
     90   Curl_uint_spbset_destroy(&bset);
     91 }
     92 
     93 static CURLcode test_unit3213(char *arg)
     94 {
     95   UNITTEST_BEGIN_SIMPLE
     96 
     97   static const unsigned int s1[] = { /* spread numbers, some at slot edges */
     98     0, 1, 4, 17, 63, 64, 65, 66,
     99     90, 99,
    100   };
    101   static const unsigned int s2[] = { /* set with all bits in slot1 set */
    102     64, 65, 66, 67, 68, 69, 70, 71,
    103     72, 73, 74, 75, 76, 77, 78, 79,
    104     80, 81, 82, 83, 84, 85, 86, 87,
    105     88, 89, 90, 91, 92, 93, 94, 95,
    106     96, 97, 98, 99, 100, 101, 102, 103,
    107     104, 105, 106, 107, 108, 109, 110, 111,
    108     112, 113, 114, 115, 116, 117, 118, 119,
    109     120, 121, 122, 123, 124, 125, 126, 127,
    110   };
    111   static const unsigned int s3[] = {  /* very spread numbers */
    112     2232, 5167, 8204, 8526, 8641, 10056, 10140, 10611,
    113     10998, 11626, 13735, 15539, 17947, 24295, 27833, 30318,
    114   };
    115 
    116   check_spbset("s1", s1, CURL_ARRAYSIZE(s1));
    117   check_spbset("s2", s2, CURL_ARRAYSIZE(s2));
    118   check_spbset("s3", s3, CURL_ARRAYSIZE(s3));
    119 
    120   UNITTEST_END_SIMPLE
    121 }