test: fileutil, time, crc32 and lookup3 tests
authorMarko Kreen <markokr@gmail.com>
Tue, 1 Feb 2011 12:37:12 +0000 (14:37 +0200)
committerMarko Kreen <markokr@gmail.com>
Tue, 1 Feb 2011 12:38:09 +0000 (14:38 +0200)
test/Makefile
test/test_common.c
test/test_common.h
test/test_fileutil.c [new file with mode: 0644]
test/test_hashing.c [new file with mode: 0644]
test/test_time.c [new file with mode: 0644]

index 89f26e71e1f39c56e6e64e9b80410edc2046938c..f90f642a685da64b9944a64071de11ef5f783d99 100644 (file)
@@ -5,7 +5,7 @@ SRCS = test_string.c test_crypto.c test_aatree.c test_heap.c \
        test_utf8.c test_strpool.c test_pgutil.c test_regex.c \
        test_cxalloc.c test_bits.c test_base.c test_netdb.c \
        test_cfparser.c test_endian.c test_hashtab.c test_mdict.c \
-       test_shlist.c
+       test_shlist.c test_time.c test_hashing.c test_fileutil.c
 OBJS = $(addprefix obj/, $(SRCS:.c=.o))
 HDRS = test_common.h test_config.h tinytest.h tinytest_macros.h
 LIBS =
