unit1602.c (2248B)
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 "hash.h" 27 28 #include "memdebug.h" /* LAST include file */ 29 30 static void t1602_mydtor(void *p) 31 { 32 int *ptr = (int *)p; 33 free(ptr); 34 } 35 36 static CURLcode t1602_setup(struct Curl_hash *hash) 37 { 38 Curl_hash_init(hash, 7, Curl_hash_str, 39 curlx_str_key_compare, t1602_mydtor); 40 return CURLE_OK; 41 } 42 43 static void t1602_stop(struct Curl_hash *hash) 44 { 45 Curl_hash_destroy(hash); 46 } 47 48 static CURLcode test_unit1602(char *arg) 49 { 50 struct Curl_hash hash; 51 52 UNITTEST_BEGIN(t1602_setup(&hash)) 53 54 int *value; 55 int *value2; 56 int *nodep; 57 size_t klen = sizeof(int); 58 59 int key = 20; 60 int key2 = 25; 61 62 value = malloc(sizeof(int)); 63 abort_unless(value != NULL, "Out of memory"); 64 *value = 199; 65 nodep = Curl_hash_add(&hash, &key, klen, value); 66 if(!nodep) 67 free(value); 68 abort_unless(nodep, "insertion into hash failed"); 69 Curl_hash_clean(&hash); 70 71 /* Attempt to add another key/value pair */ 72 value2 = malloc(sizeof(int)); 73 abort_unless(value2 != NULL, "Out of memory"); 74 *value2 = 204; 75 nodep = Curl_hash_add(&hash, &key2, klen, value2); 76 if(!nodep) 77 free(value2); 78 abort_unless(nodep, "insertion into hash failed"); 79 80 UNITTEST_END(t1602_stop(&hash)) 81 }