Rename libcommon.so to libcommon-gpl.so
[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
8#include <stdlib.h>
9#include <stdbool.h>
dbfea1ab
PP
10#include <string.h>
11#include <stdarg.h>
12#include <common/string-utils/string-utils.h>
13#include <tap/tap.h>
14
15/* Number of TAP tests in this file */
16#define NUM_TESTS 69
17
e358ddd5
JG
18/* For error.h */
19int lttng_opt_quiet = 1;
20int lttng_opt_verbose;
21int lttng_opt_mi;
22
3f322f1a 23static void test_one_split(const char *input, char delim, int escape_delim,
dbfea1ab
PP
24 ...)
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 =
740da7d5
SM
41 (const char *) lttng_dynamic_pointer_array_get_pointer(
42 &strings, i);
dbfea1ab 43
e358ddd5 44 diag(" got `%s`, expecting `%s`", substring, expected_substring);
dbfea1ab
PP
45
46 if (!expected_substring) {
47 all_ok = false;
48 break;
49 }
50
e358ddd5 51 if (strcmp(substring, expected_substring) != 0) {
dbfea1ab
PP
52 all_ok = false;
53 break;
54 }
55 }
56
e358ddd5 57 lttng_dynamic_pointer_array_reset(&strings);
dbfea1ab
PP
58 va_end(vl);
59 ok(all_ok, "strutils_split() produces the expected substrings: `%s` (delim. `%c`, escape `%d`)",
e358ddd5 60 input, delim, escape_delim);
dbfea1ab
PP
61}
62
63static void test_split(void)
64{
65 test_one_split("a/b/c/d/e", '/', false, "a", "b", "c", "d", "e", NULL);
66 test_one_split("a/b//d/e", '/', false, "a", "b", "", "d", "e", NULL);
67 test_one_split("/b/c/d/e", '/', false, "", "b", "c", "d", "e", NULL);
68 test_one_split("a/b/c/d/", '/', false, "a", "b", "c", "d", "", NULL);
69 test_one_split("/b/c/d/", '/', false, "", "b", "c", "d", "", NULL);
70 test_one_split("", '/', false, "", NULL);
71 test_one_split("/", '/', false, "", "", NULL);
72 test_one_split("//", '/', false, "", "", "", NULL);
73 test_one_split("hello+world", '+', false, "hello", "world", NULL);
74 test_one_split("hello\\+world", '+', false, "hello\\", "world", NULL);
75 test_one_split("hello\\+world", '+', true, "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\\", '+', false, "hello", "world\\", NULL);
79 test_one_split("hello+world\\", '+', true, "hello", "world\\", NULL);
80 test_one_split("\\+", '+', false, "\\", "", NULL);
81 test_one_split("\\+", '+', true, "+", NULL);
82}
83
84static void test_one_is_star_at_the_end_only_glob_pattern(const char *pattern, bool expected)
85{
86 ok(strutils_is_star_at_the_end_only_glob_pattern(pattern) == expected,
87 "strutils_is_star_at_the_end_only_glob_pattern() returns the expected result: `%s` -> %d",
88 pattern, expected);
89}
90
91static void test_is_star_at_the_end_only_glob_pattern(void)
92{
93 test_one_is_star_at_the_end_only_glob_pattern("allo*", true);
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", false);
96 test_one_is_star_at_the_end_only_glob_pattern("al*lo", 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("*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("allo**", 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}
104
105static void test_one_is_star_glob_pattern(const char *pattern, bool expected)
106{
107 ok(strutils_is_star_glob_pattern(pattern) == expected,
108 "strutils_is_star_glob_pattern() returns the expected result: `%s` -> %d",
109 pattern, expected);
110}
111
112static void test_is_star_glob_pattern(void)
113{
114 test_one_is_star_glob_pattern("allo*", true);
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("*al*lo*", true);
118 test_one_is_star_glob_pattern("al\\**lo", true);
119 test_one_is_star_glob_pattern("al\\*l*o", true);
120 test_one_is_star_glob_pattern("all*o\\", true);
121 test_one_is_star_glob_pattern("*", true);
122 test_one_is_star_glob_pattern("\\\\*", true);
123 test_one_is_star_glob_pattern("allo", false);
124 test_one_is_star_glob_pattern("allo\\*", false);
125 test_one_is_star_glob_pattern("al\\*lo", false);
126 test_one_is_star_glob_pattern("\\*allo", false);
127 test_one_is_star_glob_pattern("\\*", false);
128 test_one_is_star_glob_pattern("allo\\", false);
129}
130
131static void test_one_normalize_star_glob_pattern(const char *pattern,
132 const char *expected)
133{
134 char *rw_pattern = strdup(pattern);
135
a0377dfe 136 LTTNG_ASSERT(rw_pattern);
dbfea1ab
PP
137 strutils_normalize_star_glob_pattern(rw_pattern);
138 ok(strcmp(rw_pattern, expected) == 0,
139 "strutils_normalize_star_glob_pattern() produces the expected result: `%s` -> `%s`",
140 pattern, expected);
141 free(rw_pattern);
142}
143
144static void test_normalize_star_glob_pattern(void)
145{
146 test_one_normalize_star_glob_pattern("salut", "salut");
147 test_one_normalize_star_glob_pattern("sal*ut", "sal*ut");
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("*salut", "*salut");
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("sa\\*lut", "sa\\*lut");
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("\\*salu**t", "\\*salu*t");
161 test_one_normalize_star_glob_pattern("\\*salut**", "\\*salut*");
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("*", "*");
170 test_one_normalize_star_glob_pattern("**", "*");
171 test_one_normalize_star_glob_pattern("***", "*");
172 test_one_normalize_star_glob_pattern("**\\***", "*\\**");
173}
174
175int main(int argc, char **argv)
176{
177 plan_tests(NUM_TESTS);
178 diag("String utils unit tests");
179 test_normalize_star_glob_pattern();
180 test_is_star_glob_pattern();
181 test_is_star_at_the_end_only_glob_pattern();
182 test_split();
183
184 return exit_status();
185}
This page took 0.040028 seconds and 4 git commands to generate.