Skip to contentSkip to conversation

t: port reftable/basics_test.c to the unit testing

PATCH Superseded Chandra Pratap 4 participants 18 messages May 29, 2024 – May 30, 2024 1 message not in the archive Kbuild

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

v2 cover t: port reftable/basics_test.c to the unit testing

In the recent codebase update (commit 8bf6fbd, 2023-12-09), a new unit testing framework written entirely in C was introduced to the Git project aimed at simplifying testing and reducing test run times. Currently, tests for the reftable refs-backend are performed by a custom testing framework defined by reftable/test_framework.{c, h}. Port reftable/basics_test.c to the unit testing framework and improve upon the ported test.

---
Changes in v2:
- Split up the second patch of the previous series into sub-patches

CI for v2: https://github.com/gitgitgadget/git/pull/1736

#2 lore raw

PATCH v2 1/4 t: move reftable/basics_test.c to the unit testing framework

reftable/basics_test.c exercise the functions defined in reftable/basics.{c, h}. Migrate reftable/basics_test.c to the unit testing framework. Migration involves refactoring the tests to use the unit testing framework instead of reftable's test framework.

---
 Makefile                                      |  2 +-
 t/helper/test-reftable.c                      |  1 -
 .../unit-tests/t-reftable-basics.c            | 41 +++++++++----------
 3 files changed, 20 insertions(+), 24 deletions(-)
 rename reftable/basics_test.c => t/unit-tests/t-reftable-basics.c (65%)
Makefile +1 -1
index 8f4432ae57..36188ca256 100644
@@ -1337,6 +1337,7 @@ THIRD_PARTY_SOURCES += sha1dc/%
 UNIT_TEST_PROGRAMS += t-ctype
 UNIT_TEST_PROGRAMS += t-mem-pool
 UNIT_TEST_PROGRAMS += t-prio-queue
+UNIT_TEST_PROGRAMS += t-reftable-basics
 UNIT_TEST_PROGRAMS += t-strbuf
 UNIT_TEST_PROGRAMS += t-trailer
 UNIT_TEST_PROGS = $(patsubst %,$(UNIT_TEST_BIN)/%$X,$(UNIT_TEST_PROGRAMS))
@@ -2671,7 +2672,6 @@ REFTABLE_OBJS += reftable/stack.o
 REFTABLE_OBJS += reftable/tree.o
 REFTABLE_OBJS += reftable/writer.o
 
-REFTABLE_TEST_OBJS += reftable/basics_test.o
 REFTABLE_TEST_OBJS += reftable/block_test.o
 REFTABLE_TEST_OBJS += reftable/dump.o
 REFTABLE_TEST_OBJS += reftable/merged_test.o
t/helper/test-reftable.c +0 -1
index bae731669c..9160bc5da6 100644
@@ -5,7 +5,6 @@
 int cmd__reftable(int argc, const char **argv)
 {
 	/* test from simple to complex. */
-	basics_test_main(argc, argv);
 	record_test_main(argc, argv);
 	block_test_main(argc, argv);
 	tree_test_main(argc, argv);
t/unit-tests/t-reftable-basics.c renamed from reftable/basics_test.c +19 -22

87 lines not on this page · raw message

Signature · 1 line
2.45.GIT
#3 lore raw

PATCH v2 2/4 t: move tests from reftable/stack_test.c to the new unit test

In reply to Chandra Pratap

parse_names() and names_equal() are functions defined in reftable/basics.{c, h}. Move the tests for these functions from reftable/stack_test.c to the newly ported test.

---
 reftable/stack_test.c            | 25 -------------------------
 t/unit-tests/t-reftable-basics.c | 25 ++++++++++++++++++++++---
 2 files changed, 22 insertions(+), 28 deletions(-)
reftable/stack_test.c +0 -25
index 7889f818d1..6f6af11e53 100644
@@ -102,29 +102,6 @@ static void test_read_file(void)
 	(void) remove(fn);
 }
 
