File: cs.c

package info (click to toggle)
libigloo 0.9.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,972 kB
  • sloc: ansic: 5,536; sh: 4,996; makefile: 62
file content (332 lines) | stat: -rw-r--r-- 10,778 bytes parent folder | download
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* Copyright (C) 2018-2021  Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */


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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <igloo/cs.h>
#include <igloo/tap.h>
#include <igloo/error.h>

struct vector {
    const char *in;
    const char *out;
};

static const struct vector skip_spaces[] = {
    {"a", "a"},
    {" a", "a"},
    {"  a", "a"},
    {"a ", "a "},
    {"a b", "a b"}
};


static const struct vector to_lower[] = {
    {"a", "a"},
    {"A", "a"},
    {"0", "0"},
    {"ab", "ab"},
    {"Ab", "ab"},
    {"AB", "ab"},
    {"0b", "0b"},
    {"0B", "0b"}
};
static const struct vector to_upper[] = {
    {"a", "A"},
    {"A", "A"},
    {"0", "0"},
    {"ab", "AB"},
    {"Ab", "AB"},
    {"AB", "AB"},
    {"0b", "0B"},
    {"0B", "0B"}
};
static const struct vector to_lower_first[] = {
    {"a", "a"},
    {"A", "a"},
    {"0", "0"},
    {"ab", "ab"},
    {"Ab", "ab"},
    {"AB", "aB"},
    {"0b", "0b"},
    {"0B", "0B"}
};
static const struct vector to_upper_first[] = {
    {"a", "A"},
    {"A", "A"},
    {"0", "0"},
    {"ab", "Ab"},
    {"Ab", "Ab"},
    {"AB", "AB"},
    {"0b", "0b"},
    {"0B", "0B"}
};

static void test_vectors(const struct vector *vectors, size_t len, const char *func, igloo_error_t (*check)(char *in, char **out))
{
    size_t i;

    for (i = 0; i < len; i++) {
        const struct vector *v = &(vectors[i]);
        char buffer[80];
        char *in;
        char *out;
        bool res;

        if (v->in) {
            if (strlen(v->in) >= sizeof(buffer)) {
                igloo_tap_bail_out("ERROR: Vector is bigger than buffer");
                return;
            }

            strncpy(buffer, v->in, sizeof(buffer));
            in = buffer;
        } else {
            in = NULL;
        }

        igloo_tap_test_success(func, check(in, &out));

        if (!v->out && !out) {
            res = true;
        } else if (v->out && out && strcmp(v->out, out) == 0) {
            res = true;
        } else {
            res = false;
        }

        igloo_tap_test("output match", res);
    }
}

static igloo_error_t skip_spaces_impl(char *in, char **out)
{
    *out = in;
    return igloo_cs_skip_spaces((const char **)out);
}

static void test_skip_spaces(void)
{
    test_vectors(skip_spaces, sizeof(skip_spaces)/sizeof(*skip_spaces), "igloo_cs_skip_spaces", skip_spaces_impl);
}

static igloo_error_t to_lower_impl(char *in, char **out)
{
    *out = in;
    return igloo_cs_to_lower(in);
}

static void test_to_lower(void)
{
    test_vectors(to_lower, sizeof(to_lower)/sizeof(*to_lower), "igloo_cs_to_lower", to_lower_impl);
}

static igloo_error_t to_upper_impl(char *in, char **out)
{
    *out = in;
    return igloo_cs_to_upper(in);
}

static void test_to_upper(void)
{
    test_vectors(to_upper, sizeof(to_upper)/sizeof(*to_upper), "igloo_cs_to_upper", to_upper_impl);
}

static igloo_error_t to_lower_first_impl(char *in, char **out)
{
    *out = in;
    return igloo_cs_to_lower_first(in);
}

static void test_to_lower_first(void)
{
    test_vectors(to_lower_first, sizeof(to_lower_first)/sizeof(*to_lower_first), "igloo_cs_to_lower_first", to_lower_first_impl);
}

static igloo_error_t to_upper_first_impl(char *in, char **out)
{
    *out = in;
    return igloo_cs_to_upper_first(in);
}

static void test_to_upper_first(void)
{
    test_vectors(to_upper_first, sizeof(to_upper_first)/sizeof(*to_upper_first), "igloo_cs_to_upper_first", to_upper_first_impl);
}

