unit1603.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 2015 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 #include "curlcheck.h"
00023 
00024 #define ENABLE_CURLX_PRINTF
00025 #include "curlx.h"
00026 
00027 #include "hash.h"
00028 
00029 #include "memdebug.h" /* LAST include file */
00030 
00031 static struct curl_hash hash_static;
00032 static const int slots = 3;
00033 
00034 static void mydtor(void *p)
00035 {
00036   /* Data are statically allocated */
00037  (void)p; /* unused */
00038 }
00039 
00040 static CURLcode unit_setup(void)
00041 {
00042   return Curl_hash_init(&hash_static, slots, Curl_hash_str,
00043                         Curl_str_key_compare, mydtor);
00044 }
00045 
00046 static void unit_stop(void)
00047 {
00048   Curl_hash_destroy(&hash_static);
00049 }
00050 
00051 UNITTEST_START
00052   char key1[] = "key1";
00053   char key2[] = "key2b";
00054   char key3[] = "key3";
00055   char key4[] = "key4";
00056   char notakey[] = "notakey";
00057   char *nodep;
00058   int rc;
00059 
00060   /* Ensure the key hashes are as expected in order to test both hash
00061      collisions and a full table. Unfortunately, the hashes can vary
00062      between architectures. */
00063   if(Curl_hash_str(key1, strlen(key1), slots) != 1 ||
00064      Curl_hash_str(key2, strlen(key2), slots) != 0 ||
00065      Curl_hash_str(key3, strlen(key3), slots) != 2 ||
00066      Curl_hash_str(key4, strlen(key4), slots) != 1)
00067     fprintf(stderr, "Warning: hashes are not computed as expected on this "
00068             "architecture; test coverage will be less comprehensive\n");
00069 
00070   nodep = Curl_hash_add(&hash_static, &key1, strlen(key1), &key1);
00071   fail_unless(nodep, "insertion into hash failed");
00072   nodep = Curl_hash_pick(&hash_static, &key1, strlen(key1));
00073   fail_unless(nodep == key1, "hash retrieval failed");
00074 
00075   nodep = Curl_hash_add(&hash_static, &key2, strlen(key2), &key2);
00076   fail_unless(nodep, "insertion into hash failed");
00077   nodep = Curl_hash_pick(&hash_static, &key2, strlen(key2));
00078   fail_unless(nodep == key2, "hash retrieval failed");
00079 
00080   nodep = Curl_hash_add(&hash_static, &key3, strlen(key3), &key3);
00081   fail_unless(nodep, "insertion into hash failed");
00082   nodep = Curl_hash_pick(&hash_static, &key3, strlen(key3));
00083   fail_unless(nodep == key3, "hash retrieval failed");
00084 
00085   /* The fourth element exceeds the number of slots & collides */
00086   nodep = Curl_hash_add(&hash_static, &key4, strlen(key4), &key4);
00087   fail_unless(nodep, "insertion into hash failed");
00088   nodep = Curl_hash_pick(&hash_static, &key4, strlen(key4));
00089   fail_unless(nodep == key4, "hash retrieval failed");
00090 
00091   /* Make sure all elements are still accessible */
00092   nodep = Curl_hash_pick(&hash_static, &key1, strlen(key1));
00093   fail_unless(nodep == key1, "hash retrieval failed");
00094   nodep = Curl_hash_pick(&hash_static, &key2, strlen(key2));
00095   fail_unless(nodep == key2, "hash retrieval failed");
00096   nodep = Curl_hash_pick(&hash_static, &key3, strlen(key3));
00097   fail_unless(nodep == key3, "hash retrieval failed");
00098   nodep = Curl_hash_pick(&hash_static, &key4, strlen(key4));
00099   fail_unless(nodep == key4, "hash retrieval failed");
00100 
00101   /* Delete the second of two entries in a bucket */
00102   rc = Curl_hash_delete(&hash_static, &key4, strlen(key4));
00103   fail_unless(rc == 0, "hash delete failed");
00104   nodep = Curl_hash_pick(&hash_static, &key1, strlen(key1));
00105   fail_unless(nodep == key1, "hash retrieval failed");
00106   nodep = Curl_hash_pick(&hash_static, &key4, strlen(key4));
00107   fail_unless(!nodep, "hash retrieval should have failed");
00108 
00109   /* Insert that deleted node again */
00110   nodep = Curl_hash_add(&hash_static, &key4, strlen(key4), &key4);
00111   fail_unless(nodep, "insertion into hash failed");
00112   nodep = Curl_hash_pick(&hash_static, &key4, strlen(key4));
00113   fail_unless(nodep == key4, "hash retrieval failed");
00114 
00115   /* Delete the first of two entries in a bucket */
00116   rc = Curl_hash_delete(&hash_static, &key1, strlen(key1));
00117   fail_unless(rc == 0, "hash delete failed");
00118   nodep = Curl_hash_pick(&hash_static, &key1, strlen(key1));
00119   fail_unless(!nodep, "hash retrieval should have failed");
00120   nodep = Curl_hash_pick(&hash_static, &key4, strlen(key4));
00121   fail_unless(nodep == key4, "hash retrieval failed");
00122 
00123   /* Delete the remaining one of two entries in a bucket */
00124   rc = Curl_hash_delete(&hash_static, &key4, strlen(key4));
00125   fail_unless(rc == 0, "hash delete failed");
00126   nodep = Curl_hash_pick(&hash_static, &key1, strlen(key1));
00127   fail_unless(!nodep, "hash retrieval should have failed");
00128   nodep = Curl_hash_pick(&hash_static, &key4, strlen(key4));
00129   fail_unless(!nodep, "hash retrieval should have failed");
00130 
00131   /* Delete an already deleted node */
00132   rc = Curl_hash_delete(&hash_static, &key4, strlen(key4));
00133   fail_unless(rc, "hash delete should have failed");
00134 
00135   /* Replace an existing node */
00136   nodep = Curl_hash_add(&hash_static, &key1, strlen(key1), &notakey);
00137   fail_unless(nodep, "insertion into hash failed");
00138   nodep = Curl_hash_pick(&hash_static, &key1, strlen(key1));
00139   fail_unless(nodep == notakey, "hash retrieval failed");
00140 
00141   /* Make sure all remaining elements are still accessible */
00142   nodep = Curl_hash_pick(&hash_static, &key2, strlen(key2));
00143   fail_unless(nodep == key2, "hash retrieval failed");
00144   nodep = Curl_hash_pick(&hash_static, &key3, strlen(key3));
00145   fail_unless(nodep == key3, "hash retrieval failed");
00146 
00147   /* Clean up */
00148   Curl_hash_clean(&hash_static);
00149 
00150 UNITTEST_STOP


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:07