Skip to contentSkip to conversation

unit-tests: convert t/helper/test-oid-array.c to unit-tests

PATCH In review Ghanshyam Thakkar 2 participants 5 messages Feb 23, 2024 – Feb 27, 2024 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 unit-tests: convert t/helper/test-oid-array.c to unit-tests

Migrate t/helper/test-oid-array.c and t/t0064-oid-array.sh to the recently added unit testing framework. This would improve runtime performance and provide better debugging via displaying array content, index at which the test failed etc. directly to stdout.

There is only one change in the new testing approach. In the previous testing method, a new repo gets initialized for the test according to GIT_TEST_DEFAULT_HASH algorithm. In unit testing however, we do not need to initialize the repo. We can set the length of the hexadecimal strbuf according to the algorithm used directly.

---
[RFC]: I recently saw a series by Eric W. Biederman [1] which enables the use of oid's with different hash algorithms into the same oid_array safely. However, there were no tests added for this. So, I am wondering if we should have a input format which allows us to specify hash algo for each oid with its hex value. i.e. "sha1:55" or "sha256:55", instead of just "55" and relying on GIT_TEST_DEFAULT_HASH for algo. So far, I tried to imitate the existing tests but I suppose this may be useful in the future if that series gets merged.

 Makefile                   |   2 +-
 t/helper/test-oid-array.c  |  45 --------
 t/helper/test-tool.c       |   1 -
 t/helper/test-tool.h       |   1 -
 t/t0064-oid-array.sh       | 104 -----------------
 t/unit-tests/t-oid-array.c | 222 +++++++++++++++++++++++++++++++++++++
 6 files changed, 223 insertions(+), 152 deletions(-)
 delete mode 100644 t/helper/test-oid-array.c
 delete mode 100755 t/t0064-oid-array.sh
 create mode 100644 t/unit-tests/t-oid-array.c
Makefile +1 -1
index 78e874099d..5060d7eff3 100644
@@ -820,7 +820,6 @@ TEST_BUILTINS_OBJS += test-lazy-init-name-hash.o
 TEST_BUILTINS_OBJS += test-match-trees.o
 TEST_BUILTINS_OBJS += test-mergesort.o
 TEST_BUILTINS_OBJS += test-mktemp.o
-TEST_BUILTINS_OBJS += test-oid-array.o
 TEST_BUILTINS_OBJS += test-oidmap.o
 TEST_BUILTINS_OBJS += test-oidtree.o
 TEST_BUILTINS_OBJS += test-online-cpus.o
@@ -1346,6 +1345,7 @@ UNIT_TEST_PROGRAMS += t-mem-pool
 UNIT_TEST_PROGRAMS += t-strbuf
 UNIT_TEST_PROGRAMS += t-ctype
 UNIT_TEST_PROGRAMS += t-prio-queue
+UNIT_TEST_PROGRAMS += t-oid-array
 UNIT_TEST_PROGS = $(patsubst %,$(UNIT_TEST_BIN)/%$X,$(UNIT_TEST_PROGRAMS))
 UNIT_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(UNIT_TEST_PROGRAMS))
 UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/test-lib.o
t/helper/test-oid-array.c deleted +0 -45

45 lines not on this page · raw message

