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