File: transforms_specparse.c

package info (click to toggle)
adios 1.13.1-31
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 23,692 kB
  • sloc: ansic: 133,236; f90: 8,791; sh: 7,779; python: 7,648; xml: 3,793; makefile: 2,996; cpp: 2,340; java: 626; sed: 16; perl: 8
file content (163 lines) | stat: -rw-r--r-- 5,972 bytes parent folder | download | duplicates (5)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * transforms_specparse.c
 *
 * Tests the "specparse" functionality, which parses the string passed as transform="..." into
 * the transform ID and a list of key-value pairs.
 *
 *  Created on: Jul 25, 2013
 *      Author: David A. Boyuka II
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "core/transforms/adios_transforms_specparse.h"

#define DISABLE_SPECPARSE_TESTS

#ifdef DISABLE_SPECPARSE_TESTS

int main(int argc, char **argv) { return 0; }

#else

struct specparse_test {
    const char *specstr;
    struct adios_transform_spec expected;
} TESTS[] = {
    {   .specstr = "identity:a=123,b,c=321,,,f=12321",
        .expected = {
            .transform_type     = adios_transform_identity,
            .transform_type_str = "identity",
            .param_count        = 6,
            .params             = (struct adios_transform_spec_kv_pair *) {
                (struct adios_transform_spec_kv_pair){ .key = "a", .value = "123"  },
                (struct adios_transform_spec_kv_pair){ .key = "b", .value = NULL   },
                (struct adios_transform_spec_kv_pair){ .key = "c", .value = "321"  },
                (struct adios_transform_spec_kv_pair){ .key = "",  .value = NULL   },
                (struct adios_transform_spec_kv_pair){ .key = "",  .value = NULL   },
                (struct adios_transform_spec_kv_pair){ .key = "f", .value = "12321"},
            }
        }
    },
    {   .specstr = "identity",
        .expected = {
            .transform_type     = adios_transform_identity,
            .transform_type_str = "identity",
            .param_count        = 0,
            .params             = NULL
        }
    },
    {   .specstr = "none:a=123,b,c=321,,,f=12321",
        .expected = {
            .transform_type     = adios_transform_none,
            .transform_type_str = "none",
            .param_count        = 0,
            .params             = NULL
        }
    },
    {   .specstr = "***impossible-transform-name***:a=123,b,c=321,,,f=12321",
        .expected = {
            .transform_type     = adios_transform_unknown,
            .transform_type_str = "***impossible-transform-name***",
            .param_count        = 0,
            .params             = NULL
        }
    },
};


const int NUM_TESTS = sizeof(TESTS)/sizeof(TESTS[0]);

void run_test(struct specparse_test *test) {
    const struct adios_transform_spec *actual = adios_transform_parse_spec(test->specstr, NULL);
    const struct adios_transform_spec *expected = &test->expected;

    // Check transform type ID
    assert(actual->transform_type == expected->transform_type);

    // Check transform type string
    assert(actual->transform_type_str && expected->transform_type_str);
    assert(strcmp(actual->transform_type_str, expected->transform_type_str) == 0);

    // Check parameter count, and ensure parameter list exists for both or neither
    assert(actual->param_count == expected->param_count);
    assert((actual->params != NULL) == (expected->params != NULL));

    // If there is a parameter list, check it
    if (expected->params) {
        int i;
        for (i = 0; i < expected->param_count; i++) {
            const struct adios_transform_spec_kv_pair *actual_p = &actual->params[i];
            const struct adios_transform_spec_kv_pair *expected_p = &expected->params[i];

            // Check that the keys are non-NULL and match
            assert(actual_p->key && expected_p->key);
            assert(strcmp(actual_p->key, expected_p->key) == 0);

            // Check that values are either both or neither present, and if the former, that they match
            assert((actual_p->value != NULL) == (expected_p->value != NULL));
            if (expected_p->value != NULL)
                assert(strcmp(actual_p->value, expected_p->value) == 0);
        }
    }

    adios_transform_free_spec(&actual);
}

void init_tests() {
    TESTS[0].specstr = strdup ("identity:a=123,b,c=321,,,f=12321");
    TESTS[0].expected.transform_type = adios_transform_identity;
    TESTS[0].expected.transform_type_str = "identity";
    TESTS[0].expected.param_count = 6;
    TESTS[0].expected.params = (struct adios_transform_spec_kv_pair *)
                  malloc (sizeof (struct adios_transform_spec_kv_pair) * 6);
    TESTS[0].expected.params[0].key = strdup ("a");
    TESTS[0].expected.params[0].value = strdup ("123");
    TESTS[0].expected.params[1].key = strdup ("b");
    TESTS[0].expected.params[1].value = NULL;
    TESTS[0].expected.params[2].key = strdup ("c");
    TESTS[0].expected.params[2].value = strdup ("321");
    TESTS[0].expected.params[3].key = strdup ("");
    TESTS[0].expected.params[3].value = NULL;
    TESTS[0].expected.params[4].key = strdup ("");
    TESTS[0].expected.params[4].value = NULL;
    TESTS[0].expected.params[5].key = strdup ("f");
    TESTS[0].expected.params[5].value = strdup ("12321");

    TESTS[1].specstr = strdup ("identity");
    TESTS[1].expected.transform_type = adios_transform_identity;
    TESTS[1].expected.transform_type_str = "identity";
    TESTS[1].expected.param_count = 0;
    TESTS[1].expected.params = NULL;

    TESTS[2].specstr = strdup ("none:a=123,b,c=321,,,f=12321");
    TESTS[2].expected.transform_type = adios_transform_none;
    TESTS[2].expected.transform_type_str = "none";
    TESTS[2].expected.param_count = 0;
    TESTS[2].expected.params = NULL;

    TESTS[3].specstr = strdup ("***impossible-transform-name***:a=123,b,c=321,,,f=12321");
    TESTS[3].expected.transform_type = adios_transform_unknown;
    TESTS[3].expected.transform_type_str = "***impossible-transform-name***";
    TESTS[3].expected.param_count = 0;
    TESTS[3].expected.params = NULL;
}

void run_tests() {
    int i;
    for (i = 0; i < NUM_TESTS; i++) {
        run_test(&TESTS[i]);
    }
}

int main(int argc, char **argv) {

    init_tests();
    run_tests();

    return 0;
}

#endif /* else of DISABLE_SPECPARSE_TESTS */