00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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"
00030
00031 static struct curl_hash hash_static;
00032 static const int slots = 3;
00033
00034 static void mydtor(void *p)
00035 {
00036
00037 (void)p;
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
00061
00062
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
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
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
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
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
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
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
00132 rc = Curl_hash_delete(&hash_static, &key4, strlen(key4));
00133 fail_unless(rc, "hash delete should have failed");
00134
00135
00136 nodep = Curl_hash_add(&hash_static, &key1, strlen(key1), ¬akey);
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
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
00148 Curl_hash_clean(&hash_static);
00149
00150 UNITTEST_STOP