X-Git-Url: https://pd.if.org/git/?p=nbds;a=blobdiff_plain;f=test%2Fmap_test2.c;h=f8e0abd85b7f3958df026007b017c78db3d40729;hp=f08a37433abbe9025bda0f49c342cf8f73465346;hb=HEAD;hpb=4ae7c1069667d8f067258d89676126f9b44226d6 diff --git a/test/map_test2.c b/test/map_test2.c index f08a374..f8e0abd 100644 --- a/test/map_test2.c +++ b/test/map_test2.c @@ -19,6 +19,8 @@ #include "skiplist.h" #include "hashtable.h" #include "lwt.h" +#include "mem.h" +#include "rcu.h" #define ASSERT_EQUAL(x, y) CuAssertIntEquals(tc, x, y) @@ -48,10 +50,14 @@ void basic_test (CuTest* tc) { #ifdef TEST_STRING_KEYS map_t *map = map_alloc(map_type_, &DATATYPE_NSTRING); - nstring_t *k1 = ns_alloc(3); strcpy(k1->data, "k1"); - nstring_t *k2 = ns_alloc(3); strcpy(k1->data, "k2"); - nstring_t *k3 = ns_alloc(3); strcpy(k1->data, "k3"); - nstring_t *k4 = ns_alloc(3); strcpy(k1->data, "k4"); + nstring_t *s1 = ns_alloc(3); strcpy(s1->data, "k1"); + nstring_t *s2 = ns_alloc(3); strcpy(s2->data, "k2"); + nstring_t *s3 = ns_alloc(3); strcpy(s3->data, "k3"); + nstring_t *s4 = ns_alloc(3); strcpy(s4->data, "k4"); + map_key_t k1 = (map_key_t)s1; + map_key_t k2 = (map_key_t)s2; + map_key_t k3 = (map_key_t)s3; + map_key_t k4 = (map_key_t)s4; #else map_t *map = map_alloc(map_type_, NULL); map_key_t k1 = (map_key_t)1; @@ -121,31 +127,32 @@ void basic_test (CuTest* tc) { rcu_update(); // In a quiecent state. #ifdef TEST_STRING_KEYS - nbd_free(k1); nbd_free(k2); nbd_free(k3); nbd_free(k4); + nbd_free(s1); nbd_free(s2); nbd_free(s3); nbd_free(s4); #endif } void *add_remove_worker (void *arg) { + nbd_thread_init(); + worker_data_t *wd = (worker_data_t *)arg; map_t *map = wd->map; CuTest* tc = wd->tc; int d = wd->id; - int iters = 10000; + int iters = (map_type_ == &MAP_IMPL_LL ? 10000 : 100000); - SYNC_ADD(wd->wait, -1); + (void)SYNC_ADD(wd->wait, -1); do { } while (*wd->wait); // wait for all workers to be ready -#ifdef TEST_STRING_KEYS - nstring_t *key = ns_alloc(9); -#else map_key_t key; +#ifdef TEST_STRING_KEYS + nstring_t *s = ns_alloc(9); + key = (map_key_t)s; #endif for (int j = 0; j < 10; ++j) { for (int i = d+1; i < iters; i+=2) { #ifdef TEST_STRING_KEYS - memset(key->data, 0, key->len); - snprintf(key->data, key->len, "%llu", i); + s->len = 1 + snprintf(s->data, 9, "%u", i); #else key = (map_key_t)i; #endif @@ -155,8 +162,7 @@ void *add_remove_worker (void *arg) { } for (int i = d+1; i < iters; i+=2) { #ifdef TEST_STRING_KEYS - memset(key->data, 0, key->len); - snprintf(key->data, key->len, "%u", i); + s->len = 1 + snprintf(s->data, 9, "%u", i); #else key = (map_key_t)i; #endif @@ -165,6 +171,9 @@ void *add_remove_worker (void *arg) { rcu_update(); // In a quiecent state. } } +#ifdef TEST_STRING_KEYS + nbd_free(s); +#endif return NULL; } @@ -173,8 +182,7 @@ void concurrent_add_remove_test (CuTest* tc) { pthread_t thread[2]; worker_data_t wd[2]; - static const int num_threads = 2; - volatile int wait = num_threads; + volatile int wait = 2; #ifdef TEST_STRING_KEYS map_t *map = map_alloc(map_type_, &DATATYPE_NSTRING); #else @@ -186,23 +194,23 @@ void concurrent_add_remove_test (CuTest* tc) { // In 2 threads, add & remove even & odd elements concurrently int i; - for (i = 0; i < num_threads; ++i) { + for (i = 0; i < 2; ++i) { wd[i].id = i; wd[i].tc = tc; wd[i].map = map; wd[i].wait = &wait; - int rc = nbd_thread_create(thread + i, i, add_remove_worker, wd + i); + int rc = pthread_create(thread + i, NULL, add_remove_worker, wd + i); if (rc != 0) { perror("nbd_thread_create"); return; } } - for (i = 0; i < num_threads; ++i) { + for (i = 0; i < 2; ++i) { pthread_join(thread[i], NULL); } gettimeofday(&tv2, NULL); int ms = (int)(1000000*(tv2.tv_sec - tv1.tv_sec) + tv2.tv_usec - tv1.tv_usec) / 1000; - map_print(map); - printf("Th:%d Time:%dms\n", num_threads, ms); + map_print(map, FALSE); + printf("Time:%dms\n", ms); fflush(stdout); // In the end, all members should be removed @@ -216,8 +224,10 @@ void concurrent_add_remove_test (CuTest* tc) { void basic_iteration_test (CuTest* tc) { #ifdef TEST_STRING_KEYS map_t *map = map_alloc(map_type_, &DATATYPE_NSTRING); - nstring_t *k1 = ns_alloc(3); strcpy(k1->data, "k1"); - nstring_t *k2 = ns_alloc(3); strcpy(k1->data, "k2"); + nstring_t *s1 = ns_alloc(3); strcpy(s1->data, "k1"); + nstring_t *s2 = ns_alloc(3); strcpy(s2->data, "k2"); + map_key_t k1 = (map_key_t)s1; + map_key_t k2 = (map_key_t)s2; nstring_t *x_k; nstring_t *y_k; #else @@ -233,15 +243,15 @@ void basic_iteration_test (CuTest* tc) { map_val_t x_v, y_v; map_iter_t *iter = map_iter_begin(map, 0); - x_v = map_iter_next(iter, &x_k); - y_v = map_iter_next(iter, &y_k); + x_v = map_iter_next(iter, (map_key_t *)&x_k); + y_v = map_iter_next(iter, (map_key_t *)&y_k); ASSERT_EQUAL( DOES_NOT_EXIST, map_iter_next(iter, NULL) ); map_iter_free(iter); #ifdef TEST_STRING_KEYS - ASSERT_EQUAL( TRUE, (ns_cmp(x_k, k1) == 0 && x_v == 1) || (ns_cmp(y_k, k1) == 0 && y_v == 1) ); - ASSERT_EQUAL( TRUE, (ns_cmp(x_k, k2) == 0 && x_v == 2) || (ns_cmp(y_k, k2) == 0 && y_v == 2) ); - nbd_free(k1); - nbd_free(k2); + ASSERT_EQUAL( TRUE, (ns_cmp(x_k, s1) == 0 && x_v == 1) || (ns_cmp(y_k, s1) == 0 && y_v == 1) ); + ASSERT_EQUAL( TRUE, (ns_cmp(x_k, s2) == 0 && x_v == 2) || (ns_cmp(y_k, s2) == 0 && y_v == 2) ); + nbd_free(s1); + nbd_free(s2); #else ASSERT_EQUAL( TRUE, (x_k == k1 && x_v == 1) || (y_k == k1 && y_v == 1) ); ASSERT_EQUAL( TRUE, (x_k == k2 && x_v == 2) || (y_k == k2 && y_v == 2) ); @@ -255,9 +265,12 @@ void big_iteration_test (CuTest* tc) { #ifdef TEST_STRING_KEYS map_t *map = map_alloc(map_type_, &DATATYPE_NSTRING); - nstring_t *key = ns_alloc(9); - nstring_t *k3 = ns_alloc(3); strcpy(k1->data, "k3"); - nstring_t *k4 = ns_alloc(3); strcpy(k1->data, "k4"); + nstring_t *s = ns_alloc(9); + nstring_t *s3 = ns_alloc(3); strcpy(s3->data, "k3"); + nstring_t *s4 = ns_alloc(3); strcpy(s4->data, "k4"); + map_key_t k3 = (map_key_t)s3; + map_key_t k4 = (map_key_t)s4; + map_key_t key = (map_key_t)s; #else map_t *map = map_alloc(map_type_, NULL); map_key_t k3 = (map_key_t)3; @@ -265,10 +278,9 @@ void big_iteration_test (CuTest* tc) { map_key_t key; #endif - for (size_t i = 1; i <= n; ++i) { + for (int i = 1; i <= n; ++i) { #ifdef TEST_STRING_KEYS - memset(key->data, 0, key->len); - snprintf(key->data, key->len, "k%llu", i); + s->len = 1 + snprintf(s->data, 9, "k%d", i); #else key = (map_key_t)i; #endif @@ -300,16 +312,15 @@ void big_iteration_test (CuTest* tc) { ASSERT_EQUAL(n*(n+1)/2 - (3+4), sum); #ifdef TEST_STRING_KEYS - nbd_free(key); + nbd_free(s); #endif } int main (void) { + nbd_thread_init(); + lwt_set_trace_level("r0m3l2t0"); - nbd_init(); - lwt_set_trace_level("h3"); - - static const map_impl_t *map_types[] = { &ll_map_impl, &sl_map_impl, &ht_map_impl }; + static const map_impl_t *map_types[] = { &MAP_IMPL_LL, &MAP_IMPL_SL, &MAP_IMPL_HT }; for (int i = 0; i < sizeof(map_types)/sizeof(*map_types); ++i) { map_type_ = map_types[i]; @@ -317,10 +328,10 @@ int main (void) { CuString *output = CuStringNew(); CuSuite* suite = CuSuiteNew(); - SUITE_ADD_TEST(suite, basic_test); - SUITE_ADD_TEST(suite, basic_iteration_test); - SUITE_ADD_TEST(suite, big_iteration_test); SUITE_ADD_TEST(suite, concurrent_add_remove_test); +// SUITE_ADD_TEST(suite, basic_test); +// SUITE_ADD_TEST(suite, basic_iteration_test); +// SUITE_ADD_TEST(suite, big_iteration_test); CuSuiteRun(suite); CuSuiteDetails(suite, output);