OSSP CVS Repository

ossp - Difference in ossp-pkg/var/var_test.c versions 1.43 and 1.44
Not logged in
[Honeypot]  [Browse]  [Home]  [Login]  [Reports
[Search]  [Ticket]  [Timeline
  [History

ossp-pkg/var/var_test.c 1.43 -> 1.44

--- var_test.c   2002/03/07 12:11:09     1.43
+++ var_test.c   2002/03/07 14:58:41     1.44
@@ -32,50 +32,19 @@
 #include <errno.h>
 #include <ctype.h>
 
-#include "var.h"
+#include "config.h"
+#if defined(HAVE_DMALLOC_H) && defined(WITH_DMALLOC)
+#include "dmalloc.h"
+#endif
 
-static var_rc_t 
-var_operation(
-    var_t *var, void *ctx,
-    const char  *op_ptr, size_t op_len,
-    const char  *arg_ptr, size_t arg_len,
-    const char  *val_ptr, size_t val_len,
-    char **out_ptr, size_t *out_len, size_t *out_size)
-{
-    int i;
+#include "var.h"
+#include "ts.h"
 
-    if (val_ptr == NULL) {
-        *out_ptr = "";
-        *out_len = 0;
-        *out_size = 0;
-        return VAR_OK;
-    }
-    if (op_len == 6 && strncmp(op_ptr, "return", 6) == 0) {
-        *out_ptr = malloc(arg_len);
-        *out_len = arg_len;
-        *out_size = arg_len;
-        memcpy(*out_ptr, arg_ptr, arg_len);
-        return VAR_OK;
-    }
-    else if (op_len == 5 && strncmp(op_ptr, "upper", 5) == 0) {
-        *out_ptr = malloc(val_len);
-        *out_len = val_len;
-        *out_size = val_len;
-        for (i = 0; i < val_len; i++)
-            (*out_ptr)[i] = (char)toupper((int)(val_ptr[i]));
-        return VAR_OK;
-    }
-    else if (op_len == 5 && strncmp(op_ptr, "lower", 5) == 0) {
-        *out_ptr = malloc(val_len);
-        *out_len = val_len;
-        *out_size = val_len;
-        for (i = 0; i < val_len; i++)
-            (*out_ptr)[i] = (char)tolower((int)(val_ptr[i]));
-        return VAR_OK;
-    }
-    else 
-        return VAR_ERR_UNDEFINED_OPERATION;
-}
+/*
+**
+**  ==== VARIABLE LOOKUP CALLBACK ====
+**
+*/
 
 struct variable {
     const char *name;
@@ -83,7 +52,26 @@
     const char *data;
 };
 
-static var_rc_t var_lookup(
+static const struct variable lookup_vars[] = {
+    { "ARRAY",     0, "entry0"                 },
+    { "ARRAY",     1, "entry1"                 },
+    { "ARRAY",     2, "entry2"                 },
+    { "ARRAY",     3, "entry3"                 },
+    { "BAR",       0, "type"                   },
+    { "EMPTY",     0, ""                       },
+    { "FOO",       0, "os"                     },
+    { "HEINZ",     0, "heinz0"                 },
+    { "HEINZ",     1, "heinz1"                 },
+    { "HOME",      0, "/home/regression-tests" },
+    { "MULTILINE", 0, "line1\nline2\n"         },
+    { "NUMBER",    0, "+2"                     },
+    { "NUMEXP",    0, "((16)%5)"               },
+    { "OSTYPE",    0, "regression-os"          },
+    { "TERM",      0, "regression-term"        },
+    { NULL,        0, NULL                     }
+};
+
+static var_rc_t lookup_cb(
     var_t *var, void *context,
     const char *varname, size_t name_len, int idx,
     const char **data, size_t *data_len,
@@ -125,32 +113,67 @@
     return VAR_ERR_UNDEFINED_VARIABLE;
 }
 
-struct test_case {
-    const char *input;
-    const char *expected;
-};
+/*
+**
+**  ==== OPERATION CALLBACK ====
+**
+*/
 
-int main(int argc, char **argv)
+static var_rc_t 
+operate_cb(
+    var_t *var, void *ctx,
+    const char  *op_ptr, size_t op_len,
+    const char  *arg_ptr, size_t arg_len,
+    const char  *val_ptr, size_t val_len,
+    char **out_ptr, size_t *out_len, size_t *out_size)
 {
-    const struct variable vars[] = {
-        { "ARRAY",     0, "entry0" },
-        { "ARRAY",     1, "entry1" },
-        { "ARRAY",     2, "entry2" },
-        { "ARRAY",     3, "entry3" },
-        { "BAR",       0, "type" },
-        { "EMPTY",     0, "" },
-        { "FOO",       0, "os" },
-        { "HEINZ",     0, "heinz0" },
-        { "HEINZ",     1, "heinz1" },
-        { "HOME",      0, "/home/regression-tests" },
-        { "MULTILINE", 0, "line1\nline2\n" },
-        { "NUMBER",    0, "+2" },
-        { "NUMEXP",    0, "((16)%5)" },
-        { "OSTYPE",    0, "regression-os" },
-        { "TERM",      0, "regression-term" },
-        { NULL,        0, NULL }
-        };
+    int i;
 
+    if (val_ptr == NULL) {
+        *out_ptr = "";
+        *out_len = 0;
+        *out_size = 0;
+        return VAR_OK;
+    }
+    if (op_len == 6 && strncmp(op_ptr, "return", 6) == 0) {
+        *out_ptr = malloc(arg_len);
+        *out_len = arg_len;
+        *out_size = arg_len;
+        memcpy(*out_ptr, arg_ptr, arg_len);
+        return VAR_OK;
+    }
+    else if (op_len == 5 && strncmp(op_ptr, "upper", 5) == 0) {
+        *out_ptr = malloc(val_len);
+        *out_len = val_len;
+        *out_size = val_len;
+        for (i = 0; i < val_len; i++)
+            (*out_ptr)[i] = (char)toupper((int)(val_ptr[i]));
+        return VAR_OK;
+    }
+    else if (op_len == 5 && strncmp(op_ptr, "lower", 5) == 0) {
+        *out_ptr = malloc(val_len);
+        *out_len = val_len;
+        *out_size = val_len;
+        for (i = 0; i < val_len; i++)
+            (*out_ptr)[i] = (char)tolower((int)(val_ptr[i]));
+        return VAR_OK;
+    }
+    else 
+        return VAR_ERR_UNDEFINED_OPERATION;
+}
+
+/*
+**
+**  ==== TEST CASES ====
+**
+*/
+
+TS_TEST(test_expand)
+{
+    struct test_case {
+        const char *input;
+        const char *expected;
+    };
     const struct test_case tests[] = {
         { "${HOME}${!!}",                 "/home/regression-tests${!!}"                    },
         { "$HOME",                        "/home/regression-tests"                         },
@@ -225,26 +248,11 @@
         { "${HOME:%upper}",               "/HOME/REGRESSION-TESTS"                         },
         { "${HOME:%upper:%lower}",        "/home/regression-tests"                         },
         { "${EMPTY:%return($HOME)}",      "/home/regression-tests"                         },
-        {
-        "[${ARRAY}:${ARRAY[#]}-]",
-        "entry0:entry0-entry0:entry1-entry0:entry2-entry0:entry3-"
-        },
-        {
-        "[${HEINZ[#]}:${ARRAY[#]}-]",
-        "heinz0:entry0-heinz1:entry1-:entry2-:entry3-"
-        },
-        {
-        "[${HEINZ[#]}:[${ARRAY[#]}] ]",
-        "heinz0:entry0entry1entry2entry3 heinz1:entry0entry1entry2entry3 "
-        },
-        {
-        "[${HEINZ[#]}: [${ARRAY[#]}${ARRAY[#+1]:+, }]${HEINZ[#+1]:+; }]",
-        "heinz0: entry0, entry1, entry2, entry3; heinz1: entry0, entry1, entry2, entry3"
-        },
-        {
-        "[${ARRAY[#]}:[${ARRAY[#]},]{1,2,} ]{0,2,}",
-        "entry0:entry1,entry3, entry2:entry1,entry3, "
-        },
+        { "[${ARRAY}:${ARRAY[#]}-]",      "entry0:entry0-entry0:entry1-entry0:entry2-entry0:entry3-" },
+        { "[${HEINZ[#]}:${ARRAY[#]}-]",   "heinz0:entry0-heinz1:entry1-:entry2-:entry3-" }, 
+        { "[${HEINZ[#]}:[${ARRAY[#]}] ]", "heinz0:entry0entry1entry2entry3 heinz1:entry0entry1entry2entry3 " },
+        { "[${HEINZ[#]}: [${ARRAY[#]}${ARRAY[#+1]:+, }]${HEINZ[#+1]:+; }]", "heinz0: entry0, entry1, entry2, entry3; heinz1: entry0, entry1, entry2, entry3" },
+        { "[${ARRAY[#]}:[${ARRAY[#]},]{1,2,} ]{0,2,}", "entry0:entry1,entry3, entry2:entry1,entry3, " },
     };
     char *tmp;
     size_t tmp_len;
@@ -254,85 +262,133 @@
     var_t *var;
     char *err;
 
+    ts_test_check(TS_CTX, "create environment");
     if ((rc = var_create(&var)) != VAR_OK) {
         var_strerror(NULL, rc, &err);
-        printf("unable to create variable expansion context: %s (%d)\n", err, rc);
-        return 1;
+        ts_test_fail(TS_CTX, "var_create: %s (%d)", err, rc);
+        return;
     }
-    if ((rc = var_config(var, VAR_CONFIG_CB_VALUE, var_lookup, vars)) != VAR_OK) {
+    if ((rc = var_config(var, VAR_CONFIG_CB_VALUE, lookup_cb, lookup_vars)) != VAR_OK) {
         var_strerror(NULL, rc, &err);
-        printf("unable to configure variable expansion context: %s (%d)\n", err, rc);
-        return 1;
+        ts_test_fail(TS_CTX, "var_config: %s (%d)", err, rc);
+        return;
     }
-    if ((rc = var_config(var, VAR_CONFIG_CB_OPERATION, var_operation, NULL)) != VAR_OK) {
+    if ((rc = var_config(var, VAR_CONFIG_CB_OPERATION, operate_cb, NULL)) != VAR_OK) {
         var_strerror(NULL, rc, &err);
-        printf("unable to configure variable expansion context: %s (%d)\n", err, rc);
-        return 1;
+        ts_test_fail(TS_CTX, "var_config: %s (%d)", err, rc);
+        return;
     }
 
-    for (i = 0; i < sizeof(tests)/sizeof(struct test_case); ++i) {
-        printf("Test case #%02d: Original input is '%s'.\n", i, tests[i].input);
-        rc = var_unescape(var, tests[i].input, strlen(tests[i].input), buffer, sizeof(buffer), 0);
+    for (i = 0; i < sizeof(tests)/sizeof(struct test_case); i++) {
+        ts_test_check(TS_CTX, "expansion (#%d): raw input \"%s\"", i, tests[i].input);
+        rc = var_unescape(var, tests[i].input, strlen(tests[i].input), 
+                          buffer, sizeof(buffer), 0);
         if (rc != VAR_OK) {
             var_strerror(var, rc, &err);
-            printf("Test case #%02d: var_unescape() failed: %s (%d)\n", i, err, rc);
-            return 1;
+            ts_test_fail(TS_CTX, "var_unescape: %s (%d)", err, rc);
+            continue;
         }
-        printf("Test case #%02d: Unescaped input is '%s'.\n", i, buffer);
+        ts_test_check(TS_CTX, "expansion (#%d): unescaped input \"%s\"", i, buffer);
         rc = var_expand(var, buffer, strlen(buffer), &tmp, &tmp_len, 0);
         if (rc != VAR_OK) {
             var_strerror(var, rc, &err);
-            printf("Test case #%02d: var_expand() failed: %s (%d).\n", i, err, rc);
-            return 1;
+            ts_test_fail(TS_CTX, "var_expand: %s (%d)", err, rc);
+            continue;
         }
-        printf("Test case #%02d: Expanded output is '%s'.\n", i, tmp);
+        ts_test_check(TS_CTX, "expansion (#%d): expanded output \"%s\"", i, tmp);
         if (   tmp_len != strlen(tests[i].expected)
-            || tmp == NULL
             || memcmp(tests[i].expected, tmp, tmp_len) != 0) {
-            printf("Test case #%02d: Expected result '%s' but got '%s'.\n",
-                   i, tests[i].expected, tmp);
-            return 1;
+            ts_test_fail(TS_CTX, "expected \"%s\", got \"%s\"", tests[i].expected, tmp);
+            free(tmp);
+            continue;
         }
         free(tmp);
     }
 
-    fprintf(stderr, "Test case: formatting \"foo\"\n");
-    if ((rc = var_format(var, &tmp, 1, "foo")) != VAR_OK) {
-        var_strerror(var, rc, &err);
-        fprintf(stderr, "Formatting failed: %s (%d)\n", err, rc);
-        return 1;
+    var_destroy(var);
+
+    return;
+}
+
+TS_TEST(test_format)
+{
+    var_rc_t rc;
+    var_t *var;
+    char *err;
+    char *fmt;
+    char *exp;
+    char *got;
+
+    ts_test_check(TS_CTX, "create environment");
+    if ((rc = var_create(&var)) != VAR_OK) {
+        var_strerror(NULL, rc, &err);
+        ts_test_fail(TS_CTX, "var_create: %s (%d)", err, rc);
+        return;
     }
-    if (strcmp(tmp, "foo") != 0) {
-        fprintf(stderr, "Formatting failed: expected \"foo\", got \"%s\"\n", tmp);
-        return 1;
+    if ((rc = var_config(var, VAR_CONFIG_CB_VALUE, lookup_cb, lookup_vars)) != VAR_OK) {
+        var_strerror(NULL, rc, &err);
+        ts_test_fail(TS_CTX, "var_config: %s (%d)", err, rc);
+        return;
     }
-    free(tmp);
 
-    fprintf(stderr, "Test case: formatting \"foo<bar><123><x>\"\n");
-    if ((rc = var_format(var, &tmp, 1, "foo<%s><%d><%c>", "bar", 123, 'x')) != VAR_OK) {
+    /* check trivial formatting */
+    fmt = "foo";
+    exp = "foo";
+    ts_test_check(TS_CTX, "formatting \"%s\"", fmt);
+    if ((rc = var_format(var, &got, 1, fmt)) != VAR_OK) {
         var_strerror(var, rc, &err);
-        fprintf(stderr, "Formatting failed: %s (%d)\n", err, rc);
-        return 1;
+        ts_test_fail(TS_CTX, "var_format: %s (%d)", err, rc);
+        return;
     }
-    if (strcmp(tmp, "foo<bar><123><x>") != 0) {
-        fprintf(stderr, "Formatting failed: expected \"foo<bar><123><x>\", got \"%s\"\n", tmp);
-        return 1;
+    if (strcmp(got, exp) != 0) {
+        ts_test_fail(TS_CTX, "var_format: expected \"%s\", got \"%s\"\n", exp, got);
+        return;
+    }
+    free(got);
+
+    /* check real formatting */
+    fmt = "foo<%s><%d><%c>quux";
+    exp = "foo<bar><123><x>quux";
+    ts_test_check(TS_CTX, "formatting \"%s\"", fmt);
+    if ((rc = var_format(var, &got, 1, fmt, "bar", 123, 'x')) != VAR_OK) {
+        var_strerror(var, rc, &err);
+        ts_test_fail(TS_CTX, "var_format: %s (%d)", err, rc);
+        return;
     }
-    free(tmp);
-
-    fprintf(stderr, "Test case: formatting \"foo${ARRAY[1]}bar\"\n");
-    if ((rc = var_format(var, &tmp, 1, "foo${ARRAY[%d]}bar", 1)) != VAR_OK) {
+    if (strcmp(got, exp) != 0) {
+        ts_test_fail(TS_CTX, "var_format: expected \"%s\", got \"%s\"\n", exp, got);
+        return;
+    }
+    free(got);
+
+    /* check combined formatting and expansion */
+    fmt = "foo<${ARRAY[%d]}>bar";
+    exp = "foo<entry1>bar";
+    ts_test_check(TS_CTX, "formatting \"%s\"", fmt);
+    if ((rc = var_format(var, &got, 1, fmt, 1)) != VAR_OK) {
         var_strerror(var, rc, &err);
-        fprintf(stderr, "Formatting failed: %s (%d)\n", err, rc);
-        return 1;
+        ts_test_fail(TS_CTX, "var_format: %s (%d)", err, rc);
+        return;
     }
-    if (strcmp(tmp, "fooentry1bar") != 0) {
-        fprintf(stderr, "Formatting failed: expected \"fooentry1bar\", got \"%s\"\n", tmp);
-        return 1;
+    if (strcmp(got, exp) != 0) {
+        ts_test_fail(TS_CTX, "var_format: expected \"%s\", got \"%s\"\n", exp, got);
+        return;
     }
-    free(tmp);
+    free(got);
 
     var_destroy(var);
-    return 0;
+}
+
+int main(int argc, char *argv[])
+{
+    ts_suite_t *ts;
+    int n;
+
+    ts = ts_suite_new("OSSP var (Variable Expansion)");
+    ts_suite_test(ts, test_expand,  "expansion");
+    ts_suite_test(ts, test_format,  "formatting");
+    n = ts_suite_run(ts);
+    ts_suite_free(ts);
+    return n;
 }
 

CVSTrac 2.0.1