diff options
author | Nick Mathewson <nickm@torproject.org> | 2007-04-11 00:30:25 +0000 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2007-04-11 00:30:25 +0000 |
commit | 6ba0b0e9f46cedfa83813f1faf63ab4a24456e4d (patch) | |
tree | 52f1136888d2cd980a725e9e6d989fdb1e490ea5 /src/or/test.c | |
parent | d7359eb996218f4492a5583c34ab8ee7d9315e7d (diff) | |
download | tor-6ba0b0e9f46cedfa83813f1faf63ab4a24456e4d.tar.gz tor-6ba0b0e9f46cedfa83813f1faf63ab4a24456e4d.zip |
r12336@catbus: nickm | 2007-04-10 17:34:25 -0400
Unit tests and debugging for memory pool implementation.
svn:r9938
Diffstat (limited to 'src/or/test.c')
-rw-r--r-- | src/or/test.c | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/or/test.c b/src/or/test.c index 03a589c2c0..7294e413a7 100644 --- a/src/or/test.c +++ b/src/or/test.c @@ -23,9 +23,12 @@ const char test_c_id[] = #include <dirent.h> #endif +#define MEMPOOL_PRIVATE + #include "or.h" #include "../common/test.h" #include "../common/torgzip.h" +#include "../common/mempool.h" int have_failed = 0; @@ -2104,6 +2107,52 @@ bench_aes(void) crypto_free_cipher_env(c); } +static void +test_mempool(void) +{ + mp_pool_t *pool; + smartlist_t *allocated; + int i; + + pool = mp_pool_new(1, 100); + test_assert(pool->new_chunk_capacity >= 100); + test_assert(pool->item_alloc_size >= sizeof(void*)+1); + mp_pool_destroy(pool); + + pool = mp_pool_new(241, 10); + test_assert(pool->new_chunk_capacity >= 10); + test_assert(pool->item_alloc_size >= sizeof(void*)+241); + test_eq(pool->item_alloc_size & 0x03, 0); + test_assert(pool->new_chunk_capacity < 60); + + allocated = smartlist_create(); + for (i = 0; i < 100000; ++i) { + if (smartlist_len(allocated) < 20 || crypto_rand_int(2)) { + void *m = mp_pool_get(pool); + memset(m, 0x09, 241); + smartlist_add(allocated, m); + //printf("%d: %p\n", i, m); + //mp_pool_assert_ok(pool); + } else { + int idx = crypto_rand_int(smartlist_len(allocated)); + void *m = smartlist_get(allocated, idx); + //printf("%d: free %p\n", i, m); + smartlist_del(allocated, idx); + mp_pool_release(m); + //mp_pool_assert_ok(pool); + } + if (crypto_rand_int(777)==0) + mp_pool_clean(pool); + + if (i % 777) + mp_pool_assert_ok(pool); + } + SMARTLIST_FOREACH(allocated, void *, m, mp_pool_release(m)); + mp_pool_assert_ok(pool); + mp_pool_destroy(pool); + smartlist_free(allocated); +} + int main(int c, char**v) { @@ -2145,6 +2194,7 @@ main(int c, char**v) test_gzip(); test_util(); test_smartlist(); + test_mempool(); test_strmap(); test_control_formats(); test_pqueue(); |