index 505cbb8c63f98ff442338972a043b5e791d5b231..09d0bfbded86f722680a7f266a3aedf317d4fc5e 100644 (file)
@@ -9,6 +9,7 @@ struct testgroup_t groups[] = {
        { "cxalloc/", cxalloc_tests },
        { "cbtree/", cbtree_tests },
        { "crypto/", crypto_tests },
+       { "hashing/", hashing_tests },
        { "endian/", endian_tests },
        { "string/", string_tests },
        { "heap/", heap_tests },
@@ -22,6 +23,8 @@ struct testgroup_t groups[] = {
        { "netdb/", netdb_tests },
        { "cfparser/", cfparser_tests },
        { "mdict/", mdict_tests },
+       { "time/", time_tests },
+       { "fileutil/", fileutil_tests },
        END_OF_GROUPS
 };
 
index 0af3d689e9d68b848df6405e997bb399258aba88..338d4dd9449fb6c4524157dd89d3a363cb9f72a3 100644 (file)
@@ -27,4 +27,7 @@ extern struct testcase_t endian_tests[];
 extern struct testcase_t hashtab_tests[];
 extern struct testcase_t mdict_tests[];
 extern struct testcase_t shlist_tests[];
+extern struct testcase_t time_tests[];
+extern struct testcase_t hashing_tests[];
+extern struct testcase_t fileutil_tests[];
 
diff --git a/test/test_fileutil.c b/test/test_fileutil.c
new file mode 100644 (file)
index 0000000..b637efc
--- /dev/null
@@ -0,0 +1,75 @@
+#include <usual/fileutil.h>
+
+#include <usual/string.h>
+#include <usual/mbuf.h>
+
+#include "test_common.h"
+
+/*
+ * LN1 = 4*8
+ * LN2 = 8*4*8
+ * LN3 = 8*8*4*8
+ */
+
+#define LN1 "11112222333344445555666677778888"
+#define LN2 LN1 LN1 LN1 LN1   LN1 LN1 LN1 LN1
+#define LN3 LN2 LN2 LN2 LN2   LN2 LN2 LN2 LN2
+
+static const char fdata[] = "1\n"
+"line 2\n"
+"\n"
+LN3
+"noln";
+static const char filename[] = "test_fileutil.tmp";
+
+static bool createfile(void)
+{
+       FILE *f = fopen(filename, "w+");
+       if (!f) return false;
+       fwrite(fdata, 1, strlen(fdata), f);
+       fclose(f);
+       return true;
+}
+
+static void test_fsize(void *p)
+{
+       int_check(createfile(), 1);
+
+       tt_assert(file_size(filename) == strlen(fdata));
+       tt_assert(file_size(filename) == sizeof(fdata) - 1);
+       tt_assert(file_size("nonexist") ==-1);
+end:;
+}
+
+static bool addln(void *arg, const char *ln, ssize_t len)
+{
+       struct MBuf *buf = arg;
+       int xlen = len;
+       if (len < 0)
+               return false;
+       if (len > 0 && ln[len - 1] == '\n')
+               xlen--;
+       if (memchr(ln, '\n', xlen))
+               return false;
+       return mbuf_write(buf, ln, len);
+}
+
+static void test_getline(void *p)
+{
+       struct MBuf buf;
+
+       mbuf_init_dynamic(&buf);
+
+       tt_assert(foreach_line(filename, addln, &buf));
+       tt_assert(mbuf_write_byte(&buf, 0));
+end:
+       unlink(filename);
+       mbuf_free(&buf);
+}
+
+struct testcase_t fileutil_tests[] = {
+       { "file_size", test_fsize },
+       { "getline", test_getline },
+       END_OF_TESTCASES
+};
+
diff --git a/test/test_hashing.c b/test/test_hashing.c
new file mode 100644 (file)
index 0000000..09bdaa9
--- /dev/null
@@ -0,0 +1,58 @@
+#include <usual/crc32.h>
+#include <usual/lookup3.h>
+
+#include <usual/string.h>
+
+#include "test_common.h"
+
+static uint32_t xcrc32(const char *s)
+{
+       return calc_crc32(s, strlen(s), 0);
+}
+
+static uint32_t xlookup3(const char *s)
+{
+       return hash_lookup3(s, strlen(s));
+}
+
+static void test_crc32(void *p)
+{
+       int_check(xcrc32(""), 0);
+       int_check(xcrc32("a"), 3904355907);
+       int_check(xcrc32("abc"), 891568578);
+       int_check(xcrc32("message digest"), 538287487);
+       int_check(xcrc32("abcdefghijklmnopqrstuvwxyz"), 1277644989);
+       int_check(xcrc32("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"), 532866770);
+       int_check(xcrc32("12345678901234567890123456789012345678901234567890123456789012345678901234567890"), 2091469426);
+end:;
+}
+
+static void test_lookup3(void *p)
+{
+#ifdef WORDS_BIGENDIAN
+       #warning n/a
+       int_check(xlookup3(""), 3735928559);
+       int_check(xlookup3("a"), 1490454280);
+       int_check(xlookup3("abc"), 238646833);
+       int_check(xlookup3("message digest"), 2512672053);
+       int_check(xlookup3("abcdefghijklmnopqrstuvwxyz"), 1966650813);
+       int_check(xlookup3("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"), 3992286962);
+       int_check(xlookup3("12345678901234567890123456789012345678901234567890123456789012345678901234567890"), 2776963519);
+#else
+       int_check(xlookup3(""), 3735928559);
+       int_check(xlookup3("a"), 1490454280);
+       int_check(xlookup3("abc"), 238646833);
+       int_check(xlookup3("message digest"), 2512672053);
+       int_check(xlookup3("abcdefghijklmnopqrstuvwxyz"), 1966650813);
+       int_check(xlookup3("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"), 3992286962);
+       int_check(xlookup3("12345678901234567890123456789012345678901234567890123456789012345678901234567890"), 2776963519);
+#endif
+end:;
+}
+
+struct testcase_t hashing_tests[] = {
+       { "crc32", test_crc32 },
+       { "lookup3", test_lookup3 },
+       END_OF_TESTCASES
+};
+
diff --git a/test/test_time.c b/test/test_time.c
new file mode 100644 (file)
index 0000000..bec48bb
--- /dev/null
@@ -0,0 +1,57 @@
+
+#include <usual/time.h>
+
+#include <string.h>
+
+#include "test_common.h"
+
+
+static void test_get_time(void *p)
+{
+       usec_t t, t2;
+       usec_t ct, ct2;
+
+       t = get_time_usec();
+       ct = get_cached_time();
+
+       usleep(USEC / 4);
+       t2 = get_time_usec();
+       tt_assert(t + USEC / 4 < t2);
+
+       ct2 = get_cached_time();
+       tt_assert(ct2 == ct);
+       reset_time_cache();
+       ct2 = get_cached_time();
+       tt_assert(ct2 != ct);
+end:;
+}
+
+static void test_time_format(void *p)
+{
+       char buf[128];
+       usec_t t;
+
+#ifdef WIN32
+       tt_assert(_putenv("TZ=GMT") >= 0);
+       _tzset();
+       printf( "_daylight = %d\n", _daylight );
+       printf( "_timezone = %ld\n", _timezone );
+       printf( "_tzname[0] = %s\n", _tzname[0] );
+
+#else
+       setenv("TZ", "GMT", 1);
+       tzset();
+#endif
+
+       t = 1226059006841546;
+       str_check(format_time_ms(t, buf, sizeof(buf)), "2008-11-07 11:56:46.841");
+       str_check(format_time_s(t, buf, sizeof(buf)), "2008-11-07 11:56:46");
+end:;
+}
+
+struct testcase_t time_tests[] = {
+       { "gettime", test_get_time },
+       { "format", test_time_format },
+       END_OF_TESTCASES
+};
+