tests: Move tap-driver.sh out of the autotools aux directory
[lttng-tools.git] / tests / unit / test_string_utils.c
1 /*
2 * Copyright (C) 2017 Philippe Proulx <pproulx@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #include <stdlib.h>
9 #include <stdbool.h>
10 #include <assert.h>
11 #include <string.h>
12 #include <stdarg.h>
13 #include <common/string-utils/string-utils.h>
14 #include <tap/tap.h>
15
16 /* Number of TAP tests in this file */
17 #define NUM_TESTS 69
18
19 /* For error.h */
20 int lttng_opt_quiet = 1;
21 int lttng_opt_verbose;
22 int lttng_opt_mi;
23
24 static void test_one_split(const char *input, char delim, int escape_delim,
25 ...)
26 {
27 va_list vl;
28 bool all_ok = true;
29 struct lttng_dynamic_pointer_array strings;
30 int split_ret;
31 size_t i, string_count;
32
33 split_ret = strutils_split(input, delim, escape_delim, &strings);
34 assert(split_ret == 0);
35 va_start(vl, escape_delim);
36
37 string_count = lttng_dynamic_pointer_array_get_count(&strings);
38
39 for (i = 0; i < string_count; i++) {
40 const char *expected_substring = va_arg(vl, const char *);
41 const char *substring =
42 lttng_dynamic_pointer_array_get_pointer(
43 &strings, i);
44
45 diag(" got `%s`, expecting `%s`", substring, expected_substring);
46
47 if (!expected_substring) {
48 all_ok = false;
49 break;
50 }
51
52 if (strcmp(substring, expected_substring) != 0) {
53 all_ok = false;
54 break;
55 }
56 }
57
58 lttng_dynamic_pointer_array_reset(&strings);
59 va_end(vl);
60 ok(all_ok, "strutils_split() produces the expected substrings: `%s` (delim. `%c`, escape `%d`)",
61 input, delim, escape_delim);
62 }
63
64 static void test_split(void)
65 {
66 test_one_split("a/b/c/d/e", '/', false, "a", "b", "c", "d", "e", NULL);
67 test_one_split("a/b//d/e", '/', false, "a", "b", "", "d", "e", NULL);
68 test_one_split("/b/c/d/e", '/', false, "", "b", "c", "d", "e", NULL);
69 test_one_split("a/b/c/d/", '/', false, "a", "b", "c", "d", "", NULL);
70 test_one_split("/b/c/d/", '/', false, "", "b", "c", "d", "", NULL);
71 test_one_split("", '/', false, "", NULL);
72 test_one_split("/", '/', false, "", "", NULL);
73 test_one_split("//", '/', false, "", "", "", NULL);
74 test_one_split("hello+world", '+', false, "hello", "world", NULL);
75 test_one_split("hello\\+world", '+', false, "hello\\", "world", NULL);
76 test_one_split("hello\\+world", '+', true, "hello+world", NULL);
77 test_one_split("hello\\++world", '+', true, "hello+", "world", NULL);
78 test_one_split("hello\\\\++world", '+', true, "hello\\\\", "", "world", NULL);
79 test_one_split("hello+world\\", '+', false, "hello", "world\\", NULL);
80 test_one_split("hello+world\\", '+', true, "hello", "world\\", NULL);
81 test_one_split("\\+", '+', false, "\\", "", NULL);
82 test_one_split("\\+", '+', true, "+", NULL);
83 }
84
85 static void test_one_is_star_at_the_end_only_glob_pattern(const char *pattern, bool expected)
86 {
87 ok(strutils_is_star_at_the_end_only_glob_pattern(pattern) == expected,
88 "strutils_is_star_at_the_end_only_glob_pattern() returns the expected result: `%s` -> %d",
89 pattern, expected);
90 }
91
92 static void test_is_star_at_the_end_only_glob_pattern(void)
93 {
94 test_one_is_star_at_the_end_only_glob_pattern("allo*", true);
95 test_one_is_star_at_the_end_only_glob_pattern("allo\\\\*", true);
96 test_one_is_star_at_the_end_only_glob_pattern("allo", false);
97 test_one_is_star_at_the_end_only_glob_pattern("al*lo", false);
98 test_one_is_star_at_the_end_only_glob_pattern("al\\*lo", false);
99 test_one_is_star_at_the_end_only_glob_pattern("*allo", false);
100 test_one_is_star_at_the_end_only_glob_pattern("al*lo*", false);
101 test_one_is_star_at_the_end_only_glob_pattern("allo**", false);
102 test_one_is_star_at_the_end_only_glob_pattern("allo*\\*", false);
103 test_one_is_star_at_the_end_only_glob_pattern("allo\\*", false);
104 }
105
106 static void test_one_is_star_glob_pattern(const char *pattern, bool expected)
107 {
108 ok(strutils_is_star_glob_pattern(pattern) == expected,
109 "strutils_is_star_glob_pattern() returns the expected result: `%s` -> %d",
110 pattern, expected);
111 }
112
113 static void test_is_star_glob_pattern(void)
114 {
115 test_one_is_star_glob_pattern("allo*", true);
116 test_one_is_star_glob_pattern("*allo", true);
117 test_one_is_star_glob_pattern("*allo*", true);
118 test_one_is_star_glob_pattern("*al*lo*", true);
119 test_one_is_star_glob_pattern("al\\**lo", true);
120 test_one_is_star_glob_pattern("al\\*l*o", true);
121 test_one_is_star_glob_pattern("all*o\\", true);
122 test_one_is_star_glob_pattern("*", true);
123 test_one_is_star_glob_pattern("\\\\*", true);
124 test_one_is_star_glob_pattern("allo", false);
125 test_one_is_star_glob_pattern("allo\\*", false);
126 test_one_is_star_glob_pattern("al\\*lo", false);
127 test_one_is_star_glob_pattern("\\*allo", false);
128 test_one_is_star_glob_pattern("\\*", false);
129 test_one_is_star_glob_pattern("allo\\", false);
130 }
131
132 static void test_one_normalize_star_glob_pattern(const char *pattern,
133 const char *expected)
134 {
135 char *rw_pattern = strdup(pattern);
136
137 assert(rw_pattern);
138 strutils_normalize_star_glob_pattern(rw_pattern);
139 ok(strcmp(rw_pattern, expected) == 0,
140 "strutils_normalize_star_glob_pattern() produces the expected result: `%s` -> `%s`",
141 pattern, expected);
142 free(rw_pattern);
143 }
144
145 static void test_normalize_star_glob_pattern(void)
146 {
147 test_one_normalize_star_glob_pattern("salut", "salut");
148 test_one_normalize_star_glob_pattern("sal*ut", "sal*ut");
149 test_one_normalize_star_glob_pattern("sal**ut", "sal*ut");
150 test_one_normalize_star_glob_pattern("sal***ut", "sal*ut");
151 test_one_normalize_star_glob_pattern("*salut", "*salut");
152 test_one_normalize_star_glob_pattern("**salut", "*salut");
153 test_one_normalize_star_glob_pattern("***salut", "*salut");
154 test_one_normalize_star_glob_pattern("salut*", "salut*");
155 test_one_normalize_star_glob_pattern("salut**", "salut*");
156 test_one_normalize_star_glob_pattern("salut***", "salut*");
157 test_one_normalize_star_glob_pattern("sa\\*lut", "sa\\*lut");
158 test_one_normalize_star_glob_pattern("sa\\**lut", "sa\\**lut");
159 test_one_normalize_star_glob_pattern("sa*\\**lut", "sa*\\**lut");
160 test_one_normalize_star_glob_pattern("sa*\\***lut", "sa*\\**lut");
161 test_one_normalize_star_glob_pattern("\\*salu**t", "\\*salu*t");
162 test_one_normalize_star_glob_pattern("\\*salut**", "\\*salut*");
163 test_one_normalize_star_glob_pattern("\\*salut**\\*", "\\*salut*\\*");
164 test_one_normalize_star_glob_pattern("\\*salut", "\\*salut");
165 test_one_normalize_star_glob_pattern("\\***salut", "\\**salut");
166 test_one_normalize_star_glob_pattern("salut\\", "salut\\");
167 test_one_normalize_star_glob_pattern("salut\\**", "salut\\**");
168 test_one_normalize_star_glob_pattern("salut\\\\*", "salut\\\\*");
169 test_one_normalize_star_glob_pattern("salut\\\\***", "salut\\\\*");
170 test_one_normalize_star_glob_pattern("*", "*");
171 test_one_normalize_star_glob_pattern("**", "*");
172 test_one_normalize_star_glob_pattern("***", "*");
173 test_one_normalize_star_glob_pattern("**\\***", "*\\**");
174 }
175
176 int main(int argc, char **argv)
177 {
178 plan_tests(NUM_TESTS);
179 diag("String utils unit tests");
180 test_normalize_star_glob_pattern();
181 test_is_star_glob_pattern();
182 test_is_star_at_the_end_only_glob_pattern();
183 test_split();
184
185 return exit_status();
186 }
This page took 0.035955 seconds and 4 git commands to generate.