-static void test_parse_names(void)
-{
-	char buf[] = "line\n";
-	char **names = NULL;
-	parse_names(buf, strlen(buf), &names);
-
-	EXPECT(NULL != names[0]);
-	EXPECT(0 == strcmp(names[0], "line"));
-	EXPECT(NULL == names[1]);
-	free_names(names);
-}
-
-static void test_names_equal(void)
-{
-	char *a[] = { "a", "b", "c", NULL };
-	char *b[] = { "a", "b", "d", NULL };
-	char *c[] = { "a", "b", NULL };
-
-	EXPECT(names_equal(a, a));
-	EXPECT(!names_equal(a, b));
-	EXPECT(!names_equal(a, c));
-}
-
 static int write_test_ref(struct reftable_writer *wr, void *arg)
 {
 	struct reftable_ref_record *ref = arg;
@@ -1048,8 +1025,6 @@ static void test_reftable_stack_compaction_concurrent_clean(void)
 int stack_test_main(int argc, const char *argv[])
 {
 	RUN_TEST(test_empty_add);
-	RUN_TEST(test_names_equal);
-	RUN_TEST(test_parse_names);
 	RUN_TEST(test_read_file);
 	RUN_TEST(test_reflog_expire);
 	RUN_TEST(test_reftable_stack_add);
t/unit-tests/t-reftable-basics.c +22 -3
index 99e6c89120..55fcff12d9 100644
@@ -58,14 +58,32 @@ static void test_names_length(void)
 	check_int(names_length(a), ==, 2);
 }
 
+static void test_names_equal(void)
+{
+	char *a[] = { "a", "b", "c", NULL };
+	char *b[] = { "a", "b", "d", NULL };
+	char *c[] = { "a", "b", NULL };
+
+	check(names_equal(a, a));
+	check(!names_equal(a, b));
+	check(!names_equal(a, c));
+}
+
 static void test_parse_names_normal(void)
 {
-	char in[] = "a\nb\n";
+	char in1[] = "line\n";
+	char in2[] = "a\nb\nc";
 	char **out = NULL;
-	parse_names(in, strlen(in), &out);
+	parse_names(in1, strlen(in1), &out);
+	check_str(out[0], "line");
+	check(!out[1]);
+	free_names(out);
+
+	parse_names(in2, strlen(in2), &out);
 	check_str(out[0], "a");
 	check_str(out[1], "b");
-	check(!out[2]);
+	check_str(out[2], "c");
+	check(!out[3]);
 	free_names(out);
 }
 
@@ -97,6 +115,7 @@ int cmd_main(int argc, const char *argv[])
 	TEST(test_parse_names_drop_empty(), "parse_names drops empty string");
 	TEST(test_binsearch(), "binary search with binsearch works");
 	TEST(test_names_length(), "names_length retuns size of a NULL-terminated string array");
+	TEST(test_names_equal(), "names_equal compares NULL-terminated string arrays");
 
 	return test_done();
 }
Signature · 1 line
2.45.GIT
#4 lore raw

PATCH v2 3/4 t: move tests from reftable/record_test.c to the new unit test

In reply to Chandra Pratap

common_prefix_size(), get_be24() and put_be24() are functions defined in reftable/basics.{c, h}. Move the tests for these functions from reftable/record_test.c to the newly ported test.

---
 reftable/record_test.c           | 37 -----------------------------
 t/unit-tests/t-reftable-basics.c | 40 ++++++++++++++++++++++++++------
 2 files changed, 33 insertions(+), 44 deletions(-)
reftable/record_test.c +0 -37
index c158ee79ff..58290bdba3 100644
@@ -64,31 +64,6 @@ static void test_varint_roundtrip(void)
 	}
 }
 
-static void test_common_prefix(void)
-{
-	struct {
-		const char *a, *b;
-		int want;
-	} cases[] = {
-		{ "abc", "ab", 2 },
-		{ "", "abc", 0 },
-		{ "abc", "abd", 2 },
-		{ "abc", "pqr", 0 },
-	};
-
-	int i = 0;
-	for (i = 0; i < ARRAY_SIZE(cases); i++) {
-		struct strbuf a = STRBUF_INIT;
-		struct strbuf b = STRBUF_INIT;
-		strbuf_addstr(&a, cases[i].a);
-		strbuf_addstr(&b, cases[i].b);
-		EXPECT(common_prefix_size(&a, &b) == cases[i].want);
-
-		strbuf_release(&a);
-		strbuf_release(&b);
-	}
-}
-
 static void set_hash(uint8_t *h, int j)
 {
 	int i = 0;
@@ -258,16 +233,6 @@ static void test_reftable_log_record_roundtrip(void)
 	strbuf_release(&scratch);
 }
 
-static void test_u24_roundtrip(void)
-{
-	uint32_t in = 0x112233;
-	uint8_t dest[3];
-	uint32_t out;
-	put_be24(dest, in);
-	out = get_be24(dest);
-	EXPECT(in == out);
-}
-
 static void test_key_roundtrip(void)
 {
 	uint8_t buffer[1024] = { 0 };
@@ -411,9 +376,7 @@ int record_test_main(int argc, const char *argv[])
 	RUN_TEST(test_reftable_ref_record_roundtrip);
 	RUN_TEST(test_varint_roundtrip);
 	RUN_TEST(test_key_roundtrip);
-	RUN_TEST(test_common_prefix);
 	RUN_TEST(test_reftable_obj_record_roundtrip);
 	RUN_TEST(test_reftable_index_record_roundtrip);
-	RUN_TEST(test_u24_roundtrip);
 	return 0;
 }
