git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v3 3/4] mergesort: cover empty and small lists

From
MAMuhammed Dilshad A <dilsheddilu123@gmail.com>
Date
Oct 9, 2026, 15:08 UTC
Message-ID
<da42c96904489fcd37506916d5aac5be197df382.1791556668.git.dilsheddilu123@gmail.com>
In-Reply-To
<cover.1791556668.git.dilsheddilu123@gmail.com>

The existing tests use at least 100 items. Add empty and single-item lists, reversed and equal pairs, and a small list with duplicate values and integer limits. Add two sorted runs whose values need to interleave during the merge.

Also check that the debug version sorts a two-item list and calls both the get-next and set-next hooks.

Signed-off-by: Muhammed Dilshad A <dilsheddilu123@gmail.com>
---
 t/unit-tests/u-mergesort.c | 63 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 63 insertions(+)
diff --git a/t/unit-tests/u-mergesort.c b/t/unit-tests/u-mergesort.c
index 50bca1db46..1a5c0a60a8 100644
--- a/t/unit-tests/u-mergesort.c
+++ b/t/unit-tests/u-mergesort.c
@@ -9,6 +9,11 @@ struct number {
 
 DEFINE_LIST_SORT(static, sort_numbers, struct number, next);
 
+static int get_next_count, set_next_count;
+
+DEFINE_LIST_SORT_DEBUG(static, sort_numbers_debug, struct number, next,
+		       get_next_count++, set_next_count++);
+
 static int compare_numbers(const struct number *a, const struct number *b)
 {
 	return (a->value > b->value) - (a->value < b->value);
@@ -103,3 +108,61 @@ void test_mergesort__random(void)
 	for (size_t i = 0; i < ARRAY_SIZE(sizes); i++)
 		check_sort(input, sizes[i]);
 }
+
+void test_mergesort__empty(void)
+{
+	check_sort(NULL, 0);
+}
+
+void test_mergesort__singleton(void)
+{
+	const int input[] = { 42 };
+
+	check_sort(input, ARRAY_SIZE(input));
+}
+
+void test_mergesort__reversed_pair(void)
+{
+	const int input[] = { 2, 1 };
+
+	check_sort(input, ARRAY_SIZE(input));
+}
+
+void test_mergesort__equal_pair(void)
+{
+	const int input[] = { 1, 1 };
+
+	check_sort(input, ARRAY_SIZE(input));
+}
+
+void test_mergesort__interleaved_runs(void)
+{
+	const int input[] = { 0, 2, 4, 6, 1, 3, 5, 7 };
+
+	check_sort(input, ARRAY_SIZE(input));
+}
+
+void test_mergesort__mixed_values(void)
+{
+	const int input[] = { INT_MAX, -1, 0, INT_MIN, -1, INT_MAX, 0 };
+
+	check_sort(input, ARRAY_SIZE(input));
+}
+
+void test_mergesort__debug_hooks(void)
+{
+	struct number nodes[] = {
+		{ .value = 2 },
+		{ .value = 1 },
+	};
+	struct number *list = &nodes[0];
+
+	nodes[0].next = &nodes[1];
+	get_next_count = set_next_count = 0;
+	sort_numbers_debug(&list, compare_numbers);
+	cl_assert_equal_p(list, &nodes[1]);
+	cl_assert_equal_p(list->next, &nodes[0]);
+	cl_assert_equal_p(list->next->next, NULL);
+	cl_assert_gt_i(get_next_count, 0);
+	cl_assert_gt_i(set_next_count, 0);
+}
-- 
2.55.0
Previous: Muhammed Dilshad ANext: Muhammed Dilshad A
Message 16 of 17 in “test-mergesort: plug memory leaks in sort_stdin()”
  1. test-mergesort: plug memory leaks in sort_stdin()Muhammed Dilshad A, Oct 7, 2026
  2. Patrick SteinhardtOct 7, 2026
  3. Junio C HamanoOct 7, 2026
  4. 0/3 mergesort: move tests to Clar and retire the helperMuhammed Dilshad A, Oct 7, 2026
  5. 1/3 test-mergesort: plug memory leaks in sort_stdin()Muhammed Dilshad A, Oct 7, 2026
  6. 2/3 mergesort: move sorting tests to the unit-test frameworkMuhammed Dilshad A, Oct 7, 2026
  7. Patrick SteinhardtOct 9, 2026
  8. Muhammed Dilshad AOct 9, 2026
  9. 3/3 t: retire the sorting benchmark and mergesort helperMuhammed Dilshad A, Oct 7, 2026
  10. Patrick SteinhardtOct 9, 2026
  11. Muhammed Dilshad AOct 9, 2026
  12. 0/4 mergesort: move tests to Clar and remove the helperMuhammed Dilshad A, Oct 9, 2026
  13. 1/4 mergesort: move sorting tests to ClarMuhammed Dilshad A, Oct 9, 2026
  14. Junio C HamanoOct 11, 2026
  15. 2/4 mergesort: simplify the unit testsMuhammed Dilshad A, Oct 9, 2026
  16. 3/4 mergesort: cover empty and small listsMuhammed Dilshad A, Oct 9, 2026
  17. 4/4 t: retire the sorting benchmark and mergesort helperMuhammed Dilshad A, Oct 9, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.