static void test_to_bool(void)
{
    static const struct {
        const char *in;
        const bool res;
    } vectors[] = {
        {"true", true},
        {"1", true},
        {"yes", true},
        {"false", false},
        {"0", false},
        {"no", false},
    };
    bool res;
    size_t i;
    char testname[80];

    for (i = 0; i < (sizeof(vectors)/sizeof(*vectors)); i++) {
        snprintf(testname, sizeof(testname), "igloo_cs_to_bool str=\"%s\"", vectors[i].in);
        res = !vectors[i].res;
        igloo_tap_test_success(testname, igloo_cs_to_bool(vectors[i].in, &res));
        igloo_tap_test("res match", res == vectors[i].res);
    }

    igloo_tap_test_error("igloo_cs_to_bool str=\"\"", igloo_ERROR_ILLSEQ, igloo_cs_to_bool("", &res));
    igloo_tap_test_error("igloo_cs_to_bool str=\"a\"", igloo_ERROR_ILLSEQ, igloo_cs_to_bool("a", &res));
    igloo_tap_test_error("igloo_cs_to_bool str=\"5\"", igloo_ERROR_ILLSEQ, igloo_cs_to_bool("5", &res));
    igloo_tap_test_error("igloo_cs_to_bool str=\"-1\"", igloo_ERROR_ILLSEQ, igloo_cs_to_bool("-1", &res));
}

static void test_to_int(void)
{
    static const struct {
        const char *in;
        const int res;
    } vectors[] = {
        {"0", 0},
        {"1", 1},
        {"-1", -1},
        {" 0", 0},
        {" 1", 1},
        {" -1", -1},
        {" 0 ", 0},
        {" 1 ", 1},
        {" -1 ", -1},
        {"256", 256},
        {"-256", -256},
        {"32767", 32767},
        {"-32768", -32768}
    };
    int res;
    size_t i;
    char testname[80];

    for (i = 0; i < (sizeof(vectors)/sizeof(*vectors)); i++) {
        snprintf(testname, sizeof(testname), "igloo_cs_to_int str=\"%s\"", vectors[i].in);
        res = ~vectors[i].res;
        igloo_tap_test_success(testname, igloo_cs_to_int(vectors[i].in, &res));
        igloo_tap_test("res match", res == vectors[i].res);
    }

    igloo_tap_test_error("igloo_cs_to_int str=\"\"", igloo_ERROR_ILLSEQ, igloo_cs_to_int("", &res));
    igloo_tap_test_error("igloo_cs_to_int str=\"a\"", igloo_ERROR_ILLSEQ, igloo_cs_to_int("a", &res));
    igloo_tap_test_error("igloo_cs_to_int str=\"0g\"", igloo_ERROR_ILLSEQ, igloo_cs_to_int("0g", &res));
    igloo_tap_test_error("igloo_cs_to_int str=\"0xg\"", igloo_ERROR_ILLSEQ, igloo_cs_to_int("0xg", &res));
}

static void test_to_uint(void)
{
    static const struct {
        const char *in;
        const unsigned int res;
    } vectors[] = {
        {"0", 0},
        {"1", 1},
        {" 0", 0},
        {" 1", 1},
        {" 0 ", 0},
        {" 1 ", 1},
        {"256", 256},
        {"32767", 32767}
    };
    unsigned int res;
    size_t i;
    char testname[80];

    for (i = 0; i < (sizeof(vectors)/sizeof(*vectors)); i++) {
        snprintf(testname, sizeof(testname), "igloo_cs_to_uint str=\"%s\"", vectors[i].in);
        res = ~vectors[i].res;
        igloo_tap_test_success(testname, igloo_cs_to_uint(vectors[i].in, &res));
        igloo_tap_test("res match", res == vectors[i].res);
    }

    igloo_tap_test_error("igloo_cs_to_uint str=\"\"", igloo_ERROR_ILLSEQ, igloo_cs_to_uint("", &res));
    igloo_tap_test_error("igloo_cs_to_uint str=\"a\"", igloo_ERROR_ILLSEQ, igloo_cs_to_uint("a", &res));
    igloo_tap_test_error("igloo_cs_to_uint str=\"0g\"", igloo_ERROR_ILLSEQ, igloo_cs_to_uint("0g", &res));
    igloo_tap_test_error("igloo_cs_to_uint str=\"0xg\"", igloo_ERROR_ILLSEQ, igloo_cs_to_uint("0xg", &res));
    igloo_tap_test_error("igloo_cs_to_uint str=\"-1\"", igloo_ERROR_RANGE, igloo_cs_to_uint("-1", &res));
}