t/helper/test-tool.c +0 -1
index 482a1e58a4..ad74bfffbe 100644
@@ -42,7 +42,6 @@ static struct test_cmd cmds[] = {
 	{ "match-trees", cmd__match_trees },
 	{ "mergesort", cmd__mergesort },
 	{ "mktemp", cmd__mktemp },
-	{ "oid-array", cmd__oid_array },
 	{ "oidmap", cmd__oidmap },
 	{ "oidtree", cmd__oidtree },
 	{ "online-cpus", cmd__online_cpus },
t/helper/test-tool.h +0 -1
index b1be7cfcf5..4f961a38c0 100644
@@ -65,7 +65,6 @@ int cmd__scrap_cache_tree(int argc, const char **argv);
 int cmd__serve_v2(int argc, const char **argv);
 int cmd__sha1(int argc, const char **argv);
 int cmd__sha1_is_sha1dc(int argc, const char **argv);
-int cmd__oid_array(int argc, const char **argv);
 int cmd__sha256(int argc, const char **argv);
 int cmd__sigchain(int argc, const char **argv);
 int cmd__simple_ipc(int argc, const char **argv);
t/t0064-oid-array.sh deleted +0 -104

104 lines not on this page · raw message

t/unit-tests/t-oid-array.c new file +222 -0

222 lines not on this page · raw message

Signature · 1 line
2.43.2
#1 lore raw
Quoted 8 lines · Ghanshyam Thakkar

[RFC]: I recently saw a series by Eric W. Biederman [1] which enables the use of oid's with different hash algorithms into the same oid_array safely. However, there were no tests added for this. So, I am wondering if we should have a input format which allows us to specify hash algo for each oid with its hex value. i.e. "sha1:55" or "sha256:55", instead of just "55" and relying on GIT_TEST_DEFAULT_HASH for algo. So far, I tried to imitate the existing tests but I suppose this may be useful in the future if that series gets merged.

[1]:
https://lore.kernel.org/git/[email protected]/

apologies for the noise.

#2 lore raw
In reply to Ghanshyam Thakkar
Quoted 5 lines · Ghanshyam Thakkar

Migrate t/helper/test-oid-array.c and t/t0064-oid-array.sh to the recently added unit testing framework. This would improve runtime performance and provide better debugging via displaying array content, index at which the test failed etc. directly to stdout.

It might not be a good idea to start working on a GSoC project we propose (Move existing tests to a unit testing framework) right now. You can work on it as part of your GSoC application, to show an example of how you would do it, and we might review that as part of reviewing your application. But for such a project if many candidates started working on it and sent patches to the mailing list before they get selected, then the project might be nearly finished before the GSoC even starts.

So I think it would be better to work on other things instead, like perhaps reviewing other people's work or working on other bug fixes or features. Anyway now that this is on the mailing list, I might as well review it as it could help with your application. But please consider working on other things.

Quoted 3 lines

There is only one change in the new testing approach. In the previous testing method, a new repo gets initialized for the test according to GIT_TEST_DEFAULT_HASH algorithm.

It looks like this happens in "t/test-lib-functions.sh", right? Telling a bit more about how and where that happens might help reviewers who would like to take a look.

Quoted 3 lines

In unit testing however, we do not
need to initialize the repo. We can set the length of the hexadecimal strbuf according to the algorithm used directly.

So is your patch doing that or not? It might be better to be explicit. Also if 'strbuf's are used, then is it really worth it to set their length in advance, instead of just letting them grow to the right length as we add hex to them?

Quoted 10 lines

---
[RFC]: I recently saw a series by Eric W. Biederman [1] which enables the use of oid's with different hash algorithms into the same oid_array safely. However, there were no tests added for this. So, I am wondering if we should have a input format which allows us to specify hash algo for each oid with its hex value. i.e. "sha1:55" or "sha256:55", instead of just "55" and relying on GIT_TEST_DEFAULT_HASH for algo. So far, I tried to imitate the existing tests but I suppose this may be useful in the future if that series gets merged.

The fact that there is a series touching the same area might also hint that it might not be the right time to work on this.

Quoted 6 lines
t/unit-tests/t-oid-array.c new file +0 -0
new file mode 100644
index 0000000000..b4f43c025d

t/unit-tests/t-oid-array.c line 8

Quoted 8 lines
t/unit-tests/t-oid-array.c new file +8 -0
@@ -0,0 +1,222 @@
+#include "test-lib.h"
+#include "hex.h"
+#include "oid-array.h"
+#include "strbuf.h"
+
+#define INPUT "88", "44", "aa", "55"
+#define INPUT_DUP \
+       "88", "44", "aa", "55", "88", "44", "aa", "55", "88", "44", "aa", "55"

Can you reuse INPUT in INPUT_DUP?

Quoted 12 lines
t/unit-tests/t-oid-array.c new file +12 -0
+#define INPUT_ONLY_DUP "55", "55"
+#define ENUMERATION_RESULT_SORTED "44", "55", "88", "aa"
+
+/*
+ * allocates the memory based on the hash algorithm used and sets the length to
+ * it.
+ */
+static void hex_strbuf_init(struct strbuf *hex)
+{
+       static int sz = -1;
+
+       if (sz == -1) {

t/unit-tests/t-oid-array.c line 30

Quoted 10 lines
t/unit-tests/t-oid-array.c new file +10 -0
+               char *algo_env = getenv("GIT_TEST_DEFAULT_HASH");
+               if (algo_env && !strcmp(algo_env, "sha256"))
+                       sz = GIT_SHA256_HEXSZ;
+               else
+                       sz = GIT_SHA1_HEXSZ;
+       }
+
+       strbuf_init(hex, sz);
+       strbuf_setlen(hex, sz);
+}

A strbuf can grow when we add stuff to it. We don't need to know its size in advance. So I am not sure this function is actually useful.

t/unit-tests/t-oid-array.c line 54

Quoted 6 lines
t/unit-tests/t-oid-array.c new file +6 -0
+/* fills the hex strbuf with alternating characters from 'c' */
+static void fill_hex_strbuf(struct strbuf *hex, char *c)
+{
+       size_t i;
+       for (i = 0; i < hex->len; i++)
+               hex->buf[i] = (i & 1) ? c[1] : c[0];

There is strbuf_addch() to add a single char to a strbuf, or strbuf_add() and strbuf_addstr() to add many chars at once.

Quoted 1 line
t/unit-tests/t-oid-array.c new file +1 -0
+}
#3 lore raw
Quoted 9 lines · Christian Couder

It might not be a good idea to start working on a GSoC project we propose (Move existing tests to a unit testing framework) right now. You can work on it as part of your GSoC application, to show an example of how you would do it, and we might review that as part of reviewing your application. But for such a project if many candidates started working on it and sent patches to the mailing list before they get selected, then the project might be nearly finished before the GSoC even starts.

Quoted 5 lines

So I think it would be better to work on other things instead, like perhaps reviewing other people's work or working on other bug fixes or features. Anyway now that this is on the mailing list, I might as well review it as it could help with your application. But please consider working on other things.

I understand and will work on other things.

Quoted 7 lines
Quoted 3 lines

There is only one change in the new testing approach. In the previous testing method, a new repo gets initialized for the test according to GIT_TEST_DEFAULT_HASH algorithm.

It looks like this happens in "t/test-lib-functions.sh", right? Telling a bit more about how and where that happens might help reviewers who would like to take a look.

Yeah, that is happens in "t/test-lib-functions.sh". I will update the commit message to describe this better.

Quoted 9 lines
Quoted 4 lines

In unit testing however, we do not
need to initialize the repo. We can set the length of the hexadecimal strbuf according to the algorithm used directly.

So is your patch doing that or not? It might be better to be explicit. Also if 'strbuf's are used, then is it really worth it to set their length in advance, instead of just letting them grow to the right length as we add hex to them?

I thought of it like this: If we were to just let them grow, then we would need separate logic for reusing that strbuf or use a different one everytime since it always grows. By separating allocation (hex_strbuf_init) and manipulation (fill_hex_strbuf), that same strbuf can be reused for different hex values.

But, none of the test currently need to reuse the same strbuf, so I suppose it is better to just let it grow and even if the need arises we can use strbuf_splice().

Quoted 11 lines
Quoted 10 lines

---
[RFC]: I recently saw a series by Eric W. Biederman [1] which enables the use of oid's with different hash algorithms into the same oid_array safely. However, there were no tests added for this. So, I am wondering if we should have a input format which allows us to specify hash algo for each oid with its hex value. i.e. "sha1:55" or "sha256:55", instead of just "55" and relying on GIT_TEST_DEFAULT_HASH for algo. So far, I tried to imitate the existing tests but I suppose this may be useful in the future if that series gets merged.

Quoted 2 lines

The fact that there is a series touching the same area might also hint that it might not be the right time to work on this.

I understand.

Quoted 8 lines
Quoted 8 lines
t/unit-tests/t-oid-array.c new file +2 -0
new file mode 100644
index 0000000000..b4f43c025d
@@ -0,0 +1,222 @@
+#include "test-lib.h"
+#include "hex.h"
Quoted 8 lines

t/unit-tests/t-oid-array.c line 8

Quoted 6 lines
t/unit-tests/t-oid-array.c new file +6 -0
+#include "oid-array.h"
+#include "strbuf.h"
+
+#define INPUT "88", "44", "aa", "55"
+#define INPUT_DUP \
+       "88", "44", "aa", "55", "88", "44", "aa", "55", "88", "44", "aa", "55"

Can you reuse INPUT in INPUT_DUP?

Yeah, that would be more clearer.

Quoted 17 lines
Quoted 17 lines
t/unit-tests/t-oid-array.c new file +17 -0
+#define INPUT_ONLY_DUP "55", "55"
+#define ENUMERATION_RESULT_SORTED "44", "55", "88", "aa"
+
+/*
+ * allocates the memory based on the hash algorithm used and sets the length to
+ * it.
+ */
+static void hex_strbuf_init(struct strbuf *hex)
+{
+       static int sz = -1;
+
+       if (sz == -1) {
+               char *algo_env = getenv("GIT_TEST_DEFAULT_HASH");
+               if (algo_env && !strcmp(algo_env, "sha256"))
+                       sz = GIT_SHA256_HEXSZ;
+               else
+                       sz = GIT_SHA1_HEXSZ;
Quoted 8 lines

t/unit-tests/t-oid-array.c line 30

Quoted 5 lines
t/unit-tests/t-oid-array.c new file +5 -0
+       }
+
+       strbuf_init(hex, sz);
+       strbuf_setlen(hex, sz);
+}

A strbuf can grow when we add stuff to it. We don't need to know its size in advance. So I am not sure this function is actually useful.

Yeah, this was mainly for deciding the hash algorithm but that logic can be moved to fill_hex_strbuf.

Quoted 9 lines

t/unit-tests/t-oid-array.c line 54

Quoted 6 lines
t/unit-tests/t-oid-array.c new file +6 -0
+/* fills the hex strbuf with alternating characters from 'c' */
+static void fill_hex_strbuf(struct strbuf *hex, char *c)
+{
+       size_t i;
+       for (i = 0; i < hex->len; i++)
+               hex->buf[i] = (i & 1) ? c[1] : c[0];

There is strbuf_addch() to add a single char to a strbuf, or strbuf_add() and strbuf_addstr() to add many chars at once.

Will update it.

Thank you for the feedback and review.

#4 lore raw
Quoted 10 lines · Ghanshyam Thakkar
Quoted 5 lines · Christian Couder

So I think it would be better to work on other things instead, like perhaps reviewing other people's work or working on other bug fixes or features. Anyway now that this is on the mailing list, I might as well review it as it could help with your application. But please consider working on other things.

I understand and will work on other things.

Thanks!

Quoted 15 lines
Quoted 8 lines
Quoted 3 lines

In unit testing however, we do not
need to initialize the repo. We can set the length of the hexadecimal strbuf according to the algorithm used directly.

So is your patch doing that or not? It might be better to be explicit. Also if 'strbuf's are used, then is it really worth it to set their length in advance, instead of just letting them grow to the right length as we add hex to them?

I thought of it like this: If we were to just let them grow, then we would need separate logic for reusing that strbuf or use a different one everytime since it always grows. By separating allocation (hex_strbuf_init) and manipulation (fill_hex_strbuf), that same strbuf can be reused for different hex values.

Quoted 3 lines

But, none of the test currently need to reuse the same strbuf, so I suppose it is better to just let it grow and even if the need arises we can use strbuf_splice().

It's not a problem to use a new strbuf for each different hex value. Tests don't need a lot of performance as they are used mostly by developers, not by everyone using Git. Also if you want to reuse a strbuf, you can just use strbuf_reset() on it and then reuse it.