| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include <pj/hash.h> |
| 20 |
#include <pj/rand.h> |
| 21 |
#include <pj/log.h> |
| 22 |
#include <pj/pool.h> |
| 23 |
#include "test.h" |
| 24 |
|
| 25 |
#if INCLUDE_HASH_TEST |
| 26 |
|
| 27 |
#define HASH_COUNT 31 |
| 28 |
#define THIS_FILE "hash_test.c" |
| 29 |
|
| 30 |
|
| 31 |
static int hash_test_with_key(pj_pool_t *pool, unsigned char key) |
| 32 |
{ |
| 33 |
pj_hash_table_t *ht; |
| 34 |
unsigned value = 0x12345; |
| 35 |
pj_hash_iterator_t it_buf, *it; |
| 36 |
unsigned *entry; |
| 37 |
|
| 38 |
PJ_TEST_NOT_NULL( (ht=pj_hash_create(pool, HASH_COUNT)), NULL, return -10); |
| 39 |
|
| 40 |
pj_hash_set(pool, ht, &key, sizeof(key), 0, &value); |
| 41 |
|
| 42 |
PJ_TEST_NOT_NULL((entry=(unsigned*)pj_hash_get(ht,&key,sizeof(key),NULL)), |
| 43 |
NULL, return -20); |
| 44 |
PJ_TEST_EQ( *entry, value, NULL, return -25); |
| 45 |
PJ_TEST_EQ(pj_hash_count(ht), 1, NULL, return -30); |
| 46 |
PJ_TEST_NOT_NULL((it=pj_hash_first(ht, &it_buf)), NULL, return -40); |
| 47 |
PJ_TEST_NOT_NULL((entry=(unsigned*)pj_hash_this(ht, it)), NULL, |
| 48 |
return -50); |
| 49 |
PJ_TEST_EQ(*entry, value, NULL, return -60); |
| 50 |
PJ_TEST_EQ(pj_hash_next(ht, it), NULL, NULL, return -70); |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
pj_hash_set(NULL, ht, &key, sizeof(key), 0, NULL); |
| 55 |
|
| 56 |
PJ_TEST_EQ(pj_hash_get(ht, &key, sizeof(key), NULL), NULL, NULL, |
| 57 |
return -80); |
| 58 |
PJ_TEST_EQ(pj_hash_count(ht), 0, NULL, return -90); |
| 59 |
PJ_TEST_EQ(pj_hash_first(ht, &it_buf), NULL, NULL, return -100); |
| 60 |
return 0; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
static int hash_collision_test(pj_pool_t *pool) |
| 65 |
{ |
| 66 |
enum { |
| 67 |
COUNT = HASH_COUNT * 4 |
| 68 |
}; |
| 69 |
pj_hash_table_t *ht; |
| 70 |
pj_hash_iterator_t it_buf, *it; |
| 71 |
unsigned char *values; |
| 72 |
unsigned i; |
| 73 |
|
| 74 |
PJ_TEST_NOT_NULL((ht=pj_hash_create(pool, HASH_COUNT)), NULL, return -200); |
| 75 |
|
| 76 |
values = (unsigned char*) pj_pool_alloc(pool, COUNT); |
| 77 |
|
| 78 |
for (i=0; i<COUNT; ++i) { |
| 79 |
values[i] = (unsigned char)i; |
| 80 |
pj_hash_set(pool, ht, &i, sizeof(i), 0, &values[i]); |
| 81 |
} |
| 82 |
|
| 83 |
PJ_TEST_EQ(pj_hash_count(ht), COUNT, NULL, return -210); |
| 84 |
|
| 85 |
for (i=0; i<COUNT; ++i) { |
| 86 |
unsigned char *entry; |
| 87 |
entry = (unsigned char*) pj_hash_get(ht, &i, sizeof(i), NULL); |
| 88 |
PJ_TEST_NOT_NULL(entry, NULL, return -220); |
| 89 |
PJ_TEST_EQ(*entry, values[i], NULL, return -230); |
| 90 |
} |
| 91 |
|
| 92 |
i = 0; |
| 93 |
it = pj_hash_first(ht, &it_buf); |
| 94 |
while (it) { |
| 95 |
++i; |
| 96 |
it = pj_hash_next(ht, it); |
| 97 |
} |
| 98 |
|
| 99 |
PJ_TEST_EQ(i, COUNT, NULL, return -240); |
| 100 |
return 0; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
int hash_test(void) |
| 108 |
{ |
| 109 |
pj_pool_t *pool = pj_pool_create(mem, "hash", 512, 512, NULL); |
| 110 |
int rc; |
| 111 |
unsigned i; |
| 112 |
|
| 113 |
|
| 114 |
for (i=0; i<=HASH_COUNT; ++i) { |
| 115 |
rc = hash_test_with_key(pool, (unsigned char)i); |
| 116 |
if (rc != 0) { |
| 117 |
pj_pool_release(pool); |
| 118 |
return rc; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
rc = hash_collision_test(pool); |
| 124 |
if (rc != 0) { |
| 125 |
pj_pool_release(pool); |
| 126 |
return rc; |
| 127 |
} |
| 128 |
|
| 129 |
pj_pool_release(pool); |
| 130 |
return 0; |
| 131 |
} |
| 132 |
|
| 133 |
#endif |
| 134 |
|