t/unit-tests/t-reftable-basics.c +33 -7
index 55fcff12d9..b02ca02040 100644
@@ -99,13 +99,38 @@ static void test_parse_names_drop_empty(void)
 
 static void test_common_prefix(void)
 {
-	struct strbuf s1 = STRBUF_INIT;
-	struct strbuf s2 = STRBUF_INIT;
-	strbuf_addstr(&s1, "abcdef");
-	strbuf_addstr(&s2, "abc");
-	check_int(common_prefix_size(&s1, &s2), ==, 3);
-	strbuf_release(&s1);
-	strbuf_release(&s2);
+	struct strbuf a = STRBUF_INIT;
+	struct strbuf b = STRBUF_INIT;
+	struct {
+		const char *a, *b;
+		int want;
+	} cases[] = {
+		{"abcdef", "abc", 3},
+		{ "abc", "ab", 2 },
+		{ "", "abc", 0 },
+		{ "abc", "abd", 2 },
+		{ "abc", "pqr", 0 },
+	};
+
+	for (size_t i = 0; i < ARRAY_SIZE(cases); i++) {
+		strbuf_addstr(&a, cases[i].a);
+		strbuf_addstr(&b, cases[i].b);
+		check_int(common_prefix_size(&a, &b), ==, cases[i].want);
+		strbuf_reset(&a);
+		strbuf_reset(&b);
+	}
+	strbuf_release(&a);
+	strbuf_release(&b);
+}
+
+static void test_u24_roundtrip(void)
+{
+	uint32_t in = 0x112233;
+	uint8_t dest[3];
+	uint32_t out;
+	put_be24(dest, in);
+	out = get_be24(dest);
+	check_int(in, ==, out);
 }
 
 int cmd_main(int argc, const char *argv[])
@@ -116,6 +141,7 @@ int cmd_main(int argc, const char *argv[])
 	TEST(test_binsearch(), "binary search with binsearch works");
 	TEST(test_names_length(), "names_length retuns size of a NULL-terminated string array");
 	TEST(test_names_equal(), "names_equal compares NULL-terminated string arrays");
+	TEST(test_u24_roundtrip(), "put_be24 and get_be24 work");
 
 	return test_done();
 }
Signature · 1 line
2.45.GIT
#5 lore raw

Re: [GSoC][PATCH v2 3/4] t: move tests from reftable/record_test.c to the new unit test

On Wed, May 29, 2024 at 12:25:11PM +0530, Chandra Pratap wrote:
[snip]

Quoted 28 lines
t/unit-tests/t-reftable-basics.c +13 -7
index 55fcff12d9..b02ca02040 100644
@@ -99,13 +99,38 @@ static void test_parse_names_drop_empty(void)
 
 static void test_common_prefix(void)
 {
-	struct strbuf s1 = STRBUF_INIT;
-	struct strbuf s2 = STRBUF_INIT;
-	strbuf_addstr(&s1, "abcdef");
-	strbuf_addstr(&s2, "abc");
-	check_int(common_prefix_size(&s1, &s2), ==, 3);
-	strbuf_release(&s1);
-	strbuf_release(&s2);
+	struct strbuf a = STRBUF_INIT;
+	struct strbuf b = STRBUF_INIT;
+	struct {
+		const char *a, *b;
+		int want;
+	} cases[] = {
+		{"abcdef", "abc", 3},
+		{ "abc", "ab", 2 },
+		{ "", "abc", 0 },
+		{ "abc", "abd", 2 },
+		{ "abc", "pqr", 0 },
+	};
+

t/unit-tests/t-reftable-basics.c static void test_parse_names_drop_empty(void) · line 124

Quoted 10 lines
t/unit-tests/t-reftable-basics.c +10 -0
+	for (size_t i = 0; i < ARRAY_SIZE(cases); i++) {
+		strbuf_addstr(&a, cases[i].a);
+		strbuf_addstr(&b, cases[i].b);
+		check_int(common_prefix_size(&a, &b), ==, cases[i].want);
+		strbuf_reset(&a);
+		strbuf_reset(&b);
+	}
+	strbuf_release(&a);
+	strbuf_release(&b);
+}

Oh, so this test was even duplicated. It may make sense to point out details like this in the commit message to prepare the reader. But that's probably not worth a reroll.

Patrick

#6 lore raw

Re: [GSoC][PATCH v2 3/4] t: move tests from reftable/record_test.c to the new unit test

Quoted 7 lines · Patrick Steinhardt

t/unit-tests/t-reftable-basics.c static void test_parse_names_drop_empty(void) · line 124

Quoted 3 lines
t/unit-tests/t-reftable-basics.c +3 -0
+	strbuf_release(&a);
+	strbuf_release(&b);
+}

Oh, so this test was even duplicated. It may make sense to point out details like this in the commit message to prepare the reader. But that's probably not worth a reroll.

