changeset 319:f2af6049d958

Improve testing.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 23 Feb 2016 01:39:49 +0200
parents f8c385739571
children 0362ea9872f0
files tests.c
diffstat 1 files changed, 28 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/tests.c	Tue Feb 23 00:14:13 2016 +0200
+++ b/tests.c	Tue Feb 23 01:39:49 2016 +0200
@@ -179,7 +179,7 @@
 
 
 
-void test_snprintf_do(size_t len, const char *fmt, va_list ap, const char *msg)
+void test_snprintf_do(size_t len, const char *msg, const char *fmt, va_list ap)
 {
     int ret1, ret2;
     va_list tmp;
@@ -187,7 +187,7 @@
 
     // Test basic *printf() functionality
     test_init(&ctx);
-    test_start(&ctx, "th_vsnprintf('%s', %s)", fmt, msg);
+    test_start(&ctx, "th_vsnprintf(%" TH_PRIu_SIZE_T ", \"%s\", %s)", len, fmt, msg);
 
     memset(buf1, SET_SENTINEL_BYTE, SET_BUF_SIZE+2); buf1[SET_BUF_SIZE+1] = 0;
     memset(buf2, SET_SENTINEL_BYTE, SET_BUF_SIZE+2); buf2[SET_BUF_SIZE+1] = 0;
@@ -211,15 +211,15 @@
 }
 
 
-void test_snprintf(const char *fmt, ...)
+void test_snprintf(const char *msg, const char *fmt, ...)
 {
     va_list ap, tmp;
     va_start(ap, fmt);
-    va_copy(tmp, ap); test_snprintf_do(0, fmt, tmp, "0");
-    va_copy(tmp, ap); test_snprintf_do(1, fmt, tmp, "1");
-    va_copy(tmp, ap); test_snprintf_do(2, fmt, tmp, "2");
-    va_copy(tmp, ap); test_snprintf_do(16, fmt, tmp, "16");
-    va_copy(tmp, ap); test_snprintf_do(SET_BUF_SIZE, fmt, tmp, "SET_BUF_SIZE");
+    va_copy(tmp, ap); test_snprintf_do(0, msg, fmt, tmp);
+    va_copy(tmp, ap); test_snprintf_do(1, msg, fmt, tmp);
+    va_copy(tmp, ap); test_snprintf_do(2, msg, fmt, tmp);
+    va_copy(tmp, ap); test_snprintf_do(16, msg, fmt, tmp);
+    va_copy(tmp, ap); test_snprintf_do(SET_BUF_SIZE, msg, fmt, tmp);
     va_end(ap); 
     tprint(2,
         "-----------------------------------------------------\n");
@@ -267,6 +267,7 @@
 int main(int argc, char *argv[])
 {
     size_t i1, i2;
+    char buf[64];
 
     //
     // Initialization
@@ -302,9 +303,9 @@
 
         for (i1 = 0; i1 < sizeof(i_vals) / sizeof(i_vals[0]); i1++)
         {
-            tprint(1, "Value %d\n", i_vals[i1]);
+            snprintf(buf, sizeof(buf), "%d", i_vals[i1]);
             for (i2 = 0; i2 < sizeof(i_fmts) / sizeof(i_fmts[0]); i2++)
-                test_snprintf(i_fmts[i2], i_vals[i1]);
+                test_snprintf(buf, i_fmts[i2], i_vals[i1]);
         }
     }
 
@@ -315,9 +316,9 @@
 
         for (i1 = 0; i1 < sizeof(i_vals) / sizeof(i_vals[0]); i1++)
         {
-            tprint(1, "Value %d\n", i_vals[i1]);
+            snprintf(buf, sizeof(buf), "%d", i_vals[i1]);
             for (i2 = 0; i2 < sizeof(i_fmts) / sizeof(i_fmts[0]); i2++)
-                test_snprintf(i_fmts[i2], i_vals[i1]);
+                test_snprintf(buf, i_fmts[i2], i_vals[i1]);
         }
     }
 
@@ -328,9 +329,9 @@
 
         for (i1 = 0; i1 < sizeof(f_vals) / sizeof(f_vals[0]); i1++)
         {
-            tprint(1, "Value %f\n", f_vals[i1]);
+            snprintf(buf, sizeof(buf), "%f", f_vals[i1]);
             for (i2 = 0; i2 < sizeof(f_fmts) / sizeof(f_fmts[0]); i2++)
-                test_snprintf(f_fmts[i2], f_vals[i1]);
+                test_snprintf(buf, f_fmts[i2], f_vals[i1]);
         }
     }
 
@@ -341,24 +342,25 @@
 
         for (i1 = 0; i1 < sizeof(s_vals) / sizeof(s_vals[0]); i1++)
         {
-            tprint(1, "Value '%s'\n", s_vals[i1]);
             for (i2 = 0; i2 < sizeof(s_fmts) / sizeof(s_fmts[0]); i2++)
-                test_snprintf(s_fmts[i2], s_vals[i1]);
+                test_snprintf(s_vals[i1], s_fmts[i2], s_vals[i1]);
         }
     }
 
     if (test_set_start("printf() char"))
     {
-        test_snprintf("a%cBC", 'x');
-        test_snprintf("%c", 'x');
-        test_snprintf("", 'x');
-        test_snprintf("%0c", 'x');
-        test_snprintf("%1c", 'x');
-        test_snprintf("% c", 'x');
-        test_snprintf("%-3c", 'x');
-        test_snprintf("%3c", 'x');
-        test_snprintf("%.3c", 'x');
-        test_snprintf("%-.3c", 'x');
+        const char c_val = 'x';
+        const char *c_msg = "x";
+        test_snprintf(c_msg, "a%cBC", c_val);
+        test_snprintf(c_msg, "%c", c_val);
+        test_snprintf(c_msg, "", c_val);
+        test_snprintf(c_msg, "%0c", c_val);
+        test_snprintf(c_msg, "%1c", c_val);
+        test_snprintf(c_msg, "% c", c_val);
+        test_snprintf(c_msg, "%-3c", c_val);
+        test_snprintf(c_msg, "%3c", c_val);
+        test_snprintf(c_msg, "%.3c", c_val);
+        test_snprintf(c_msg, "%-.3c", c_val);
     }
 
     //