[or-cvs] r13751: Write some unit tests for a few functions and cases that nee (in tor/trunk: . src/common src/or)

nickm at seul.org nickm at seul.org
Wed Feb 27 08:10:28 UTC 2008


Author: nickm
Date: 2008-02-27 03:10:28 -0500 (Wed, 27 Feb 2008)
New Revision: 13751

Modified:
   tor/trunk/
   tor/trunk/src/common/test.h
   tor/trunk/src/or/test.c
Log:
 r14516 at tombo:  nickm | 2008-02-27 03:10:26 -0500
 Write some unit tests for a few functions and cases that needed them.



Property changes on: tor/trunk
___________________________________________________________________
 svk:merge ticket from /tor/trunk [r14516] on 49666b30-7950-49c5-bedf-9dc8f3168102

Modified: tor/trunk/src/common/test.h
===================================================================
--- tor/trunk/src/common/test.h	2008-02-27 07:57:20 UTC (rev 13750)
+++ tor/trunk/src/common/test.h	2008-02-27 08:10:28 UTC (rev 13751)
@@ -125,14 +125,20 @@
 #define test_memeq(expr1, expr2, len)                           \
   STMT_BEGIN                                                    \
     const void *_test_v1=(expr1), *_test_v2=(expr2);            \
+    char *mem1, *mem2;                                          \
     if (!memcmp(_test_v1,_test_v2,(len))) {                     \
       printf("."); fflush(stdout); } else {                     \
     have_failed = 1;                                            \
-    printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n", \
+    mem1 = tor_malloc(len*2+1);                                    \
+    mem2 = tor_malloc(len*2+1);                                    \
+    base16_encode(mem1, len*2+1, _test_v1, len);                   \
+    base16_encode(mem2, len*2+1, _test_v2, len);                   \
+    printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n" \
+           "      %s != %s\n",                                     \
       _SHORT_FILE_,                                             \
       __LINE__,                                                 \
       PRETTY_FUNCTION,                                          \
-      #expr1, #expr2);                                          \
+      #expr1, #expr2, mem1, mem2);                              \
     return;                                                     \
   } STMT_END
 

Modified: tor/trunk/src/or/test.c
===================================================================
--- tor/trunk/src/or/test.c	2008-02-27 07:57:20 UTC (rev 13750)
+++ tor/trunk/src/or/test.c	2008-02-27 08:10:28 UTC (rev 13751)
@@ -1381,6 +1381,7 @@
 {
   smartlist_t *sl;
   char *cp;
+  size_t sz;
 
   /* XXXX test sort_digests, uniq_strings, uniq_digests */
 
@@ -1569,6 +1570,8 @@
   test_eq(smartlist_len(sl), 6);
   SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
   smartlist_clear(sl);
+  cp = smartlist_pop_last(sl);
+  test_eq(cp, NULL);
 
   /* Test uniq() */
   smartlist_split_string(sl,
@@ -1603,8 +1606,9 @@
   test_streq(cp, "Some,say,the,Earth,fire,end,in,ice,and,some,in");
   tor_free(cp);
   smartlist_string_remove(sl, "in");
-  cp = smartlist_join_strings2(sl, "+XX", 1, 0, NULL);
+  cp = smartlist_join_strings2(sl, "+XX", 1, 0, &sz);
   test_streq(cp, "Some+say+the+Earth+fire+end+some+ice+and");
+  test_eq((int)sz, 40);
   tor_free(cp);
 
   SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
@@ -1658,6 +1662,33 @@
     smartlist_clear(sl);
   }
 
+  {
+    /* digest_isin. */
+    smartlist_add(sl, tor_memdup("AAAAAAAAAAAAAAAAAAAA", DIGEST_LEN));
+    smartlist_add(sl, tor_memdup("\00090AAB2AAAAaasdAAAAA", DIGEST_LEN));
+    smartlist_add(sl, tor_memdup("\00090AAB2AAAAaasdAAAAA", DIGEST_LEN));
+    test_eq(0, smartlist_digest_isin(NULL, "AAAAAAAAAAAAAAAAAAAA"));
+    test_assert(smartlist_digest_isin(sl, "AAAAAAAAAAAAAAAAAAAA"));
+    test_assert(smartlist_digest_isin(sl, "\00090AAB2AAAAaasdAAAAA"));
+    test_eq(0, smartlist_digest_isin(sl, "\00090AAB2AAABaasdAAAAA"));
+
+    /* sort digests */
+    smartlist_sort_digests(sl);
+    test_memeq(smartlist_get(sl, 0), "\00090AAB2AAAAaasdAAAAA", DIGEST_LEN);
+    test_memeq(smartlist_get(sl, 1), "\00090AAB2AAAAaasdAAAAA", DIGEST_LEN);
+    test_memeq(smartlist_get(sl, 2), "AAAAAAAAAAAAAAAAAAAA", DIGEST_LEN);
+    test_eq(3, smartlist_len(sl));
+
+    /* uniq_digests */
+    smartlist_uniq_digests(sl);
+    test_eq(2, smartlist_len(sl));
+    test_memeq(smartlist_get(sl, 0), "\00090AAB2AAAAaasdAAAAA", DIGEST_LEN);
+    test_memeq(smartlist_get(sl, 1), "AAAAAAAAAAAAAAAAAAAA", DIGEST_LEN);
+
+    SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
+    smartlist_clear(sl);
+  }
+
   smartlist_free(sl);
 }
 
@@ -2079,6 +2110,13 @@
   tor_munmap_file(mapping);
 #endif
 
+  /* Now a zero-length file. */
+  write_str_to_file(fname1, "", 1);
+  mapping = tor_mmap_file(fname1);
+  test_eq(mapping, NULL);
+  test_eq(ERANGE, errno);
+  unlink(fname1);
+
   /* Make sure that we fail to map a no-longer-existent file. */
   mapping = tor_mmap_file(fname1);
   test_assert(mapping == NULL);



More information about the tor-commits mailing list