Probably. But if you are sending out another round anyway, then it is a good opportunity to update the proposed log message.

;-)

#7 lore raw

PATCH v2 4/4 t: add test for put_be16() and improve test-case for parse_names()

In reply to Chandra Pratap

put_be16() is a function defined in reftable/basics.{c, h} for which there are no tests in the current setup. Add a test for the same and improve the existing test-case for parse_names().

---
 t/unit-tests/t-reftable-basics.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
t/unit-tests/t-reftable-basics.c +12 -4
index b02ca02040..8372faec8c 100644
@@ -89,11 +89,13 @@ static void test_parse_names_normal(void)
 
 static void test_parse_names_drop_empty(void)
 {
-	char in[] = "a\n\n";
+	char in[] = "a\n\nb\n";
 	char **out = NULL;
 	parse_names(in, strlen(in), &out);
 	check_str(out[0], "a");
-	check(!out[1]);
+	/* simply '\n' should be dropped as empty string */
+	check_str(out[1], "b");
+	check(!out[2]);
 	free_names(out);
 }
 
@@ -123,14 +125,20 @@ static void test_common_prefix(void)
 	strbuf_release(&b);
 }
 
-static void test_u24_roundtrip(void)
+static void test_be_roundtrip(void)
 {
 	uint32_t in = 0x112233;
 	uint8_t dest[3];
 	uint32_t out;
+	/* test put_be24 and get_be24 roundtrip */
 	put_be24(dest, in);
 	out = get_be24(dest);
 	check_int(in, ==, out);
+	/* test put_be16 and get_be16 roundtrip */
+	in = 0xfef1;
+	put_be16(dest, in);
+	out = get_be16(dest);
+	check_int(in, ==, out);
 }
 
 int cmd_main(int argc, const char *argv[])
@@ -141,7 +149,7 @@ int cmd_main(int argc, const char *argv[])
 	TEST(test_binsearch(), "binary search with binsearch works");
 	TEST(test_names_length(), "names_length retuns size of a NULL-terminated string array");
 	TEST(test_names_equal(), "names_equal compares NULL-terminated string arrays");
-	TEST(test_u24_roundtrip(), "put_be24 and get_be24 work");
+	TEST(test_be_roundtrip(), "put_be24, get_be24 and put_be16 work");
 
 	return test_done();
 }
Signature · 1 line
2.45.GIT
#8 lore raw

Re: [GSoC][PATCH v2 4/4] t: add test for put_be16() and improve test-case for parse_names()

Quoted 20 lines · Chandra Pratap

put_be16() is a function defined in reftable/basics.{c, h} for which there are no tests in the current setup. Add a test for the same and improve the existing test-case for parse_names().

---
 t/unit-tests/t-reftable-basics.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
t/unit-tests/t-reftable-basics.c +0 -1
index b02ca02040..8372faec8c 100644
@@ -89,11 +89,13 @@ static void test_parse_names_normal(void)
 
 static void test_parse_names_drop_empty(void)
 {
-	char in[] = "a\n\n";

t/unit-tests/t-reftable-basics.c static void test_parse_names_normal(void) · line 100

Quoted 10 lines
t/unit-tests/t-reftable-basics.c +4 -1
+	char in[] = "a\n\nb\n";
 	char **out = NULL;
 	parse_names(in, strlen(in), &out);
 	check_str(out[0], "a");
-	check(!out[1]);
+	/* simply '\n' should be dropped as empty string */
+	check_str(out[1], "b");
+	check(!out[2]);
 	free_names(out);
 }

I'd split out this change into yet another commit. Also, you say that the test case is being "improved", but without mentioning what the improvement actually is.

Quoted 10 lines
t/unit-tests/t-reftable-basics.c +1 -1
@@ -123,14 +125,20 @@ static void test_common_prefix(void)
 	strbuf_release(&b);
 }
 
-static void test_u24_roundtrip(void)
+static void test_be_roundtrip(void)
 {
 	uint32_t in = 0x112233;
 	uint8_t dest[3];
 	uint32_t out;

t/unit-tests/t-reftable-basics.c static void test_common_prefix(void) · line 142

Quoted 10 lines
t/unit-tests/t-reftable-basics.c +6 -0
+	/* test put_be24 and get_be24 roundtrip */
 	put_be24(dest, in);
 	out = get_be24(dest);
 	check_int(in, ==, out);
+	/* test put_be16 and get_be16 roundtrip */
+	in = 0xfef1;
+	put_be16(dest, in);
+	out = get_be16(dest);
+	check_int(in, ==, out);
 }

Would it make sense to have separate tests for each of the variants instead of one test for all of these? Might make things a bit easier to follow.

Patrick

#9 lore raw
In reply to Chandra Pratap
Quoted 11 lines · Chandra Pratap

