diff options
author | Nick Mathewson <nickm@torproject.org> | 2004-03-19 22:07:24 +0000 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2004-03-19 22:07:24 +0000 |
commit | 9199696182ed385b5645ffce6b0f8e7d74d57a35 (patch) | |
tree | 2dd20875044b762c8e1d3a5869728d67c661964d /src/or/test.c | |
parent | df3f37b84f81ec7757e056ff82530ef6e863fb95 (diff) | |
download | tor-9199696182ed385b5645ffce6b0f8e7d74d57a35.tar tor-9199696182ed385b5645ffce6b0f8e7d74d57a35.tar.gz |
Add some wrappers around SPLAY_* for the common map-from-string-to-X case.
It will probably be less blindingly fast than using SPLAY_* directly, but
only slightly so.
svn:r1306
Diffstat (limited to 'src/or/test.c')
-rw-r--r-- | src/or/test.c | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/src/or/test.c b/src/or/test.c index a3ab992ae..b4810d2e4 100644 --- a/src/or/test.c +++ b/src/or/test.c @@ -466,6 +466,81 @@ test_util() { test_eq((time_t) 1076393695UL, tor_timegm(&a_time)); } +static void* _squareAndRemoveK4(const char *key, void*val, void *data) +{ + int *ip = (int*)data; + int v; + if (strcmp(key,"K4") == 0) { + ++(*ip); + return NULL; + } + v = (int)val; + return (void*)(v*v); +} + +void test_strmap() { + strmap_t *map; + strmap_iter_t *iter; + const char *k; + void *v; + int count; + + map = strmap_new(); + v = strmap_set(map, "K1", (void*)99); + test_eq(v, NULL); + v = strmap_set(map, "K2", (void*)101); + test_eq(v, NULL); + v = strmap_set(map, "K1", (void*)100); + test_eq(v, (void*)99); + test_eq(strmap_get(map,"K1"), (void*)100); + test_eq(strmap_get(map,"K2"), (void*)101); + test_eq(strmap_get(map,"K-not-there"), NULL); + + v = strmap_remove(map,"K2"); + test_eq(v, (void*)101); + test_eq(strmap_get(map,"K2"), NULL); + test_eq(strmap_remove(map,"K2"), NULL); + + strmap_set(map, "K2", (void*)101); + strmap_set(map, "K3", (void*)102); + strmap_set(map, "K4", (void*)103); + strmap_set(map, "K5", (void*)104); + strmap_set(map, "K6", (void*)105); + + count = 0; + strmap_foreach(map, _squareAndRemoveK4, &count); + test_eq(count, 1); + test_eq(strmap_get(map, "K4"), NULL); + test_eq(strmap_get(map, "K1"), (void*)10000); + test_eq(strmap_get(map, "K6"), (void*)11025); + + iter = strmap_iter_init(map); + strmap_iter_get(iter,&k,&v); + test_streq(k, "K1"); + test_eq(v, (void*)10000); + iter = strmap_iter_next(map,iter); + strmap_iter_get(iter,&k,&v); + test_streq(k, "K2"); + test_eq(v, (void*)10201); + iter = strmap_iter_next_rmv(map,iter); + strmap_iter_get(iter,&k,&v); + test_streq(k, "K3"); + test_eq(v, (void*)10404); + iter = strmap_iter_next(map,iter); /* K5 */ + test_assert(!strmap_iter_done(iter)); + iter = strmap_iter_next(map,iter); /* K6 */ + test_assert(!strmap_iter_done(iter)); + iter = strmap_iter_next(map,iter); /* done */ + test_assert(strmap_iter_done(iter)); + + /* Make sure we removed K2, but not the others. */ + test_eq(strmap_get(map, "K2"), NULL); + test_eq(strmap_get(map, "K5"), (void*)10816); + + /* Clean up after ourselves. */ + strmap_free(map, NULL); +} + void test_onion() { #if 0 char **names; @@ -711,6 +786,7 @@ main(int c, char**v){ test_crypto_dh(); puts("\n========================= Util ============================"); test_util(); + test_strmap(); puts("\n========================= Onion Skins ====================="); test_onion(); test_onion_handshake(); |