aboutsummaryrefslogtreecommitdiff
path: root/tests/test_str.c
blob: ad0c07a3f8ffa82aa1281914a966dce86fd3174c (plain) (blame)
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include "testing.h"

void test_to_short_version() {
    struct testcase {
        const char *data;
        const char *expected;
    };

    struct testcase tc[] = {
            {.data = "1.2.3", .expected = "123"},
            {.data = "py3.12", .expected = "py312"},
            {.data = "generic-1.2.3", .expected = "generic-123"},
            {.data = "nothing to do", .expected = "nothing to do"},
    };

    for (size_t i = 0; i < sizeof(tc) / sizeof(*tc); i++) {
        char *result = to_short_version(tc[i].data);
        STASIS_ASSERT_FATAL(result != NULL, "should not be NULL");
        STASIS_ASSERT(strcmp(result, tc[i].expected) == 0, "unexpected result");
        guard_free(result);
    }

}

void test_tolower_s() {
    struct testcase {
        const char *data;
        const char *expected;
    };

    struct testcase tc[] = {
        {.data = "HELLO WORLD", .expected = "hello world"},
        {.data = "Hello World", .expected = "hello world"},
        {.data = "hello world", .expected = "hello world"},
    };

    for (size_t i = 0; i < sizeof(tc) / sizeof(*tc); i++) {
        char input[100] = {0};
        strcpy(input, tc[i].data);
        tolower_s(input);
        STASIS_ASSERT(strcmp(input, tc[i].expected) == 0, "unexpected result");
    }
}

void test_isdigit_s() {
    struct testcase {
        const char *data;
        int expected;
    };

    struct testcase tc[] = {
        {.data = "", .expected = false},
        {.data = "1", .expected = true},
        {.data = "1234567890", .expected = true},
        {.data = " 1 ", .expected = false},
        {.data = "a number", .expected = false},
        {.data = "10 numbers", .expected = false},
        {.data = "10 10", .expected = false}
    };

    for (size_t i = 0; i < sizeof(tc) / sizeof(*tc); i++) {
        STASIS_ASSERT(isdigit_s(tc[i].data) == tc[i].expected, "unexpected result");
    }
}

void test_strdup_array_and_strcmp_array() {
    struct testcase {
        const char **data;
        const char **expected;
    };
    const struct testcase tc[] = {
        {.data = (const char *[]) {"abc", "123", NULL},
         .expected = (const char *[]) {"abc", "123", NULL}},
        {.data = (const char *[]) {"I", "have", "a", "pencil", "box", NULL},
         .expected = (const char *[]) {"I", "have", "a", "pencil", "box", NULL}},
    };

    STASIS_ASSERT(strdup_array(NULL) == NULL, "returned non-NULL on NULL argument");
    for (size_t outer = 0; outer < sizeof(tc) / sizeof(*tc); outer++) {
        char **result = strdup_array((char **) tc[outer].data);
        STASIS_ASSERT(strcmp_array((const char **) result, tc[outer].expected) == 0, "array members were different");
        guard_array_free(result);
    }

    const struct testcase tc_bad[] = {
            {.data = (const char *[]) {"ab", "123", NULL},
                    .expected = (const char *[]) {"abc", "123", NULL}},
            {.data = (const char *[]) {"have", "a", "pencil", "box", NULL},
                    .expected = (const char *[]) {"I", "have", "a", "pencil", "box", NULL}},
    };

    STASIS_ASSERT(strcmp_array(tc[0].data, NULL) > 0, "right argument is NULL, expected positive return");
    STASIS_ASSERT(strcmp_array(NULL, tc[0].data) < 0, "left argument is NULL, expected negative return");
    STASIS_ASSERT(strcmp_array(NULL, NULL) == 0, "left and right arguments are NULL, expected zero return");
    for (size_t outer = 0; outer < sizeof(tc_bad) / sizeof(*tc_bad); outer++) {
        char **result = strdup_array((char **) tc_bad[outer].data);
        STASIS_ASSERT(strcmp_array((const char **) result, tc_bad[outer].expected) != 0, "array members were identical");
        guard_array_free(result);
    }
}