In the recent codebase update (commit 8bf6fbd, 2023-12-09), a new unit testing framework written entirely in C was introduced to the Git project aimed at simplifying testing and reducing test run times. Currently, tests for the reftable refs-backend are performed by a custom testing framework defined by reftable/test_framework.{c, h}. Port reftable/basics_test.c to the unit testing framework and improve upon the ported test.

The evolution of a patch series can be followed a bit easier if subsequent versions are attached to the initial thread. You can do this by passing e.g. --in-reply-to=<message-id> to git-format-patch(1), where the message ID is the one of the cover letter of the first version.

I'd also recommend to attach a range diff to your cover letter via the --range-diff= parameter. This range diff helps the reviewer to spot what has changed between your preceding version and this one.

Patrick

#10 lore raw

v3 cover t: port reftable/basics_test.c to the unit testing

In reply to Chandra Pratap

In the recent codebase update (commit 8bf6fbd, 2023-12-09), a new unit testing framework written entirely in C was introduced to the Git project aimed at simplifying testing and reducing test run times. Currently, tests for the reftable refs-backend are performed by a custom testing framework defined by reftable/test_framework.{c, h}. Port reftable/basics_test.c to the unit testing framework and improve upon the ported test.

---
Changes in v3:
- Split up the 4th patch of the previous series into 2 sub-patches

CI/PR for v3: https://github.com/gitgitgadget/git/pull/1736

range-diff against v2:

1:  3ab1b415f4 = 1:  3ab1b415f4 t: move reftable/basics_test.c to the unit testing framework
2:  51fec8a376 = 2:  0143bbd2e4 t: move tests from reftable/stack_test.c to the new unit test
3:  e0e9adfdf6 = 3:  2f1d02e945 t: move tests from reftable/record_test.c to the new unit test
-:  ---------- > 4:  14606ac8db t: add test for put_be16()
4:  81b8975b4c ! 5:  2e741bab6d t: add test for put_be16() and improve test-case for parse_names()

@@ Metadata
Author: Chandra Pratap <[email protected]>

## Commit message ##
- t: add test for put_be16() and improve test-case for parse_names()
+ t: improve the test-case for parse_names()

- put_be16() is a function defined in reftable/basics.{c, h} for which
- there are no tests in the current setup. Add a test for the same and
- improve the existing test-case for parse_names().
+ In the existing test-case for parse_names(), the fact that empty
+ lines should be ignored is not obvious because the empty line is
+ immediately followed by end-of-string. This can be mistaken as the
+ empty line getting replaced by NULL. Improve this by adding a
+ non-empty line after the empty one to demonstrate the intended behavior.

@@ t/unit-tests/t-reftable-basics.c: static void test_parse_names_normal(void)
free_names(out);
}

-@@ t/unit-tests/t-reftable-basics.c: static void test_common_prefix(void)
- strbuf_release(&b);
- }
-
--static void test_u24_roundtrip(void)
-+static void test_be_roundtrip(void)
- {
- uint32_t in = 0x112233;
- uint8_t dest[3];
- uint32_t out;
-+ /* test put_be24 and get_be24 roundtrip */
- put_be24(dest, in);
- out = get_be24(dest);
- check_int(in, ==, out);
-+ /* test put_be16 and get_be16 roundtrip */
-+ in = 0xfef1;
-+ put_be16(dest, in);
-+ out = get_be16(dest);
-+ check_int(in, ==, out);
- }
-
- int cmd_main(int argc, const char *argv[])
-@@ t/unit-tests/t-reftable-basics.c: int cmd_main(int argc, const char *argv[])
- TEST(test_binsearch(), "binary search with binsearch works");
- TEST(test_names_length(), "names_length retuns size of a NULL-terminated string array");
- TEST(test_names_equal(), "names_equal compares NULL-terminated string arrays");
-- TEST(test_u24_roundtrip(), "put_be24 and get_be24 work");
-+ TEST(test_be_roundtrip(), "put_be24, get_be24 and put_be16 work");
-
- return test_done();
- }

#11 lore raw

PATCH v3 1/5 t: move reftable/basics_test.c to the unit testing framework

reftable/basics_test.c exercise the functions defined in reftable/basics.{c, h}. Migrate reftable/basics_test.c to the unit testing framework. Migration involves refactoring the tests to use the unit testing framework instead of reftable's test framework.

---
 Makefile                                      |  2 +-
 t/helper/test-reftable.c                      |  1 -
 .../unit-tests/t-reftable-basics.c            | 41 +++++++++----------
 3 files changed, 20 insertions(+), 24 deletions(-)
 rename reftable/basics_test.c => t/unit-tests/t-reftable-basics.c (65%)