static void test_api_errors(void) {
    const char *null_pointer = NULL;
    bool rb;
    int rsi;
    unsigned int rui;
    char *rs;

    igloo_tap_test_error("igloo_cs_skip_spaces str=NULL", igloo_ERROR_FAULT, igloo_cs_skip_spaces(NULL));
    igloo_tap_test_error("igloo_cs_skip_spaces *str=NULL", igloo_ERROR_FAULT, igloo_cs_skip_spaces(&null_pointer));

    igloo_tap_test_error("igloo_cs_to_bool str=NULL", igloo_ERROR_FAULT, igloo_cs_to_bool(NULL, &rb));
    igloo_tap_test_error("igloo_cs_to_bool res=NULL", igloo_ERROR_FAULT, igloo_cs_to_bool("", NULL));
    igloo_tap_test_error("igloo_cs_to_bool str=NULL, res=NULL", igloo_ERROR_FAULT, igloo_cs_to_bool(NULL, NULL));

    igloo_tap_test_error("igloo_cs_to_int str=NULL", igloo_ERROR_FAULT, igloo_cs_to_int(NULL, &rsi));
    igloo_tap_test_error("igloo_cs_to_int res=NULL", igloo_ERROR_FAULT, igloo_cs_to_int("", NULL));
    igloo_tap_test_error("igloo_cs_to_int str=NULL, res=NULL", igloo_ERROR_FAULT, igloo_cs_to_int(NULL, NULL));

    igloo_tap_test_error("igloo_cs_to_uint str=NULL", igloo_ERROR_FAULT, igloo_cs_to_uint(NULL, &rui));
    igloo_tap_test_error("igloo_cs_to_uint res=NULL", igloo_ERROR_FAULT, igloo_cs_to_uint("", NULL));
    igloo_tap_test_error("igloo_cs_to_uint str=NULL, res=NULL", igloo_ERROR_FAULT, igloo_cs_to_uint(NULL, NULL));

    igloo_tap_test_error("igloo_cs_to_lower str=NULL", igloo_ERROR_FAULT, igloo_cs_to_lower(NULL));
    igloo_tap_test_error("igloo_cs_to_upper str=NULL", igloo_ERROR_FAULT, igloo_cs_to_upper(NULL));
    igloo_tap_test_error("igloo_cs_to_lower_first str=NULL", igloo_ERROR_FAULT, igloo_cs_to_lower_first(NULL));
    igloo_tap_test_error("igloo_cs_to_upper_first str=NULL", igloo_ERROR_FAULT, igloo_cs_to_upper_first(NULL));

    rs = NULL;
    igloo_tap_test_error("igloo_cs_to_hex in=NULL, len=0", igloo_ERROR_FAULT, igloo_cs_to_hex(NULL, &rs, 0));
    igloo_tap_test("rs == NULL", rs == NULL);
    rs = NULL;
    igloo_tap_test_error("igloo_cs_to_hex in=NULL, len=1", igloo_ERROR_FAULT, igloo_cs_to_hex(NULL, &rs, 1));
    igloo_tap_test("rs == NULL", rs == NULL);
    igloo_tap_test_error("igloo_cs_to_hex res=NULL, len=0", igloo_ERROR_FAULT, igloo_cs_to_hex("", NULL, 0));
    igloo_tap_test_error("igloo_cs_to_hex res=NULL, len=1", igloo_ERROR_FAULT, igloo_cs_to_hex("a", NULL, 1));
}

int main (void) {
    igloo_tap_init();
    igloo_tap_exit_on(igloo_TAP_EXIT_ON_FIN, NULL);
    igloo_tap_group_run("skip spaces", test_skip_spaces);
    igloo_tap_group_run("to upper", test_to_upper);
    igloo_tap_group_run("to lower", test_to_lower);
    igloo_tap_group_run("to upper first", test_to_upper_first);
    igloo_tap_group_run("to lower first", test_to_lower_first);
    igloo_tap_group_run("to bool", test_to_bool);
    igloo_tap_group_run("to int", test_to_int);
    igloo_tap_group_run("to uint", test_to_uint);
    igloo_tap_group_run("API errors", test_api_errors);
    igloo_tap_fin();

    return EXIT_FAILURE; // return failure as we should never reach this point!
}