diff options
Diffstat (limited to 'src/test/test_containers.c')
-rw-r--r-- | src/test/test_containers.c | 133 |
1 files changed, 88 insertions, 45 deletions
diff --git a/src/test/test_containers.c b/src/test/test_containers.c index d8b82e0661..3fc3523af4 100644 --- a/src/test/test_containers.c +++ b/src/test/test_containers.c @@ -1,10 +1,11 @@ /* Copyright (c) 2001-2004, Roger Dingledine. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. - * Copyright (c) 2007-2016, The Tor Project, Inc. */ + * Copyright (c) 2007-2017, The Tor Project, Inc. */ /* See LICENSE for licensing information */ #include "orconfig.h" #include "or.h" +#include "crypto_rand.h" #include "fp_pair.h" #include "test.h" @@ -501,31 +502,31 @@ test_container_smartlist_pos(void *arg) (void) arg; smartlist_t *sl = smartlist_new(); - smartlist_add(sl, tor_strdup("This")); - smartlist_add(sl, tor_strdup("is")); - smartlist_add(sl, tor_strdup("a")); - smartlist_add(sl, tor_strdup("test")); - smartlist_add(sl, tor_strdup("for")); - smartlist_add(sl, tor_strdup("a")); - smartlist_add(sl, tor_strdup("function")); + smartlist_add_strdup(sl, "This"); + smartlist_add_strdup(sl, "is"); + smartlist_add_strdup(sl, "a"); + smartlist_add_strdup(sl, "test"); + smartlist_add_strdup(sl, "for"); + smartlist_add_strdup(sl, "a"); + smartlist_add_strdup(sl, "function"); /* Test string_pos */ - tt_int_op(smartlist_string_pos(NULL, "Fred"), ==, -1); - tt_int_op(smartlist_string_pos(sl, "Fred"), ==, -1); - tt_int_op(smartlist_string_pos(sl, "This"), ==, 0); - tt_int_op(smartlist_string_pos(sl, "a"), ==, 2); - tt_int_op(smartlist_string_pos(sl, "function"), ==, 6); + tt_int_op(smartlist_string_pos(NULL, "Fred"), OP_EQ, -1); + tt_int_op(smartlist_string_pos(sl, "Fred"), OP_EQ, -1); + tt_int_op(smartlist_string_pos(sl, "This"), OP_EQ, 0); + tt_int_op(smartlist_string_pos(sl, "a"), OP_EQ, 2); + tt_int_op(smartlist_string_pos(sl, "function"), OP_EQ, 6); /* Test pos */ - tt_int_op(smartlist_pos(NULL, "Fred"), ==, -1); - tt_int_op(smartlist_pos(sl, "Fred"), ==, -1); - tt_int_op(smartlist_pos(sl, "This"), ==, -1); - tt_int_op(smartlist_pos(sl, "a"), ==, -1); - tt_int_op(smartlist_pos(sl, "function"), ==, -1); - tt_int_op(smartlist_pos(sl, smartlist_get(sl,0)), ==, 0); - tt_int_op(smartlist_pos(sl, smartlist_get(sl,2)), ==, 2); - tt_int_op(smartlist_pos(sl, smartlist_get(sl,5)), ==, 5); - tt_int_op(smartlist_pos(sl, smartlist_get(sl,6)), ==, 6); + tt_int_op(smartlist_pos(NULL, "Fred"), OP_EQ, -1); + tt_int_op(smartlist_pos(sl, "Fred"), OP_EQ, -1); + tt_int_op(smartlist_pos(sl, "This"), OP_EQ, -1); + tt_int_op(smartlist_pos(sl, "a"), OP_EQ, -1); + tt_int_op(smartlist_pos(sl, "function"), OP_EQ, -1); + tt_int_op(smartlist_pos(sl, smartlist_get(sl,0)), OP_EQ, 0); + tt_int_op(smartlist_pos(sl, smartlist_get(sl,2)), OP_EQ, 2); + tt_int_op(smartlist_pos(sl, smartlist_get(sl,5)), OP_EQ, 5); + tt_int_op(smartlist_pos(sl, smartlist_get(sl,6)), OP_EQ, 6); done: SMARTLIST_FOREACH(sl, char *, str, tor_free(str)); @@ -681,7 +682,7 @@ test_container_pqueue(void *arg) { smartlist_t *sl = smartlist_new(); int (*cmp)(const void *, const void*); - const int offset = STRUCT_OFFSET(pq_entry_t, idx); + const int offset = offsetof(pq_entry_t, idx); #define ENTRY(s) pq_entry_t s = { #s, -1 } ENTRY(cows); ENTRY(zebras); @@ -830,7 +831,7 @@ test_container_strmap(void *arg) found_keys = smartlist_new(); while (!strmap_iter_done(iter)) { strmap_iter_get(iter,&k,&v); - smartlist_add(found_keys, tor_strdup(k)); + smartlist_add_strdup(found_keys, k); tt_ptr_op(v,OP_EQ, strmap_get(map, k)); if (!strcmp(k, "K2")) { @@ -882,6 +883,46 @@ test_container_strmap(void *arg) tor_free(v105); } +static void +test_container_smartlist_remove(void *arg) +{ + (void) arg; + int array[5]; + smartlist_t *sl = smartlist_new(); + int i,j; + + for (j=0; j < 2; ++j) + for (i=0; i < 5; ++i) + smartlist_add(sl, &array[i]); + + smartlist_remove(sl, &array[0]); + smartlist_remove(sl, &array[3]); + smartlist_remove(sl, &array[4]); + tt_assert(! smartlist_contains(sl, &array[0])); + tt_assert(smartlist_contains(sl, &array[1])); + tt_assert(smartlist_contains(sl, &array[2])); + tt_assert(! smartlist_contains(sl, &array[3])); + tt_assert(! smartlist_contains(sl, &array[4])); + tt_int_op(smartlist_len(sl), OP_EQ, 4); + + smartlist_clear(sl); + for (j=0; j < 2; ++j) + for (i=0; i < 5; ++i) + smartlist_add(sl, &array[i]); + + smartlist_remove_keeporder(sl, &array[0]); + smartlist_remove_keeporder(sl, &array[3]); + smartlist_remove_keeporder(sl, &array[4]); + tt_int_op(smartlist_len(sl), OP_EQ, 4); + tt_ptr_op(smartlist_get(sl, 0), OP_EQ, &array[1]); + tt_ptr_op(smartlist_get(sl, 1), OP_EQ, &array[2]); + tt_ptr_op(smartlist_get(sl, 2), OP_EQ, &array[1]); + tt_ptr_op(smartlist_get(sl, 3), OP_EQ, &array[2]); + + done: + smartlist_free(sl); +} + /** Run unit tests for getting the median of a list. */ static void test_container_order_functions(void *arg) @@ -950,13 +991,13 @@ test_container_order_functions(void *arg) tt_assert(15 == median_time(times, 5)); int32_t int32s[] = { -5, -10, -50, 100 }; - tt_int_op(-5, ==, median_int32(int32s, 1)); - tt_int_op(-10, ==, median_int32(int32s, 2)); - tt_int_op(-10, ==, median_int32(int32s, 3)); - tt_int_op(-10, ==, median_int32(int32s, 4)); + tt_int_op(-5, OP_EQ, median_int32(int32s, 1)); + tt_int_op(-10, OP_EQ, median_int32(int32s, 2)); + tt_int_op(-10, OP_EQ, median_int32(int32s, 3)); + tt_int_op(-10, OP_EQ, median_int32(int32s, 4)); long longs[] = { -30, 30, 100, -100, 7 }; - tt_int_op(7, ==, find_nth_long(longs, 5, 2)); + tt_int_op(7, OP_EQ, find_nth_long(longs, 5, 2)); done: ; @@ -1066,7 +1107,7 @@ test_container_fp_pair_map(void *arg) tt_int_op(fp_pair_map_size(map),OP_EQ, 4); fp_pair_map_assert_ok(map); fp_pair_map_set(map, &fp5, v104); - fp_pair_map_set(map, &fp6, v105); + fp_pair_map_set_by_digests(map, fp6.first, fp6.second, v105); fp_pair_map_assert_ok(map); /* Test iterator. */ @@ -1084,7 +1125,8 @@ test_container_fp_pair_map(void *arg) /* Make sure we removed fp2, but not the others. */ tt_ptr_op(fp_pair_map_get(map, &fp2),OP_EQ, NULL); - tt_ptr_op(fp_pair_map_get(map, &fp5),OP_EQ, v104); + tt_ptr_op(fp_pair_map_get_by_digests(map, fp5.first, fp5.second), + OP_EQ, v104); fp_pair_map_assert_ok(map); /* Clean up after ourselves. */ @@ -1113,31 +1155,31 @@ test_container_smartlist_most_frequent(void *arg) const char *cp; cp = smartlist_get_most_frequent_string_(sl, &count); - tt_int_op(count, ==, 0); - tt_ptr_op(cp, ==, NULL); + tt_int_op(count, OP_EQ, 0); + tt_ptr_op(cp, OP_EQ, NULL); /* String must be sorted before we call get_most_frequent */ smartlist_split_string(sl, "abc:def:ghi", ":", 0, 0); cp = smartlist_get_most_frequent_string_(sl, &count); - tt_int_op(count, ==, 1); - tt_str_op(cp, ==, "ghi"); /* Ties broken in favor of later element */ + tt_int_op(count, OP_EQ, 1); + tt_str_op(cp, OP_EQ, "ghi"); /* Ties broken in favor of later element */ smartlist_split_string(sl, "def:ghi", ":", 0, 0); smartlist_sort_strings(sl); cp = smartlist_get_most_frequent_string_(sl, &count); - tt_int_op(count, ==, 2); - tt_ptr_op(cp, !=, NULL); - tt_str_op(cp, ==, "ghi"); /* Ties broken in favor of later element */ + tt_int_op(count, OP_EQ, 2); + tt_ptr_op(cp, OP_NE, NULL); + tt_str_op(cp, OP_EQ, "ghi"); /* Ties broken in favor of later element */ smartlist_split_string(sl, "def:abc:qwop", ":", 0, 0); smartlist_sort_strings(sl); cp = smartlist_get_most_frequent_string_(sl, &count); - tt_int_op(count, ==, 3); - tt_ptr_op(cp, !=, NULL); - tt_str_op(cp, ==, "def"); /* No tie */ + tt_int_op(count, OP_EQ, 3); + tt_ptr_op(cp, OP_NE, NULL); + tt_str_op(cp, OP_EQ, "def"); /* No tie */ done: SMARTLIST_FOREACH(sl, char *, str, tor_free(str)); @@ -1166,7 +1208,7 @@ test_container_smartlist_sort_ptrs(void *arg) smartlist_shuffle(sl); smartlist_sort_pointers(sl); for (j = 0; j < ARRAY_LENGTH(arrayptrs); ++j) { - tt_ptr_op(smartlist_get(sl, j), ==, arrayptrs[j]); + tt_ptr_op(smartlist_get(sl, j), OP_EQ, arrayptrs[j]); } } @@ -1192,11 +1234,11 @@ test_container_smartlist_strings_eq(void *arg) } while (0) /* Both NULL, so equal */ - tt_int_op(1, ==, smartlist_strings_eq(NULL, NULL)); + tt_int_op(1, OP_EQ, smartlist_strings_eq(NULL, NULL)); /* One NULL, not equal. */ - tt_int_op(0, ==, smartlist_strings_eq(NULL, sl1)); - tt_int_op(0, ==, smartlist_strings_eq(sl1, NULL)); + tt_int_op(0, OP_EQ, smartlist_strings_eq(NULL, sl1)); + tt_int_op(0, OP_EQ, smartlist_strings_eq(sl1, NULL)); /* Both empty, both equal. */ EQ_SHOULD_SAY("", "", 1); @@ -1239,6 +1281,7 @@ struct testcase_t container_tests[] = { CONTAINER_LEGACY(smartlist_digests), CONTAINER_LEGACY(smartlist_join), CONTAINER_LEGACY(smartlist_pos), + CONTAINER(smartlist_remove, 0), CONTAINER(smartlist_ints_eq, 0), CONTAINER_LEGACY(bitarray), CONTAINER_LEGACY(digestset), |