Makefile +1 -1
index 8f4432ae57..36188ca256 100644
@@ -1337,6 +1337,7 @@ THIRD_PARTY_SOURCES += sha1dc/%
 UNIT_TEST_PROGRAMS += t-ctype
 UNIT_TEST_PROGRAMS += t-mem-pool
 UNIT_TEST_PROGRAMS += t-prio-queue
+UNIT_TEST_PROGRAMS += t-reftable-basics
 UNIT_TEST_PROGRAMS += t-strbuf
 UNIT_TEST_PROGRAMS += t-trailer
 UNIT_TEST_PROGS = $(patsubst %,$(UNIT_TEST_BIN)/%$X,$(UNIT_TEST_PROGRAMS))
@@ -2671,7 +2672,6 @@ REFTABLE_OBJS += reftable/stack.o
 REFTABLE_OBJS += reftable/tree.o
 REFTABLE_OBJS += reftable/writer.o
 
-REFTABLE_TEST_OBJS += reftable/basics_test.o
 REFTABLE_TEST_OBJS += reftable/block_test.o
 REFTABLE_TEST_OBJS += reftable/dump.o
 REFTABLE_TEST_OBJS += reftable/merged_test.o
t/helper/test-reftable.c +0 -1
index bae731669c..9160bc5da6 100644
@@ -5,7 +5,6 @@
 int cmd__reftable(int argc, const char **argv)
 {
 	/* test from simple to complex. */
-	basics_test_main(argc, argv);
 	record_test_main(argc, argv);
 	block_test_main(argc, argv);
 	tree_test_main(argc, argv);
t/unit-tests/t-reftable-basics.c renamed from reftable/basics_test.c +19 -22

87 lines not on this page · raw message

Signature · 1 line
2.45.GIT
#12 lore raw

PATCH v3 2/5 t: move tests from reftable/stack_test.c to the new unit test

In reply to Chandra Pratap

parse_names() and names_equal() are functions defined in reftable/basics.{c, h}. Move the tests for these functions from reftable/stack_test.c to the newly ported test.

---
 reftable/stack_test.c            | 25 -------------------------
 t/unit-tests/t-reftable-basics.c | 25 ++++++++++++++++++++++---
 2 files changed, 22 insertions(+), 28 deletions(-)
reftable/stack_test.c +0 -25
index 7889f818d1..6f6af11e53 100644
@@ -102,29 +102,6 @@ static void test_read_file(void)
 	(void) remove(fn);
 }
 
-static void test_parse_names(void)
-{
-	char buf[] = "line\n";
-	char **names = NULL;
-	parse_names(buf, strlen(buf), &names);
-
-	EXPECT(NULL != names[0]);
-	EXPECT(0 == strcmp(names[0], "line"));
-	EXPECT(NULL == names[1]);
-	free_names(names);
-}
-
-static void test_names_equal(void)
-{
-	char *a[] = { "a", "b", "c", NULL };
-	char *b[] = { "a", "b", "d", NULL };
-	char *c[] = { "a", "b", NULL };
-
-	EXPECT(names_equal(a, a));
-	EXPECT(!names_equal(a, b));
-	EXPECT(!names_equal(a, c));
-}
-
 static int write_test_ref(struct reftable_writer *wr, void *arg)
 {
 	struct reftable_ref_record *ref = arg;
@@ -1048,8 +1025,6 @@ static void test_reftable_stack_compaction_concurrent_clean(void)
 int stack_test_main(int argc, const char *argv[])
 {
 	RUN_TEST(test_empty_add);
-	RUN_TEST(test_names_equal);
-	RUN_TEST(test_parse_names);
 	RUN_TEST(test_read_file);
 	RUN_TEST(test_reflog_expire);
 	RUN_TEST(test_reftable_stack_add);
t/unit-tests/t-reftable-basics.c +22 -3
index 99e6c89120..55fcff12d9 100644
@@ -58,14 +58,32 @@ static void test_names_length(void)
 	check_int(names_length(a), ==, 2);
 }
 
+static void test_names_equal(void)
+{
+	char *a[] = { "a", "b", "c", NULL };
+	char *b[] = { "a", "b", "d", NULL };
+	char *c[] = { "a", "b", NULL };
+
+	check(names_equal(a, a));
+	check(!names_equal(a, b));
+	check(!names_equal(a, c));
+}
+
 static void test_parse_names_normal(void)
 {
-	char in[] = "a\nb\n";
+	char in1[] = "line\n";
+	char in2[] = "a\nb\nc";
 	char **out = NULL;
-	parse_names(in, strlen(in), &out);
+	parse_names(in1, strlen(in1), &out);
+	check_str(out[0], "line");
+	check(!out[1]);
+	free_names(out);
+
+	parse_names(in2, strlen(in2), &out);
 	check_str(out[0], "a");
 	check_str(out[1], "b");
-	check(!out[2]);
+	check_str(out[2], "c");
+	check(!out[3]);
 	free_names(out);
 }
 
@@ -97,6 +115,7 @@ int cmd_main(int argc, const char *argv[])
 	TEST(test_parse_names_drop_empty(), "parse_names drops empty string");
 	TEST(test_binsearch(), "binary search with binsearch works");
 	TEST(test_names_length(), "names_length retuns size of a NULL-terminated string array");
+	TEST(test_names_equal(), "names_equal compares NULL-terminated string arrays");
 
 	return test_done();
 }