void test_strchrdel() {
    struct testcase {
        const char *data;
        const char *input;
        const char *expected;
    };
    const struct testcase tc[] = {
            {.data ="aaaabbbbcccc", .input = "ac", .expected = "bbbb"},
            {.data = "1I 2have 3a 4pencil 5box.", .input = "1245", .expected = "I have 3a pencil box."},
            {.data = "\v\v\vI\t\f  ha\tve   a\t    pen\tcil     b\tox.", .input = " \f\t\v", "Ihaveapencilbox."},
    };

    for (size_t i = 0; i < sizeof(tc) / sizeof(*tc); i++) {
        char *data = strdup(tc[i].data);
        strchrdel(data, tc[i].input);
        STASIS_ASSERT(strcmp(data, tc[i].expected) == 0, "wrong status for condition");
        guard_free(data);
    }
}

void test_endswith() {
    struct testcase {
        const char *data;
        const char *input;
        const int expected;
    };
    struct testcase tc[] = {
            {.data = "I have a pencil box.", .input = ".", .expected = true},
            {.data = "I have a pencil box.", .input = "box.", .expected = true},
            {.data = "I have a pencil box.", .input = "pencil box.", .expected = true},
            {.data = "I have a pencil box.", .input = "a pencil box.", .expected = true},
            {.data = "I have a pencil box.", .input = "have a pencil box.", .expected = true},
            {.data = "I have a pencil box.", .input = "I have a pencil box.", .expected = true},
            {.data = ".    ", .input = ".", .expected = false},
            {.data = "I have a pencil box.", .input = "pencil box", .expected = false},
            {.data = NULL, .input = "test", .expected = false},
            {.data = "I have a pencil box", .input = NULL, .expected = false},
            {.data = NULL, .input = NULL, .expected = false},
    };
    for (size_t i = 0; i < sizeof(tc) / sizeof(*tc); i++) {
        int result = endswith(tc[i].data, tc[i].input);
        STASIS_ASSERT(result == tc[i].expected, "wrong status for condition");
    }
}

void test_startswith() {
    struct testcase {
        const char *data;
        const char *input;
        const int expected;
    };
    struct testcase tc[] = {
            {.data = "I have a pencil box.", .input = "I", .expected = true},
            {.data = "I have a pencil box.", .input = "I have", .expected = true},
            {.data = "I have a pencil box.", .input = "I have a", .expected = true},
            {.data = "I have a pencil box.", .input = "I have a pencil", .expected = true},
            {.data = "I have a pencil box.", .input = "I have a pencil box", .expected = true},
            {.data = "I have a pencil box.", .input = "I have a pencil box.", .expected = true},
            {.data = "    I have a pencil box.", .input = "I have", .expected = false},
            {.data = "I have a pencil box.", .input = "pencil box", .expected = false},
            {.data = NULL, .input = "test", .expected = false},
            {.data = "I have a pencil box", .input = NULL, .expected = false},
            {.data = NULL, .input = NULL, .expected = false},
    };
    for (size_t i = 0; i < sizeof(tc) / sizeof(*tc); i++) {
        int result = startswith(tc[i].data, tc[i].input);
        STASIS_ASSERT(result == tc[i].expected, "wrong status for condition");
    }
}

void test_num_chars() {
    struct testcase {
        const char *data;
        const char input;
        const int expected;
    };
    struct testcase tc[] = {
            {.data = "         ", .input = ' ', .expected = 9},
            {.data = "1 1 1 1 1", .input = '1', .expected = 5},
            {.data = "abc\t\ndef\nabc\ndef\n", .input = 'c', .expected = 2},
            {.data = "abc\t\ndef\nabc\ndef\n", .input = '\t', .expected = 1},
    };
    for (size_t i = 0; i < sizeof(tc) / sizeof(*tc); i++) {
        STASIS_ASSERT(num_chars(tc[i].data, tc[i].input) == tc[i].expected, "incorrect number of characters detected");
    }
}

