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 =
{ "cxalloc/", cxalloc_tests },
{ "cbtree/", cbtree_tests },
{ "crypto/", crypto_tests },
+ { "hashing/", hashing_tests },
{ "endian/", endian_tests },
{ "string/", string_tests },
{ "heap/", heap_tests },
{ "netdb/", netdb_tests },
{ "cfparser/", cfparser_tests },
{ "mdict/", mdict_tests },
+ { "time/", time_tests },
+ { "fileutil/", fileutil_tests },
END_OF_GROUPS
};
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[];
--- /dev/null
+#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
+};
+
--- /dev/null
+#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
+};
+
--- /dev/null
+
+#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
+};
+