public inbox for git@vger.kernel.org
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: git@vger.kernel.org
Subject: Re: [PATCH v2 1/2] u-string-list: add unit tests for string-list methods
Date: Mon, 26 Jan 2026 11:50:45 -0800	[thread overview]
Message-ID: <xmqqa4y0i25m.fsf@gitster.g> (raw)
In-Reply-To: 20260126185604.90089-1-amishhhaaaa@gmail.com

Amisha Chhajed <amishhhaaaa@gmail.com> writes:

> +void test_string_list__remove(void)
> +{
> +	struct string_list expected_strings = STRING_LIST_INIT_DUP;
> +	struct string_list list = STRING_LIST_INIT_DUP;
> +
> +	t_create_string_list_dup(&expected_strings, 0, NULL);
> +	t_create_string_list_dup(&list, 0, NULL);
> +	t_string_list_remove(&expected_strings, &list, "");
> +
> +	t_create_string_list_dup(&expected_strings, 0, "a", NULL);
> +	t_create_string_list_dup(&list, 0, "a", "a", NULL);
> +	t_string_list_remove(&expected_strings, &list, "a");

Not a complaint, not a suggestion to change anything, but just an
observation.  While "remove" requires the string-list to be sorted,
its implementation does not seem to care if you by mistake fed an
unsorted string list.

After seeing this particular test that feeds a list with two "a"
and expects in the resulting list a single "a", I naturally wondered
which one of these two "a" survives and which one is dropped.

"remove" removes only one matching element that is picked at random
among the duplicates, but because the input is expected to be
sorted, these duplicate elements sit next to each other forming a
single strand of identical pearls.  The end result of picking one of
these pearls out would not be different no matter which one of them
you pick.  So the answer to my "which one of these 'a'?" question
turns out to be "you cannot tell, but it does not matter" ;-)

> +static void t_string_list_remove_empty_items(struct string_list *expected_strings, struct string_list *list)
> +{
> +	string_list_remove_empty_items(list, 0);
> +	t_string_list_equal(list, expected_strings);
> +}
> +
> +void test_string_list__remove_empty_items(void)
> +{
> +	struct string_list expected_strings = STRING_LIST_INIT_DUP;
> +	struct string_list list = STRING_LIST_INIT_DUP;
> +
> +	t_create_string_list_dup(&expected_strings, 0, NULL);
> +	t_create_string_list_dup(&list, 0, "", "", "", NULL);
> +	t_string_list_remove_empty_items(&expected_strings, &list);

Again, not a complaint, not a suggestion to change anything, but
just an observation.  As we saw earlier, "remove" is "remove just
one of many", but "remove_empty" is "remove all empties".  Simply
makes me wonder if the API looked more sane if we had "remove_all"
whose signature is the same as string_list_remove().

> +static void t_string_list_unsorted_string_list_delete_item(struct string_list *expected_list, struct string_list *list, int i)

This is a way overlong line.

> +{
> +	unsorted_string_list_delete_item(list, i, 0);
> +
> +	t_string_list_equal(list, expected_list);
> +}
> +
> +void test_string_list__unsorted_string_list_delete_item(void)
> +{
> +	struct string_list expected_strings = STRING_LIST_INIT_DUP;
> +	struct string_list list = STRING_LIST_INIT_DUP;
> +
> +	t_create_string_list_dup(&expected_strings, 0, "a", "c", "b", NULL);
> +	t_create_string_list_dup(&list, 0, "a", "d", "b", "c", NULL);
> +	t_string_list_unsorted_string_list_delete_item(&expected_strings, &list, 1);

This demonstrates one peculiar aspect of this "delete item from
unsorted list" API function very well.  If one is expected to name
an element to delete by specifying its position in the list, it is
natural to expect that the elements in the list to be ordered in
some way that is meaningful to the application [*], and the API is
not expected to shuffle the resulting list in such a way that makes
further use of the list cumbersome.  Yet, the function does exactly
that by moving the element at the end of the list to the place the
location of the deleted element.

	Side note: [*] The "unsorted" in the name of the function is
	a reference to the fact that the elements are not sorted by
	the natural order string-list API uses to allow it to binary
	search; it does not mean the elements are entirely randomly
	thrown in and it shouldn't mean that the application cannot
	rely on

The only caller of this function is git.c::list_cmds() that is asked
to remove the helper binaries (i.e., those whose name contains
"--"), so even though git.c::commands[] list is in sorted order, and
the list_builtins() function slurps them into a working list with
string_list_append(), processing "nohelpers" will splinkle command
names from near the tail of the list into random places in the
middle of the list.

> +	t_string_list_clear(&expected_strings, 0);
> +	t_string_list_clear(&list, 0);
> +}
> \ No newline at end of file

Don't.  Always end a text file with a complete line, please.


      parent reply	other threads:[~2026-01-26 19:50 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-22 17:15 [RFC PATCH 0/2] Adding string_list_sort_u to replace combined calls of string_list_sort and string_list_remove_duplicates calls Amisha Chhajed
2026-01-22 17:15 ` [RFC PATCH 1/2] Adding string_list_sort_u which sorts a list then deduplicates it Amisha Chhajed
2026-01-22 22:07   ` Junio C Hamano
2026-01-25 20:23     ` Amisha Chhajed
2026-01-22 17:15 ` [RFC PATCH 2/2] Replacing calls of string_list_sort and string_list_remove_duplicates with the combined variant string_list_u Amisha Chhajed
2026-01-22 22:19   ` Junio C Hamano
2026-01-22 22:09 ` [RFC PATCH 0/2] Adding string_list_sort_u to replace combined calls of string_list_sort and string_list_remove_duplicates calls Junio C Hamano
2026-01-25 20:14 ` [PATCH 1/2] u-string-list: add unit tests for string-list methods Amisha Chhajed
2026-01-25 20:15   ` [PATCH 2/2] string-list: add string_list_sort_u() that mimics "sort -u" Amisha Chhajed
2026-01-29 12:12     ` [PATCH v3 1/2] u-string-list: add unit tests for string-list methods Amisha Chhajed
2026-01-29 12:12       ` [PATCH v3 2/2] string-list: add string_list_sort_u() that mimics "sort -u" Amisha Chhajed
2026-01-29 12:14       ` [PATCH v3 1/2] u-string-list: add unit tests for string-list methods Amisha Chhajed
2026-01-30 19:51     ` [PATCH 2/2] string-list: add string_list_sort_u() that mimics "sort -u" Kristoffer Haugsbakk
2026-01-30 21:45       ` Junio C Hamano
2026-01-26  7:12   ` [PATCH 1/2] u-string-list: add unit tests for string-list methods Junio C Hamano
2026-01-26 18:57     ` Amisha Chhajed
2026-01-26 19:12       ` Junio C Hamano
2026-01-25 20:17 ` Amisha Chhajed
2026-01-25 20:17   ` [PATCH 2/2] string-list: add string_list_sort_u() that mimics "sort -u" Amisha Chhajed
2026-01-26 18:56 ` [PATCH v2 1/2] u-string-list: add unit tests for string-list methods Amisha Chhajed
2026-01-26 18:56   ` [PATCH v2 2/2] string-list: add string_list_sort_u() that mimics "sort -u" Amisha Chhajed
2026-01-26 20:11     ` Junio C Hamano
2026-01-27  1:27       ` Amisha Chhajed
2026-01-26 19:50   ` Junio C Hamano [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqa4y0i25m.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox