u-string-list: move "remove duplicates" test to "u-string-list.c"

We use "test-tool string-list remove_duplicates" to test the "string_list_remove_duplicates" function. As we have introduced the unit test, we'd better remove the logic from shell script to C program to improve test speed and readability. As all the tests in shell script are removed, let's just delete the "t0063-string-list.sh" and update the "meson.build" file to align with this change. Also we could simply remove "DISABLE_SIGN_COMPARE_WARNINGS" due to we have already deleted related code. Unfortunately, we cannot totally remove "test-string-list.c" due to that we would test the performance of sorting about string list by executing "test-tool string-list sort" in "p0071-sort.sh". Signed-off-by: shejialuo <shejialuo@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

shejialuo committed Jun 29, 2025 at 12:28 UTC 6e5b26c3ff639211147ccb2b1ca681c768b8db11
4 files changed +62 -67
t/helper/test-string-list.c
-39
@@ -1,48 +1,9 @@
1 -#define DISABLE_SIGN_COMPARE_WARNINGS
2 -
1 #include "test-tool.h"
2 #include "strbuf.h"
3 #include "string-list.h"
4
7 -/*
8 - * Parse an argument into a string list. arg should either be a
9 - * ':'-separated list of strings, or "-" to indicate an empty string
10 - * list (as opposed to "", which indicates a string list containing a
11 - * single empty string). list->strdup_strings must be set.
12 - */
13 -static void parse_string_list(struct string_list *list, const char *arg)
14 -{
15 - if (!strcmp(arg, "-"))
16 - return;
17 -
18 - (void)string_list_split(list, arg, ':', -1);
19 -}
20 -
21 -static void write_list_compact(const struct string_list *list)
22 -{
23 - int i;
24 - if (!list->nr)
25 - printf("-\n");
26 - else {
27 - printf("%s", list->items[0].string);
28 - for (i = 1; i < list->nr; i++)
29 - printf(":%s", list->items[i].string);
30 - printf("\n");
31 - }
32 -}
33 -
5 int cmd__string_list(int argc, const char **argv)
6 {
36 - if (argc == 3 && !strcmp(argv[1], "remove_duplicates")) {
37 - struct string_list list = STRING_LIST_INIT_DUP;
38 -
39 - parse_string_list(&list, argv[2]);
40 - string_list_remove_duplicates(&list, 0);
41 - write_list_compact(&list);
42 - string_list_clear(&list, 0);
43 - return 0;
44 - }
45 -
7 if (argc == 2 && !strcmp(argv[1], "sort")) {
8 struct string_list list = STRING_LIST_INIT_NODUP;
9 struct strbuf sb = STRBUF_INIT;
t/meson.build
-1
@@ -124,7 +124,6 @@ integration_tests = [
124 't0060-path-utils.sh',
125 't0061-run-command.sh',
126 't0062-revision-walking.sh',
127 - 't0063-string-list.sh',
127 't0066-dir-iterator.sh',
128 't0067-parse_pathspec_file.sh',
129 't0068-for-each-repo.sh',
t/t0063-string-list.sh deleted
-27
@@ -1,27 +0,0 @@
1 -#!/bin/sh
2 -#
3 -# Copyright (c) 2012 Michael Haggerty
4 -#
5 -
6 -test_description='Test string list functionality'
7 -
8 -. ./test-lib.sh
9 -
10 -test_expect_success "test remove_duplicates" '
11 - test "x-" = "x$(test-tool string-list remove_duplicates -)" &&
12 - test "x" = "x$(test-tool string-list remove_duplicates "")" &&
13 - test a = "$(test-tool string-list remove_duplicates a)" &&
14 - test a = "$(test-tool string-list remove_duplicates a:a)" &&
15 - test a = "$(test-tool string-list remove_duplicates a:a:a:a:a)" &&
16 - test a:b = "$(test-tool string-list remove_duplicates a:b)" &&
17 - test a:b = "$(test-tool string-list remove_duplicates a:a:b)" &&
18 - test a:b = "$(test-tool string-list remove_duplicates a:b:b)" &&
19 - test a:b:c = "$(test-tool string-list remove_duplicates a:b:c)" &&
20 - test a:b:c = "$(test-tool string-list remove_duplicates a:a:b:c)" &&
21 - test a:b:c = "$(test-tool string-list remove_duplicates a:b:b:c)" &&
22 - test a:b:c = "$(test-tool string-list remove_duplicates a:b:c:c)" &&
23 - test a:b:c = "$(test-tool string-list remove_duplicates a:a:b:b:c:c)" &&
24 - test a:b:c = "$(test-tool string-list remove_duplicates a:a:a:b:b:b:c:c:c)"
25 -'
26 -
27 -test_done
t/unit-tests/u-string-list.c
+62
@@ -163,3 +163,65 @@ void test_string_list__filter(void)
163
164 t_string_list_clear(&list, 0);
165 }
166 +
167 +static void t_string_list_remove_duplicates(struct string_list *list, ...)
168 +{
169 + struct string_list expected_strings = STRING_LIST_INIT_DUP;
170 + va_list ap;
171 +
172 + va_start(ap, list);
173 + t_vcreate_string_list_dup(&expected_strings, 0, ap);
174 + va_end(ap);
175 +
176 + string_list_remove_duplicates(list, 0);
177 + t_string_list_equal(list, &expected_strings);
178 +
179 + string_list_clear(&expected_strings, 0);
180 +}
181 +
182 +void test_string_list__remove_duplicates(void)
183 +{
184 + struct string_list list = STRING_LIST_INIT_DUP;
185 +
186 + t_create_string_list_dup(&list, 0, NULL);
187 + t_string_list_remove_duplicates(&list, NULL);
188 +
189 + t_create_string_list_dup(&list, 0, "", NULL);
190 + t_string_list_remove_duplicates(&list, "", NULL);
191 +
192 + t_create_string_list_dup(&list, 0, "a", NULL);
193 + t_string_list_remove_duplicates(&list, "a", NULL);
194 +
195 + t_create_string_list_dup(&list, 0, "a", "a", NULL);
196 + t_string_list_remove_duplicates(&list, "a", NULL);
197 +
198 + t_create_string_list_dup(&list, 0, "a", "a", "a", NULL);
199 + t_string_list_remove_duplicates(&list, "a", NULL);
200 +
201 + t_create_string_list_dup(&list, 0, "a", "a", "b", NULL);
202 + t_string_list_remove_duplicates(&list, "a", "b", NULL);
203 +
204 + t_create_string_list_dup(&list, 0, "a", "b", "b", NULL);
205 + t_string_list_remove_duplicates(&list, "a", "b", NULL);
206 +
207 + t_create_string_list_dup(&list, 0, "a", "b", "c", NULL);
208 + t_string_list_remove_duplicates(&list, "a", "b", "c", NULL);
209 +
210 + t_create_string_list_dup(&list, 0, "a", "a", "b", "c", NULL);
211 + t_string_list_remove_duplicates(&list, "a", "b", "c", NULL);
212 +
213 + t_create_string_list_dup(&list, 0, "a", "b", "b", "c", NULL);
214 + t_string_list_remove_duplicates(&list, "a", "b", "c", NULL);
215 +
216 + t_create_string_list_dup(&list, 0, "a", "b", "c", "c", NULL);
217 + t_string_list_remove_duplicates(&list, "a", "b", "c", NULL);
218 +
219 + t_create_string_list_dup(&list, 0, "a", "a", "b", "b", "c", "c", NULL);
220 + t_string_list_remove_duplicates(&list, "a", "b", "c", NULL);
221 +
222 + t_create_string_list_dup(&list, 0, "a", "a", "a", "b", "b", "b",
223 + "c", "c", "c", NULL);
224 + t_string_list_remove_duplicates(&list, "a", "b", "c", NULL);
225 +
226 + t_string_list_clear(&list, 0);
227 +}