OSSP CVS Repository

ossp - Difference in ossp-pkg/str/str_test.c versions 1.24 and 1.25
Not logged in
[Honeypot]  [Browse]  [Home]  [Login]  [Reports
[Search]  [Ticket]  [Timeline
  [History

ossp-pkg/str/str_test.c 1.24 -> 1.25

--- str_test.c   2002/04/01 08:32:54     1.24
+++ str_test.c   2002/04/01 09:03:49     1.25
@@ -30,40 +30,40 @@
 #include <stdio.h>
 #include <stdlib.h>
 
-#include "str.h"
-#include "str_config.h"
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
 
 #if defined(HAVE_DMALLOC_H) && defined(WITH_DMALLOC)
 #include "dmalloc.h"
 #endif
 
+#include "str.h"
+#include "ts.h"
+
 #define LONG_STRING 1024
 
 /*
  * Test String Length
  */
 
-static void test_length(int *ok, int *fail)
+TS_TEST(test_length)
 {
-    if (str_len(NULL) == 0)
-        (*ok)++;
-    else 
-        (*fail)++;
-
-    if (str_len("") == 0)
-        (*ok)++;
-    else 
-        (*fail)++;
-
-    if (str_len("a") == 1)
-        (*ok)++;
-    else 
-        (*fail)++;
-
-    if (str_len("foo bar quux") == 12)
-        (*ok)++;
-    else 
-        (*fail)++;
+    ts_test_check(TS_CTX, "NULL handling");
+    if (str_len(NULL) != 0)
+        ts_test_fail(TS_CTX, "unexpected non-zero return");
+
+    ts_test_check(TS_CTX, "empty string handling");
+    if (str_len("") != 0)
+        ts_test_fail(TS_CTX, "unexpected non-zero return");
+
+    ts_test_check(TS_CTX, "short string handling");
+    if (str_len("a") != 1)
+        ts_test_fail(TS_CTX, "unexpected return != 1");
+
+    ts_test_check(TS_CTX, "longer string handling");
+    if (str_len("foo bar quux") != 12)
+        ts_test_fail(TS_CTX, "unexpected return != 12");
     return;
 }
 
@@ -87,22 +87,18 @@
     { NULL, 0, NULL, 0 }
 };
 
-static void test_locate(int *ok, int *fail)
+TS_TEST(test_locate)
 {
     int i;
     char *rv;
 
     for (i = 0; loctab[i].s != NULL; i++) {
         rv = str_locate(loctab[i].s, loctab[i].n, loctab[i].p);
-        printf("str_locate(\"%s\", %d, \"%s\") = \"%s\"\n",
-                loctab[i].s, loctab[i].n, loctab[i].p, rv == NULL ? "[NULL]" : rv);
-        if ((rv-loctab[i].s == loctab[i].o) || (rv == NULL && loctab[i].o == -1))
-            (*ok)++;
-        else {
-            fprintf(stderr, "ERROR: result was \"%s\", expected \"%s\"\n",
-                    rv, loctab[i].s+loctab[i].o);
-            (*fail)++;
-        }
+        ts_test_check(TS_CTX, "str_locate(\"%s\", %d, \"%s\") = \"%s\"",
+                      loctab[i].s, loctab[i].n, loctab[i].p, rv == NULL ? "[NULL]" : rv);
+        if (!((rv-loctab[i].s == loctab[i].o) || (rv == NULL && loctab[i].o == -1)))
+            ts_test_fail(TS_CTX, "result was \"%s\", expected \"%s\"",
+                         rv, loctab[i].s+loctab[i].o);
     }
     return;
 }
@@ -129,22 +125,18 @@
     { NULL, 0, NULL, 0, 0 }
 };
 
-static void test_span(int *ok, int *fail)
+TS_TEST(test_span)
 {
     int i;
     char *rv;
 
     for (i = 0; spantab[i].s != NULL; i++) {
         rv = str_span(spantab[i].s, spantab[i].n, spantab[i].cs, spantab[i].m);
-        printf("str_span(\"%s\", %d, \"%s\", %d) = \"%s\"\n",
-                spantab[i].s, spantab[i].n, spantab[i].cs, spantab[i].m, rv);
-        if (rv-spantab[i].s == spantab[i].o)
-            (*ok)++;
-        else {
-            fprintf(stderr, "ERROR: result was \"%s\", expected \"%s\"\n",
-                    rv, spantab[i].s+spantab[i].o);
-            (*fail)++;
-        }
+        ts_test_check(TS_CTX, "str_span(\"%s\", %d, \"%s\", %d) = \"%s\"",
+                      spantab[i].s, spantab[i].n, spantab[i].cs, spantab[i].m, rv);
+        if (rv-spantab[i].s != spantab[i].o)
+            ts_test_fail(TS_CTX, "result was \"%s\", expected \"%s\"",
+                         rv, spantab[i].s+spantab[i].o);
     }
     return;
 }
@@ -198,7 +190,7 @@
     return;
 }
 
