unit3212.c (5224B)
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-table.h" 28 #include "curl_trc.h" 29 #include "unitprotos.h" 30 31 #define TBL_SIZE 100 32 33 static CURLcode t3212_setup(struct uint_tbl *tbl) 34 { 35 Curl_uint_tbl_init(tbl, NULL); 36 return Curl_uint_tbl_resize(tbl, TBL_SIZE); 37 } 38 39 static void t3212_stop(struct uint_tbl *tbl) 40 { 41 Curl_uint_tbl_destroy(tbl); 42 } 43 44 static CURLcode test_unit3212(char *arg) 45 { 46 struct uint_tbl tbl; 47 int dummy; 48 49 UNITTEST_BEGIN(t3212_setup(&tbl)) 50 51 unsigned int i, key, n; 52 void *entry; 53 54 fail_unless(Curl_uint_tbl_capacity(&tbl) == TBL_SIZE, "wrong capacity"); 55 56 for(i = 0; i < TBL_SIZE; ++i) { 57 fail_unless(Curl_uint_tbl_add(&tbl, &dummy, &key), "failed to add"); 58 fail_unless(key == i, "unexpected key assigned"); 59 } 60 /* table should be full now */ 61 fail_unless(Curl_uint_tbl_count(&tbl) == TBL_SIZE, "wrong count"); 62 fail_unless(!Curl_uint_tbl_add(&tbl, &dummy, &key), "could add more"); 63 /* remove every 2nd entry, from full table */ 64 n = TBL_SIZE; 65 for(i = 0; i < TBL_SIZE; i += 2) { 66 Curl_uint_tbl_remove(&tbl, i); 67 --n; 68 fail_unless(Curl_uint_tbl_count(&tbl) == n, "wrong count after remove"); 69 } 70 /* remove same again, should not change count */ 71 for(i = 0; i < TBL_SIZE; i += 2) { 72 Curl_uint_tbl_remove(&tbl, i); 73 fail_unless(Curl_uint_tbl_count(&tbl) == n, "wrong count after remove"); 74 } 75 /* still contains all odd entries */ 76 for(i = 1; i < TBL_SIZE; i += 2) { 77 fail_unless(Curl_uint_tbl_contains(&tbl, i), "does not contain"); 78 fail_unless(Curl_uint_tbl_get(&tbl, i) == &dummy, 79 "does not contain dummy"); 80 } 81 /* get the first key */ 82 fail_unless(Curl_uint_tbl_first(&tbl, &key, &entry), "first failed"); 83 fail_unless(key == 1, "unexpected first key"); 84 fail_unless(entry == &dummy, "unexpected first entry"); 85 /* get the second key */ 86 fail_unless(Curl_uint_tbl_next(&tbl, 1, &key, &entry), "next1 failed"); 87 fail_unless(key == 3, "unexpected second key"); 88 fail_unless(entry == &dummy, "unexpected second entry"); 89 /* get the key after 42 */ 90 fail_unless(Curl_uint_tbl_next(&tbl, 42, &key, &entry), "next42 failed"); 91 fail_unless(key == 43, "unexpected next42 key"); 92 fail_unless(entry == &dummy, "unexpected next42 entry"); 93 94 /* double capacity */ 95 n = Curl_uint_tbl_count(&tbl); 96 fail_unless(!Curl_uint_tbl_resize(&tbl, TBL_SIZE * 2), 97 "error doubling size"); 98 fail_unless(Curl_uint_tbl_count(&tbl) == n, "wrong resize count"); 99 /* resize to half of original */ 100 fail_unless(!Curl_uint_tbl_resize(&tbl, TBL_SIZE / 2), "error halving size"); 101 fail_unless(Curl_uint_tbl_count(&tbl) == n / 2, "wrong half size count"); 102 for(i = 1; i < TBL_SIZE / 2; i += 2) { 103 fail_unless(Curl_uint_tbl_contains(&tbl, i), "does not contain"); 104 fail_unless(Curl_uint_tbl_get(&tbl, i) == &dummy, 105 "does not contain dummy"); 106 } 107 /* clear */ 108 Curl_uint_tbl_clear(&tbl); 109 fail_unless(!Curl_uint_tbl_count(&tbl), "count not 0 after clear"); 110 for(i = 0; i < TBL_SIZE / 2; ++i) { 111 fail_unless(!Curl_uint_tbl_contains(&tbl, i), "does contain, should not"); 112 } 113 /* add after clear gets key 0 again */ 114 fail_unless(Curl_uint_tbl_add(&tbl, &dummy, &key), "failed to add"); 115 fail_unless(key == 0, "unexpected key assigned"); 116 /* remove it again and add, should get key 1 */ 117 Curl_uint_tbl_remove(&tbl, key); 118 fail_unless(Curl_uint_tbl_add(&tbl, &dummy, &key), "failed to add"); 119 fail_unless(key == 1, "unexpected key assigned"); 120 /* clear, fill, remove one, add, should get the removed key again */ 121 Curl_uint_tbl_clear(&tbl); 122 for(i = 0; i < Curl_uint_tbl_capacity(&tbl); ++i) 123 fail_unless(Curl_uint_tbl_add(&tbl, &dummy, &key), "failed to add"); 124 fail_unless(!Curl_uint_tbl_add(&tbl, &dummy, &key), "add on full"); 125 Curl_uint_tbl_remove(&tbl, 17); 126 fail_unless(Curl_uint_tbl_add(&tbl, &dummy, &key), "failed to add again"); 127 fail_unless(key == 17, "unexpected key assigned"); 128 /* and again, triggering key search wrap around */ 129 Curl_uint_tbl_remove(&tbl, 17); 130 fail_unless(Curl_uint_tbl_add(&tbl, &dummy, &key), "failed to add again"); 131 fail_unless(key == 17, "unexpected key assigned"); 132 133 UNITTEST_END(t3212_stop(&tbl)) 134 }