void test_split() {
    struct testcase {
            const char *data;
            const size_t max_split;
            const char *delim;
            const char **expected;
    };
    struct testcase tc[] = {
            {.data = "a/b/c/d/e", .delim = "/", .max_split = 0, .expected = (const char *[]) {"a", "b", "c", "d", "e", NULL}},
            {.data = "a/b/c/d/e", .delim = "/", .max_split = 1, .expected = (const char *[]) {"a", "b/c/d/e", NULL}},
            {.data = "a/b/c/d/e", .delim = "/", .max_split = 2, .expected = (const char *[]) {"a", "b", "c/d/e", NULL}},
            {.data = "a/b/c/d/e", .delim = "/", .max_split = 3, .expected = (const char *[]) {"a", "b", "c", "d/e", NULL}},
            {.data = "a/b/c/d/e", .delim = "/", .max_split = 4, .expected = (const char *[]) {"a", "b", "c", "d", "e", NULL}},
            {.data = "multiple words split n times", .delim = " ", .max_split = 0, .expected = (const char *[]) {"multiple", "words", "split", "n", "times", NULL}},
            {.data = "multiple words split n times", .delim = " ", .max_split = 1, .expected = (const char *[]) {"multiple", "words split n times", NULL}},
            {.data = NULL, .delim = NULL, NULL},
    };
    for (size_t i = 0; i < sizeof(tc) / sizeof(*tc); i++) {
        char **result = split((char *) tc[i].data, tc[i].delim, tc[i].max_split);
        STASIS_ASSERT(strcmp_array((const char **) result, tc[i].expected) == 0, "Split failed");
        guard_array_free(result);
    }
}

void test_join() {
    struct testcase {
        const char **data;
        const char *delim;
        const char *expected;
    };
    struct testcase tc[] = {
            {.data = (const char *[]) {"a", "b", "c", "d", "e", NULL}, .delim = "", .expected = "abcde"},
            {.data = (const char *[]) {"a", NULL}, .delim = "", .expected = "a"},
            {.data = (const char *[]) {"a", "word", "or", "two", NULL}, .delim = "/", .expected = "a/word/or/two"},
            {.data = NULL, .delim = NULL, .expected = ""},
    };
    for (size_t i = 0; i < sizeof(tc) / sizeof(*tc); i++) {
        char *result;
        result = join((char **) tc[i].data, tc[i].delim);
        STASIS_ASSERT(strcmp(result ? result : "", tc[i].expected) == 0, "failed to join array");
        guard_free(result);
    }
}

void test_join_ex() {
    struct testcase {
        const char *delim;
        const char *expected;
    };
    struct testcase tc[] = {
            {.delim = "", .expected = "abcde"},
            {.delim = "/", .expected = "a/b/c/d/e"},
            {.delim = "\n", .expected = "a\nb\nc\nd\ne"},
            {.delim = "\n\n", .expected = "a\n\nb\n\nc\n\nd\n\ne"},
    };
    for (size_t i = 0; i < sizeof(tc) / sizeof(*tc); i++) {
        char *result = join_ex((char *) tc[i].delim, "a", "b", "c", "d", "e", NULL);
        STASIS_ASSERT(strcmp(result ? result : "", tc[i].expected) == 0, "failed to join array");
        guard_free(result);
    }
}

void test_substring_between() {
    struct testcase {
        const char *data;
        const char *delim;
        const char *expected;
    };
    struct testcase tc[] = {
            {.data = "I like {{adjective}} spaceships", .delim = "{{}}", .expected = "adjective"},
            {.data = "I like {adjective} spaceships", .delim = "{}", .expected = "adjective"},
            {.data = "one = 'two'", .delim = "''", .expected = "two"},
            {.data = "I like {adjective> spaceships", .delim = "{}", .expected = ""}, // no match
            {.data = NULL, .delim = NULL, .expected = ""}, // both arguments NULL
            {.data = "nothing!", .delim = NULL, .expected = ""}, // null delim
            {.data = "", .delim = "{}", .expected = ""}, // no match
            {.data = NULL, .delim = "nothing!", .expected = ""}, // null sptr
            {.data = "()", .delim = "(", .expected = ""}, // delim not divisible by 2
            {.data = "()", .delim = "( )", .expected = ""}, // delim not divisible by 2
            {.data = "nothing () here", .delim = "()", .expected = ""}, // nothing exists between delimiters
    };
    for (size_t i = 0; i < sizeof(tc) / sizeof(*tc); i++) {
        char *result = substring_between((char *) tc[i].data, tc[i].delim);
        STASIS_ASSERT(strcmp(result ? result : "", tc[i].expected) == 0, "unable to extract substring");
        guard_free(result);
    }
}

