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

Re: [RFC][Outreachy] Seeking Git Community Feedback on My Application

From
PWPhillip Wood <phil@crinan.ddns.net>
Date
Oct 29, 2023, 14:43 UTC
Message-ID
<9c317b54-7ed4-4ca6-ad75-6857ded0d658@crinan.ddns.net>
In-Reply-To
<CAJHH8bHCfx3vknPCGATbLZeTA7hYrVVtnYqfE1avWkiL1PvU1g@mail.gmail.com>
Hi Isoken
On 28/10/2023 15:07, Isoken Ibizugbe wrote:
Show 15 quoted lines
> #include "test-lib.h"
> #include "ctype.h"
> 
> static void t_digit_type(void)
> {
>      int i;
> 
> for (i = 0; i < 256; i++)
>          {
>              if (i < '0' || i > '9')
>                  check_int(isdigit(i), ==, 0);
>              else
>                  check_int(isdigit(i), ==, 1);
>          }
> }

I think this is correct but when you are writing tests it is important to think about how easy they will be to debug if they fail. In this case because there is a single test to check all the characters it will be hard to tell which character caused the test to fail. If we restructure the code to use a separate test for each character then we will be able to see which characters are causing isdigit() to fail. To do that we need a function that prints the character that we're testing. Because we don't want to print raw control characters in the test name we need to check if the character can be printed as is or if it needs to be printed as an octal escape sequence. We can do that by writing a function like

static const char* char_name(int i)
{
	static char buf[5];
	if (i < ' ' || i >= 127)
		xsnprintf(buf, sizeof(buf), "\\%03o", (unsigned int)i);
	else
		xsnprintf(buf, sizeof(buf), "%c", i);
	return buf;
}
Then we can write a test function defines a separate test for each character
static void t_isdigit(void)
{
	for (int i = 0; i < 256; i++) {
		if (i < '0' || i > '9')
			TEST(check(!isdigit(i)), "'%s' is not a digit",
			     char_name(i));
		else
			TEST(check(isdigit(i)), "'%s' is a digit",
			     char_name(i));
	}
}

Note that as isdigit() returns a boolean we simplify things by using check() rather than check_int().

Now we can easily see which character is being tested when a check fails as the character being tested is in the test name. You would call this function with

int cmd_main(int argc, const char** argv)
{
	t_isdigit();
	return test_done();
}

I think it would be helpful for you to try and build and run this test by checking out the unit test branch from Junio's tree[1] and adding this test. You could then try making the test fail to see what the output for a failing test looks like.

Best Wishes
Phillip
[1] You can fetch that branch with
         git fetch https://github.com/gitster/git.git 
js/doc-unit-tests-with-cmake
     and then create your branch with
         git checkout -b isdigit-unit-tests FETCH_HEAD
Previous: Isoken Ibizugbe
Message 9 of 9 in “[RFC][Outreachy] Seeking Git Community Feedback on My Application”
  1. Isoken IbizugbeOct 19, 2023
  2. Isoken IbizugbeOct 20, 2023
  3. Christian CouderOct 23, 2023
  4. Isoken IbizugbeOct 25, 2023
  5. Christian CouderOct 28, 2023
  6. Isoken IbizugbeOct 28, 2023
  7. Christian CouderOct 28, 2023
  8. Isoken IbizugbeOct 28, 2023
  9. Phillip WoodOct 29, 2023

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.