aboutsummaryrefslogtreecommitdiff
path: root/src/test/test_containers.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2012-10-23 21:48:50 -0400
committerNick Mathewson <nickm@torproject.org>2012-10-23 21:48:50 -0400
commitb0646cc1423d4ea28d68c84e38e90ba8a6cb4e29 (patch)
tree4429b11bc470572281744383acc05580202f9258 /src/test/test_containers.c
parent2ecee3fce2179c6fe9f9c748522e417b887ee021 (diff)
parentcb693ef56e5fcef2a6d1844cb71c849022628bd8 (diff)
downloadtor-b0646cc1423d4ea28d68c84e38e90ba8a6cb4e29.tar
tor-b0646cc1423d4ea28d68c84e38e90ba8a6cb4e29.tar.gz
Merge remote-tracking branch 'origin/maint-0.2.2' into maint-0.2.3
Diffstat (limited to 'src/test/test_containers.c')
-rw-r--r--src/test/test_containers.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/test/test_containers.c b/src/test/test_containers.c
index 615c489f4..9a306ad79 100644
--- a/src/test/test_containers.c
+++ b/src/test/test_containers.c
@@ -16,6 +16,15 @@ _compare_strs(const void **a, const void **b)
return strcmp(s1, s2);
}
+/** Helper: return a tristate based on comparing the strings in <b>a</b> and
+ * *<b>b</b>. */
+static int
+compare_strs_for_bsearch_(const void *a, const void **b)
+{
+ const char *s1 = a, *s2 = *b;
+ return strcmp(s1, s2);
+}
+
/** Helper: return a tristate based on comparing the strings in *<b>a</b> and
* *<b>b</b>, excluding a's first character, and ignoring case. */
static int
@@ -204,6 +213,8 @@ test_container_smartlist_strings(void)
/* Test bsearch_idx */
{
int f;
+ smartlist_t *tmp = NULL;
+
test_eq(0, smartlist_bsearch_idx(sl," aaa",_compare_without_first_ch,&f));
test_eq(f, 0);
test_eq(0, smartlist_bsearch_idx(sl," and",_compare_without_first_ch,&f));
@@ -216,6 +227,31 @@ test_container_smartlist_strings(void)
test_eq(f, 0);
test_eq(7, smartlist_bsearch_idx(sl," zzzz",_compare_without_first_ch,&f));
test_eq(f, 0);
+
+ /* Test trivial cases for list of length 0 or 1 */
+ tmp = smartlist_create();
+ test_eq(0, smartlist_bsearch_idx(tmp, "foo",
+ compare_strs_for_bsearch_, &f));
+ test_eq(f, 0);
+ smartlist_insert(tmp, 0, (void *)("bar"));
+ test_eq(1, smartlist_bsearch_idx(tmp, "foo",
+ compare_strs_for_bsearch_, &f));
+ test_eq(f, 0);
+ test_eq(0, smartlist_bsearch_idx(tmp, "aaa",
+ compare_strs_for_bsearch_, &f));
+ test_eq(f, 0);
+ test_eq(0, smartlist_bsearch_idx(tmp, "bar",
+ compare_strs_for_bsearch_, &f));
+ test_eq(f, 1);
+ /* ... and one for length 2 */
+ smartlist_insert(tmp, 1, (void *)("foo"));
+ test_eq(1, smartlist_bsearch_idx(tmp, "foo",
+ compare_strs_for_bsearch_, &f));
+ test_eq(f, 1);
+ test_eq(2, smartlist_bsearch_idx(tmp, "goo",
+ compare_strs_for_bsearch_, &f));
+ test_eq(f, 0);
+ smartlist_free(tmp);
}
/* Test reverse() and pop_last() */