void test_strdeldup() {
    struct testcase {
        char **data;
        const char **expected;
    };
    struct testcase tc[] = {
        {.data = NULL, .expected = NULL},
        {.data = (char *[]) {"a", "a", "a", "b", "b", "b", "c", "c", "c", NULL}, .expected = (const char *[]) {"a", "b", "c", NULL}},
        {.data = (char *[]) {"a", "b", "c", "a", "b", "c", "a", "b", "c", NULL}, .expected = (const char *[]) {"a", "b", "c", NULL}},
        {.data = (char *[]) {"apple", "banana", "coconut", NULL}, .expected = (const char *[]) {"apple", "banana", "coconut", NULL}},
        {.data = (char *[]) {"apple", "banana", "apple", "coconut", NULL}, .expected = (const char *[]) {"apple", "banana", "coconut", NULL}},
    };
    for (size_t i = 0; i < sizeof(tc) / sizeof(*tc); i++) {
        char **result = strdeldup(tc[i].data);
        STASIS_ASSERT(strcmp_array((const char **) result, tc[i].expected) == 0, "incorrect number of duplicates removed");
        guard_array_free(result);
    }
}

void test_strsort() {

}

void test_strstr_array() {

}

void test_lstrip() {
    struct testcase {
        const char *data;
        const char *expected;
    };
    struct testcase tc[] = {
        {.data = "I am a string.\n", .expected = "I am a string.\n"}, // left strip only
        {.data = "I am a string.\n", .expected = "I am a string.\n"},
        {.data = "\t\t\t\tI am a string.\n", .expected = "I am a string.\n"},
        {.data = "    I    am a string.\n", .expected = "I    am a string.\n"},
    };

    STASIS_ASSERT(lstrip(NULL) == NULL, "incorrect return type");
    for (size_t i = 0; i < sizeof(tc) / sizeof(*tc); i++) {
        char *buf = calloc(255, sizeof(*buf));
        char *result;
        strcpy(buf, tc[i].data);
        result = lstrip(buf);
        STASIS_ASSERT(strcmp(result ? result : "", tc[i].expected) == 0, "incorrect strip-from-left");
        guard_free(buf);
    }
}

void test_strip() {
    struct testcase {
        const char *data;
        const char *expected;
    };
    struct testcase tc[] = {
            {.data = "  I am a string.\n", .expected = "  I am a string."}, // right strip only
            {.data = "\tI am a string.\n\t", .expected = "\tI am a string."},
            {.data = "\t\t\t\tI am a string.\n", .expected = "\t\t\t\tI am a string."},
            {.data = "I am a string.\n\n\n\n", .expected = "I am a string."},
            {.data = "", .expected = ""},
            {.data = " ", .expected = ""},
    };

    STASIS_ASSERT(strip(NULL) == NULL, "incorrect return type");
    for (size_t i = 0; i < sizeof(tc) / sizeof(*tc); i++) {
        char *buf = calloc(255, sizeof(*buf));
        char *result;
        strcpy(buf, tc[i].data);
        result = strip(buf);
        STASIS_ASSERT(strcmp(result ? result : "", tc[i].expected) == 0, "incorrect strip-from-right");
        guard_free(buf);
    }
}

void test_isempty() {

}

int main(int argc, char *argv[]) {
    STASIS_TEST_BEGIN_MAIN();
    STASIS_TEST_FUNC *tests[] = {
            test_to_short_version,
            test_tolower_s,
            test_isdigit_s,
            test_strdup_array_and_strcmp_array,
            test_strchrdel,
            test_strdeldup,
            test_lstrip,
            test_strip,
            test_num_chars,
            test_startswith,
            test_endswith,
            test_split,
            test_join,
            test_join_ex,
            test_substring_between,
    };
    STASIS_TEST_RUN(tests);
    STASIS_TEST_END_MAIN();
}