From: "René Scharfe" <l.s.r@web.de>
To: Josh Steadmon <steadmon@google.com>,
Git List <git@vger.kernel.org>,
Phillip Wood <phillip.wood@dunelm.org.uk>
Subject: Re: [PATCH 6/6] t-strbuf: use TEST_RUN
Date: Mon, 1 Jul 2024 22:18:45 +0200 [thread overview]
Message-ID: <6bd64e76-46b1-4511-a8f7-bfe28af48fb3@web.de> (raw)
In-Reply-To: <bh5ectgtwdaeltflggzwnwhobewvcqqelforr4sfcmq47rnnrl@mzspptlhpofx>
Am 01.07.24 um 21:58 schrieb Josh Steadmon:
> On 2024.06.29 17:47, René Scharfe wrote:
>> The macro TEST takes a single expression. If a test requires multiple
>> statements then they need to be placed in a function that's called in
>> the TEST expression. The functions setup() and setup_populated() here
>> are used for that purpose and take another function as an argument,
>> making the control flow hard to follow.
>>
>> Remove the overhead of these functions by using TEST_RUN instead. Move
>> their duplicate post-condition checks into a new helper, t_release(),
>> and let t_addch() and t_addstr() accept properly typed input parameters
>> instead of void pointers.
>>
>> Use the fully checking t_addstr() for adding initial values instead of
>> only doing only a length comparison -- there's no need for skipping the
>> other checks.
>>
>> This results in test cases that look much more like strbuf usage in
>> production code, only with checked strbuf functions replaced by checking
>> wrappers.
>>
>> Signed-off-by: René Scharfe <l.s.r@web.de>
>> ---
>> t/unit-tests/t-strbuf.c | 79 +++++++++++++++++++++--------------------
>> 1 file changed, 41 insertions(+), 38 deletions(-)
>>
>> diff --git a/t/unit-tests/t-strbuf.c b/t/unit-tests/t-strbuf.c
>> index 6027dafef7..c8e39ddda7 100644
>> --- a/t/unit-tests/t-strbuf.c
>> +++ b/t/unit-tests/t-strbuf.c
>> @@ -1,32 +1,6 @@
>> #include "test-lib.h"
>> #include "strbuf.h"
>>
>> -/* wrapper that supplies tests with an empty, initialized strbuf */
>> -static void setup(void (*f)(struct strbuf*, const void*),
>> - const void *data)
>> -{
>> - struct strbuf buf = STRBUF_INIT;
>> -
>> - f(&buf, data);
>> - strbuf_release(&buf);
>> - check_uint(buf.len, ==, 0);
>> - check_uint(buf.alloc, ==, 0);
>> -}
>> -
>> -/* wrapper that supplies tests with a populated, initialized strbuf */
>> -static void setup_populated(void (*f)(struct strbuf*, const void*),
>> - const char *init_str, const void *data)
>> -{
>> - struct strbuf buf = STRBUF_INIT;
>> -
>> - strbuf_addstr(&buf, init_str);
>> - check_uint(buf.len, ==, strlen(init_str));
>> - f(&buf, data);
>> - strbuf_release(&buf);
>> - check_uint(buf.len, ==, 0);
>> - check_uint(buf.alloc, ==, 0);
>> -}
>> -
>> static int assert_sane_strbuf(struct strbuf *buf)
>> {
>> /* Initialized strbufs should always have a non-NULL buffer */
>> @@ -66,10 +40,8 @@ static void t_dynamic_init(void)
>> strbuf_release(&buf);
>> }
>>
>> -static void t_addch(struct strbuf *buf, const void *data)
>> +static void t_addch(struct strbuf *buf, int ch)
>> {
>> - const char *p_ch = data;
>> - const char ch = *p_ch;
>> size_t orig_alloc = buf->alloc;
>> size_t orig_len = buf->len;
>>
>> @@ -85,9 +57,8 @@ static void t_addch(struct strbuf *buf, const void *data)
>> check_char(buf->buf[buf->len], ==, '\0');
>> }
>>
>> -static void t_addstr(struct strbuf *buf, const void *data)
>> +static void t_addstr(struct strbuf *buf, const char *text)
>> {
>> - const char *text = data;
>> size_t len = strlen(text);
>> size_t orig_alloc = buf->alloc;
>> size_t orig_len = buf->len;
>> @@ -105,18 +76,50 @@ static void t_addstr(struct strbuf *buf, const void *data)
>> check_str(buf->buf + orig_len, text);
>> }
>>
>> +static void t_release(struct strbuf *sb)
>> +{
>> + strbuf_release(sb);
>> + check_uint(sb->len, ==, 0);
>> + check_uint(sb->alloc, ==, 0);
>> +}
>> +
>> int cmd_main(int argc, const char **argv)
>> {
>> if (!TEST(t_static_init(), "static initialization works"))
>> test_skip_all("STRBUF_INIT is broken");
>> TEST(t_dynamic_init(), "dynamic initialization works");
>
> IIUC you're leaving t_static_init() as-is so that we can determine
> whether or not to skip the rest of the tests,
Right. And that place might be a use case for an official version of
test__run_end(), but it's probably better to gather a few more such
candidates before drawing conclusions.
> but is there a reason you
> didn't convert t_dynamic_init() here?
Good question, I don't remember. Perhaps an oversight or laziness.
Or just a general feeling to leave the init tests alone since the
first one doesn't map to TEST_RUN easily. So, in a word: No.
>> - TEST(setup(t_addch, "a"), "strbuf_addch adds char");
>> - TEST(setup(t_addch, ""), "strbuf_addch adds NUL char");
>> - TEST(setup_populated(t_addch, "initial value", "a"),
>> - "strbuf_addch appends to initial value");
>> - TEST(setup(t_addstr, "hello there"), "strbuf_addstr adds string");
>> - TEST(setup_populated(t_addstr, "initial value", "hello there"),
>> - "strbuf_addstr appends string to initial value");
>> +
>> + if (TEST_RUN("strbuf_addch adds char")) {
>> + struct strbuf sb = STRBUF_INIT;
>> + t_addch(&sb, 'a');
>> + t_release(&sb);
>> + }
>> +
>> + if (TEST_RUN("strbuf_addch adds NUL char")) {
>> + struct strbuf sb = STRBUF_INIT;
>> + t_addch(&sb, '\0');
>> + t_release(&sb);
>> + }
>> +
>> + if (TEST_RUN("strbuf_addch appends to initial value")) {
>> + struct strbuf sb = STRBUF_INIT;
>> + t_addstr(&sb, "initial value");
>> + t_addch(&sb, 'a');
>> + t_release(&sb);
>> + }
>> +
>> + if (TEST_RUN("strbuf_addstr adds string")) {
>> + struct strbuf sb = STRBUF_INIT;
>> + t_addstr(&sb, "hello there");
>> + t_release(&sb);
>> + }
>> +
>> + if (TEST_RUN("strbuf_addstr appends string to initial value")) {
>> + struct strbuf sb = STRBUF_INIT;
>> + t_addstr(&sb, "initial value");
>> + t_addstr(&sb, "hello there");
>> + t_release(&sb);
>> + }
>>
>> return test_done();
>> }
>> --
>> 2.45.2
>
> I think this commit in particular shows how TEST_RUN() is more
> convenient than TEST(). (Although, arguably we shouldn't have allowed
> the setup() + callback situation to start with.)
Great, thanks!
René
next prev parent reply other threads:[~2024-07-01 20:18 UTC|newest]
Thread overview: 115+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-06-29 15:33 [PATCH 0/6] unit-tests: add and use TEST_RUN to simplify tests René Scharfe
2024-06-29 15:35 ` [PATCH 1/6] t0080: move expected output to a file René Scharfe
2024-07-01 3:20 ` Jeff King
2024-07-01 19:17 ` Junio C Hamano
2024-07-01 22:10 ` Jeff King
2024-07-01 23:38 ` Junio C Hamano
2024-07-02 0:57 ` Jeff King
2024-07-01 19:51 ` René Scharfe
2024-07-01 22:18 ` Jeff King
2024-06-29 15:43 ` [PATCH 2/6] unit-tests: add TEST_RUN René Scharfe
2024-07-02 15:13 ` phillip.wood123
2024-07-02 15:51 ` Junio C Hamano
2024-07-02 20:55 ` René Scharfe
2024-07-02 20:55 ` René Scharfe
2024-07-05 9:42 ` phillip.wood123
2024-07-05 18:01 ` René Scharfe
2024-07-07 7:20 ` René Scharfe
2024-07-08 15:18 ` phillip.wood123
2024-07-08 15:39 ` Junio C Hamano
2024-07-11 15:34 ` Junio C Hamano
2024-07-13 13:27 ` Phillip Wood
2024-07-13 15:48 ` Junio C Hamano
2024-07-08 15:12 ` phillip.wood123
2024-06-29 15:44 ` [PATCH 3/6] t-ctype: use TEST_RUN René Scharfe
2024-07-01 19:49 ` Josh Steadmon
2024-07-01 20:04 ` René Scharfe
2024-07-02 15:14 ` phillip.wood123
2024-07-02 20:55 ` René Scharfe
2024-06-29 15:45 ` [PATCH 4/6] t-reftable-basics: " René Scharfe
2024-06-29 15:46 ` [PATCH 5/6] t-strvec: " René Scharfe
2024-07-02 15:14 ` phillip.wood123
2024-07-02 20:55 ` René Scharfe
2024-06-29 15:47 ` [PATCH 6/6] t-strbuf: " René Scharfe
2024-07-01 19:58 ` Josh Steadmon
2024-07-01 20:18 ` René Scharfe [this message]
2024-07-02 15:14 ` phillip.wood123
2024-07-02 20:55 ` René Scharfe
2024-07-04 13:09 ` phillip.wood123
2024-07-10 13:55 ` Phillip Wood
2024-07-14 11:44 ` René Scharfe
2024-07-15 14:46 ` Ghanshyam Thakkar
2024-07-02 17:29 ` Ghanshyam Thakkar
2024-07-02 20:55 ` René Scharfe
2024-07-03 3:42 ` Ghanshyam Thakkar
2024-07-08 18:11 ` Josh Steadmon
2024-07-08 21:59 ` Ghanshyam Thakkar
2024-07-01 19:59 ` [PATCH 0/6] unit-tests: add and use TEST_RUN to simplify tests Josh Steadmon
2024-07-10 22:13 ` Junio C Hamano
2024-07-11 10:05 ` Phillip Wood
2024-07-11 15:12 ` Junio C Hamano
2024-07-14 10:35 ` René Scharfe
2024-07-21 6:12 ` [PATCH v2 0/6] unit-tests: add and use for_test " René Scharfe
2024-07-21 6:15 ` [PATCH v2 1/6] t0080: move expected output to a file René Scharfe
2024-07-23 20:54 ` Jeff King
2024-07-21 6:21 ` [PATCH v2 2/6] unit-tests: add for_test René Scharfe
2024-07-22 19:13 ` Kyle Lippincott
2024-07-22 19:36 ` Junio C Hamano
2024-07-22 20:31 ` René Scharfe
2024-07-22 20:41 ` Junio C Hamano
2024-07-22 22:47 ` Kyle Lippincott
2024-07-23 12:37 ` René Scharfe
2024-07-23 6:02 ` [PATCH v2] unit-tests: show location of checks outside of tests René Scharfe
2024-07-23 13:25 ` Phillip Wood
2024-07-22 22:41 ` [PATCH v2 2/6] unit-tests: add for_test Kyle Lippincott
2024-07-23 7:18 ` René Scharfe
2024-07-23 6:36 ` Patrick Steinhardt
2024-07-23 9:25 ` René Scharfe
2024-07-23 9:53 ` Patrick Steinhardt
2024-07-23 12:37 ` René Scharfe
2024-07-23 13:00 ` Patrick Steinhardt
2024-07-23 13:23 ` Phillip Wood
2024-07-23 13:58 ` René Scharfe
2024-07-23 13:24 ` Phillip Wood
2024-07-25 9:45 ` Phillip Wood
2024-07-30 14:00 ` René Scharfe
2024-07-21 6:22 ` [PATCH v2 3/6] t-ctype: use for_test René Scharfe
2024-07-21 6:23 ` [PATCH v2 4/6] t-reftable-basics: " René Scharfe
2024-07-21 6:24 ` [PATCH v2 5/6] t-strvec: " René Scharfe
2024-07-21 6:26 ` [PATCH v2 6/6] t-strbuf: " René Scharfe
2024-07-23 13:23 ` Phillip Wood
2024-07-24 14:42 ` [PATCH v3 0/7] add and use for_test to simplify tests René Scharfe
2024-07-24 14:48 ` [PATCH v3 1/7] t0080: use here-doc test body René Scharfe
2024-07-24 14:50 ` [PATCH v3 2/7] unit-tests: show location of checks outside of tests René Scharfe
2024-07-24 14:51 ` [PATCH v3 3/7] unit-tests: add for_test René Scharfe
2024-07-24 19:24 ` Kyle Lippincott
2024-07-25 9:45 ` Phillip Wood
2024-07-25 16:02 ` Junio C Hamano
2024-07-25 21:31 ` Kyle Lippincott
2024-07-26 2:41 ` Junio C Hamano
2024-07-26 12:56 ` Patrick Steinhardt
2024-07-26 15:59 ` Junio C Hamano
2024-07-29 9:48 ` Patrick Steinhardt
2024-07-29 18:55 ` Junio C Hamano
2024-07-30 4:49 ` Patrick Steinhardt
2024-07-30 14:00 ` René Scharfe
2024-07-31 5:19 ` Patrick Steinhardt
2024-07-31 16:48 ` René Scharfe
2024-08-01 6:51 ` Patrick Steinhardt
2024-07-24 14:52 ` [PATCH v3 4/7] t-ctype: use for_test René Scharfe
2024-07-24 14:54 ` [PATCH v3 5/7] t-reftable-basics: " René Scharfe
2024-07-24 14:54 ` [PATCH v3 6/7] t-strvec: " René Scharfe
2024-07-24 14:55 ` [PATCH v3 7/7] t-strbuf: " René Scharfe
2024-07-30 14:03 ` [PATCH v4 0/6] add and use if_test to simplify tests René Scharfe
2024-07-30 14:05 ` [PATCH v4 1/6] t0080: use here-doc test body René Scharfe
2024-07-31 20:52 ` Kyle Lippincott
2024-07-30 14:07 ` [PATCH v4 2/6] unit-tests: show location of checks outside of tests René Scharfe
2024-07-31 21:03 ` Kyle Lippincott
2024-08-01 7:23 ` René Scharfe
2024-07-30 14:08 ` [PATCH v4 3/6] unit-tests: add if_test René Scharfe
2024-07-31 22:04 ` Kyle Lippincott
2024-08-01 7:32 ` René Scharfe
2024-08-02 0:48 ` Kyle Lippincott
2024-07-30 14:10 ` [PATCH v4 4/6] t-ctype: use if_test René Scharfe
2024-07-30 14:10 ` [PATCH v4 5/6] t-reftable-basics: " René Scharfe
2024-07-30 14:12 ` [PATCH v4 6/6] t-strvec: " René Scharfe
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=6bd64e76-46b1-4511-a8f7-bfe28af48fb3@web.de \
--to=l.s.r@web.de \
--cc=git@vger.kernel.org \
--cc=phillip.wood@dunelm.org.uk \
--cc=steadmon@google.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 NNTP newsgroup(s).