Signature · 1 line
2.45.GIT
#13 lore raw

PATCH v3 3/5 t: move tests from reftable/record_test.c to the new unit test

In reply to Chandra Pratap

common_prefix_size(), get_be24() and put_be24() are functions defined in reftable/basics.{c, h}. Move the tests for these functions from reftable/record_test.c to the newly ported test.

---
 reftable/record_test.c           | 37 -----------------------------
 t/unit-tests/t-reftable-basics.c | 40 ++++++++++++++++++++++++++------
 2 files changed, 33 insertions(+), 44 deletions(-)
reftable/record_test.c +0 -37
index c158ee79ff..58290bdba3 100644
@@ -64,31 +64,6 @@ static void test_varint_roundtrip(void)
 	}
 }
 
-static void test_common_prefix(void)
-{
-	struct {
-		const char *a, *b;
-		int want;
-	} cases[] = {
-		{ "abc", "ab", 2 },
-		{ "", "abc", 0 },
-		{ "abc", "abd", 2 },
-		{ "abc", "pqr", 0 },
-	};
-
-	int i = 0;
-	for (i = 0; i < ARRAY_SIZE(cases); i++) {
-		struct strbuf a = STRBUF_INIT;
-		struct strbuf b = STRBUF_INIT;
-		strbuf_addstr(&a, cases[i].a);
-		strbuf_addstr(&b, cases[i].b);
-		EXPECT(common_prefix_size(&a, &b) == cases[i].want);
-
-		strbuf_release(&a);
-		strbuf_release(&b);
-	}
-}
-
 static void set_hash(uint8_t *h, int j)
 {
 	int i = 0;
@@ -258,16 +233,6 @@ static void test_reftable_log_record_roundtrip(void)
 	strbuf_release(&scratch);
 }
 
-static void test_u24_roundtrip(void)
-{
-	uint32_t in = 0x112233;
-	uint8_t dest[3];
-	uint32_t out;
-	put_be24(dest, in);
-	out = get_be24(dest);
-	EXPECT(in == out);
-}
-
 static void test_key_roundtrip(void)
 {
 	uint8_t buffer[1024] = { 0 };
@@ -411,9 +376,7 @@ int record_test_main(int argc, const char *argv[])
 	RUN_TEST(test_reftable_ref_record_roundtrip);
 	RUN_TEST(test_varint_roundtrip);
 	RUN_TEST(test_key_roundtrip);
-	RUN_TEST(test_common_prefix);
 	RUN_TEST(test_reftable_obj_record_roundtrip);
 	RUN_TEST(test_reftable_index_record_roundtrip);
-	RUN_TEST(test_u24_roundtrip);
 	return 0;
 }
t/unit-tests/t-reftable-basics.c +33 -7
index 55fcff12d9..b02ca02040 100644
@@ -99,13 +99,38 @@ static void test_parse_names_drop_empty(void)
 
 static void test_common_prefix(void)
 {
-	struct strbuf s1 = STRBUF_INIT;
-	struct strbuf s2 = STRBUF_INIT;
-	strbuf_addstr(&s1, "abcdef");
-	strbuf_addstr(&s2, "abc");
-	check_int(common_prefix_size(&s1, &s2), ==, 3);
-	strbuf_release(&s1);
-	strbuf_release(&s2);
+	struct strbuf a = STRBUF_INIT;
+	struct strbuf b = STRBUF_INIT;
+	struct {
+		const char *a, *b;
+		int want;
+	} cases[] = {
+		{"abcdef", "abc", 3},
+		{ "abc", "ab", 2 },
+		{ "", "abc", 0 },
+		{ "abc", "abd", 2 },
+		{ "abc", "pqr", 0 },
+	};
+
+	for (size_t i = 0; i < ARRAY_SIZE(cases); i++) {
+		strbuf_addstr(&a, cases[i].a);
+		strbuf_addstr(&b, cases[i].b);
+		check_int(common_prefix_size(&a, &b), ==, cases[i].want);
+		strbuf_reset(&a);
+		strbuf_reset(&b);
+	}
+	strbuf_release(&a);
+	strbuf_release(&b);
+}
+
+static void test_u24_roundtrip(void)
+{
+	uint32_t in = 0x112233;
+	uint8_t dest[3];
+	uint32_t out;
+	put_be24(dest, in);
+	out = get_be24(dest);
+	check_int(in, ==, out);
 }
 
 int cmd_main(int argc, const char *argv[])
