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
|
/* PipeWire */
/* SPDX-FileCopyrightText: Copyright © 2019 Wim Taymans */
/* SPDX-License-Identifier: MIT */
#include "pwtest.h"
#include <limits.h>
#include <pipewire/utils.h>
#include <spa/utils/string.h>
static void test_destroy(void *object)
{
pwtest_fail_if_reached();
}
PWTEST(utils_abi)
{
pw_destroy_t f = test_destroy;
pwtest_ptr_eq(f, &test_destroy);
return PWTEST_PASS;
}
static void test__pw_split_walk(void)
{
const struct test_case {
const char * const input;
const char * const delim;
const char * const * const expected;
} test_cases[] = {
{
.input = "a \n test string \n \r ",
.delim = " \r\n",
.expected = (const char *[]) {
"a",
"test",
"string",
NULL
},
},
{
.input = "::field1::field2:: field3:::::",
.delim = ":",
.expected = (const char *[]) {
"field1",
"field2",
" field3",
NULL
},
},
{
.input = ",,,,,,,,,,,,",
.delim = ",",
.expected = (const char *[]) {
NULL
},
},
{
.input = ",;,,,'''':::':::,,,,;",
.delim = ",:';",
.expected = (const char *[]) {
NULL
},
},
{
.input = "aaa:bbb,ccc##ddd/#,eee?fff...",
.delim = ":,#/?",
.expected = (const char *[]) {
"aaa",
"bbb",
"ccc",
"ddd",
"eee",
"fff...",
NULL
},
},
{
.input = "line 1\na different line\nthe third line\n",
.delim = "\n",
.expected = (const char *[]) {
"line 1",
"a different line",
"the third line",
NULL
},
},
{
.input = "no delimiters",
.delim = ",:/;",
.expected = (const char *[]) {
"no delimiters",
NULL
},
},
{
.input = "delimiter at the end,;",
.delim = ",;",
.expected = (const char *[]) {
"delimiter at the end",
NULL
},
},
{
.input = "/delimiter on both ends,",
.delim = "/,",
.expected = (const char *[]) {
"delimiter on both ends",
NULL
},
},
{
.input = ",delimiter at the beginning",
.delim = ",",
.expected = (const char *[]) {
"delimiter at the beginning",
NULL
},
},
{
.input = "/usr/lib/pipewire-0.3/libpipewire.so",
.delim = "/",
.expected = (const char *[]) {
"usr",
"lib",
"pipewire-0.3",
"libpipewire.so",
NULL
}
},
{
.input = "/home/x/.ladspa:/usr/lib/ladspa:/usr/local/lib/ladspa",
.delim = ":",
.expected = (const char *[]) {
"/home/x/.ladspa",
"/usr/lib/ladspa",
"/usr/local/lib/ladspa",
NULL
}
},
{
.input = "\n field1 \t\n field2 \t \t field3",
.delim = " \n\t",
.expected = (const char *[]) {
"field1",
"field2",
"field3",
NULL
}
},
};
SPA_FOR_EACH_ELEMENT_VAR(test_cases, tc) {
const char *str = tc->input, *s;
const char *state = NULL;
size_t j = 0, len;
while ((s = pw_split_walk(str, tc->delim, &len, &state)) != NULL && tc->expected[j] != NULL) {
pwtest_int_eq(strlen(tc->expected[j]), len);
pwtest_str_eq_n(s, tc->expected[j], len);
j += 1;
}
pwtest_ptr_null(s);
pwtest_ptr_null(tc->expected[j]);
}
}
static void test__pw_split_strv(void)
{
const char *test1 = "a \n test string \n \r ";
const char *del = "\n\r ";
const char *test2 = "a:";
const char *del2 = ":";
int n_tokens;
char **res;
res = pw_split_strv(test1, del, INT_MAX, &n_tokens);
pwtest_ptr_notnull(res);
pwtest_int_eq(n_tokens, 3);
pwtest_str_eq(res[0], "a");
pwtest_str_eq(res[1], "test");
pwtest_str_eq(res[2], "string");
pwtest_ptr_null(res[3]);
pw_free_strv(res);
res = pw_split_strv(test1, del, 2, &n_tokens);
pwtest_ptr_notnull(res);
pwtest_int_eq(n_tokens, 2);
pwtest_str_eq(res[0], "a");
pwtest_str_eq(res[1], "test string \n \r ");
pwtest_ptr_null(res[2]);
pw_free_strv(res);
res = pw_split_strv(test2, del2, 2, &n_tokens);
pwtest_ptr_notnull(res);
pwtest_int_eq(n_tokens, 1);
pwtest_str_eq(res[0], "a");
pwtest_ptr_null(res[1]);
pw_free_strv(res);
}
PWTEST(utils_split)
{
test__pw_split_walk();
test__pw_split_strv();
return PWTEST_PASS;
}
PWTEST(utils_strip)
{
char test1[] = " \n\r \n a test string \n \r ";
char test2[] = " \n\r \n \n \r ";
char test3[] = "a test string";
spa_assert_se(spa_streq(pw_strip(test1, "\n\r "), "a test string"));
spa_assert_se(spa_streq(pw_strip(test2, "\n\r "), ""));
spa_assert_se(spa_streq(pw_strip(test3, "\n\r "), "a test string"));
return PWTEST_PASS;
}
PWTEST(utils_strv_parse)
{
char **res = NULL;
int n_tokens = -1;
static const char test1[] = "[ x \"y\" \" z \" ]";
res = pw_strv_parse(test1, strlen(test1), INT_MAX, &n_tokens);
pwtest_ptr_notnull(res);
pwtest_int_eq(n_tokens, 3);
pwtest_ptr_eq(res[n_tokens], NULL);
spa_assert_se(spa_streq(res[0], "x"));
spa_assert_se(spa_streq(res[1], "y"));
spa_assert_se(spa_streq(res[2], " z "));
pw_free_strv(res);
static const char test2[] = "[ x y { [ 1 = 2 ] }";
res = pw_strv_parse(test2, strlen(test2), INT_MAX, &n_tokens);
pwtest_ptr_null(res);
pwtest_int_eq(n_tokens, 0);
return PWTEST_PASS;
}
PWTEST_SUITE(utils)
{
pwtest_add(utils_abi, PWTEST_NOARG);
pwtest_add(utils_split, PWTEST_NOARG);
pwtest_add(utils_strip, PWTEST_NOARG);
pwtest_add(utils_strv_parse, PWTEST_NOARG);
return PWTEST_PASS;
}
|