63815551cfe5562ca2b01aa34d6494154b2c2e38
[platform/upstream/curl.git] / tests / unit / unit1602.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2011, 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.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   
68   Curl_hash_clean(&hash_static);
69
70   /* Attempt to add another key/value pair */
71   value2 = malloc(sizeof(int));
72   abort_unless(value2 != NULL, "Out of memory");
73   *value2 = 204;
74   nodep = Curl_hash_add(&hash_static, &key2, klen, value2);
75   if(!nodep)
76     free(value2);
77   abort_unless(nodep, "insertion into hash failed");
78
79 UNITTEST_STOP