diff tests.c @ 254:3d1e2af4e4e6

Start of a very simplistic unit test suite. Initially handling just printf() style functions.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 16 Feb 2016 21:28:00 +0200
parents
children 7549e279fe18
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests.c	Tue Feb 16 21:28:00 2016 +0200
@@ -0,0 +1,118 @@
+#include "th_types.h"
+#include "th_util.h"
+#include "th_string.h"
+#include "th_crypto.h"
+
+#define SET_BUF_SIZE 128
+
+int failed, passed;
+char buf1[SET_BUF_SIZE+2], buf2[SET_BUF_SIZE+2];
+
+
+int test_snprintf_do2(size_t len, const char *fmt, va_list ap)
+{
+    int ret1, ret2, ret = 0;
+    va_list tmp;
+
+    printf("th_vsnprintf(%" TH_PRIu_SIZE_T ", '%s'): ", len, fmt);
+
+    va_copy(tmp, ap);
+
+    ret1 = th_vsnprintf(buf1, len, fmt, ap);
+    ret2 = vsnprintf(buf2, len, fmt, tmp);
+
+    if (ret1 != ret2)
+    {
+        printf("ret mismatch (%d != %d)\n", ret1, ret2);
+        ret = -1;
+    }
+
+    if (strcmp(buf1, buf2))
+    {
+        printf("result mismatch:\n'%s' VS\n'%s'\n", buf1, buf2);
+        ret = -2;
+    }
+
+    if (ret == 0)
+        printf("OK\n");
+    return ret;
+}
+
+
+void test_snprintf_do(size_t len, const char *fmt, va_list ap)
+{
+    int ret;
+
+    memset(buf1, 0xe5, SET_BUF_SIZE+2); buf1[SET_BUF_SIZE+1] = 0;
+    memset(buf2, 0xe5, SET_BUF_SIZE+2); buf2[SET_BUF_SIZE+1] = 0;
+
+    ret = test_snprintf_do2(len, fmt, ap);
+
+    if ((unsigned char) buf1[len] != 0xe5)
+    {
+        printf("  buffer #1 overflow, sentinel 0x%02x\n", buf1[len]);
+        ret = -2;
+    }
+
+    if ((unsigned char) buf2[len] != 0xe5)
+    {
+        printf("  buffer #2 overflow, sentinel 0x%02x\n", buf2[len]);
+        ret = -2;
+    }
+
+    if (ret == 0)
+        passed++;
+    else
+        failed++;
+}
+
+
+void test_snprintf(const char *fmt, ...)
+{
+    va_list ap, tmp;
+    printf("----------------------------------------------\n");
+    va_start(ap, fmt);
+    va_copy(tmp, ap); test_snprintf_do(0, fmt, tmp);
+    va_copy(tmp, ap); test_snprintf_do(1, fmt, tmp);
+    va_copy(tmp, ap); test_snprintf_do(2, fmt, tmp);
+    va_copy(tmp, ap); test_snprintf_do(16, fmt, tmp);
+    va_copy(tmp, ap); test_snprintf_do(SET_BUF_SIZE, fmt, tmp);
+    va_end(ap);
+    printf("----------------------------------------------\n");
+}
+
+
+int main(int argc, char *argv[])
+{
+    (void) argc;
+    (void) argv;
+
+    th_init("th-test", "th-libs unit tests", "0.0.1", NULL, NULL);
+    th_verbosityLevel = 1;
+
+    if (sizeof(char) != sizeof(unsigned char))
+    {
+        THERR("sizeof(char) != sizeof(unsigned char)???\n");
+        return -1;
+    }
+
+    failed = passed = 0;
+
+    int i_vals[] = { 2, 612342, -2, -612342, 0x1fff, 0x8000000, };
+    char *i_fmts[] = { "%d", "%x", "%05d", "%5d", "%-5d", "%05x", "%5x", };
+    int i1, i2;
+
+    for (i1 = 0; i1 < sizeof(i_vals) / sizeof(i_vals[0]); i1++)
+    for (i2 = 0; i2 < sizeof(i_fmts) / sizeof(i_fmts[0]); i2++)
+        test_snprintf(i_fmts[i2], i_vals);
+
+    char *s_vals[] = { "", "asdf", "xxx yyy zzz ppp fff", NULL, };
+    char *s_fmts[] = { "%s", "%2s", "%-2s", "%5s", "%-5s", "%16s", "%-16s", };
+
+    for (i1 = 0; i1 < sizeof(s_vals) / sizeof(s_vals[0]); i1++)
+    for (i2 = 0; i2 < sizeof(s_fmts) / sizeof(s_fmts[0]); i2++)
+        test_snprintf(s_fmts[i2], s_vals);
+
+    printf("%d tests failed, %d passed.\n", failed, passed);
+    return 0;
+}