@@ -116,6 +141,7 @@ int cmd_main(int argc, const char *argv[])
 	TEST(test_binsearch(), "binary search with binsearch works");
 	TEST(test_names_length(), "names_length retuns size of a NULL-terminated string array");
 	TEST(test_names_equal(), "names_equal compares NULL-terminated string arrays");
+	TEST(test_u24_roundtrip(), "put_be24 and get_be24 work");
 
 	return test_done();
 }
Signature · 1 line
2.45.GIT
#14 lore raw

PATCH v3 4/5 t: add test for put_be16()

In reply to Chandra Pratap

put_be16() is a function defined in reftable/basics.{c, h} for which there are no tests in the current setup. Add a test for the same.

---
 t/unit-tests/t-reftable-basics.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
t/unit-tests/t-reftable-basics.c +11 -0
index b02ca02040..3c08218257 100644
@@ -133,6 +133,16 @@ static void test_u24_roundtrip(void)
 	check_int(in, ==, out);
 }
 
+static void test_u16_roundtrip(void)
+{
+	uint32_t in = 0xfef1;
+	uint8_t dest[3];
+	uint32_t out;
+	put_be16(dest, in);
+	out = get_be16(dest);
+	check_int(in, ==, out);
+}
+
 int cmd_main(int argc, const char *argv[])
 {
 	TEST(test_common_prefix(), "common_prefix_size works");
@@ -142,6 +152,7 @@ int cmd_main(int argc, const char *argv[])
 	TEST(test_names_length(), "names_length retuns size of a NULL-terminated string array");
 	TEST(test_names_equal(), "names_equal compares NULL-terminated string arrays");
 	TEST(test_u24_roundtrip(), "put_be24 and get_be24 work");
+	TEST(test_u16_roundtrip(), "put_be16 and get_be16 work");
 
 	return test_done();
 }
Signature · 1 line
2.45.GIT
#15 lore raw

PATCH v3 5/5 t: improve the test-case for parse_names()

In reply to Chandra Pratap

In the existing test-case for parse_names(), the fact that empty lines should be ignored is not obvious because the empty line is immediately followed by end-of-string. This can be mistaken as the empty line getting replaced by NULL. Improve this by adding a non-empty line after the empty one to demonstrate the intended behavior.

---
 t/unit-tests/t-reftable-basics.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
t/unit-tests/t-reftable-basics.c +4 -2
index 3c08218257..529049af12 100644
@@ -89,11 +89,13 @@ static void test_parse_names_normal(void)
 
 static void test_parse_names_drop_empty(void)
 {
-	char in[] = "a\n\n";
+	char in[] = "a\n\nb\n";
 	char **out = NULL;
 	parse_names(in, strlen(in), &out);
 	check_str(out[0], "a");
-	check(!out[1]);
+	/* simply '\n' should be dropped as empty string */
+	check_str(out[1], "b");
+	check(!out[2]);
 	free_names(out);
 }
 
Signature · 1 line
2.45.GIT
#16 lore raw
In reply to Chandra Pratap
Quoted 11 lines · Chandra Pratap

In the recent codebase update (commit 8bf6fbd, 2023-12-09), a new unit testing framework written entirely in C was introduced to the Git project aimed at simplifying testing and reducing test run times. Currently, tests for the reftable refs-backend are performed by a custom testing framework defined by reftable/test_framework.{c, h}. Port reftable/basics_test.c to the unit testing framework and improve upon the ported test.

This version looks good to me, thanks!

Patrick

#17 lore raw
Quoted 10 lines · Patrick Steinhardt
Quoted 8 lines · Chandra Pratap

In the recent codebase update (commit 8bf6fbd, 2023-12-09), a new unit testing framework written entirely in C was introduced to the Git project aimed at simplifying testing and reducing test run times. Currently, tests for the reftable refs-backend are performed by a custom testing framework defined by reftable/test_framework.{c, h}. Port reftable/basics_test.c to the unit testing framework and improve upon the ported test.

Quoted 5 lines
Quoted 3 lines

This version looks good to me, thanks!

It looks good to me too.

#18 lore raw
Quoted 11 lines · Christian Couder
Quoted 10 lines · Patrick Steinhardt
Quoted 8 lines · Chandra Pratap

In the recent codebase update (commit 8bf6fbd, 2023-12-09), a new unit testing framework written entirely in C was introduced to the Git project aimed at simplifying testing and reducing test run times. Currently, tests for the reftable refs-backend are performed by a custom testing framework defined by reftable/test_framework.{c, h}. Port reftable/basics_test.c to the unit testing framework and improve upon the ported test.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

This version looks good to me, thanks!

It looks good to me too.

Thanks, all. Queued.