Patrick Steinhardt | d7f0c47 | 2024-09-04 16:17:17 +0200 | [diff] [blame] | 1 | #include "unit-test.h" |
| 2 | #include "strbuf.h" |
| 3 | #include "strvec.h" |
| 4 | |
| 5 | #define check_strvec(vec, ...) \ |
| 6 | do { \ |
| 7 | const char *expect[] = { __VA_ARGS__ }; \ |
| 8 | size_t expect_len = ARRAY_SIZE(expect); \ |
| 9 | cl_assert(expect_len > 0); \ |
| 10 | cl_assert_equal_p(expect[expect_len - 1], NULL); \ |
| 11 | cl_assert_equal_i((vec)->nr, expect_len - 1); \ |
| 12 | cl_assert((vec)->nr <= (vec)->alloc); \ |
| 13 | for (size_t i = 0; i < expect_len; i++) \ |
| 14 | cl_assert_equal_s((vec)->v[i], expect[i]); \ |
| 15 | } while (0) |
| 16 | |
| 17 | void test_strvec__init(void) |
| 18 | { |
| 19 | struct strvec vec = STRVEC_INIT; |
| 20 | |
| 21 | cl_assert_equal_p(vec.v, empty_strvec); |
| 22 | cl_assert_equal_i(vec.nr, 0); |
| 23 | cl_assert_equal_i(vec.alloc, 0); |
| 24 | } |
| 25 | |
| 26 | void test_strvec__dynamic_init(void) |
| 27 | { |
| 28 | struct strvec vec; |
| 29 | |
| 30 | strvec_init(&vec); |
| 31 | cl_assert_equal_p(vec.v, empty_strvec); |
| 32 | cl_assert_equal_i(vec.nr, 0); |
| 33 | cl_assert_equal_i(vec.alloc, 0); |
| 34 | } |
| 35 | |
| 36 | void test_strvec__clear(void) |
| 37 | { |
| 38 | struct strvec vec = STRVEC_INIT; |
| 39 | |
| 40 | strvec_push(&vec, "foo"); |
| 41 | strvec_clear(&vec); |
| 42 | cl_assert_equal_p(vec.v, empty_strvec); |
| 43 | cl_assert_equal_i(vec.nr, 0); |
| 44 | cl_assert_equal_i(vec.alloc, 0); |
| 45 | } |
| 46 | |
| 47 | void test_strvec__push(void) |
| 48 | { |
| 49 | struct strvec vec = STRVEC_INIT; |
| 50 | |
| 51 | strvec_push(&vec, "foo"); |
| 52 | check_strvec(&vec, "foo", NULL); |
| 53 | |
| 54 | strvec_push(&vec, "bar"); |
| 55 | check_strvec(&vec, "foo", "bar", NULL); |
| 56 | |
| 57 | strvec_clear(&vec); |
| 58 | } |
| 59 | |
| 60 | void test_strvec__pushf(void) |
| 61 | { |
| 62 | struct strvec vec = STRVEC_INIT; |
| 63 | |
| 64 | strvec_pushf(&vec, "foo: %d", 1); |
| 65 | check_strvec(&vec, "foo: 1", NULL); |
| 66 | strvec_clear(&vec); |
| 67 | } |
| 68 | |
| 69 | void test_strvec__pushl(void) |
| 70 | { |
| 71 | struct strvec vec = STRVEC_INIT; |
| 72 | |
| 73 | strvec_pushl(&vec, "foo", "bar", "baz", NULL); |
| 74 | check_strvec(&vec, "foo", "bar", "baz", NULL); |
| 75 | strvec_clear(&vec); |
| 76 | } |
| 77 | |
| 78 | void test_strvec__pushv(void) |
| 79 | { |
| 80 | const char *strings[] = { |
| 81 | "foo", "bar", "baz", NULL, |
| 82 | }; |
| 83 | struct strvec vec = STRVEC_INIT; |
| 84 | |
| 85 | strvec_pushv(&vec, strings); |
| 86 | check_strvec(&vec, "foo", "bar", "baz", NULL); |
| 87 | |
| 88 | strvec_clear(&vec); |
| 89 | } |
| 90 | |
Rubén Justo | 14ef8c0 | 2024-12-04 23:44:25 +0100 | [diff] [blame] | 91 | void test_strvec__splice_just_initialized_strvec(void) |
| 92 | { |
| 93 | struct strvec vec = STRVEC_INIT; |
| 94 | const char *replacement[] = { "foo" }; |
| 95 | |
| 96 | strvec_splice(&vec, 0, 0, replacement, ARRAY_SIZE(replacement)); |
| 97 | check_strvec(&vec, "foo", NULL); |
| 98 | strvec_clear(&vec); |
| 99 | } |
| 100 | |
Patrick Steinhardt | 3f5fade | 2024-11-20 14:39:38 +0100 | [diff] [blame] | 101 | void test_strvec__splice_with_same_size_replacement(void) |
| 102 | { |
| 103 | struct strvec vec = STRVEC_INIT; |
| 104 | const char *replacement[] = { "1" }; |
| 105 | |
| 106 | strvec_pushl(&vec, "foo", "bar", "baz", NULL); |
| 107 | strvec_splice(&vec, 1, 1, replacement, ARRAY_SIZE(replacement)); |
| 108 | check_strvec(&vec, "foo", "1", "baz", NULL); |
| 109 | strvec_clear(&vec); |
| 110 | } |
| 111 | |
| 112 | void test_strvec__splice_with_smaller_replacement(void) |
| 113 | { |
| 114 | struct strvec vec = STRVEC_INIT; |
| 115 | const char *replacement[] = { "1" }; |
| 116 | |
| 117 | strvec_pushl(&vec, "foo", "bar", "baz", NULL); |
| 118 | strvec_splice(&vec, 1, 2, replacement, ARRAY_SIZE(replacement)); |
| 119 | check_strvec(&vec, "foo", "1", NULL); |
| 120 | strvec_clear(&vec); |
| 121 | } |
| 122 | |
| 123 | void test_strvec__splice_with_bigger_replacement(void) |
| 124 | { |
| 125 | struct strvec vec = STRVEC_INIT; |
| 126 | const char *replacement[] = { "1", "2", "3" }; |
| 127 | |
| 128 | strvec_pushl(&vec, "foo", "bar", "baz", NULL); |
| 129 | strvec_splice(&vec, 0, 2, replacement, ARRAY_SIZE(replacement)); |
| 130 | check_strvec(&vec, "1", "2", "3", "baz", NULL); |
| 131 | strvec_clear(&vec); |
| 132 | } |
| 133 | |
| 134 | void test_strvec__splice_with_empty_replacement(void) |
| 135 | { |
| 136 | struct strvec vec = STRVEC_INIT; |
| 137 | |
| 138 | strvec_pushl(&vec, "foo", "bar", "baz", NULL); |
| 139 | strvec_splice(&vec, 0, 2, NULL, 0); |
| 140 | check_strvec(&vec, "baz", NULL); |
| 141 | strvec_clear(&vec); |
| 142 | } |
| 143 | |
| 144 | void test_strvec__splice_with_empty_original(void) |
| 145 | { |
| 146 | struct strvec vec = STRVEC_INIT; |
| 147 | const char *replacement[] = { "1", "2" }; |
| 148 | |
| 149 | strvec_pushl(&vec, "foo", "bar", "baz", NULL); |
| 150 | strvec_splice(&vec, 1, 0, replacement, ARRAY_SIZE(replacement)); |
| 151 | check_strvec(&vec, "foo", "1", "2", "bar", "baz", NULL); |
| 152 | strvec_clear(&vec); |
| 153 | } |
| 154 | |
| 155 | void test_strvec__splice_at_tail(void) |
| 156 | { |
| 157 | struct strvec vec = STRVEC_INIT; |
| 158 | const char *replacement[] = { "1", "2" }; |
| 159 | |
| 160 | strvec_pushl(&vec, "foo", "bar", NULL); |
| 161 | strvec_splice(&vec, 2, 0, replacement, ARRAY_SIZE(replacement)); |
| 162 | check_strvec(&vec, "foo", "bar", "1", "2", NULL); |
| 163 | strvec_clear(&vec); |
| 164 | } |
| 165 | |
Patrick Steinhardt | d7f0c47 | 2024-09-04 16:17:17 +0200 | [diff] [blame] | 166 | void test_strvec__replace_at_head(void) |
| 167 | { |
| 168 | struct strvec vec = STRVEC_INIT; |
| 169 | |
| 170 | strvec_pushl(&vec, "foo", "bar", "baz", NULL); |
| 171 | strvec_replace(&vec, 0, "replaced"); |
| 172 | check_strvec(&vec, "replaced", "bar", "baz", NULL); |
| 173 | strvec_clear(&vec); |
| 174 | } |
| 175 | |
| 176 | void test_strvec__replace_at_tail(void) |
| 177 | { |
| 178 | struct strvec vec = STRVEC_INIT; |
| 179 | strvec_pushl(&vec, "foo", "bar", "baz", NULL); |
| 180 | strvec_replace(&vec, 2, "replaced"); |
| 181 | check_strvec(&vec, "foo", "bar", "replaced", NULL); |
| 182 | strvec_clear(&vec); |
| 183 | } |
| 184 | |
| 185 | void test_strvec__replace_in_between(void) |
| 186 | { |
| 187 | struct strvec vec = STRVEC_INIT; |
| 188 | |
| 189 | strvec_pushl(&vec, "foo", "bar", "baz", NULL); |
| 190 | strvec_replace(&vec, 1, "replaced"); |
| 191 | check_strvec(&vec, "foo", "replaced", "baz", NULL); |
| 192 | strvec_clear(&vec); |
| 193 | } |
| 194 | |
| 195 | void test_strvec__replace_with_substring(void) |
| 196 | { |
| 197 | struct strvec vec = STRVEC_INIT; |
| 198 | |
| 199 | strvec_pushl(&vec, "foo", NULL); |
| 200 | strvec_replace(&vec, 0, vec.v[0] + 1); |
| 201 | check_strvec(&vec, "oo", NULL); |
| 202 | strvec_clear(&vec); |
| 203 | } |
| 204 | |
| 205 | void test_strvec__remove_at_head(void) |
| 206 | { |
| 207 | struct strvec vec = STRVEC_INIT; |
| 208 | |
| 209 | strvec_pushl(&vec, "foo", "bar", "baz", NULL); |
| 210 | strvec_remove(&vec, 0); |
| 211 | check_strvec(&vec, "bar", "baz", NULL); |
| 212 | strvec_clear(&vec); |
| 213 | } |
| 214 | |
| 215 | void test_strvec__remove_at_tail(void) |
| 216 | { |
| 217 | struct strvec vec = STRVEC_INIT; |
| 218 | |
| 219 | strvec_pushl(&vec, "foo", "bar", "baz", NULL); |
| 220 | strvec_remove(&vec, 2); |
| 221 | check_strvec(&vec, "foo", "bar", NULL); |
| 222 | strvec_clear(&vec); |
| 223 | } |
| 224 | |
| 225 | void test_strvec__remove_in_between(void) |
| 226 | { |
| 227 | struct strvec vec = STRVEC_INIT; |
| 228 | |
| 229 | strvec_pushl(&vec, "foo", "bar", "baz", NULL); |
| 230 | strvec_remove(&vec, 1); |
| 231 | check_strvec(&vec, "foo", "baz", NULL); |
| 232 | strvec_clear(&vec); |
| 233 | } |
| 234 | |
| 235 | void test_strvec__pop_empty_array(void) |
| 236 | { |
| 237 | struct strvec vec = STRVEC_INIT; |
| 238 | |
| 239 | strvec_pop(&vec); |
| 240 | check_strvec(&vec, NULL); |
| 241 | strvec_clear(&vec); |
| 242 | } |
| 243 | |
| 244 | void test_strvec__pop_non_empty_array(void) |
| 245 | { |
| 246 | struct strvec vec = STRVEC_INIT; |
| 247 | |
| 248 | strvec_pushl(&vec, "foo", "bar", "baz", NULL); |
| 249 | strvec_pop(&vec); |
| 250 | check_strvec(&vec, "foo", "bar", NULL); |
| 251 | strvec_clear(&vec); |
| 252 | } |
| 253 | |
| 254 | void test_strvec__split_empty_string(void) |
| 255 | { |
| 256 | struct strvec vec = STRVEC_INIT; |
| 257 | |
| 258 | strvec_split(&vec, ""); |
| 259 | check_strvec(&vec, NULL); |
| 260 | strvec_clear(&vec); |
| 261 | } |
| 262 | |
| 263 | void test_strvec__split_single_item(void) |
| 264 | { |
| 265 | struct strvec vec = STRVEC_INIT; |
| 266 | |
| 267 | strvec_split(&vec, "foo"); |
| 268 | check_strvec(&vec, "foo", NULL); |
| 269 | strvec_clear(&vec); |
| 270 | } |
| 271 | |
| 272 | void test_strvec__split_multiple_items(void) |
| 273 | { |
| 274 | struct strvec vec = STRVEC_INIT; |
| 275 | |
| 276 | strvec_split(&vec, "foo bar baz"); |
| 277 | check_strvec(&vec, "foo", "bar", "baz", NULL); |
| 278 | strvec_clear(&vec); |
| 279 | } |
| 280 | |
| 281 | void test_strvec__split_whitespace_only(void) |
| 282 | { |
| 283 | struct strvec vec = STRVEC_INIT; |
| 284 | |
| 285 | strvec_split(&vec, " \t\n"); |
| 286 | check_strvec(&vec, NULL); |
| 287 | strvec_clear(&vec); |
| 288 | } |
| 289 | |
| 290 | void test_strvec__split_multiple_consecutive_whitespaces(void) |
| 291 | { |
| 292 | struct strvec vec = STRVEC_INIT; |
| 293 | |
| 294 | strvec_split(&vec, "foo\n\t bar"); |
| 295 | check_strvec(&vec, "foo", "bar", NULL); |
| 296 | strvec_clear(&vec); |
| 297 | } |
| 298 | |
| 299 | void test_strvec__detach(void) |
| 300 | { |
| 301 | struct strvec vec = STRVEC_INIT; |
| 302 | const char **detached; |
| 303 | |
| 304 | strvec_push(&vec, "foo"); |
| 305 | |
| 306 | detached = strvec_detach(&vec); |
| 307 | cl_assert_equal_s(detached[0], "foo"); |
| 308 | cl_assert_equal_p(detached[1], NULL); |
| 309 | |
| 310 | cl_assert_equal_p(vec.v, empty_strvec); |
| 311 | cl_assert_equal_i(vec.nr, 0); |
| 312 | cl_assert_equal_i(vec.alloc, 0); |
| 313 | |
| 314 | free((char *) detached[0]); |
| 315 | free(detached); |
| 316 | } |