| 1 |
/*************************************************************************** |
| 2 |
* _ _ ____ _ |
| 3 |
* Project ___| | | | _ \| | |
| 4 |
* / __| | | | |_) | | |
| 5 |
* | (__| |_| | _ <| |___ |
| 6 |
* \___|\___/|_| \_\_____| |
| 7 |
* |
| 8 |
* Copyright (C) 1998 - 2016, Daniel Stenberg, <[email protected]>, 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.haxx.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 |
***************************************************************************/ |
| 22 |
#include "curlcheck.h" |
| 23 |
|
| 24 |
#define ENABLE_CURLX_PRINTF |
| 25 |
#include "curlx.h" |
| 26 |
|
| 27 |
#include "hash.h" |
| 28 |
|
| 29 |
#include "memdebug.h" /* LAST include file */ |
| 30 |
|
| 31 |
static struct curl_hash hash_static; |
| 32 |
|
| 33 |
static void mydtor(void *p) |
| 34 |
{ |
| 35 |
int *ptr = (int *)p; |
| 36 |
free(ptr); |
| 37 |
} |
| 38 |
|
| 39 |
static CURLcode unit_setup(void) |
| 40 |
{ |
| 41 |
return Curl_hash_init(&hash_static, 7, Curl_hash_str, |
| 42 |
Curl_str_key_compare, mydtor); |
| 43 |
} |
| 44 |
|
| 45 |
static void unit_stop(void) |
| 46 |
{ |
| 47 |
Curl_hash_destroy(&hash_static); |
| 48 |
} |
| 49 |
|
| 50 |
UNITTEST_START |
| 51 |
int *value; |
| 52 |
int *value2; |
| 53 |
int *nodep; |
| 54 |
size_t klen = sizeof(int); |
| 55 |
|
| 56 |
int key = 20; |
| 57 |
int key2 = 25; |
| 58 |
|
| 59 |
|
| 60 |
value = malloc(sizeof(int)); |
| 61 |
abort_unless(value != NULL, "Out of memory"); |
| 62 |
*value = 199; |
| 63 |
nodep = Curl_hash_add(&hash_static, &key, klen, value); |
| 64 |
if(!nodep) |
| 65 |
free(value); |
| 66 |
abort_unless(nodep, "insertion into hash failed"); |
| 67 |
Curl_hash_clean(&hash_static); |
| 68 |
|
| 69 |
/* Attempt to add another key/value pair */ |
| 70 |
value2 = malloc(sizeof(int)); |
| 71 |
abort_unless(value2 != NULL, "Out of memory"); |
| 72 |
*value2 = 204; |
| 73 |
nodep = Curl_hash_add(&hash_static, &key2, klen, value2); |
| 74 |
if(!nodep) |
| 75 |
free(value2); |
| 76 |
abort_unless(nodep, "insertion into hash failed"); |
| 77 |
|
| 78 |
UNITTEST_STOP |