ctest_resourcematch.c 4.83 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
/* Icecast
 *
 * This program is distributed under the GNU General Public License, version 2.
 * A copy of this license is included with this source.
 *
 * Copyright 2018,      Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>,
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stddef.h> /* for NULL */
#include <stdio.h> /* for snprintf() */

#include "ctest_lib.h"

#include "../src/resourcematch.h"

struct test {
    const char *pattern;
    const char *string;
    resourcematch_result_t expected_result;
};

static const struct test tests[] = {
    {NULL,      NULL,       RESOURCEMATCH_ERROR},
    {"",        NULL,       RESOURCEMATCH_ERROR},
    {NULL,      "",         RESOURCEMATCH_ERROR},
    {"",        "",         RESOURCEMATCH_MATCH},
    {"a",       "a",        RESOURCEMATCH_MATCH},
    {"aa",      "b",        RESOURCEMATCH_NOMATCH},
    {"aa",      "ab",       RESOURCEMATCH_NOMATCH},
    {"aa",      "ba",       RESOURCEMATCH_NOMATCH},
    {"aa",      "aa",       RESOURCEMATCH_MATCH},
    {"a/a",     "a/a",      RESOURCEMATCH_MATCH},
    {"a/%%a",   "a/%a",     RESOURCEMATCH_MATCH},

    {"a/%i",    "a/0",      RESOURCEMATCH_MATCH},
    {"a/%i",    "a/1",      RESOURCEMATCH_MATCH},

    {"a/%i",    "a/12",     RESOURCEMATCH_MATCH},
    {"a/%i",    "a/0x12",   RESOURCEMATCH_MATCH},
    {"a/%i",    "a/012",    RESOURCEMATCH_MATCH},

    {"a/%d",    "a/12",     RESOURCEMATCH_MATCH},
    {"a/%d",    "a/0x12",   RESOURCEMATCH_NOMATCH},
    {"a/%d",    "a/012",    RESOURCEMATCH_MATCH},

    {"a/%x",    "a/12",     RESOURCEMATCH_MATCH},
    {"a/%x",    "a/0x12",   RESOURCEMATCH_MATCH},
    {"a/%x",    "a/012",    RESOURCEMATCH_MATCH},

    {"a/%o",    "a/12",     RESOURCEMATCH_MATCH},
    {"a/%o",    "a/0x12",   RESOURCEMATCH_NOMATCH},
    {"a/%o",    "a/012",    RESOURCEMATCH_MATCH},

    {"a/%i/b",  "a/X/b",    RESOURCEMATCH_NOMATCH},

    {"a/%d/b",  "a/12/b",   RESOURCEMATCH_MATCH},
    {"a/%d/b",  "a/0x12/b", RESOURCEMATCH_NOMATCH},
    {"a/%d/b",  "a/012/b",  RESOURCEMATCH_MATCH},

    {"a/%x/b",  "a/12/b",   RESOURCEMATCH_MATCH},
    {"a/%x/b",  "a/0x12/b", RESOURCEMATCH_MATCH},
    {"a/%x/b",  "a/012/b",  RESOURCEMATCH_MATCH},

    {"a/%o/b",  "a/12/b",   RESOURCEMATCH_MATCH},
    {"a/%o/b",  "a/0x12/b", RESOURCEMATCH_NOMATCH},
    {"a/%o/b",  "a/012/b",  RESOURCEMATCH_MATCH},

    {"a/%i/%i/b", "a/1/2/b", RESOURCEMATCH_MATCH}
};

static const char *res2str(resourcematch_result_t res)
{
    switch (res) {
        case RESOURCEMATCH_ERROR:
            return "error";
        break;
        case RESOURCEMATCH_MATCH:
            return "match";
        break;
        case RESOURCEMATCH_NOMATCH:
            return "nomatch";
        break;
        default:
            return "<unknown>";
        break;
    }
}

static inline resourcematch_result_t run_test_base(const struct test *test, resourcematch_extract_t **extract)
{
    char name[128];
    resourcematch_result_t ret;
    int ok = 1;

    ret = resourcematch_match(test->pattern, test->string, extract);

    //printf(" expected %s, got %s", res2str(test->expected_result), res2str(ret));

    if (extract) {
        if (test->expected_result == RESOURCEMATCH_MATCH) {
            if (*extract) {
                ctest_diagnostic(" got extract");
            } else {
                ctest_diagnostic(" got no extract");
                ok = 0;
            }
        }
    }

    snprintf(name, sizeof(name), "pattern \"%s\" and string \"%s\" %s extract", test->pattern, test->string, extract ? "with" : "without");
    ctest_test(name, test->expected_result == ret && ok);

    return ret;
}

static inline void print_extract_group(resourcematch_extract_t *extract, size_t idx)
{
    switch (extract->group[idx].type) {
        case 'i':
        case 'd':
        case 'x':
        case 'o':
            ctest_diagnostic_printf("   Group %zu, type \"%c\": value is %lli", idx, extract->group[idx].type, extract->group[idx].result.lli);
        break;
        default:
            ctest_diagnostic_printf("   Group %zu, type \"%c\": <raw value at %p>", idx, extract->group[idx].type, extract->group[idx].raw);
        break;
    }
}

static inline void print_extract(resourcematch_extract_t *extract)
{
    size_t i;

Philipp Schafft's avatar
Philipp Schafft committed
139
    ctest_diagnostic_printf("  Extract with %zu groups:", extract->groups);
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173

    for (i = 0; i < extract->groups; i++) {
        print_extract_group(extract, i);
    }
}

static void run_test(const struct test *test)
{
    resourcematch_result_t ret;
    resourcematch_extract_t *extract = NULL;

    run_test_base(test, NULL);

    ret = run_test_base(test, &extract);
    if (extract) {
        if (ret == RESOURCEMATCH_MATCH)
            print_extract(extract);

        resourcematch_extract_free(extract);
    }
}

int main (void)
{
    size_t i;

    ctest_init();
    for (i = 0; i < (sizeof(tests)/sizeof(*tests)); i++) {
        run_test(&(tests[i]));
    }
    ctest_fin();

    return 0;
}