unit1616.c (2592B)
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 "uint-hash.h" 27 28 #include "memdebug.h" /* LAST include file */ 29 30 static void t1616_mydtor(unsigned int id, void *elem) 31 { 32 int *ptr = (int *)elem; 33 (void)id; 34 free(ptr); 35 } 36 37 static CURLcode t1616_setup(struct uint_hash *hash) 38 { 39 Curl_uint_hash_init(hash, 15, t1616_mydtor); 40 return CURLE_OK; 41 } 42 43 static void t1616_stop(struct uint_hash *hash) 44 { 45 Curl_uint_hash_destroy(hash); 46 } 47 48 static CURLcode test_unit1616(char *arg) 49 { 50 struct uint_hash hash; 51 52 UNITTEST_BEGIN(t1616_setup(&hash)) 53 54 int *value, *v; 55 int *value2; 56 bool ok; 57 58 unsigned int key = 20; 59 unsigned int key2 = 25; 60 61 value = malloc(sizeof(int)); 62 abort_unless(value != NULL, "Out of memory"); 63 *value = 199; 64 ok = Curl_uint_hash_set(&hash, key, value); 65 if(!ok) 66 free(value); 67 abort_unless(ok, "insertion into hash failed"); 68 v = Curl_uint_hash_get(&hash, key); 69 abort_unless(v == value, "lookup present entry failed"); 70 v = Curl_uint_hash_get(&hash, key2); 71 abort_unless(!v, "lookup missing entry failed"); 72 Curl_uint_hash_clear(&hash); 73 74 /* Attempt to add another key/value pair */ 75 value2 = malloc(sizeof(int)); 76 abort_unless(value2 != NULL, "Out of memory"); 77 *value2 = 204; 78 ok = Curl_uint_hash_set(&hash, key2, value2); 79 if(!ok) 80 free(value2); 81 abort_unless(ok, "insertion into hash failed"); 82 v = Curl_uint_hash_get(&hash, key2); 83 abort_unless(v == value2, "lookup present entry failed"); 84 v = Curl_uint_hash_get(&hash, key); 85 abort_unless(!v, "lookup missing entry failed"); 86 87 UNITTEST_END(t1616_stop(&hash)) 88 }