-static void test_tokenize(int *ok, int *fail)
+TS_TEST(test_tokenize)
 {  
     char *cp;
     char *cp2;
@@ -207,23 +199,24 @@
     int i, j;
 
     for (i = 0; toktab[i].s != NULL; i++) {
-        fprintf(stderr, "Testing tokenization of %s\n", prstr(toktab[i].s)), prstr_free();
+        ts_test_check(TS_CTX, "tokenization of \"%s\"\n", prstr(toktab[i].s));
+        prstr_free();
         cp2 = cp = strdup(toktab[i].s);
         for (j = 0; j < 4; j++) {
             cp3 = strdup(cp);
             rc = str_token(&cp, toktab[i].d, toktab[i].q, toktab[i].c, toktab[i].f);
-            fprintf(stderr, "str_token(&%s, %s, %s, %s, %d) = %s\n",
-                    prstr(cp3), prstr(toktab[i].d),
-                    prstr(toktab[i].q), prstr(toktab[i].c),
-                    toktab[i].f, prstr(rc)), prstr_free();
+            ts_test_check(TS_CTX, "str_token(&%s, %s, %s, %s, %d) = %s",
+                          prstr(cp3), prstr(toktab[i].d),
+                          prstr(toktab[i].q), prstr(toktab[i].c),
+                          toktab[i].f, prstr(rc)), prstr_free();
             free(cp3);
             if (!(   (rc == NULL && toktab[i].r[j] == NULL)
                   || (rc != NULL && toktab[i].r[j] != NULL && strcmp(rc, toktab[i].r[j]) == 0))) {
-                fprintf(stderr, "ERROR: expected result is %s\n", prstr(toktab[i].r[j])), prstr_free();
+                ts_test_fail(TS_CTX, "expected result is \"%s\"", prstr(toktab[i].r[j]));
+                prstr_free();
             }
         }
         free(cp2);
-        fprintf(stderr, "\n");
     }
     return;
 }
@@ -253,14 +246,14 @@
     { NULL, NULL, NULL, NULL, NULL, NULL, 0 }
 };
 
