t/unit-tests: convert hash tests to use clar
Subsystems (2)
cover t/unit-tests: convert hash tests to use clar
Hello,
This small patch series transitions the existing unit test file t-hash.c to the Clar testing framework. This change is part of our ongoing effort to standardize our testing approach and enhance maintainability.
Thanks
Seyi
Seyi Kuforiji (2):
t/unit-tests: match functions signature with trailing code t/unit-tests: convert hash to use clar test framework
Makefile | 2 +- t/meson.build | 2 +- t/unit-tests/generate-clar-decls.sh | 2 +- t/unit-tests/t-hash.c | 84 ---------------------------- t/unit-tests/u-hash.c | 85 +++++++++++++++++++++++++++++ 5 files changed, 88 insertions(+), 87 deletions(-) delete mode 100644 t/unit-tests/t-hash.c create mode 100644 t/unit-tests/u-hash.c
Signature · 1 line
2.34.1
PATCH 1/2 t/unit-tests: match functions signature with trailing code
The generate-clar-decls.sh script extracts signatures of test functions from our unit tests, which will later get used by the clar to automatically wire up tests. The sed command only matches lines that ended immediately after void), causing it to miss declarations with additional content such as comments or annotations.
Relax the regular expression by making it match lines with trailing data after the function signature. This ensures that all valid function declarations are captured and formatted as extern declarations regardless of their formatting style, improving the robustness of the script when parsing $suite files.
This will be used in subsequent commits to match and capture the function signature correctly, regardless of any trailing content.
--- t/unit-tests/generate-clar-decls.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
t/unit-tests/generate-clar-decls.sh +1 -1
@@ -14,6 +14,6 @@ do suite_name=$(basename "$suite") suite_name=${suite_name%.c} suite_name=${suite_name#u-} - sed -ne "s/^\(void test_${suite_name}__[a-zA-Z_0-9][a-zA-Z_0-9]*(void)\)$/extern \1;/p" "$suite" || + sed -ne "s/^\(void test_${suite_name}__[a-zA-Z_0-9][a-zA-Z_0-9]*(void)\).*/extern \1;/p" "$suite" || exit 1 done >"$OUTPUT"
--
2.34.1
Re: [PATCH 1/2] t/unit-tests: match functions signature with trailing code
Quoted 6 lines · Seyi Kuforiji
The
generate-clar-decls.shscript extracts signatures of test functions from our unit tests, which will later get used by the clar to automatically wire up tests. The sed command only matches lines that ended immediately aftervoid), causing it to miss declarations with additional content such as comments or annotations.
Quoted 8 lines
Relax the regular expression by making it match lines with trailing data after the function signature. This ensures that all valid function declarations are captured and formatted as
externdeclarations regardless of their formatting style, improving the robustness of the script when parsing$suitefiles.This will be used in subsequent commits to match and capture the function signature correctly, regardless of any trailing content.
I am not sure if this is going in the right direction, though.
Especially for things like test suites that are looked at and worked on only by develoeprs *and* these tools, being uniform and consistent weighs more than being more flexible.
Let me state it in another way. How many of the existing test pieces are picked up by the current pattern, and among them how many of them would see vast improvements if they are allowed to have arbitrary garbage after their "I do not take any arguments" function signature? Are new tests you are migrating from outside the clar world lose a lot if they are no longer allowed to have comments there, or would it be suffice to have the comments before the functions (which many of our function definition do anyway)?
A quick peek at [PATCH 2/2] tells me that this is not even something that would make it easier to port the existing tests by allowing more straight line-by-line copies or something. The patch splits many in-line test pieces in the "main" into separate functions, and it does so in a rather unusual format, e.g.,
void test_hash__multi_character(void) TEST_HASH_STR("abc",
"a9993e364706816aba3e25717850c26c9cd0d89d",
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")where TEST_HASH_STR() expands to the function body that starts with a "{" and ends with a "}". It can well be written more like
void test_hash__multi_character(void)
{
TEST_HASH_STR("abc",
"a9993e364706816aba3e25717850c26c9cd0d89d",
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
}and we do not need this step at all if we did so. Such a construct would be a lot friendlier to the editors that auto-indent, too.
So, I do not quite see much value in this particular change.
Quoted 20 lines
Mentored-by Patrick Steinhardt <[email protected]>Signed-off-by Seyi Kuforiji <[email protected]>--- t/unit-tests/generate-clar-decls.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)t/unit-tests/generate-clar-decls.sh +1
-2@@ -14,6 +14,6 @@ do suite_name=$(basename "$suite") suite_name=${suite_name%.c} suite_name=${suite_name#u-} - sed -ne "s/^\(void test_${suite_name}__[a-zA-Z_0-9][a-zA-Z_0-9]*(void)\)$/extern \1;/p" "$suite" || + sed -ne "s/^\(void test_${suite_name}__[a-zA-Z_0-9][a-zA-Z_0-9]*(void)\).*/extern \1;/p" "$suite" || exit 1 done >"$OUTPUT" --2.34.1
Re: [PATCH 1/2] t/unit-tests: match functions signature with trailing code
Quoted 20 lines · Junio C Hamano
A quick peek at [PATCH 2/2] tells me that this is not even something that would make it easier to port the existing tests by allowing more straight line-by-line copies or something. The patch splits many in-line test pieces in the "main" into separate functions, and it does so in a rather unusual format, e.g.,
void
test_hash__multi_character(void)TEST_HASH_STR("abc","a9993e364706816aba3e25717850c26c9cd0d89d", "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")where
TEST_HASH_STR()expands to the function body that starts with a "{" and ends with a "}". It can well be written more likevoid test_hash__multi_character(void) { TEST_HASH_STR("abc", "a9993e364706816aba3e25717850c26c9cd0d89d", "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"); }
Quoted 4 lines
and we do not need this step at all if we did so. Such a construct would be a lot friendlier to the editors that auto-indent, too.
So, I do not quite see much value in this particular change.
Having said that, if this were more like that you write a series of
DEF_HASH_TEST(multi_character, "abc", "a9993e...", "ba7816bf...")
and they expand to
void test_hash__multi_character(void)
{
const char *expected[] = {"a9993e...", "ba7816bf..."};
check_hash_data("abc", strlen("abc"), expected);
}then a preparatory step like this patch _might_ be justifiable. You may want to avoid having to write too many boilerplate, and a special rule to find "DEF_HASH_TEST(name, ...)" and it might make sense to add support to extract the name of the test function being defined by the macro automatically.
Not that I think such a sequence of DEF_HASH_TEST(), one per line, is an improvement at all (it also is unfriendly to editors that auto-indent the same way as your original version). I just wanted to say that a change to the pattern to pick up the function name may be justifiable if it were so.
Thanks.
Re: [PATCH 1/2] t/unit-tests: match functions signature with trailing code
In reply to Junio C HamanoQuoted 52 lines · Junio C Hamano
Quoted 6 lines · Seyi Kuforiji
The
generate-clar-decls.shscript extracts signatures of test functions from our unit tests, which will later get used by the clar to automatically wire up tests. The sed command only matches lines that ended immediately aftervoid), causing it to miss declarations with additional content such as comments or annotations.Quoted 8 lines
Relax the regular expression by making it match lines with trailing data after the function signature. This ensures that all valid function declarations are captured and formatted as
externdeclarations regardless of their formatting style, improving the robustness of the script when parsing$suitefiles.This will be used in subsequent commits to match and capture the function signature correctly, regardless of any trailing content.
I am not sure if this is going in the right direction, though.
Especially for things like test suites that are looked at and worked on only by develoeprs *and* these tools, being uniform and consistent weighs more than being more flexible.
Let me state it in another way. How many of the existing test pieces are picked up by the current pattern, and among them how many of them would see vast improvements if they are allowed to have arbitrary garbage after their "I do not take any arguments" function signature? Are new tests you are migrating from outside the clar world lose a lot if they are no longer allowed to have comments there, or would it be suffice to have the comments before the functions (which many of our function definition do anyway)?
A quick peek at [PATCH 2/2] tells me that this is not even something that would make it easier to port the existing tests by allowing more straight line-by-line copies or something. The patch splits many in-line test pieces in the "main" into separate functions, and it does so in a rather unusual format, e.g.,
void
test_hash__multi_character(void)TEST_HASH_STR("abc","a9993e364706816aba3e25717850c26c9cd0d89d", "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")where
TEST_HASH_STR()expands to the function body that starts with a "{" and ends with a "}". It can well be written more likevoid test_hash__multi_character(void) { TEST_HASH_STR("abc", "a9993e364706816aba3e25717850c26c9cd0d89d", "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"); }
Quoted 4 lines
and we do not need this step at all if we did so. Such a construct would be a lot friendlier to the editors that auto-indent, too.
So, I do not quite see much value in this particular change.
Yeah. This was something I proposed, but I already mentioned to Seyi that I'm not all that happy with the outcome as it has a couple of downsides, for example broken syntax highlighting in lots of editors. I said he can send that version to the mailing list anyway and get some feedback on it to figure out whether my discomfort with my own idea is warranted or not. And your comment here basically confirms that my idea wasn't that great after all :)
So I agree with you, let's scrap the idea and have proper function bodies instead.
Patrick
Re: [PATCH 1/2] t/unit-tests: match functions signature with trailing code
Quoted 67 lines · Patrick Steinhardt
Quoted 52 lines · Junio C Hamano
Quoted 6 lines · Seyi Kuforiji
The
generate-clar-decls.shscript extracts signatures of test functions from our unit tests, which will later get used by the clar to automatically wire up tests. The sed command only matches lines that ended immediately aftervoid), causing it to miss declarations with additional content such as comments or annotations.Quoted 8 lines
Relax the regular expression by making it match lines with trailing data after the function signature. This ensures that all valid function declarations are captured and formatted as
externdeclarations regardless of their formatting style, improving the robustness of the script when parsing$suitefiles.This will be used in subsequent commits to match and capture the function signature correctly, regardless of any trailing content.
I am not sure if this is going in the right direction, though.
Especially for things like test suites that are looked at and worked on only by develoeprs *and* these tools, being uniform and consistent weighs more than being more flexible.
Let me state it in another way. How many of the existing test pieces are picked up by the current pattern, and among them how many of them would see vast improvements if they are allowed to have arbitrary garbage after their "I do not take any arguments" function signature? Are new tests you are migrating from outside the clar world lose a lot if they are no longer allowed to have comments there, or would it be suffice to have the comments before the functions (which many of our function definition do anyway)?
A quick peek at [PATCH 2/2] tells me that this is not even something that would make it easier to port the existing tests by allowing more straight line-by-line copies or something. The patch splits many in-line test pieces in the "main" into separate functions, and it does so in a rather unusual format, e.g.,
void
test_hash__multi_character(void)TEST_HASH_STR("abc","a9993e364706816aba3e25717850c26c9cd0d89d", "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")where
TEST_HASH_STR()expands to the function body that starts with a "{" and ends with a "}". It can well be written more likevoid test_hash__multi_character(void) { TEST_HASH_STR("abc", "a9993e364706816aba3e25717850c26c9cd0d89d", "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"); }Quoted 4 lines
and we do not need this step at all if we did so. Such a construct would be a lot friendlier to the editors that auto-indent, too.
So, I do not quite see much value in this particular change.
Yeah. This was something I proposed, but I already mentioned to Seyi that I'm not all that happy with the outcome as it has a couple of downsides, for example broken syntax highlighting in lots of editors. I said he can send that version to the mailing list anyway and get some feedback on it to figure out whether my discomfort with my own idea is warranted or not. And your comment here basically confirms that my idea wasn't that great after all :)
Quoted 4 lines
So I agree with you, let's scrap the idea and have proper function bodies instead.
Patrick
Thank you for the feedback and insights. I'll update the patch to use standard function bodies.
Thanks
Seyi
Re: [PATCH 1/2] t/unit-tests: match functions signature with trailing code
In reply to Patrick SteinhardtQuoted 2 lines · Patrick Steinhardt
So I agree with you, let's scrap the idea and have proper function bodies instead.
Yup, sometimes, simple, stupid, and good enough is the way to go.
We could do
-- >8 --
#define T(testname, input, expect1, expect256) \
void test_hash__ ## testname(void) \
{ \
const char *expect[] = { expect1, expect256 }; \
check_hash_data(input, strlen(input), expect); \
} extern void test_hash__ ## testname()T(empty_string, "", "da39...", "e3b0c4..."); T(single_character, "a", "86f7e4...", "ca97811...");
-- 8< --
which may not upset syntax-aware editors too much.
Unless there are more than several dozens of them, I do not think it is worth it, though ;-)
Re: [PATCH 1/2] t/unit-tests: match functions signature with trailing code
Quoted 21 lines · Junio C Hamano
Quoted 2 lines · Patrick Steinhardt
So I agree with you, let's scrap the idea and have proper function bodies instead.
Yup, sometimes, simple, stupid, and good enough is the way to go.
We could do
-- >8 --
#define T(testname, input, expect1, expect256) \ void test_hash__ ## testname(void) \ { \ const char *expect[] = { expect1, expect256 }; \ check_hash_data(input, strlen(input), expect); \ } extern void test_hash__ ## testname()
T(empty_string, "", "da39...", "e3b0c4...");T(single_character, "a", "86f7e4...", "ca97811...");
Quoted 6 lines
-- 8< --
which may not upset syntax-aware editors too much.
Unless there are more than several dozens of them, I do not think it is worth it, though ;-)
Agreed, I'd go with the simple solution for now, which is to have function bodies.
Patrick
PATCH 2/2 t/unit-tests: convert hash to use clar test framework
In reply to Seyi KuforijiAdapt the hash test functions to clar framework by using clar assertions where necessary. Following the consensus to convert the unit-tests scripts found in the t/unit-tests folder to clar driven by Patrick Steinhardt. Test functions are structured as a standalone to test individual hash string and literal case.
--- Makefile | 2 +- t/meson.build | 2 +- t/unit-tests/t-hash.c | 84 ------------------------------------------ t/unit-tests/u-hash.c | 85 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 86 deletions(-) delete mode 100644 t/unit-tests/t-hash.c create mode 100644 t/unit-tests/u-hash.c
Makefile +1 -1
@@ -1338,6 +1338,7 @@ THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/% THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/clar/% CLAR_TEST_SUITES += u-ctype +CLAR_TEST_SUITES += u-hash CLAR_TEST_SUITES += u-strvec CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X) CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES))
@@ -1345,7 +1346,6 @@ CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/clar/clar.o CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/unit-test.o UNIT_TEST_PROGRAMS += t-example-decorate -UNIT_TEST_PROGRAMS += t-hash UNIT_TEST_PROGRAMS += t-hashmap UNIT_TEST_PROGRAMS += t-mem-pool UNIT_TEST_PROGRAMS += t-oid-array
t/meson.build +1 -1
@@ -1,6 +1,7 @@ clar_test_suites = [ 'unit-tests/u-ctype.c', 'unit-tests/u-strvec.c', + 'unit-tests/u-hash.c', ] clar_sources = [
@@ -41,7 +42,6 @@ test('unit-tests', clar_unit_tests) unit_test_programs = [ 'unit-tests/t-example-decorate.c', - 'unit-tests/t-hash.c', 'unit-tests/t-hashmap.c', 'unit-tests/t-mem-pool.c', 'unit-tests/t-oid-array.c',
t/unit-tests/t-hash.c deleted +0 -84
84 lines not on this page · raw message
t/unit-tests/u-hash.c new file +85 -0
85 lines not on this page · raw message
Signature · 1 line
2.34.1
v2 cover t/unit-tests: convert hash to use clar test framework
In reply to Seyi KuforijiHello,
This small patch series transitions the existing unit test file t-hash.c to the Clar testing framework. This change is part of our ongoing effort to standardize our testing approach and enhance maintainability.
changes in v2:
- Some small fixes were made to the macros and test functions
- Link to v1: https://public-inbox.org/git/[email protected]/T/#t
Thanks
Seyi
Seyi Kuforiji (1):
t/unit-tests: convert hash to use clar test framework
Makefile | 2 +-
t/meson.build | 2 +-
t/unit-tests/{t-hash.c => u-hash.c} | 75 +++++++++++++++++++----------
3 files changed, 52 insertions(+), 27 deletions(-)
rename t/unit-tests/{t-hash.c => u-hash.c} (79%)Range-diff against v1:
-: ---------- > 1: 6fde57893d t/unit-tests: convert hash to use clar test framework
Signature · 1 line
2.34.1
PATCH v2 1/1 t/unit-tests: convert hash to use clar test framework
Adapt the hash test functions to clar framework by using clar assertions where necessary. Following the consensus to convert the unit-tests scripts found in the t/unit-tests folder to clar driven by Patrick Steinhardt. Test functions are structured as a standalone to test individual hash string and literal case.
---
Makefile | 2 +-
t/meson.build | 2 +-
t/unit-tests/{t-hash.c => u-hash.c} | 75 +++++++++++++++++++----------
3 files changed, 52 insertions(+), 27 deletions(-)
rename t/unit-tests/{t-hash.c => u-hash.c} (79%)Makefile +1 -1
@@ -1338,6 +1338,7 @@ THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/% THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/clar/% CLAR_TEST_SUITES += u-ctype +CLAR_TEST_SUITES += u-hash CLAR_TEST_SUITES += u-strvec CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X) CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES))
@@ -1345,7 +1346,6 @@ CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/clar/clar.o CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/unit-test.o UNIT_TEST_PROGRAMS += t-example-decorate -UNIT_TEST_PROGRAMS += t-hash UNIT_TEST_PROGRAMS += t-hashmap UNIT_TEST_PROGRAMS += t-mem-pool UNIT_TEST_PROGRAMS += t-oid-array
t/meson.build +1 -1
@@ -1,6 +1,7 @@ clar_test_suites = [ 'unit-tests/u-ctype.c', 'unit-tests/u-strvec.c', + 'unit-tests/u-hash.c', ] clar_sources = [
@@ -41,7 +42,6 @@ test('unit-tests', clar_unit_tests) unit_test_programs = [ 'unit-tests/t-example-decorate.c', - 'unit-tests/t-hash.c', 'unit-tests/t-hashmap.c', 'unit-tests/t-mem-pool.c', 'unit-tests/t-oid-array.c',
t/unit-tests/u-hash.c renamed from t/unit-tests/t-hash.c +50 -25
130 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v2 1/1] t/unit-tests: convert hash to use clar test framework
Quoted 3 lines · Seyi Kuforiji
CLAR_TEST_SUITES+= u-ctype
+CLAR_TEST_SUITES += u-hash
CLAR_TEST_SUITES+= u-strvec
This is inserted in the middle, presumably because a list without inherent ordering is by default maintained as a lexicographically sorted list.
t/meson.build line 5
Quoted 10 lines
t/meson.build +1
-0@@ -1,6 +1,7 @@ clar_test_suites = [ 'unit-tests/u-ctype.c', 'unit-tests/u-strvec.c', + 'unit-tests/u-hash.c', ]
So, shouldn't we do the same here?
t/unit-tests/u-hash.c static void check_hash_data(const void *data, size_t data_length, · line 27
Quoted 12 lines
t/unit-tests/u-hash.c renamed from t/unit-tests/t-hash.c +2
-3#define TEST_HASH_STR (data, expected_sha1, expected_sha256) do { \ const char *expected_hashes[] = { expected_sha1, expected_sha256 }; \ - TEST(check_hash_data(data, strlen(data), expected_hashes), \ - "SHA1 and SHA256 (%s) works", #data); \ - } while (0) + check_hash_data(data, strlen(data), expected_hashes); \ + } while(0)
Unwanted droppage of SP between "while" and "(0)".
t/unit-tests/u-hash.c static void check_hash_data(const void *data, size_t data_length, · line 33
Quoted 8 lines
/* Only works with a literal string, useful when it contains a NUL character. */ #define
TEST_HASH_LITERAL(literal, expected_sha1, expected_sha256)do { \t/unit-tests/u-hash.c renamed from t/unit-tests/t-hash.c +2
-3const char *expected_hashes[] = { expected_sha1, expected_sha256 }; \ -TEST (check_hash_data (literal, (sizeof (literal) -1 ), expected_hashes), \ - "SHA1 and SHA256 (%s) works", #literal); \ - }while (0 ) +check_hash_data (literal, (sizeof (literal) -1 ), expected_hashes); \ + }while (0 )
Ditto.
Quoted 10 lines
t/unit-tests/u-hash.c renamed from t/unit-tests/t-hash.c +1
-7-int cmd_main(int argc UNUSED, const char **argv UNUSED) +void test_hash__empty_string (void ) { -struct strbuf aaaaaaaaaa_100000 =STRBUF_INIT ; -struct strbuf alphabet_100000 =STRBUF_INIT ; - -strbuf_addstrings (&aaaaaaaaaa_100000,"aaaaaaaaaa" ,100000 ); -strbuf_addstrings (&alphabet_100000,"abcdefghijklmnopqrstuvwxyz" ,100000 ); - TEST_HASH_STR("",
t/unit-tests/u-hash.c static void check_hash_data(const void *data, size_t data_length, · line 47
Quoted 10 lines
t/unit-tests/u-hash.c renamed from t/unit-tests/t-hash.c +5
-0"da39a3ee5e6b4b0d3255bfef95601890afd80709" , "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); +} + +void test_hash__single_character (void ) +{TEST_HASH_STR ("a" ,"86f7e437faa5a7fce15d1ddcb9eaeaea377667b8" ,"ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb" ); +}
OK.
I'll skip the rest as I expect they would be faithful conversions.
Thanks.
Re: [PATCH v2 1/1] t/unit-tests: convert hash to use clar test framework
Quoted 11 lines · Junio C Hamano
Quoted 3 lines · Seyi Kuforiji
CLAR_TEST_SUITES+= u-ctype
+CLAR_TEST_SUITES += u-hash
CLAR_TEST_SUITES+= u-strvecThis is inserted in the middle, presumably because a list without inherent ordering is by default maintained as a lexicographically sorted list.
Quoted 13 lines
t/meson.build line 5
Quoted 10 lines
t/meson.build +1
-0@@ -1,6 +1,7 @@ clar_test_suites = [ 'unit-tests/u-ctype.c', 'unit-tests/u-strvec.c', + 'unit-tests/u-hash.c', ]So, shouldn't we do the same here?
Yes, we should. I'll make the necessary adjustment in the next iteration. Thanks!
Quoted 49 lines
t/unit-tests/u-hash.c static void check_hash_data(const void *data, size_t data_length, · line 27
Quoted 12 lines
t/unit-tests/u-hash.c renamed from t/unit-tests/t-hash.c +2
-3#define TEST_HASH_STR (data, expected_sha1, expected_sha256) do { \ const char *expected_hashes[] = { expected_sha1, expected_sha256 }; \ - TEST(check_hash_data(data, strlen(data), expected_hashes), \ - "SHA1 and SHA256 (%s) works", #data); \ - } while (0) + check_hash_data(data, strlen(data), expected_hashes); \ + } while(0)Unwanted droppage of SP between "while" and "(0)".
t/unit-tests/u-hash.c static void check_hash_data(const void *data, size_t data_length, · line 33
Quoted 8 lines
/* Only works with a literal string, useful when it contains a NUL character. */
#define TEST_HASH_LITERAL(literal, expected_sha1, expected_sha256) do { \ const char *expected_hashes[] = { expected_sha1, expected_sha256 }; \t/unit-tests/u-hash.c renamed from t/unit-tests/t-hash.c +2
-3-TEST (check_hash_data (literal, (sizeof (literal) -1 ), expected_hashes), \ - "SHA1 and SHA256 (%s) works", #literal); \ - }while (0 ) +check_hash_data (literal, (sizeof (literal) -1 ), expected_hashes); \ + }while (0 )Ditto.
Quoted 10 lines
t/unit-tests/u-hash.c renamed from t/unit-tests/t-hash.c +1
-7-int cmd_main(int argc UNUSED, const char **argv UNUSED) +void test_hash__empty_string (void ) { -struct strbuf aaaaaaaaaa_100000 =STRBUF_INIT ; -struct strbuf alphabet_100000 =STRBUF_INIT ; - -strbuf_addstrings (&aaaaaaaaaa_100000,"aaaaaaaaaa" ,100000 ); -strbuf_addstrings (&alphabet_100000,"abcdefghijklmnopqrstuvwxyz" ,100000 ); - TEST_HASH_STR("",t/unit-tests/u-hash.c static void check_hash_data(const void *data, size_t data_length, · line 47
Quoted 10 lines
t/unit-tests/u-hash.c renamed from t/unit-tests/t-hash.c +5
-0"da39a3ee5e6b4b0d3255bfef95601890afd80709" , "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); +} + +void test_hash__single_character (void ) +{TEST_HASH_STR ("a" ,"86f7e437faa5a7fce15d1ddcb9eaeaea377667b8" ,"ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb" ); +}OK.
Quoted 4 lines
I'll skip the rest as I expect they would be faithful conversions.
Thanks.
I'll make the required changes, thank you for pointing them out.
Thanks
Seyi
Re: [PATCH v2 0/1] t/unit-tests: convert hash to use clar test framework
In reply to Seyi KuforijiQuoted 5 lines · Seyi Kuforiji
Hello,
This small patch series transitions the existing unit test file
t-hash.cto the Clar testing framework. This change is part of our ongoing effort to standardize our testing approach and enhance maintainability.
Thanks; this is no longer a series but a single patch ;-)
Re: [PATCH v2 0/1] t/unit-tests: convert hash to use clar test framework
Quoted 10 lines · Junio C Hamano
Quoted 5 lines · Seyi Kuforiji
Hello,
This small patch series transitions the existing unit test file
t-hash.cto the Clar testing framework. This change is part of our ongoing effort to standardize our testing approach and enhance maintainability.Thanks; this is no longer a series but a single patch ;-)
You're absolutely right.Thank you for pointing that out :), I'll make sure to update my phrasing in future submissions.
Thanks
Seyi
PATCH v3 1/1 t/unit-tests: convert hash to use clar test framework
In reply to Seyi KuforijiAdapt the hash test functions to clar framework by using clar assertions where necessary. Following the consensus to convert the unit-tests scripts found in the t/unit-tests folder to clar driven by Patrick Steinhardt. Test functions are structured as a standalone to test individual hash string and literal case.
---
Changes relative to v2:
- A couple of fixes to code formatting to match our standards
Thanks
Seyi
---
Range-diff against v2:
-: ---------- > 1: fcc2a376a5 t/unit-tests: convert hash to use clar test framework
Makefile | 2 +-
t/meson.build | 2 +-
t/unit-tests/{t-hash.c => u-hash.c} | 71 +++++++++++++++++++----------
3 files changed, 50 insertions(+), 25 deletions(-)
rename t/unit-tests/{t-hash.c => u-hash.c} (80%)Makefile +1 -1
@@ -1338,6 +1338,7 @@ THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/% THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/clar/% CLAR_TEST_SUITES += u-ctype +CLAR_TEST_SUITES += u-hash CLAR_TEST_SUITES += u-strvec CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X) CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES))
@@ -1345,7 +1346,6 @@ CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/clar/clar.o CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/unit-test.o UNIT_TEST_PROGRAMS += t-example-decorate -UNIT_TEST_PROGRAMS += t-hash UNIT_TEST_PROGRAMS += t-hashmap UNIT_TEST_PROGRAMS += t-mem-pool UNIT_TEST_PROGRAMS += t-oid-array
t/meson.build +1 -1
@@ -1,5 +1,6 @@ clar_test_suites = [ 'unit-tests/u-ctype.c', + 'unit-tests/u-hash.c', 'unit-tests/u-strvec.c', ]
@@ -41,7 +42,6 @@ test('unit-tests', clar_unit_tests) unit_test_programs = [ 'unit-tests/t-example-decorate.c', - 'unit-tests/t-hash.c', 'unit-tests/t-hashmap.c', 'unit-tests/t-mem-pool.c', 'unit-tests/t-oid-array.c',
t/unit-tests/u-hash.c renamed from t/unit-tests/t-hash.c +48 -23
128 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v3] t/unit-tests: convert hash to use clar test framework
Quoted 2 lines · Seyi Kuforiji
Range-diff against v2:
-: ---------- > 1:fcc2a376a5t/unit-tests: convert hash to use clar test framework
Hm. The range-diff is a bit funny -- I would have expected it to recognize that it's the same patch as not a lot of things have changed compared to v2. Did you maybe compare to v1 by accident?
Anyway, this is of course no reason to send a revised version. The changes themselves look good to me, thanks!
Partick
Re: [PATCH v3] t/unit-tests: convert hash to use clar test framework
In reply to Seyi KuforijiQuoted 8 lines · Seyi Kuforiji
Adapt the hash test functions to clar framework by using clar assertions where necessary. Following the consensus to convert the unit-tests scripts found in the t/unit-tests folder to clar driven by Patrick Steinhardt. Test functions are structured as a standalone to test individual hash string and literal case.
Mentored-by Patrick Steinhardt <[email protected]>Signed-off-by Seyi Kuforiji <[email protected]>
The change to the test program was a very pleasant read. It was trivially obvious that the new version faithfully rewrites the original. E.g., ...
t/unit-tests/u-hash.c line 8
Quoted 8 lines
static void check_hash_data(const void *data,
size_tdata_length,t/unit-tests/u-hash.c renamed from t/unit-tests/t-hash.c +1
-4const char *expected_hashes[]) { -if (!check (data !=NULL )) { -test_msg ("BUG: NULL data pointer provided" ); -return ; - } +cl_assert (data !=NULL );
... instead of using check() and giving message with test_msg(), the clar framework gives cl_assert() for us to use.
And ...
t/unit-tests/u-hash.c static void check_hash_data(const void *data, size_t data_length, · line 26
Quoted 5 lines
#define
TEST_HASH_STR(data, expected_sha1, expected_sha256)do { \t/unit-tests/u-hash.c renamed from t/unit-tests/t-hash.c +1
-2const char *expected_hashes[] = { expected_sha1, expected_sha256 }; \ -TEST (check_hash_data (data,strlen (data), expected_hashes), \ - "SHA1 and SHA256 (%s) works", #data); \ +check_hash_data (data,strlen (data), expected_hashes); \
... instead of TEST() macro with the title string, we call the underlying test function. The loss of the message does not hurt us, as both the test suite name and name of each test are shown by the clar framework.
t/unit-tests/u-hash.c static void check_hash_data(const void *data, size_t data_length, · line 40
Quoted 13 lines
t/unit-tests/u-hash.c renamed from t/unit-tests/t-hash.c +2
-7-int cmd_main(int argc UNUSED, const char **argv UNUSED) +void test_hash__empty_string (void ) { -struct strbuf aaaaaaaaaa_100000 =STRBUF_INIT ; -struct strbuf alphabet_100000 =STRBUF_INIT ; - -strbuf_addstrings (&aaaaaaaaaa_100000,"aaaaaaaaaa" ,100000 ); -strbuf_addstrings (&alphabet_100000,"abcdefghijklmnopqrstuvwxyz" ,100000 ); -TEST_HASH_STR ("" ,"da39a3ee5e6b4b0d3255bfef95601890afd80709" ,"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" ); +}
And the fact that the body of each test function are unchanged from the original helps to build confidence in the faithfulness of the conversion.
The strbuf allocation is lost from here, and clean-up is lost from the end of the file, and they are done in the function that needs the strbuf, which also contributes to the clarity of the new version.
Nicely done. Will queue.
Thanks.