aboutsummaryrefslogtreecommitdiff
path: root/src/test/test_util.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/test_util.c')
-rw-r--r--src/test/test_util.c59
1 files changed, 36 insertions, 23 deletions
diff --git a/src/test/test_util.c b/src/test/test_util.c
index 656b77b1a..151ec6912 100644
--- a/src/test/test_util.c
+++ b/src/test/test_util.c
@@ -12,7 +12,9 @@
#include "config.h"
#include "control.h"
#include "test.h"
+#ifdef ENABLE_MEMPOOLS
#include "mempool.h"
+#endif /* ENABLE_MEMPOOLS */
#include "memarea.h"
#include "util_process.h"
@@ -152,7 +154,7 @@ test_util_write_chunks_to_file(void *arg)
// assert the file has been written (expected size)
str = read_file_to_str(fname, RFTS_BIN, &st);
tt_assert(str != NULL);
- tt_int_op(st.st_size, ==, data_str_len);
+ tt_u64_op((uint64_t)st.st_size, ==, data_str_len);
test_mem_op(data_str, ==, str, data_str_len);
tor_free(str);
@@ -183,14 +185,14 @@ test_util_write_chunks_to_file(void *arg)
// assert the file has been written (expected size)
str = read_file_to_str(fname, RFTS_BIN, &st);
tt_assert(str != NULL);
- tt_int_op(st.st_size, ==, data_str_len);
+ tt_u64_op((uint64_t)st.st_size, ==, data_str_len);
test_mem_op(data_str, ==, str, data_str_len);
tor_free(str);
// assert the tempfile still contains the known string
str = read_file_to_str(tempname, RFTS_BIN, &st);
tt_assert(str != NULL);
- tt_int_op(st.st_size, ==, temp_str_len);
+ tt_u64_op((uint64_t)st.st_size, ==, temp_str_len);
test_mem_op(temp_str, ==, str, temp_str_len);
done:
@@ -345,7 +347,7 @@ test_util_time(void)
tv.tv_sec = (time_t)1326296338;
tv.tv_usec = 3060;
- format_iso_time(timestr, tv.tv_sec);
+ format_iso_time(timestr, (time_t)tv.tv_sec);
test_streq("2012-01-11 15:38:58", timestr);
/* The output of format_local_iso_time will vary by timezone, and setting
our timezone for testing purposes would be a nontrivial flaky pain.
@@ -353,7 +355,7 @@ test_util_time(void)
format_local_iso_time(timestr, tv.tv_sec);
test_streq("2012-01-11 10:38:58", timestr);
*/
- format_iso_time_nospace(timestr, tv.tv_sec);
+ format_iso_time_nospace(timestr, (time_t)tv.tv_sec);
test_streq("2012-01-11T15:38:58", timestr);
test_eq(strlen(timestr), ISO_TIME_LEN);
format_iso_time_nospace_usec(timestr, &tv);
@@ -1091,7 +1093,7 @@ test_util_strmisc(void)
test_eq(i, 0);
test_eq(0UL, tor_parse_ulong(TOOBIG, 10, 0, ULONG_MAX, &i, NULL));
test_eq(i, 0);
- test_eq(U64_LITERAL(0), tor_parse_uint64(TOOBIG, 10,
+ tt_u64_op(U64_LITERAL(0), ==, tor_parse_uint64(TOOBIG, 10,
0, UINT64_MAX, &i, NULL));
test_eq(i, 0);
}
@@ -1289,21 +1291,21 @@ test_util_pow2(void)
test_eq(tor_log2(UINT64_MAX), 63);
/* Test round_to_power_of_2 */
- test_eq(round_to_power_of_2(120), 128);
- test_eq(round_to_power_of_2(128), 128);
- test_eq(round_to_power_of_2(130), 128);
- test_eq(round_to_power_of_2(U64_LITERAL(40000000000000000)),
- U64_LITERAL(1)<<55);
- test_eq(round_to_power_of_2(U64_LITERAL(0xffffffffffffffff)),
+ tt_u64_op(round_to_power_of_2(120), ==, 128);
+ tt_u64_op(round_to_power_of_2(128), ==, 128);
+ tt_u64_op(round_to_power_of_2(130), ==, 128);
+ tt_u64_op(round_to_power_of_2(U64_LITERAL(40000000000000000)), ==,
+ U64_LITERAL(1)<<55);
+ tt_u64_op(round_to_power_of_2(U64_LITERAL(0xffffffffffffffff)), ==,
U64_LITERAL(1)<<63);
- test_eq(round_to_power_of_2(0), 1);
- test_eq(round_to_power_of_2(1), 1);
- test_eq(round_to_power_of_2(2), 2);
- test_eq(round_to_power_of_2(3), 2);
- test_eq(round_to_power_of_2(4), 4);
- test_eq(round_to_power_of_2(5), 4);
- test_eq(round_to_power_of_2(6), 4);
- test_eq(round_to_power_of_2(7), 8);
+ tt_u64_op(round_to_power_of_2(0), ==, 1);
+ tt_u64_op(round_to_power_of_2(1), ==, 1);
+ tt_u64_op(round_to_power_of_2(2), ==, 2);
+ tt_u64_op(round_to_power_of_2(3), ==, 2);
+ tt_u64_op(round_to_power_of_2(4), ==, 4);
+ tt_u64_op(round_to_power_of_2(5), ==, 4);
+ tt_u64_op(round_to_power_of_2(6), ==, 4);
+ tt_u64_op(round_to_power_of_2(7), ==, 8);
done:
;
@@ -1900,6 +1902,8 @@ test_util_path_is_relative(void)
;
}
+#ifdef ENABLE_MEMPOOLS
+
/** Run unittests for memory pool allocator */
static void
test_util_mempool(void)
@@ -1958,6 +1962,8 @@ test_util_mempool(void)
mp_pool_destroy(pool);
}
+#endif /* ENABLE_MEMPOOLS */
+
/** Run unittests for memory area allocator */
static void
test_util_memarea(void)
@@ -2242,18 +2248,21 @@ test_util_asprintf(void *ptr)
test_assert(cp);
test_streq("simple string 100% safe", cp);
test_eq(strlen(cp), r);
+ tor_free(cp);
/* empty string */
r = tor_asprintf(&cp, "%s", "");
test_assert(cp);
test_streq("", cp);
test_eq(strlen(cp), r);
+ tor_free(cp);
/* numbers (%i) */
r = tor_asprintf(&cp, "I like numbers-%2i, %i, etc.", -1, 2);
test_assert(cp);
test_streq("I like numbers--1, 2, etc.", cp);
test_eq(strlen(cp), r);
+ /* don't free cp; next test uses it. */
/* numbers (%d) */
r = tor_asprintf(&cp2, "First=%d, Second=%d", 101, 202);
@@ -3241,6 +3250,8 @@ smartlist_new_from_text_lines(const char *lines)
last_line = smartlist_pop_last(sl);
if (last_line != NULL && *last_line != '\0') {
smartlist_add(sl, last_line);
+ } else {
+ tor_free(last_line);
}
return sl;
@@ -3720,17 +3731,17 @@ test_util_max_mem(void *arg)
r = get_total_system_memory(&memory1);
r2 = get_total_system_memory(&memory2);
tt_int_op(r, ==, r2);
- tt_int_op(memory2, ==, memory1);
+ tt_uint_op(memory2, ==, memory1);
TT_BLATHER(("System memory: "U64_FORMAT, U64_PRINTF_ARG(memory1)));
if (r==0) {
/* You have at least a megabyte. */
- tt_int_op(memory1, >, (1<<20));
+ tt_uint_op(memory1, >, (1<<20));
} else {
/* You do not have a petabyte. */
#if SIZEOF_SIZE_T == SIZEOF_UINT64_T
- tt_int_op(memory1, <, (U64_LITERAL(1)<<50));
+ tt_uint_op(memory1, <, (U64_LITERAL(1)<<50));
#endif
}
@@ -3754,7 +3765,9 @@ struct testcase_t util_tests[] = {
UTIL_LEGACY(pow2),
UTIL_LEGACY(gzip),
UTIL_LEGACY(datadir),
+#ifdef ENABLE_MEMPOOLS
UTIL_LEGACY(mempool),
+#endif
UTIL_LEGACY(memarea),
UTIL_LEGACY(control_formats),
UTIL_LEGACY(mmap),