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

Re: [PATCH] Port helper/test-ctype.c to unit-tests/t-ctype.c

From
Junio C Hamano <gitster@pobox.com>
Date
Dec 26, 2023, 18:45 UTC
Message-ID
<xmqqsf3ohkew.fsf@gitster.g>
In-Reply-To
<20231221231527.8130-1-ach.lumap@gmail.com>
Achu Luma <ach.lumap@gmail.com> writes:
Show 15 quoted lines
> diff --git a/t/helper/test-ctype.c b/t/helper/test-ctype.c
> deleted file mode 100644
> index e5659df40b..0000000000
> --- a/t/helper/test-ctype.c
> +++ /dev/null
> @@ -1,70 +0,0 @@
> -#include "test-tool.h"
> -
> -static int rc;
> -
> -static void report_error(const char *class, int ch)
> -{
> -	printf("%s classifies char %d (0x%02x) wrongly\n", class, ch, ch);
> -	rc = 1;
> -}

So, if we have a is_foo() that characterises a byte that ought to be "foo" but gets miscategorised not to be "foo", we used to pinpoint exactly the byte value that was an issue. We did not do any early return ...

Show 10 quoted lines
> ...
> -#define TEST_CLASS(t,s) {			\
> -	int i;					\
> -	for (i = 0; i < 256; i++) {		\
> -		if (is_in(s, i) != t(i))	\
> -			report_error(#t, i);	\
> -	}					\
> -	if (t(EOF))				\
> -		report_error(#t, EOF);		\
> -}
... and reported for all errors in the "class".
Show 29 quoted lines
> diff --git a/t/unit-tests/t-ctype.c b/t/unit-tests/t-ctype.c
> new file mode 100644
> index 0000000000..41189ba9f9
> --- /dev/null
> +++ b/t/unit-tests/t-ctype.c
> @@ -0,0 +1,76 @@
> +#include "test-lib.h"
> +
> +static int is_in(const char *s, int ch)
> +{
> +	/*
> +	 * We can't find NUL using strchr. Accept it as the first
> +	 * character in the spec -- there are no empty classes.
> +	 */
> +	if (ch == '\0')
> +		return ch == *s;
> +	if (*s == '\0')
> +		s++;
> +	return !!strchr(s, ch);
> +}
> +
> +/* Macro to test a character type */
> +#define TEST_CTYPE_FUNC(func, string)			\
> +static void test_ctype_##func(void)				\
> +{								\
> +	int i;                                     	 	\
> +	for (i = 0; i < 256; i++)                 		\
> +		check_int(func(i), ==, is_in(string, i)); 	\
> +}

Now, we let check_int() to do the checking for each and every byte value for the class. check_int() uses different reporting and shows the problematic value in a way that is more verbose and at the same time is a less specific and harder to understand:

		test_msg("   left: %"PRIdMAX, a);
		test_msg("  right: %"PRIdMAX, b);

But that is probably the price to pay to use a more generic framework, I guess.

Show 19 quoted lines
> +int cmd_main(int argc, const char **argv) {
> +	/* Run all character type tests */
> +	TEST(test_ctype_isspace(), "isspace() works as we expect");
> +	TEST(test_ctype_isdigit(), "isdigit() works as we expect");
> +	TEST(test_ctype_isalpha(), "isalpha() works as we expect");
> +	TEST(test_ctype_isalnum(), "isalnum() works as we expect");
> +	TEST(test_ctype_is_glob_special(), "is_glob_special() works as we expect");
> +	TEST(test_ctype_is_regex_special(), "is_regex_special() works as we expect");
> +	TEST(test_ctype_is_pathspec_magic(), "is_pathspec_magic() works as we expect");
> +	TEST(test_ctype_isascii(), "isascii() works as we expect");
> +	TEST(test_ctype_islower(), "islower() works as we expect");
> +	TEST(test_ctype_isupper(), "isupper() works as we expect");
> +	TEST(test_ctype_iscntrl(), "iscntrl() works as we expect");
> +	TEST(test_ctype_ispunct(), "ispunct() works as we expect");
> +	TEST(test_ctype_isxdigit(), "isxdigit() works as we expect");
> +	TEST(test_ctype_isprint(), "isprint() works as we expect");
> +
> +	return test_done();
> +}

As a practice to use the unit-tests framework, the patch looks OK. helper/test-ctype.c indeed is an oddball that runs once and checks everything it wants to check, for which the unit tests framework is much more suited.

Let's see how others react and then queue.
Thanks.
Previous: Achu LumaNext: Christian Couder
Message 2 of 24 in “Port helper/test-ctype.c to unit-tests/t-ctype.c”
  1. Port helper/test-ctype.c to unit-tests/t-ctype.cAchu Luma, Dec 21, 2023
  2. Junio C HamanoDec 26, 2023
  3. Christian CouderDec 27, 2023
  4. René ScharfeDec 27, 2023
  5. Phillip WoodDec 27, 2023
  6. Junio C HamanoDec 27, 2023
  7. René ScharfeDec 28, 2023
  8. Taylor BlauJan 2, 2024
  9. [Outreachy][PATCH v2] Port helper/test-ctype.c to unit-tests/t-ctype.cAchu Luma, Dec 30, 2023
  10. [Outreachy][PATCH v3] Port helper/test-ctype.c to unit-tests/t-ctype.cAchu Luma, Jan 1, 2024
  11. René ScharfeJan 1, 2024
  12. Junio C HamanoJan 2, 2024
  13. [Outreachy][PATCH v4] Port helper/test-ctype.c to unit-tests/t-ctype.cAchu Luma, Jan 5, 2024
  14. René ScharfeJan 7, 2024
  15. Junio C HamanoJan 8, 2024
  16. Phillip WoodJan 9, 2024
  17. Junio C HamanoJan 9, 2024
  18. [Outreachy][PATCH v5] Port helper/test-ctype.c to unit-tests/t-ctype.cAchu Luma, Jan 12, 2024
  19. Phillip WoodJan 15, 2024
  20. Junio C HamanoJan 16, 2024
  21. René ScharfeJan 16, 2024
  22. Christian CouderJan 16, 2024
  23. Junio C HamanoJan 16, 2024
  24. Josh SteadmonJan 17, 2024

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.