-static void test_parsing(int *ok, int *fail)
+TS_TEST(test_parsing)
 {
     int i;
     int rv;
     char *r1, *r2, *r3, *r4;
 
     for (i = 0; test2_tab[i].s != NULL; i++) {
-        fprintf(stderr, "%d. str_parse(\"%s\", \"%s\", ...)\n", i, test2_tab[i].s, test2_tab[i].p);
+        ts_test_check(TS_CTX, "str_parse(\"%s\", \"%s\", ...)", test2_tab[i].s, test2_tab[i].p);
         if (*(test2_tab[i].p) == 's') {
             r1 = NULL;
             r2 = test2_tab[i].r2;
@@ -272,10 +265,6 @@
             r1 = r2 = r3 = r4 = NULL;
             rv = str_parse(test2_tab[i].s, test2_tab[i].p, &r1, &r2, &r3, &r4);
         }
-        fprintf(stderr, "%d. str_parse(\"%s\", \"%s\", ...) = %d + <%s><%s><%s><%s>\n",
-                i, test2_tab[i].s, test2_tab[i].p, rv,
-                r1 == NULL ? "NULL" : r1, r2 == NULL ? "NULL" : r2, 
-                r3 == NULL ? "NULL" : r3, r4 == NULL ? "NULL" : r4);
         if (rv != test2_tab[i].rv ||
             ((r1 == NULL && test2_tab[i].r1 != NULL) || 
              (r1 != NULL && test2_tab[i].r1 == NULL) || 
@@ -289,16 +278,13 @@
             ((r4 == NULL && test2_tab[i].r4 != NULL) || 
              (r4 != NULL && test2_tab[i].r4 == NULL) || 
              (r4 != NULL && test2_tab[i].r4 != NULL && strcmp(r4, test2_tab[i].r4) != 0))) {
-            fprintf(stderr, "    ERROR: expected result: %d + <%s><%s><%s><%s>\n",
-                    test2_tab[i].rv, 
-                    test2_tab[i].r1 == NULL ? "NULL" : test2_tab[i].r1,
-                    test2_tab[i].r2 == NULL ? "NULL" : test2_tab[i].r2,
-                    test2_tab[i].r3 == NULL ? "NULL" : test2_tab[i].r3,
-                    test2_tab[i].r4 == NULL ? "NULL" : test2_tab[i].r4);
-            (*fail)++;
+            ts_test_fail(TS_CTX, "expected result: %d + <%s><%s><%s><%s>",
+                         test2_tab[i].rv, 
+                         test2_tab[i].r1 == NULL ? "NULL" : test2_tab[i].r1,
+                         test2_tab[i].r2 == NULL ? "NULL" : test2_tab[i].r2,
+                         test2_tab[i].r3 == NULL ? "NULL" : test2_tab[i].r3,
+                         test2_tab[i].r4 == NULL ? "NULL" : test2_tab[i].r4);
         }
-        else
-            (*ok)++;
     }
     str_parse(NULL, NULL);
     return;
@@ -308,7 +294,7 @@
  * Test String Formatting
  */
 
-static void test_formatting(int *ok, int *fail)
+TS_TEST(test_formatting)
 {
     char buf1[LONG_STRING];
     char buf2[LONG_STRING];
@@ -347,160 +333,102 @@
     int x, y;
     int len;
 
-    fprintf(stderr, "\n** Testing str_format format codes against vendor sprintf **\n\n");
+    ts_test_check(TS_CTX, "str_format vs. vendor sprintf comparison");
 
     for (x = 0; fp_fmt[x] != NULL; x++) {
-        fprintf(stderr, "testing \"%s\"\n", fp_fmt[x]);
+        ts_test_check(TS_CTX, "str_format(..,..,\"%s\",..)", fp_fmt[x]);
         for (y = 0; fp_nums[y] != 0; y++) {
             len = str_format(buf1, sizeof(buf1), fp_fmt[x], fp_nums[y]);
             sprintf(buf2, fp_fmt[x], fp_nums[y]);
-            if (strcmp(buf1, buf2) != 0) {
-                fprintf(stderr, "  mismatch:\n");
-                fprintf(stderr, "    str_format: \"%s\"\n"
-                                "    sprintf:    \"%s\"\n", buf1, buf2);
-                (*fail)++;
-            }
-            else {
-                fprintf(stderr, "  ok: %d \"%s\" (%d)\n", len, buf1, (int)strlen(buf1));
-                (*ok)++;
-            }
+            if (strcmp(buf1, buf2) != 0)
+                ts_test_fail(TS_CTX, "mismatch: str_format: \"%s\", snprintf: \"%s\"", buf1, buf2);
         }
     }
 
     for (x = 0; int_fmt[x] != NULL; x++) {
-        fprintf(stderr, "testing \"%s\"\n", int_fmt[x]);
+        ts_test_check(TS_CTX, "str_format(..,..,\"%s\",..)", int_fmt[x]);
         for (y = 0; int_nums[y] != 0; y++) {
             len = str_format(buf1, sizeof(buf1), int_fmt[x], int_nums[y]);
             sprintf(buf2, int_fmt[x], int_nums[y]);
-            if (strcmp(buf1, buf2) != 0) {
-                fprintf(stderr, "  mismatch:\n");
-                fprintf(stderr, "    str_format: \"%s\"\n"
-                                "    sprintf:    \"%s\"\n", buf1, buf2);
-                (*fail)++;
-            }
-            else {
-                fprintf(stderr, "  ok: %d \"%s\" (%d)\n", len, buf1, (int)strlen(buf1));
-                (*ok)++;
-            }
+            if (strcmp(buf1, buf2) != 0)
+                ts_test_fail(TS_CTX, "mismatch: str_format: \"%s\", snprintf: \"%s\"", buf1, buf2);
         }
     }
     return;
 }
 
-static void test_base64_do(unsigned char *ucp, int ulen, int mode, int *ok, int *fail)
+/*
+ * Test Base64 Encoding/Decoding
+ */
+
+static void test_base64_do(ts_test_t *_t, unsigned char *ucp, int ulen, int mode)
 {
     unsigned char ucp2[1024];
     char cp[1024];
     int n1, n2, n3, n4;
     int i;
-    int fine;
 
     n1 = str_base64(NULL, 0, ucp, ulen, STR_BASE64_ENCODE|mode);
     n2 = str_base64(cp, sizeof(cp), ucp, ulen, STR_BASE64_ENCODE|mode);
-    if (n1 != n2) {
-        fprintf(stderr, "   ERROR: encoding length mismatch: %d vs. %d\n", n1, n2);
-        (*fail)++;
-    }
-    else
-        (*ok)++;
+    if (n1 != n2)
+        ts_test_fail(TS_CTX, "encoding length mismatch: %d vs. %d\n", n1, n2);
     n3 = str_base64(cp, n2, NULL, 0, STR_BASE64_DECODE|mode);
-    if (n3 != ulen) {
-        fprintf(stderr, "   ERROR: decoding check length mismatch: %d vs. %d\n", n3, ulen);
-        (*fail)++;
-    }
-    else
-        (*ok)++;
+    if (n3 != ulen)
+        ts_test_fail(TS_CTX, "decoding check length mismatch: %d vs. %d\n", n3, ulen);
     n4 = str_base64(cp, n2, ucp2, ulen, STR_BASE64_DECODE|mode);
-    if (n3 != n4) {
-        fprintf(stderr, "   ERROR: decoding length mismatch: %d vs. %d\n", n3, n4);
-        (*fail)++;
-    }
-    else
-        (*ok)++;
-    fine = TRUE;
+    if (n3 != n4)
+        ts_test_fail(TS_CTX, "decoding length mismatch: %d vs. %d\n", n3, n4);
     for (i = 0; i < 256; i++) {
         if (ucp[i] != ucp2[i]) {
-            fine = FALSE;
+            ts_test_fail(TS_CTX, "decoding contents mismatch\n");
             break;
         }
     }
-    if (!fine) {
-        fprintf(stderr, "   ERROR: decoding mismatch\n");
-        (*fail)++;
-    }
-    else
-        (*ok)++;
     return;
 }
 
-static void test_base64(int *ok, int *fail)
+TS_TEST(test_base64)
 {
     unsigned char ucp[256];
     int i;
     
-    fprintf(stderr, "\n** Testing str_base64 **\n\n");
-
-    fprintf(stderr, "1. Encode/Decode of 0 bytes\n");
+    ts_test_check(TS_CTX, "encode/decode of 0 bytes");
     for (i = 0; i < 256; i++)
         ucp[i] = 0x55;
-    test_base64_do(ucp, 256, STR_BASE64_STRICT, ok, fail);
+    test_base64_do(_t, ucp, 256, STR_BASE64_STRICT);
 
-    fprintf(stderr, "2. Encode/Decode of increasing bytes\n");
+    ts_test_check(TS_CTX, "encode/decode of increasing bytes\n");
     for (i = 0; i < 256; i++)
         ucp[i] = i;
-    test_base64_do(ucp, 256, STR_BASE64_STRICT, ok, fail);
+    test_base64_do(_t, ucp, 256, STR_BASE64_STRICT);
 
-    fprintf(stderr, "3. Encode/Decode of distributed bytes\n");
+    ts_test_check(TS_CTX, "encode/decode of distributed bytes\n");
     for (i = 0; i < 256; i++)
         ucp[i] = i*31;
-    test_base64_do(ucp, 256, STR_BASE64_STRICT, ok, fail);
+    test_base64_do(_t, ucp, 256, STR_BASE64_STRICT);
 
     return;
 }
 
 /*
- * Main Test Driver Program
+ * Main Test Suite Procedure
  */
 
-struct {
-    char *name;
-    void (*func)(int *, int *);
-    int ok;
-    int fail;
-} tests[] = {
-    { "Length",      test_length,      0, 0 },
-    { "Locating",    test_locate,      0, 0 },
-    { "Spanning",    test_span,        0, 0 },
-    { "Tokenizing",  test_tokenize,    0, 0 },
-    { "Parsing",     test_parsing,     0, 0 },
-    { "Formatting",  test_formatting,  0, 0 },
-    { "Base64",      test_base64,      0, 0 },
-    { NULL, NULL, 0,0 }
-};
-
 int main(int argc, char *argv[])
 {
-    int i;
-    int fail;
-    int ok;
+    ts_suite_t *ts;
+    int n;
 
-    ok = 0;
-    fail = 0;
-    fprintf(stderr, "\n");
-    fprintf(stderr, "String Library Test Suite\n");
-    fprintf(stderr, "________________________________________\n\n");
-    for (i = 0; tests[i].name != NULL; i++) {
-        fprintf(stderr, "==== Testing %s ====\n", tests[i].name);
-        tests[i].ok = tests[i].fail = 0;
-        tests[i].func(&tests[i].ok, &tests[i].fail);
-        fprintf(stderr, "==== Tests failed %d/%d ====\n", tests[i].fail, tests[i].ok+tests[i].fail);
-        fprintf(stderr, "\n");
-        ok += tests[i].ok;
-        fail += tests[i].fail;
-    }
-    fprintf(stderr, "________________________________________\n\n");
-    fprintf(stderr, "TOTAL: failed %d/%d\n", fail, fail+ok);
-    fprintf(stderr, "\n");
-    return fail;
+    ts = ts_suite_new("OSSP str (String Handling)");
+    ts_suite_test(ts, test_length,     "String Length Determination");
+    ts_suite_test(ts, test_locate,     "String Locating");
+    ts_suite_test(ts, test_span,       "String Spanning");
+    ts_suite_test(ts, test_tokenize,   "String Tokenizing");
+    ts_suite_test(ts, test_parsing,    "String Parsing");
+    ts_suite_test(ts, test_formatting, "String Formatting");
+    ts_suite_test(ts, test_base64,     "String Encoding/Decoding");
+    n = ts_suite_run(ts);
+    ts_suite_free(ts);
+    return n;
 }
 

CVSTrac 2.0.1