Git Mailing List Archive mirror
 help / color / mirror / Atom feed
From: Phillip Wood <phil@crinan.ddns.net>
To: Isoken Ibizugbe <isokenjune@gmail.com>,
	Christian Couder <christian.couder@gmail.com>
Cc: git@vger.kernel.org
Subject: Re: [RFC][Outreachy] Seeking Git Community Feedback on My Application
Date: Sun, 29 Oct 2023 14:43:33 +0000	[thread overview]
Message-ID: <9c317b54-7ed4-4ca6-ad75-6857ded0d658@crinan.ddns.net> (raw)
In-Reply-To: <CAJHH8bHCfx3vknPCGATbLZeTA7hYrVVtnYqfE1avWkiL1PvU1g@mail.gmail.com>

Hi Isoken

On 28/10/2023 15:07, Isoken Ibizugbe wrote:
> #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

      reply	other threads:[~2023-10-29 14:43 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-19  9:25 [RFC][Outreachy] Seeking Git Community Feedback on My Application Isoken Ibizugbe
2023-10-20  4:31 ` Isoken Ibizugbe
2023-10-23 14:24 ` Christian Couder
2023-10-25 12:45   ` Isoken Ibizugbe
2023-10-28  8:07     ` Christian Couder
2023-10-28 10:40       ` Isoken Ibizugbe
2023-10-28 12:37         ` Christian Couder
2023-10-28 14:07           ` Isoken Ibizugbe
2023-10-29 14:43             ` Phillip Wood [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9c317b54-7ed4-4ca6-ad75-6857ded0d658@crinan.ddns.net \
    --to=phil@crinan.ddns.net \
    --cc=christian.couder@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=isokenjune@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).