From patchwork Wed Jul 24 14:48:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ren=C3=A9_Scharfe?= X-Patchwork-Id: 13741054 Received: from mout.web.de (mout.web.de [212.227.15.14]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A4684158DD4 for ; Wed, 24 Jul 2024 14:48:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=212.227.15.14 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721832507; cv=none; b=qX394QmKCBtdsYWvdwDDLedQVcplI7zPxH9F6q4mlk17uRTJ2rnZHxUoV4FdVpcnZoCDg0fy5GkzZFuQ84fDtoI7GytoNDuHc6uFXosSxNGVvV8dIZe9JTg+ditODqCcmS7ElSymvvkjtHWqwh+SRKdVffgoNp2U7BBZTcgUD4o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721832507; c=relaxed/simple; bh=2T39HwW7jfB7FH9RsOEa+35qVN26kLZrugEeuUFgE9M=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=GkYlxk4ueuTRvNdhr/sv2bBBz+kKfBm8Bj0F0r1MQ/4nikfIY4X5vhy1dr2CS/CSzpKVwtIyeN5NyKUUVzVNqqts7HrZNLwD3zayaElmTyf7qcL9DEP+eSHqX7+Bt3Aj6pPUeYEfXQ8nEAT8cyp3qxSu52eLzY4tNCHN7Mkags4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=web.de; spf=pass smtp.mailfrom=web.de; dkim=pass (2048-bit key) header.d=web.de header.i=l.s.r@web.de header.b=BsRJwM1R; arc=none smtp.client-ip=212.227.15.14 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=web.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=web.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=web.de header.i=l.s.r@web.de header.b="BsRJwM1R" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=web.de; s=s29768273; t=1721832482; x=1722437282; i=l.s.r@web.de; bh=gy99trdlEczpdYT3QX9KpWaeR5GNVGtne8HIXDLrJRQ=; h=X-UI-Sender-Class:Message-ID:Date:MIME-Version:Subject:From:To: Cc:References:In-Reply-To:Content-Type:Content-Transfer-Encoding: cc:content-transfer-encoding:content-type:date:from:message-id: mime-version:reply-to:subject:to; b=BsRJwM1RTgEc0bNVJPuPUa1f1yr0ZZZwUBWwALaDY1IfT91yEKdJKqKQwl+3nwAD iZciqh+88/Nf074+ad98v+tQlDezmCNtI92ORZVk6FvbRa16GZLqJxbvhIiT+tQqx HM4kVWZQwEU82fgEeTNb35b7EQGgJrdzVtkOQv7olxKcDjd2j8QtDxs2wENTKzzUc 0tNlG7Yt1Hiqnpn1fI0B3u86festOSwL7Is5nRgcjwuo1E/TbehRiuAvvdDfuQVlz +k57Xd6y4Loy4fL5srsfqy7Sr4mOfRXCIHs0TSbsTwwWE2zeal4+ugwGwZ/ukaG5U Le/rrVl463AJPstS1w== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Received: from [192.168.178.29] ([91.47.153.221]) by smtp.web.de (mrweb006 [213.165.67.108]) with ESMTPSA (Nemesis) id 1MnG2C-1rpu4M3K1k-00nzaO; Wed, 24 Jul 2024 16:48:02 +0200 Message-ID: <33073397-cd86-49ea-a5fd-d045dc98725a@web.de> Date: Wed, 24 Jul 2024 16:48:01 +0200 Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: [PATCH v3 1/7] t0080: use here-doc test body From: =?utf-8?q?Ren=C3=A9_Scharfe?= To: Git List Cc: Phillip Wood , Josh Steadmon , Junio C Hamano , Jeff King References: <85b6b8a9-ee5f-42ab-bcbc-49976b30ef33@web.de> <73465c3d-1be0-456b-9471-f875e819c566@web.de> Content-Language: en-US In-Reply-To: <73465c3d-1be0-456b-9471-f875e819c566@web.de> X-Provags-ID: V03:K1:0YTPFxrjaj/aKBsGERI884w2UulDlS7jhSFevG4aIitj3IyiVvV KmzQVSIxl9l/yEOWsSQHx3Od3iQkI3RVk+91KbPmfsGSYVbPEfUbumOXAiDL6EPdQVXOb/P QOdSkMks892ac7MQJOY/x5h6TQBWu+EdtNjuUopWVjQQ+nRmLCtRSz6622dddvfq1VIHPOs r3zwz1M9/zUetWuRc0glw== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:ikcasEc5nTY=;LcpBB3L7zqDb8HCm0M1pJHLXvjV sx62+/eccFhstNn/HqG3gQ9/4FgUiRJNsD2ypZNPcJIwatcLDjXDnFOA3JJ3hfGlzAwIBdv1Z bay1Z58PSl9ZBjgVuIcw5X/ziS2gUu53gG9NpWMzdBAmBFaWRO8Layfs//iM6oY7AGQ6GG+5a FPsnZOtNpUCDGCqerX7UgmIZ69b0FGzxBtApnOFJ7kN9Ii/kLdQrDYKZOJFkrbe2pizKPIbRE FiBmHWaId+GKj9ztYajBHbtSicla6NIQePMWGMPpc0hs5tpoyJ8XclED36j1z85/Nxt0Xru6r VzGHUjc0lRHaFXCjOEhVzLqWjUU+ZOrFn4zuau2IpnSLMAjuHiSHkEALIldrAzOBEPT0SQ6AL 1M1hCUwgi0DQmDEd64Fjl6XPuMxLyYP5rW7q13Pk0pvXm1JL+Vie8xkzzWIb/wRu/1yds/bDp RQtOhUsXEF5SPHgPPL7fgwS9H2zCvxx0VjhQVKKhnuiY407mpfbVsMKxOS+n7MJQYz08dqJC/ MYfD+1UICEhABljeT9fEh8tLvLeqFJGScVyu2fk4+o2DPKoh3uxAP7qS2Y4SQePqBw5w78VJw LpercAZfmXy0S9vZ2WeLzQx+uX0cnXxWf8V06ohi6Ho6PlrFPUazKvj6AV2nc9hmzyxo1oPTr DCwKEAvICd2PUOmz0lN9svj0JAal9wQ+mV8M5crQk1CK+rydKPUJjBmGHssc8z0Emdomark+z XcZZmDTG2+rBJDQBQW8SXAZh4NMbPI0jVn9CI16kt/kbw1o27wLtLISAKbTaVtRs7jyCzvJkK /Xmy/L/uYFSDq4x1/EhIymKQ== Improve the readability of the expected output by using a here-doc for the test body and replacing the unwieldy ${SQ} references with literal single quotes. Suggested-by: Jeff King Signed-off-by: René Scharfe --- t/t0080-unit-test-output.sh | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) -- 2.45.2 diff --git a/t/t0080-unit-test-output.sh b/t/t0080-unit-test-output.sh index 7bbb065d58..9ec47b7360 100755 --- a/t/t0080-unit-test-output.sh +++ b/t/t0080-unit-test-output.sh @@ -5,7 +5,7 @@ test_description='Test the output of the unit test framework' TEST_PASSES_SANITIZE_LEAK=true . ./test-lib.sh -test_expect_success 'TAP output from unit tests' ' +test_expect_success 'TAP output from unit tests' - <<\EOT cat >expect <<-EOF && ok 1 - passing test ok 2 - passing test and assertion return 1 @@ -16,12 +16,12 @@ test_expect_success 'TAP output from unit tests' ' ok 4 - failing test and assertion return 0 not ok 5 - passing TEST_TODO() # TODO ok 6 - passing TEST_TODO() returns 1 - # todo check ${SQ}check(x)${SQ} succeeded at t/helper/test-example-tap.c:26 + # todo check 'check(x)' succeeded at t/helper/test-example-tap.c:26 not ok 7 - failing TEST_TODO() ok 8 - failing TEST_TODO() returns 0 # check "0" failed at t/helper/test-example-tap.c:31 # skipping test - missing prerequisite - # skipping check ${SQ}1${SQ} at t/helper/test-example-tap.c:33 + # skipping check '1' at t/helper/test-example-tap.c:33 ok 9 - test_skip() # SKIP ok 10 - skipped test returns 1 # skipping test - missing prerequisite @@ -39,12 +39,12 @@ test_expect_success 'TAP output from unit tests' ' # check "!strcmp("NULL", NULL)" failed at t/helper/test-example-tap.c:63 # left: "NULL" # right: NULL - # check "${SQ}a${SQ} == ${SQ}\n${SQ}" failed at t/helper/test-example-tap.c:64 - # left: ${SQ}a${SQ} - # right: ${SQ}\012${SQ} - # check "${SQ}\\\\${SQ} == ${SQ}\\${SQ}${SQ}" failed at t/helper/test-example-tap.c:65 - # left: ${SQ}\\\\${SQ} - # right: ${SQ}\\${SQ}${SQ} + # check "'a' == '\n'" failed at t/helper/test-example-tap.c:64 + # left: 'a' + # right: '\012' + # check "'\\\\' == '\\''" failed at t/helper/test-example-tap.c:65 + # left: '\\\\' + # right: '\\'' not ok 17 - messages from failing string and char comparison # BUG: test has no checks at t/helper/test-example-tap.c:92 not ok 18 - test with no checks @@ -54,6 +54,6 @@ test_expect_success 'TAP output from unit tests' ' ! test-tool example-tap >actual && test_cmp expect actual -' +EOT test_done From patchwork Wed Jul 24 14:50:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ren=C3=A9_Scharfe?= X-Patchwork-Id: 13741059 Received: from mout.web.de (mout.web.de [212.227.15.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7893D1591F0 for ; Wed, 24 Jul 2024 14:51:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=212.227.15.4 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721832674; cv=none; b=loh52xSNg+KS1k0KauuL1IGR7rHeAo9OTkCLCbNr56TbUfri3UXSzBwsxbrN2Q4hAXGQ8jVfmuXTPRvXk8CR6tHK43ShecVrOM6f/XNYZq7C1YzD0HHWDbYP1kOMqMV7fG48/MkjQkQDCSUuBQOe+k6YfTOEEr/ukQo5mgE2Gng= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721832674; c=relaxed/simple; bh=JDq64RXePG+G8EDeSz5JWoDmqg39Hi1oSpwuaPQinyI=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=Kk5/e1c7cQnvFtTp5uXQAMBnQb3DY5QDNHRgf7LZY2EHwA0bm6BFIjknZayNSRC8DH8bH1PPWTCugIKbKr2Jduv77QbXojyhEwNPNsZygpCHalumFNVkgTopZyBo4quEd0orBcGvCjZEUEVH4aLrF14tTfvuVEt73IACsJgv0Nc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=web.de; spf=pass smtp.mailfrom=web.de; dkim=pass (2048-bit key) header.d=web.de header.i=l.s.r@web.de header.b=c5ZE0vIP; arc=none smtp.client-ip=212.227.15.4 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=web.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=web.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=web.de header.i=l.s.r@web.de header.b="c5ZE0vIP" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=web.de; s=s29768273; t=1721832659; x=1722437459; i=l.s.r@web.de; bh=eMTG/630xRM8kg9Zt0ucP/5EufuV5k0wWTLaXLoczgM=; h=X-UI-Sender-Class:Message-ID:Date:MIME-Version:Subject:From:To: Cc:References:In-Reply-To:Content-Type:Content-Transfer-Encoding: cc:content-transfer-encoding:content-type:date:from:message-id: mime-version:reply-to:subject:to; b=c5ZE0vIP1jUS+RLS5oc1nihWEYFIrB2/TdZ4ArOaKTN+2AXZLK3O6x4dVdKAeuxL dvn6BnizQdosA0y9p55SaQjZ112FLyIUU77pBngCP1Yq+c7ATOL0yEsIvouNUxX9i BpX4bkuy7612Yq/SoArrMIEjmzZ/k79lLCjy/AcargtwZXMwx4ht6tVIpwUX/jz7b l2eGmDyzfJJXPSfp0uxdO5kFAMi1GZwBBV6xDyfbKwnZX77M/mpVbAU1Dkg2rCgcK puGg+QwHlBunMSsJtSLBCK5KNrgOQPo4wLnaU1FNRyne/CAXAbxFF31ZemWwek8P+ Gzu3EdKQs1TRXtfWNg== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Received: from [192.168.178.29] ([91.47.153.221]) by smtp.web.de (mrweb006 [213.165.67.108]) with ESMTPSA (Nemesis) id 1Mt8gP-1sHAsc0S9h-00xZJv; Wed, 24 Jul 2024 16:50:59 +0200 Message-ID: <92741509-106a-4eba-b662-74b50f7f859f@web.de> Date: Wed, 24 Jul 2024 16:50:58 +0200 Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: [PATCH v3 2/7] unit-tests: show location of checks outside of tests From: =?utf-8?q?Ren=C3=A9_Scharfe?= To: Git List Cc: Phillip Wood , Josh Steadmon , Junio C Hamano References: <85b6b8a9-ee5f-42ab-bcbc-49976b30ef33@web.de> <73465c3d-1be0-456b-9471-f875e819c566@web.de> Content-Language: en-US In-Reply-To: <73465c3d-1be0-456b-9471-f875e819c566@web.de> X-Provags-ID: V03:K1:XOTP+chh/WrwqESJtiqRa/ULcPpLHHIbDcf7FpYhxgNEnmEb/Y6 Q6aqf4yeYPPvI7Qd96gSLYv5/19pn7AsvqyYPDadg03RpYtjOAg8eZ/1slwyVsjkLTatPtU aWFLYhfZeUdhVOHBb3igX8h5W5ehfuW8NfcKMutYzcPRSiMuOoKFwFxE6Y33QKUTvEzOc8V Vj7KF5QEEpGEsacDVtYqA== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:J6YbM3dobg4=;QG147Z5vXk5/V2CaouzoXiR1VLn ZFr/M1UXW/cpCV/gjYEfPh/429PceFEf4+JPz5JRudLKo4h3x96lHSGF1VZ57B5TIGPSo+QN7 SqQ5OHcSgc/hwuYm15ng/BGgVwaFfc7+FsnkylpZ8frMPjIHcbBm5uGc1Iiew5uoU/blWP1SI WcI1iJaFzQJLWrzFk7PB65B+504bIjLqcabYiAz6QMItQZRrju9E3yUYNmlHRRuAukDdvcHZ4 ARvdvtpPz6NkqfH5wa5hue+1dZVTwA6i3Kf+JkACd0In4rVp2cRsDgNIdZ1OM8pYpDDnt9M85 HkXaRu0QGugFPcwi/Cv4jENG+adEsRo0UxDuxS3DQO4YCe8OBaImjx+5PxhWXQeePaaoIFIEx j9eT8M4VOkN7zcHLUoTrrwBtHtyASNfwynsQu9ElDM/f3sUFU5DFDkW5iacNDosbKTvDQKXqu p087uljJ5gzeRW06yk7jZhgIKH0DX3CQ888ixExfNFqKnX37tuWLTOw4KJAWgy5XLtpGimTan pFzv+SRelqv/pei8B5qGRGcwZOV+jEi4BX9kbCaKcjSfg+GJIwDWq6qTJns06Aki/6gI9rhOW pBRi/fZcUpxl9jNQUWfzYU4llM6ekudydxnRx4+UqiN9EbVZLh0LC0SQF8XJqkDYs/eT/Zc8n WqFG0O3c9K0r/X4UQvN9t7Y6icA8C/hpWUxIAKF8sveiwR8Cd3n0v2pTo+tnViJkbGvzGbFNz hBk14r6b/0IpLsfzbCJZA1RCJJMJcRUC3qc0aFqAzj2miPPX+d30VGI09sd7IVTQHfTugyrAP xF0LMnjuYFYDuC0ep/BH6g6g== Checks outside of tests are caught at runtime and reported like this: Assertion failed: (ctx.running), function test_assert, file test-lib.c, line 267. The assert() call aborts the unit test and doesn't reveal the location or even the type of the offending check, as test_assert() is called by all of them. Handle it like the opposite case, a test without any checks: Don't abort, but report the location of the actual check, along with a message explaining the situation. The output for example above becomes: # BUG: check outside of test at t/helper/test-example-tap.c:75 ... and the unit test program continues and indicates the error in its exit code at the end. Signed-off-by: René Scharfe --- Supersedes 9cea2b43b9 (unit-tests: show location of checks outside of tests, 2024-07-22) in seen. Changes: Got a test. t/helper/test-example-tap.c | 2 ++ t/t0080-unit-test-output.sh | 5 +++-- t/unit-tests/test-lib.c | 7 ++++++- 3 files changed, 11 insertions(+), 3 deletions(-) -- 2.45.2 diff --git a/t/helper/test-example-tap.c b/t/helper/test-example-tap.c index d072ad559f..79c12b01cd 100644 --- a/t/helper/test-example-tap.c +++ b/t/helper/test-example-tap.c @@ -72,6 +72,8 @@ static void t_empty(void) int cmd__example_tap(int argc, const char **argv) { + check(1); + test_res = TEST(check_res = check_int(1, ==, 1), "passing test"); TEST(t_res(1), "passing test and assertion return 1"); test_res = TEST(check_res = check_int(1, ==, 2), "failing test"); diff --git a/t/t0080-unit-test-output.sh b/t/t0080-unit-test-output.sh index 9ec47b7360..fe221f3bdb 100755 --- a/t/t0080-unit-test-output.sh +++ b/t/t0080-unit-test-output.sh @@ -7,9 +7,10 @@ TEST_PASSES_SANITIZE_LEAK=true test_expect_success 'TAP output from unit tests' - <<\EOT cat >expect <<-EOF && + # BUG: check outside of test at t/helper/test-example-tap.c:75 ok 1 - passing test ok 2 - passing test and assertion return 1 - # check "1 == 2" failed at t/helper/test-example-tap.c:77 + # check "1 == 2" failed at t/helper/test-example-tap.c:79 # left: 1 # right: 2 not ok 3 - failing test @@ -46,7 +47,7 @@ test_expect_success 'TAP output from unit tests' - <<\EOT # left: '\\\\' # right: '\\'' not ok 17 - messages from failing string and char comparison - # BUG: test has no checks at t/helper/test-example-tap.c:92 + # BUG: test has no checks at t/helper/test-example-tap.c:94 not ok 18 - test with no checks ok 19 - test with no checks returns 0 1..19 diff --git a/t/unit-tests/test-lib.c b/t/unit-tests/test-lib.c index 3c513ce59a..989dc758e6 100644 --- a/t/unit-tests/test-lib.c +++ b/t/unit-tests/test-lib.c @@ -264,7 +264,12 @@ static void test_todo(void) int test_assert(const char *location, const char *check, int ok) { - assert(ctx.running); + if (!ctx.running) { + test_msg("BUG: check outside of test at %s", + make_relative(location)); + ctx.failed = 1; + return 0; + } if (ctx.result == RESULT_SKIP) { test_msg("skipping check '%s' at %s", check, From patchwork Wed Jul 24 14:51:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ren=C3=A9_Scharfe?= X-Patchwork-Id: 13741060 Received: from mout.web.de (mout.web.de [212.227.15.14]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 67122158D80 for ; Wed, 24 Jul 2024 14:52:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=212.227.15.14 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721832724; cv=none; b=nJIr1usMCmXUJQ9fPxnEcteJj/az+CCSuG9VpJQ/f2CdCEuaQfkOQbX3QTgCsMA1/27lQuu239yyb67LKrqfIqaAtrtU5ttNH1vlaeh/qWZvzFaC1P/w63cF3+wavWQknA/21QzYfi2y2fuUXrIDKK7Ztxg/Bj/wFdr8zwMLxQ8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721832724; c=relaxed/simple; bh=DNeJwx2Ixw+KgNGRRO+ADQTKTagL6sw4+Y/rHzYkZOs=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=IsG1SuxswgNUFk6TaJdhBgo8FSZvvAsTHYrRufrytIEF/rNITbsrpAf/sxoGJqSqyu3HFa0pXM5XSbOqOZ1D/DlzTiMcHrwIR7eSSLvHZBBVcXFN3pLzQYAdYREJDYX/4F1dP3+N+ckjFcSF0LfybmZcJB2hy+ggir3mYreswFg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=web.de; spf=pass smtp.mailfrom=web.de; dkim=pass (2048-bit key) header.d=web.de header.i=l.s.r@web.de header.b=FGYhiVrm; arc=none smtp.client-ip=212.227.15.14 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=web.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=web.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=web.de header.i=l.s.r@web.de header.b="FGYhiVrm" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=web.de; s=s29768273; t=1721832712; x=1722437512; i=l.s.r@web.de; bh=G0fsCcC4GBxvPK3VY3VIpbfkzJnGygXB7gkw0Bkt0po=; h=X-UI-Sender-Class:Message-ID:Date:MIME-Version:Subject:From:To: Cc:References:In-Reply-To:Content-Type:Content-Transfer-Encoding: cc:content-transfer-encoding:content-type:date:from:message-id: mime-version:reply-to:subject:to; b=FGYhiVrmTs4jhBzr1VUD7Lgga8P2eZgluKgztg/DqTAxNcG/sE+/f9A4zsR3vYVd K5rfIVrn7Bib61UWOxpMhEwQcJZSMI6ZQ4FMeg0plketifI4kuf9G13+8aJjGdDa2 rqGV4wour0oE6DvYgCX9uT5iQSI7VVAwEnnfDiwd1vnnSDfimS4PuLRgySL59clW4 KGaAIPz7JHr2C024cLY7tqQVBT/ROSNiQcUlqcxjoUq17WTm5A2Z5afPBUNZApxJl fUOc8b95h3m433vqv45CJAWIhgDWY5uS30f4FjmVf0z3YHcb3ZMdm0tgYiw6lIrME nqOGIgX8GLNNX8KwOA== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Received: from [192.168.178.29] ([91.47.153.221]) by smtp.web.de (mrweb006 [213.165.67.108]) with ESMTPSA (Nemesis) id 1N01di-1sLGvu1dTy-00svFz; Wed, 24 Jul 2024 16:51:52 +0200 Message-ID: Date: Wed, 24 Jul 2024 16:51:51 +0200 Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: [PATCH v3 3/7] unit-tests: add for_test From: =?utf-8?q?Ren=C3=A9_Scharfe?= To: Git List Cc: Phillip Wood , Josh Steadmon , Junio C Hamano References: <85b6b8a9-ee5f-42ab-bcbc-49976b30ef33@web.de> <73465c3d-1be0-456b-9471-f875e819c566@web.de> Content-Language: en-US In-Reply-To: <73465c3d-1be0-456b-9471-f875e819c566@web.de> X-Provags-ID: V03:K1:9f/h/mdeQpqgMaQ5/DcYFGAA2m/Nbiwd0gSj7ejbQjFyoJ1Mt4/ BlT0ZGhmm/bvjqeNDdpZ/qsGz7pw2+CVjXhWahIfQp3gv18/pLikWrdcFrXwy0korsc4M4m ezqkwXx7BeI9FIi4s1syjlNvNNhb6Gonvf/37MyLgsLDiOfNV7io9R3VWgz7Ct5SFR/FvfE 9mT3OJo4goQWlsvMhz7ug== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:jUSUcF5pHao=;mGXdNDJ7EP4QzkYn2WpnDPbMXQp EzOQu8NJ+V+8Hl3Lzj11rAROoplhSaZ4lF7BwLLlBPqTEhGFbT4A4wRB2IsrIEPZwXq3cTzv6 odKmFwCG5lzOF7lxM9OgOP1Ef+jXhlaLS0YxrAG20hHL2Q+QO6z4wRa7ybVA6isN69flTrNwJ iSWU+9DfF9OTdBpk63tWHedC6G9w9lBac7Te6MZ2hxRFxalWKj/rZI0BiOMus/Z5CljZQZQc+ e8G48IBL1cDu6wpK75IiPAMaaTrAoomIw1ysmm/lcWrAw6b2GtrNieXEWlJa7blSdIw4BClFD GE48/PRA9F9H9+rZSlKSdHG9WlwiyNYTM9huGg5ovLN6pQFWQ/xuoV3877BlKFeyPNHJljGLq RBLhehTltLxKEn1yXQToYvGe1KpRVSBe8+OLa9yOJkW6OOe9Co79DVH/lV3Dkq2Nft4F4vfWb zQSMFhLmvvnz64ax2ngM4FPyKPRQu9yKb1b3ihYxJAw0zUGxgl6g/beNf2/VUvr87xmS04xHn DrwYIkI9fel8EwFYxmRpyJ0IQk6thewx18iRJSbRzhGAVlNwzdaXKcvNvxCpG40CUYk+1ZSsn f6Aj06NnHoDaKbRidtsj8PRvBNgd93Rso8acb+r0Vj5L/HokK79G5dvysFSr01t5HvKmoMIVS nN2Yjb8TqFYYt03YWWpUlMT+MvJ4lfaRTdJUh9cVaUi6Icw+KFb+AQdj5hp82pdkYap1RZbbS coCYfLredsdsGgE1D5jZs+4+4LXsRKItXRUSQegMT/axfYwWoDygs4zFFhDdvY0anhDTovJSa MDx9qTFbhP1e9gaZBj+6dOHw== The macro TEST only allows defining a test that consists of a single expression. Add a new macro, for_test, which provides a way to define unit tests that are made up of one or more statements. for_test allows defining self-contained tests en bloc, a bit like test_expect_success does for regular tests. It acts like a for loop that runs at most once; the test body is executed if test_skip_all() had not been called before. Signed-off-by: René Scharfe --- .clang-format | 2 ++ t/helper/test-example-tap.c | 33 +++++++++++++++++++++++++++++++++ t/t0080-unit-test-output.sh | 35 ++++++++++++++++++++++++++++++++++- t/unit-tests/test-lib.h | 20 ++++++++++++++++++++ 4 files changed, 89 insertions(+), 1 deletion(-) -- 2.45.2 diff --git a/.clang-format b/.clang-format index 6408251577..863dc87dfc 100644 --- a/.clang-format +++ b/.clang-format @@ -151,6 +151,7 @@ Cpp11BracedListStyle: false # function calls. Taken from: # git grep -h '^#define [^[:space:]]*for_\?each[^[:space:]]*(' | # sed "s/^#define / - '/; s/(.*$/'/" | sort | uniq +# Added for_test from t/unit-tests/test-lib.h manually as a special case. ForEachMacros: - 'for_each_builtin' - 'for_each_string_list_item' @@ -168,6 +169,7 @@ ForEachMacros: - 'strintmap_for_each_entry' - 'strmap_for_each_entry' - 'strset_for_each_entry' + - 'for_test' # The maximum number of consecutive empty lines to keep. MaxEmptyLinesToKeep: 1 diff --git a/t/helper/test-example-tap.c b/t/helper/test-example-tap.c index 79c12b01cd..5e49fb1e7e 100644 --- a/t/helper/test-example-tap.c +++ b/t/helper/test-example-tap.c @@ -94,5 +94,38 @@ int cmd__example_tap(int argc, const char **argv) test_res = TEST(t_empty(), "test with no checks"); TEST(check_int(test_res, ==, 0), "test with no checks returns 0"); + for_test ("for_test passing test") + check_int(1, ==, 1); + for_test ("for_test failing test") + check_int(1, ==, 2); + for_test ("for_test passing TEST_TODO()") + TEST_TODO(check(0)); + for_test ("for_test failing TEST_TODO()") + TEST_TODO(check(1)); + for_test ("for_test test_skip()") { + check(0); + test_skip("missing prerequisite"); + check(1); + } + for_test ("for_test test_skip() inside TEST_TODO()") + TEST_TODO((test_skip("missing prerequisite"), 1)); + for_test ("for_test TEST_TODO() after failing check") { + check(0); + TEST_TODO(check(0)); + } + for_test ("for_test failing check after TEST_TODO()") { + check(1); + TEST_TODO(check(0)); + check(0); + } + for_test ("for_test messages from failing string and char comparison") { + check_str("\thello\\", "there\"\n"); + check_str("NULL", NULL); + check_char('a', ==, '\n'); + check_char('\\', ==, '\''); + } + for_test ("for_test test with no checks") + ; /* nothing */ + return test_done(); } diff --git a/t/t0080-unit-test-output.sh b/t/t0080-unit-test-output.sh index fe221f3bdb..5185154414 100755 --- a/t/t0080-unit-test-output.sh +++ b/t/t0080-unit-test-output.sh @@ -50,7 +50,40 @@ test_expect_success 'TAP output from unit tests' - <<\EOT # BUG: test has no checks at t/helper/test-example-tap.c:94 not ok 18 - test with no checks ok 19 - test with no checks returns 0 - 1..19 + ok 20 - for_test passing test + # check "1 == 2" failed at t/helper/test-example-tap.c:100 + # left: 1 + # right: 2 + not ok 21 - for_test failing test + not ok 22 - for_test passing TEST_TODO() # TODO + # todo check 'check(1)' succeeded at t/helper/test-example-tap.c:104 + not ok 23 - for_test failing TEST_TODO() + # check "0" failed at t/helper/test-example-tap.c:106 + # skipping test - missing prerequisite + # skipping check '1' at t/helper/test-example-tap.c:108 + ok 24 - for_test test_skip() # SKIP + # skipping test - missing prerequisite + ok 25 - for_test test_skip() inside TEST_TODO() # SKIP + # check "0" failed at t/helper/test-example-tap.c:113 + not ok 26 - for_test TEST_TODO() after failing check + # check "0" failed at t/helper/test-example-tap.c:119 + not ok 27 - for_test failing check after TEST_TODO() + # check "!strcmp("\thello\\\\", "there\"\n")" failed at t/helper/test-example-tap.c:122 + # left: "\011hello\\\\" + # right: "there\"\012" + # check "!strcmp("NULL", NULL)" failed at t/helper/test-example-tap.c:123 + # left: "NULL" + # right: NULL + # check "'a' == '\n'" failed at t/helper/test-example-tap.c:124 + # left: 'a' + # right: '\012' + # check "'\\\\' == '\\''" failed at t/helper/test-example-tap.c:125 + # left: '\\\\' + # right: '\\'' + not ok 28 - for_test messages from failing string and char comparison + # BUG: test has no checks at t/helper/test-example-tap.c:127 + not ok 29 - for_test test with no checks + 1..29 EOF ! test-tool example-tap >actual && diff --git a/t/unit-tests/test-lib.h b/t/unit-tests/test-lib.h index 2de6d715d5..598c6ff9f3 100644 --- a/t/unit-tests/test-lib.h +++ b/t/unit-tests/test-lib.h @@ -14,6 +14,26 @@ test__run_end(test__run_begin() ? 0 : (t, 1), \ TEST_LOCATION(), __VA_ARGS__) +/* + * Run a test unless test_skip_all() has been called. Acts like a for + * loop that runs at most once, with the test description between the + * parentheses and the test body as a statement or block after them. + * Supports continue to end the test early, but not break. The + * description for each test should be unique. E.g.: + * + * for_test ("something else %d %d", arg1, arg2) { + * prepare(); + * test_something_else(arg1, arg2); + * cleanup(); + * } + */ +#define for_test(...) \ + for (int for_test_running_ = test__run_begin() ? \ + (test__run_end(0, TEST_LOCATION(), __VA_ARGS__), 0) : 1;\ + for_test_running_; \ + test__run_end(1, TEST_LOCATION(), __VA_ARGS__), \ + for_test_running_ = 0) + /* * Print a test plan, should be called before any tests. If the number * of tests is not known in advance test_done() will automatically From patchwork Wed Jul 24 14:52:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ren=C3=A9_Scharfe?= X-Patchwork-Id: 13741062 Received: from mout.web.de (mout.web.de [212.227.15.3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CC8B015B134 for ; Wed, 24 Jul 2024 14:53:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=212.227.15.3 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721832792; cv=none; b=QIbjrOh+sbJgNbTrccjsbTVfoXKQd8Lb0N5ULG2dGbB6jzRdFbLx3KfcT+ZstF14tifxMi7aR01uAEACjF0muxY9Q1hZHMOF5xcygbEpbHGtJSzHF3fj8rNiBUW88KpzPd4ouDzVPE1M+hcyy0jeHSdYySgkErCarIccFu/0DpU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721832792; c=relaxed/simple; bh=438lcOvmlew/vUJkbq2Wo+GsT0Zjvn85P8sXcjgd4ew=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=rj41dHX/pK2GKykQwnJxWpYhWZn4kvPAWJEJ+9RS07Li76hKF0HZheRxHl/ReQwY91tZmwm9WZdP5GlaqN2S5sOaU42tr6zluLff6wVm3h4kxAlVtWinwVhhe8oampOvCI0rPeUhaFWTOPKtyGtyLy/oBuQWRkvul7d+PW6qbIw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=web.de; spf=pass smtp.mailfrom=web.de; dkim=pass (2048-bit key) header.d=web.de header.i=l.s.r@web.de header.b=YwjMrueI; arc=none smtp.client-ip=212.227.15.3 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=web.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=web.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=web.de header.i=l.s.r@web.de header.b="YwjMrueI" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=web.de; s=s29768273; t=1721832776; x=1722437576; i=l.s.r@web.de; bh=KNsd2zaRe+Lni3oRy7QaGElWYVhhLSZsuuFxqqYmBS0=; h=X-UI-Sender-Class:Message-ID:Date:MIME-Version:Subject:From:To: Cc:References:In-Reply-To:Content-Type:Content-Transfer-Encoding: cc:content-transfer-encoding:content-type:date:from:message-id: mime-version:reply-to:subject:to; b=YwjMrueIOnsXqK/gOxkBZDdskIaQLHbxuayod4THLcWRTG30QgZjwp3Y1YYric1m rhwmHffLIRGXcnJWq3RTi4dOfcpVpVsC1tWZ2UutqJneZWRbE/u5/qDdxLTH04y1h awmnemRNZhXcNZXQJGOWj2nheJsUY3seZwRuJ0orClqh5QgqX11wHZ02aRSRF1DT/ lwZ33KSrMdzBh2G3SEyh4Hlf6RKlWogWo9uVWGMIO/HfWe71lndt+AiaBrIpA31X5 mmOr4TkRh17bBqdpuPoSqxUIhF8NF6rRYb7xwP61qpdFT6vWaFJb08GRRitSKNo1v JZO9i+r/IGKG6k48uQ== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Received: from [192.168.178.29] ([91.47.153.221]) by smtp.web.de (mrweb005 [213.165.67.108]) with ESMTPSA (Nemesis) id 1Mr7ac-1ruq7C2HZ8-00a3DF; Wed, 24 Jul 2024 16:52:56 +0200 Message-ID: <0fa8b2fc-ac59-45da-abcc-1ac758e4813e@web.de> Date: Wed, 24 Jul 2024 16:52:56 +0200 Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: [PATCH v3 4/7] t-ctype: use for_test From: =?utf-8?q?Ren=C3=A9_Scharfe?= To: Git List Cc: Phillip Wood , Josh Steadmon , Junio C Hamano References: <85b6b8a9-ee5f-42ab-bcbc-49976b30ef33@web.de> <73465c3d-1be0-456b-9471-f875e819c566@web.de> Content-Language: en-US In-Reply-To: <73465c3d-1be0-456b-9471-f875e819c566@web.de> X-Provags-ID: V03:K1:o4ebfqNkB/nveyLtNlhfS+5fpBxolb0BwdQVYG6JcLBqaQSTGfo FuMNgZj/2HvlQgeIySLt/BO6/ZdTlp5/4cStEb+x+HybI1tpWKGAKcbRTVNkhH/BLUVKV2N NaPM+FWGsWohaxhgnyUzg7aHPpZbfwMvyZJT1Jz4Z5mOi32Xn2AEDswgYHGk9NjrhkMrzOp uhgCk1q5Z+pwMLtglfnxQ== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:YLmno0FPWbc=;AfC2INQQ/ngqzRCW7upKwa2m3U6 hSCV5Ur0SqW1zGuPNssIII9GJzdt03TIDP8CX+77AVur9GQqdtbYmyoTnRQG693hHFBJnerHA fn/6/05rW/CqlK1enYpNhP6H5A66/v+ySje57JfV1CwFgkHHuGQo7PIDPAxyhSEUgUl3GKSqc Z0QgSb+UmMc2hHyxPA/Xvr+OSIAtTSLED3HQKT0DWLEkzjVFlmurC5WeLw6Z64h66bUl2+yRO 9PpTxIs74CWg2UNK3J4sXo/7EgW1IHZbVmt/PoaloKA6VgWN2+n525kR0kPgy0B1HsIVqtGrs hSFuCNbhpYxpTd+lE98qXaneliyNzRk5ybTP2teKej78n7UPmUk9s5JNo+CerdEp01EPlv6y5 ta0j1fRvr7Js/oZqqIWIsROEeY3zsMZlCmoIDMSp+a0ofVg3dk34bvb4TAPxN+dvhToayoosA VFBS/93Z49Jfp8SmbYUWcerht9OZSXcsOWNrjeMy49dCGke/Z8cKJfT9TAzR4FVCjsLnETW4s Mp82FxQyDCKV1Dq/Zfo02jlzaLBXkzLhS23j4WadIW6cLVg/ZBBSLb8IBIHb7xRXBpLOKty6c B4CE4mG4UgcUhxKIT+iqsIf4FyAz7BlIznAGW07GCU98JA7XlAbOrqSFH8zCIzSCNbvjs7/V3 slFto/i1KVxybjlJxscmtKFcALOHJbri8fwMX0pfk+8Fo/Svx7tCsqpn4daFRViAvdAa1kCIk Yb+JeYJN9ci+eveXOR55ItttlodOdSGjZmmLSMgyiWVC+Zi/3GUVuLcwKaZTEMkQVrcojldYc n3WESf+hrLzi83schbx30Avg== Use the documented macro for_test instead of the internal functions test__run_begin() and test__run_end(), which are supposed to be private to the unit test framework. Signed-off-by: René Scharfe --- t/unit-tests/t-ctype.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) -- 2.45.2 diff --git a/t/unit-tests/t-ctype.c b/t/unit-tests/t-ctype.c index d6ac1fe678..92a05f02b3 100644 --- a/t/unit-tests/t-ctype.c +++ b/t/unit-tests/t-ctype.c @@ -4,15 +4,13 @@ size_t len = ARRAY_SIZE(string) - 1 + \ BUILD_ASSERT_OR_ZERO(ARRAY_SIZE(string) > 0) + \ BUILD_ASSERT_OR_ZERO(sizeof(string[0]) == sizeof(char)); \ - int skip = test__run_begin(); \ - if (!skip) { \ + for_test (#class " works") { \ for (int i = 0; i < 256; i++) { \ if (!check_int(class(i), ==, !!memchr(string, i, len)))\ test_msg(" i: 0x%02x", i); \ } \ check(!class(EOF)); \ } \ - test__run_end(!skip, TEST_LOCATION(), #class " works"); \ } while (0) #define DIGIT "0123456789" From patchwork Wed Jul 24 14:54:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ren=C3=A9_Scharfe?= X-Patchwork-Id: 13741063 Received: from mout.web.de (mout.web.de [212.227.15.3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2A373383A9 for ; Wed, 24 Jul 2024 14:54:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=212.227.15.3 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721832857; cv=none; b=kGdTdSakn8j8UmoluyZXrCsrGENsvSz/3v3DA6vHwMS1jeZ7sA4AxT+TE3mMLtc7W/JASWYHfqjWuKWbS5Wns3bVTHFcI+SbsYUsmQhFUTk7rk9HoHK6SQl3xaZBmEoCHcs6c9LLxjoBri3xWnXKrQyuKpHNr2gIuO9SLL7hW/s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721832857; c=relaxed/simple; bh=io31Oy+5/NlXxIZbQzVmGIU6zi1zoyNKY1/Zc4oPfpg=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=emf+E2zvzpGRGxrRDyqYKWVsg437beLjr9DBTfbVcA2JfRxCh9P2e/PTCKHI6bKRNY5zW/BxBguDw9IST9c7vISYJVQOby5scm8edykov8acZNzO9wTTqyv4zOOxSlpl0rhKDMfdB2RoP2r/ar3FW60UeMWK9kVMC60Iq+Sduvs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=web.de; spf=pass smtp.mailfrom=web.de; dkim=pass (2048-bit key) header.d=web.de header.i=l.s.r@web.de header.b=v2NhM8jp; arc=none smtp.client-ip=212.227.15.3 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=web.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=web.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=web.de header.i=l.s.r@web.de header.b="v2NhM8jp" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=web.de; s=s29768273; t=1721832844; x=1722437644; i=l.s.r@web.de; bh=LZOyqKP3WaNYlJzJ0ni/WGj1sjbYetLjofukSC3TDFE=; h=X-UI-Sender-Class:Message-ID:Date:MIME-Version:Subject:From:To: Cc:References:In-Reply-To:Content-Type:Content-Transfer-Encoding: cc:content-transfer-encoding:content-type:date:from:message-id: mime-version:reply-to:subject:to; b=v2NhM8jpTk5U9NVmjGaZPtGIm1yAnHniSJ1DuZF1W0R1cj6opRD90vx1CnVWUye/ EL3oQxT67HYxX5ROsK6pkSgg4ulFELrjSm2XFrZHLbBJoGfU68Ro2LkbecJmwGQYY J2plH/EP4aZbU8i8e7kUJ4UnQ3e9qUmCPQP+8eMAd1DmuUXM54sun1602syG/rMb1 3Qf8N1cSDAhbSwBAN7jkyCTJYBW6DG2nqluPjfpDg1QpNbao3RFfOVRsLzeTWK5h7 MJe/1z762KOQugRK+ylUNiJ06OXiYMChLr4jbjVPwcBnfKeXyy8FcloJEFvVndQsd Bp5+8J2RvPBwYKbTqQ== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Received: from [192.168.178.29] ([91.47.153.221]) by smtp.web.de (mrweb006 [213.165.67.108]) with ESMTPSA (Nemesis) id 1N01di-1sLGtm0aPu-00svN0; Wed, 24 Jul 2024 16:54:04 +0200 Message-ID: Date: Wed, 24 Jul 2024 16:54:03 +0200 Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: [PATCH v3 5/7] t-reftable-basics: use for_test From: =?utf-8?q?Ren=C3=A9_Scharfe?= To: Git List Cc: Phillip Wood , Josh Steadmon , Junio C Hamano References: <85b6b8a9-ee5f-42ab-bcbc-49976b30ef33@web.de> <73465c3d-1be0-456b-9471-f875e819c566@web.de> Content-Language: en-US In-Reply-To: <73465c3d-1be0-456b-9471-f875e819c566@web.de> X-Provags-ID: V03:K1:BOxviBKN/nDQS1jf8aqrASu5GAjIFL/VnUmObP6g+kwkP2kK+kk 1qtI2ngWSLjBUyLnV6hh2rC8Hgc+Wl64/Lj6Kkqh7VIc83vPqBZ+M5s17eXYdF3Jw3AJdNC lmlPYmS6JBUndTPCuc1oa6LCIrMI8e+WswkgR1DRvhr7kMeyLx/4HA+K/36QLjEsgl97myP Ndo0kPm/5HiNqb9v1IDwA== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:5cXWuRkcBVE=;PdIjTEA+p08D8O8CFl/nOa4YhFm YUvkqnaSQCXCznewxNYcryxwhrRjotjU387dAfIlsfIni6ZFnGQ3dRz1ojyNB9XbWWKBKaN62 0EF41L6bv50GRllE1IonNNTHxCl9J9CFoFEVUgOLIpm/3dbAwHRU7dA/ig5zccT7VFp5bRuhQ Igrf6a+jysWnhfMJLdWgbI2yssjVKr4Ynqagfb9PBrJHsplKFOcBj5sn1TbTcg127wH6Fomw1 5YKV7Ao9Ghj3L93AcDw2F3GB9iRaDUOWmG0RqG9EcbURQCHl1id5Wmmlj1I42QsrWTItH38X6 dqa0JngoOpjbo3Ac4KNjL3i3ojDUIF6mIsgJpEKC8HbsXHwDrM09b3gU2AvROvOeqI1VTsxPs D4DUPyJrsHiTl56jDNnSnX9UvVxtPyco4F/tLTQEZ2SikQEPI+gis6LqH14xSPe/pVbPFm2UY KBYw9qiOWprlPXtXZtqlUkngR9eMHaaMeTdEaa0aKXy0TyZJefZABSD5zrlEDHvztoAoE9eOP 8zfbBwBq3GcXbzUJPyULcNPEi9z93Wq4NQ0NUzRc1pmMkEl/n110P3MF3NfG2AM+esPMQ3CDH Gk3bXvr+u595ho/ym4tIzss4lAQy2TYcq8DAxHaZLodlJ/jSzFiYkRc0+XSaQPS/eyN3xp4Sa eSCX7Xwqbj08M5szCx21mRM5+RYbIHObdFQKPeWprZZl9WNZ6YzPgwfUe+KEAaUZ9n2wF30yD TGpo233nHYPD/NIsGOSzBdw/D2ue58nhyoqclhmyO1X0RpVgfIlJXxo+cUSEdGZPPskAP3i8S VBFprAwDZt5gWP37VapCupAA== 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. Remove the overhead of defining and calling single-use functions by using for_test instead. Run the tests in the order of definition. We can reorder them like that because they are independent. Technically this changes the output, but retains the meaning of a full run and allows for easier review e.g. with --- 2.45.2 diff option --ignore-all-space. Signed-off-by: René Scharfe --- t/unit-tests/t-reftable-basics.c | 228 ++++++++++++++----------------- 1 file changed, 106 insertions(+), 122 deletions(-) diff --git a/t/unit-tests/t-reftable-basics.c b/t/unit-tests/t-reftable-basics.c index 4e80bdf16d..eb7af9ade4 100644 --- a/t/unit-tests/t-reftable-basics.c +++ b/t/unit-tests/t-reftable-basics.c @@ -20,141 +20,125 @@ static int integer_needle_lesseq(size_t i, void *_args) return args->needle <= args->haystack[i]; } -static void test_binsearch(void) +int cmd_main(int argc, const char *argv[]) { - int haystack[] = { 2, 4, 6, 8, 10 }; - struct { - int needle; - size_t expected_idx; - } testcases[] = { - {-9000, 0}, - {-1, 0}, - {0, 0}, - {2, 0}, - {3, 1}, - {4, 1}, - {7, 3}, - {9, 4}, - {10, 4}, - {11, 5}, - {9000, 5}, - }; - - for (size_t i = 0; i < ARRAY_SIZE(testcases); i++) { - struct integer_needle_lesseq_args args = { - .haystack = haystack, - .needle = testcases[i].needle, + for_test ("binary search with binsearch works") { + int haystack[] = { 2, 4, 6, 8, 10 }; + struct { + int needle; + size_t expected_idx; + } testcases[] = { + {-9000, 0}, + {-1, 0}, + {0, 0}, + {2, 0}, + {3, 1}, + {4, 1}, + {7, 3}, + {9, 4}, + {10, 4}, + {11, 5}, + {9000, 5}, }; - size_t idx; - idx = binsearch(ARRAY_SIZE(haystack), &integer_needle_lesseq, &args); - check_int(idx, ==, testcases[i].expected_idx); + for (size_t i = 0; i < ARRAY_SIZE(testcases); i++) { + struct integer_needle_lesseq_args args = { + .haystack = haystack, + .needle = testcases[i].needle, + }; + size_t idx; + + idx = binsearch(ARRAY_SIZE(haystack), + &integer_needle_lesseq, &args); + check_int(idx, ==, testcases[i].expected_idx); + } } -} -static void test_names_length(void) -{ - const char *a[] = { "a", "b", NULL }; - check_int(names_length(a), ==, 2); -} - -static void test_names_equal(void) -{ - const char *a[] = { "a", "b", "c", NULL }; - const char *b[] = { "a", "b", "d", NULL }; - const char *c[] = { "a", "b", NULL }; + for_test ("names_length retuns size of a NULL-terminated string array") { + const char *a[] = { "a", "b", NULL }; + check_int(names_length(a), ==, 2); + } - check(names_equal(a, a)); - check(!names_equal(a, b)); - check(!names_equal(a, c)); -} + for_test ("names_equal compares NULL-terminated string arrays") { + const char *a[] = { "a", "b", "c", NULL }; + const char *b[] = { "a", "b", "d", NULL }; + const char *c[] = { "a", "b", NULL }; -static void test_parse_names_normal(void) -{ - char in1[] = "line\n"; - char in2[] = "a\nb\nc"; - char **out = NULL; - parse_names(in1, strlen(in1), &out); - check_str(out[0], "line"); - check(!out[1]); - free_names(out); - - parse_names(in2, strlen(in2), &out); - check_str(out[0], "a"); - check_str(out[1], "b"); - check_str(out[2], "c"); - check(!out[3]); - free_names(out); -} + check(names_equal(a, a)); + check(!names_equal(a, b)); + check(!names_equal(a, c)); + } -static void test_parse_names_drop_empty(void) -{ - char in[] = "a\n\nb\n"; - char **out = NULL; - parse_names(in, strlen(in), &out); - check_str(out[0], "a"); - /* simply '\n' should be dropped as empty string */ - check_str(out[1], "b"); - check(!out[2]); - free_names(out); -} + for_test ("parse_names works for basic input") { + char in1[] = "line\n"; + char in2[] = "a\nb\nc"; + char **out = NULL; + parse_names(in1, strlen(in1), &out); + check_str(out[0], "line"); + check(!out[1]); + free_names(out); + + parse_names(in2, strlen(in2), &out); + check_str(out[0], "a"); + check_str(out[1], "b"); + check_str(out[2], "c"); + check(!out[3]); + free_names(out); + } -static void test_common_prefix(void) -{ - struct strbuf a = STRBUF_INIT; - struct strbuf b = STRBUF_INIT; - struct { - const char *a, *b; - int want; - } cases[] = { - {"abcdef", "abc", 3}, - { "abc", "ab", 2 }, - { "", "abc", 0 }, - { "abc", "abd", 2 }, - { "abc", "pqr", 0 }, - }; - - for (size_t i = 0; i < ARRAY_SIZE(cases); i++) { - strbuf_addstr(&a, cases[i].a); - strbuf_addstr(&b, cases[i].b); - check_int(common_prefix_size(&a, &b), ==, cases[i].want); - strbuf_reset(&a); - strbuf_reset(&b); + for_test ("parse_names drops empty string") { + char in[] = "a\n\nb\n"; + char **out = NULL; + parse_names(in, strlen(in), &out); + check_str(out[0], "a"); + /* simply '\n' should be dropped as empty string */ + check_str(out[1], "b"); + check(!out[2]); + free_names(out); } - strbuf_release(&a); - strbuf_release(&b); -} -static void test_u24_roundtrip(void) -{ - uint32_t in = 0x112233; - uint8_t dest[3]; - uint32_t out; - put_be24(dest, in); - out = get_be24(dest); - check_int(in, ==, out); -} + for_test ("common_prefix_size works") { + struct strbuf a = STRBUF_INIT; + struct strbuf b = STRBUF_INIT; + struct { + const char *a, *b; + int want; + } cases[] = { + {"abcdef", "abc", 3}, + { "abc", "ab", 2 }, + { "", "abc", 0 }, + { "abc", "abd", 2 }, + { "abc", "pqr", 0 }, + }; -static void test_u16_roundtrip(void) -{ - uint32_t in = 0xfef1; - uint8_t dest[3]; - uint32_t out; - put_be16(dest, in); - out = get_be16(dest); - check_int(in, ==, out); -} + for (size_t i = 0; i < ARRAY_SIZE(cases); i++) { + strbuf_addstr(&a, cases[i].a); + strbuf_addstr(&b, cases[i].b); + check_int(common_prefix_size(&a, &b), ==, cases[i].want); + strbuf_reset(&a); + strbuf_reset(&b); + } + strbuf_release(&a); + strbuf_release(&b); + } -int cmd_main(int argc, const char *argv[]) -{ - TEST(test_common_prefix(), "common_prefix_size works"); - TEST(test_parse_names_normal(), "parse_names works for basic input"); - TEST(test_parse_names_drop_empty(), "parse_names drops empty string"); - TEST(test_binsearch(), "binary search with binsearch works"); - TEST(test_names_length(), "names_length retuns size of a NULL-terminated string array"); - TEST(test_names_equal(), "names_equal compares NULL-terminated string arrays"); - TEST(test_u24_roundtrip(), "put_be24 and get_be24 work"); - TEST(test_u16_roundtrip(), "put_be16 and get_be16 work"); + for_test ("put_be24 and get_be24 work") { + uint32_t in = 0x112233; + uint8_t dest[3]; + uint32_t out; + put_be24(dest, in); + out = get_be24(dest); + check_int(in, ==, out); + } + + for_test ("put_be16 and get_be16 work") { + uint32_t in = 0xfef1; + uint8_t dest[3]; + uint32_t out; + put_be16(dest, in); + out = get_be16(dest); + check_int(in, ==, out); + } return test_done(); } From patchwork Wed Jul 24 14:54:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ren=C3=A9_Scharfe?= X-Patchwork-Id: 13741064 Received: from mout.web.de (mout.web.de [212.227.15.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 65545134AC for ; Wed, 24 Jul 2024 14:55:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=212.227.15.4 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721832904; cv=none; b=bs7o2oqAtsiGq/Xf49KKxAO4Ww/7j/Hf3m5L6oki9cMbA2I3hb6cDStKcSnuWhlX+PkMVN6FKFU38KVXRyCUOUExegtr48dIHvSFCP7/UsDZwdGB07/mjkfRtCb+iBMNggK5wkDgXu6IiqmBDJqyCtrTLWa59HIbXDHLr+4cQsw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721832904; c=relaxed/simple; bh=6/zvWS+uE6ktrh+krjLd4/FgIGdgDm4NxCPScVMw+to=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=sbGAueEERMf99jeRjnPg25/A4835UcxKJHl2dCOBgnWwqqmaVTd1qzujfFbp9xn5euQXyID9rbn5QfQvuhNlti83JGrtBnRfCQ2PH26vXKvyIxltXn2dGqATwHRJyIVWMudSlcpqG3TF18f6r/PsCyDAR975mPZHdOY3d7ZJzfM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=web.de; spf=pass smtp.mailfrom=web.de; dkim=pass (2048-bit key) header.d=web.de header.i=l.s.r@web.de header.b=YKJIQVW2; arc=none smtp.client-ip=212.227.15.4 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=web.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=web.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=web.de header.i=l.s.r@web.de header.b="YKJIQVW2" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=web.de; s=s29768273; t=1721832892; x=1722437692; i=l.s.r@web.de; bh=TLZAFtLvqkXdr4Vzjeyfun4VV8Y4MioPLDGDzJv46+k=; h=X-UI-Sender-Class:Message-ID:Date:MIME-Version:Subject:From:To: Cc:References:In-Reply-To:Content-Type:Content-Transfer-Encoding: cc:content-transfer-encoding:content-type:date:from:message-id: mime-version:reply-to:subject:to; b=YKJIQVW21LLfA6BGwT+PILr9klaIX0Z+YE6HTKdOMU1VK9q0AtiqNehIGH0mFqID 6nj8XbIBc72mrpxnwXqRbYpqEFEtnEN/yIWFnm6r4myZgKsAzbDzh9ahGsRE377BN E3E0zV5RBWNF6r5O4Rhn74gbtS7zOvBGFLAq47ayqVN8cGWsx/RTeu6o8C849/TdK SJiIyRjAz9Fuox5lURznmHoSK/Mu55akU3kxp/BsL/TNfB7lRLCtoNMGn+pq0F7nP AOyzjsfKSNM/V4njmeqogwoJqfQ5mI/LAJ2X08QC3zLtBfzwBGo4Yhkt/uw+UsxsK R2stmdTXhxwPlAWLUg== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Received: from [192.168.178.29] ([91.47.153.221]) by smtp.web.de (mrweb006 [213.165.67.108]) with ESMTPSA (Nemesis) id 1MtPvm-1sJ6701yKE-00zUsz; Wed, 24 Jul 2024 16:54:52 +0200 Message-ID: <89c04cd4-11da-46cf-a061-b2e4ba09b99e@web.de> Date: Wed, 24 Jul 2024 16:54:52 +0200 Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: [PATCH v3 6/7] t-strvec: use for_test From: =?utf-8?q?Ren=C3=A9_Scharfe?= To: Git List Cc: Phillip Wood , Josh Steadmon , Junio C Hamano References: <85b6b8a9-ee5f-42ab-bcbc-49976b30ef33@web.de> <73465c3d-1be0-456b-9471-f875e819c566@web.de> Content-Language: en-US In-Reply-To: <73465c3d-1be0-456b-9471-f875e819c566@web.de> X-Provags-ID: V03:K1:k+ggB/jRcuhrFxA/N0rIeeS9vmzISBMqIx8yyaETnEN0A25f7ns /u67K8K/XaFFD8ITHT3dRj5fjn8ZpnKHpBanrT8uPaBxbJfrhaHDd24iCtXwDa1iCWZJvI5 QZD8XYz4DM/v/h7M0FuKhg1oNaERIjHZp2LyjPvRik4+H8iw6vHUGNNnP8nhSlFSNg+QOF5 8Es3Nv29cAmtUfyGE1/Lg== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:Y+/s8tI8TYE=;fuoXKOQAGR17GJtPuuWKVA9oBSU CNwJN502ecJrhpqaj2UjVaZEQspzRD/Cb9s2XgglZ5urMEol6gK2VmTFLhaArTvUF1D5rBAjQ TCfG/wUp/w9zIrB04+mTE3zQ+iDINakuKEj2agdr4L+zaPm6NTJHVRIOXVAnCxc20BITgqRSM yb3+sJWtvCEzX2TVEIJV1eTz/awnEi3FM+kkJl1up7RmvBSiVTmpc4mNodnGuctFH5td6cvmU h+oP7falBDeUQ6WdlQht7zKZgrnRYh5lnejWu+KEAxOgYO8z9/CDeNAiuvt6GjqtreETjFTxx a8XOmJ+CZeihQjMRibQBiFU5vtUvivziOVpePfl9xnbIS1BHz9j/HEaeLJ+xwwFu+eCjuk1rN sOgOFbRlhnEsR/y9Y636lR9FW8HXdZkim3lgtudKGIvY4t+hvqtlOwgCHxjj5b8pkb/VnxQAQ Z9ogZwNruoMMgNFBS7jptFxOa2joQ37CYDr2tUCKjJ6316Y2uGq9tBKBV4hN4bc45qUlEL7Lq T9DBGYa9MWr8DYmUbb0ebsMqNKKHGXBf9A7y3S6XrWLmB6KSQPyZ1f+ZYFXfQ02NL6jJjaqi1 bZZfLIAybmdMJLeTrZx5g58tROg7Evv1nvC2oLT6P16oSgv5nlhL+djcLTen1Cz1QkvICfD4S OhzyiqfBhzMTUI5ele5zZmtWruG1XnsY8Aw/YMwfq2M8eIEJggWTzItynMgh+FVo2KsEqQoC6 6CWkKdk4P3RwWkxECkcKmya3+foV24xSHvRp6G/JdUgkCPDutip89K1kqIAqAvlIoHzroznmB x8HO5OOlfWH0WClH53RDgdfg== 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. Remove the cognitive overhead of defining and calling single-use functions by using for_test instead. Signed-off-by: René Scharfe --- t/unit-tests/t-strvec.c | 356 ++++++++++++++++++---------------------- 1 file changed, 156 insertions(+), 200 deletions(-) -- 2.45.2 diff --git a/t/unit-tests/t-strvec.c b/t/unit-tests/t-strvec.c index d4615ab06d..4fa2c21afb 100644 --- a/t/unit-tests/t-strvec.c +++ b/t/unit-tests/t-strvec.c @@ -36,237 +36,193 @@ static void check_strvec_loc(const char *loc, struct strvec *vec, ...) check_pointer_eq(vec->v[nr], NULL); } -static void t_static_init(void) +int cmd_main(int argc, const char **argv) { - struct strvec vec = STRVEC_INIT; - check_pointer_eq(vec.v, empty_strvec); - check_uint(vec.nr, ==, 0); - check_uint(vec.alloc, ==, 0); -} + for_test ("static initialization") { + struct strvec vec = STRVEC_INIT; + check_pointer_eq(vec.v, empty_strvec); + check_uint(vec.nr, ==, 0); + check_uint(vec.alloc, ==, 0); + } -static void t_dynamic_init(void) -{ - struct strvec vec; - strvec_init(&vec); - check_pointer_eq(vec.v, empty_strvec); - check_uint(vec.nr, ==, 0); - check_uint(vec.alloc, ==, 0); -} + for_test ("dynamic initialization") { + struct strvec vec; + strvec_init(&vec); + check_pointer_eq(vec.v, empty_strvec); + check_uint(vec.nr, ==, 0); + check_uint(vec.alloc, ==, 0); + } -static void t_clear(void) -{ - struct strvec vec = STRVEC_INIT; - strvec_push(&vec, "foo"); - strvec_clear(&vec); - check_pointer_eq(vec.v, empty_strvec); - check_uint(vec.nr, ==, 0); - check_uint(vec.alloc, ==, 0); -} + for_test ("clear") { + struct strvec vec = STRVEC_INIT; + strvec_push(&vec, "foo"); + strvec_clear(&vec); + check_pointer_eq(vec.v, empty_strvec); + check_uint(vec.nr, ==, 0); + check_uint(vec.alloc, ==, 0); + } -static void t_push(void) -{ - struct strvec vec = STRVEC_INIT; + for_test ("push") { + struct strvec vec = STRVEC_INIT; - strvec_push(&vec, "foo"); - check_strvec(&vec, "foo", NULL); + strvec_push(&vec, "foo"); + check_strvec(&vec, "foo", NULL); - strvec_push(&vec, "bar"); - check_strvec(&vec, "foo", "bar", NULL); + strvec_push(&vec, "bar"); + check_strvec(&vec, "foo", "bar", NULL); - strvec_clear(&vec); -} + strvec_clear(&vec); + } -static void t_pushf(void) -{ - struct strvec vec = STRVEC_INIT; - strvec_pushf(&vec, "foo: %d", 1); - check_strvec(&vec, "foo: 1", NULL); - strvec_clear(&vec); -} + for_test ("pushf") { + struct strvec vec = STRVEC_INIT; + strvec_pushf(&vec, "foo: %d", 1); + check_strvec(&vec, "foo: 1", NULL); + strvec_clear(&vec); + } -static void t_pushl(void) -{ - struct strvec vec = STRVEC_INIT; - strvec_pushl(&vec, "foo", "bar", "baz", NULL); - check_strvec(&vec, "foo", "bar", "baz", NULL); - strvec_clear(&vec); -} + for_test ("pushl") { + struct strvec vec = STRVEC_INIT; + strvec_pushl(&vec, "foo", "bar", "baz", NULL); + check_strvec(&vec, "foo", "bar", "baz", NULL); + strvec_clear(&vec); + } -static void t_pushv(void) -{ - const char *strings[] = { - "foo", "bar", "baz", NULL, - }; - struct strvec vec = STRVEC_INIT; + for_test ("pushv") { + const char *strings[] = { + "foo", "bar", "baz", NULL, + }; + struct strvec vec = STRVEC_INIT; - strvec_pushv(&vec, strings); - check_strvec(&vec, "foo", "bar", "baz", NULL); + strvec_pushv(&vec, strings); + check_strvec(&vec, "foo", "bar", "baz", NULL); - strvec_clear(&vec); -} + strvec_clear(&vec); + } -static void t_replace_at_head(void) -{ - struct strvec vec = STRVEC_INIT; - strvec_pushl(&vec, "foo", "bar", "baz", NULL); - strvec_replace(&vec, 0, "replaced"); - check_strvec(&vec, "replaced", "bar", "baz", NULL); - strvec_clear(&vec); -} + for_test ("replace at head") { + struct strvec vec = STRVEC_INIT; + strvec_pushl(&vec, "foo", "bar", "baz", NULL); + strvec_replace(&vec, 0, "replaced"); + check_strvec(&vec, "replaced", "bar", "baz", NULL); + strvec_clear(&vec); + } -static void t_replace_at_tail(void) -{ - struct strvec vec = STRVEC_INIT; - strvec_pushl(&vec, "foo", "bar", "baz", NULL); - strvec_replace(&vec, 2, "replaced"); - check_strvec(&vec, "foo", "bar", "replaced", NULL); - strvec_clear(&vec); -} + for_test ("replace at tail") { + struct strvec vec = STRVEC_INIT; + strvec_pushl(&vec, "foo", "bar", "baz", NULL); + strvec_replace(&vec, 2, "replaced"); + check_strvec(&vec, "foo", "bar", "replaced", NULL); + strvec_clear(&vec); + } -static void t_replace_in_between(void) -{ - struct strvec vec = STRVEC_INIT; - strvec_pushl(&vec, "foo", "bar", "baz", NULL); - strvec_replace(&vec, 1, "replaced"); - check_strvec(&vec, "foo", "replaced", "baz", NULL); - strvec_clear(&vec); -} + for_test ("replace in between") { + struct strvec vec = STRVEC_INIT; + strvec_pushl(&vec, "foo", "bar", "baz", NULL); + strvec_replace(&vec, 1, "replaced"); + check_strvec(&vec, "foo", "replaced", "baz", NULL); + strvec_clear(&vec); + } -static void t_replace_with_substring(void) -{ - struct strvec vec = STRVEC_INIT; - strvec_pushl(&vec, "foo", NULL); - strvec_replace(&vec, 0, vec.v[0] + 1); - check_strvec(&vec, "oo", NULL); - strvec_clear(&vec); -} + for_test ("replace with substring") { + struct strvec vec = STRVEC_INIT; + strvec_pushl(&vec, "foo", NULL); + strvec_replace(&vec, 0, vec.v[0] + 1); + check_strvec(&vec, "oo", NULL); + strvec_clear(&vec); + } -static void t_remove_at_head(void) -{ - struct strvec vec = STRVEC_INIT; - strvec_pushl(&vec, "foo", "bar", "baz", NULL); - strvec_remove(&vec, 0); - check_strvec(&vec, "bar", "baz", NULL); - strvec_clear(&vec); -} + for_test ("remove at head") { + struct strvec vec = STRVEC_INIT; + strvec_pushl(&vec, "foo", "bar", "baz", NULL); + strvec_remove(&vec, 0); + check_strvec(&vec, "bar", "baz", NULL); + strvec_clear(&vec); + } -static void t_remove_at_tail(void) -{ - struct strvec vec = STRVEC_INIT; - strvec_pushl(&vec, "foo", "bar", "baz", NULL); - strvec_remove(&vec, 2); - check_strvec(&vec, "foo", "bar", NULL); - strvec_clear(&vec); -} + for_test ("remove at tail") { + struct strvec vec = STRVEC_INIT; + strvec_pushl(&vec, "foo", "bar", "baz", NULL); + strvec_remove(&vec, 2); + check_strvec(&vec, "foo", "bar", NULL); + strvec_clear(&vec); + } -static void t_remove_in_between(void) -{ - struct strvec vec = STRVEC_INIT; - strvec_pushl(&vec, "foo", "bar", "baz", NULL); - strvec_remove(&vec, 1); - check_strvec(&vec, "foo", "baz", NULL); - strvec_clear(&vec); -} + for_test ("remove in between") { + struct strvec vec = STRVEC_INIT; + strvec_pushl(&vec, "foo", "bar", "baz", NULL); + strvec_remove(&vec, 1); + check_strvec(&vec, "foo", "baz", NULL); + strvec_clear(&vec); + } -static void t_pop_empty_array(void) -{ - struct strvec vec = STRVEC_INIT; - strvec_pop(&vec); - check_strvec(&vec, NULL); - strvec_clear(&vec); -} + for_test ("pop with empty array") { + struct strvec vec = STRVEC_INIT; + strvec_pop(&vec); + check_strvec(&vec, NULL); + strvec_clear(&vec); + } -static void t_pop_non_empty_array(void) -{ - struct strvec vec = STRVEC_INIT; - strvec_pushl(&vec, "foo", "bar", "baz", NULL); - strvec_pop(&vec); - check_strvec(&vec, "foo", "bar", NULL); - strvec_clear(&vec); -} + for_test ("pop with non-empty array") { + struct strvec vec = STRVEC_INIT; + strvec_pushl(&vec, "foo", "bar", "baz", NULL); + strvec_pop(&vec); + check_strvec(&vec, "foo", "bar", NULL); + strvec_clear(&vec); + } -static void t_split_empty_string(void) -{ - struct strvec vec = STRVEC_INIT; - strvec_split(&vec, ""); - check_strvec(&vec, NULL); - strvec_clear(&vec); -} + for_test ("split empty string") { + struct strvec vec = STRVEC_INIT; + strvec_split(&vec, ""); + check_strvec(&vec, NULL); + strvec_clear(&vec); + } -static void t_split_single_item(void) -{ - struct strvec vec = STRVEC_INIT; - strvec_split(&vec, "foo"); - check_strvec(&vec, "foo", NULL); - strvec_clear(&vec); -} + for_test ("split single item") { + struct strvec vec = STRVEC_INIT; + strvec_split(&vec, "foo"); + check_strvec(&vec, "foo", NULL); + strvec_clear(&vec); + } -static void t_split_multiple_items(void) -{ - struct strvec vec = STRVEC_INIT; - strvec_split(&vec, "foo bar baz"); - check_strvec(&vec, "foo", "bar", "baz", NULL); - strvec_clear(&vec); -} + for_test ("split multiple items") { + struct strvec vec = STRVEC_INIT; + strvec_split(&vec, "foo bar baz"); + check_strvec(&vec, "foo", "bar", "baz", NULL); + strvec_clear(&vec); + } -static void t_split_whitespace_only(void) -{ - struct strvec vec = STRVEC_INIT; - strvec_split(&vec, " \t\n"); - check_strvec(&vec, NULL); - strvec_clear(&vec); -} + for_test ("split whitespace only") { + struct strvec vec = STRVEC_INIT; + strvec_split(&vec, " \t\n"); + check_strvec(&vec, NULL); + strvec_clear(&vec); + } -static void t_split_multiple_consecutive_whitespaces(void) -{ - struct strvec vec = STRVEC_INIT; - strvec_split(&vec, "foo\n\t bar"); - check_strvec(&vec, "foo", "bar", NULL); - strvec_clear(&vec); -} + for_test ("split multiple consecutive whitespaces") { + struct strvec vec = STRVEC_INIT; + strvec_split(&vec, "foo\n\t bar"); + check_strvec(&vec, "foo", "bar", NULL); + strvec_clear(&vec); + } -static void t_detach(void) -{ - struct strvec vec = STRVEC_INIT; - const char **detached; + for_test ("detach") { + struct strvec vec = STRVEC_INIT; + const char **detached; - strvec_push(&vec, "foo"); + strvec_push(&vec, "foo"); - detached = strvec_detach(&vec); - check_str(detached[0], "foo"); - check_pointer_eq(detached[1], NULL); + detached = strvec_detach(&vec); + check_str(detached[0], "foo"); + check_pointer_eq(detached[1], NULL); - check_pointer_eq(vec.v, empty_strvec); - check_uint(vec.nr, ==, 0); - check_uint(vec.alloc, ==, 0); + check_pointer_eq(vec.v, empty_strvec); + check_uint(vec.nr, ==, 0); + check_uint(vec.alloc, ==, 0); - free((char *) detached[0]); - free(detached); -} + free((char *) detached[0]); + free(detached); + } -int cmd_main(int argc, const char **argv) -{ - TEST(t_static_init(), "static initialization"); - TEST(t_dynamic_init(), "dynamic initialization"); - TEST(t_clear(), "clear"); - TEST(t_push(), "push"); - TEST(t_pushf(), "pushf"); - TEST(t_pushl(), "pushl"); - TEST(t_pushv(), "pushv"); - TEST(t_replace_at_head(), "replace at head"); - TEST(t_replace_in_between(), "replace in between"); - TEST(t_replace_at_tail(), "replace at tail"); - TEST(t_replace_with_substring(), "replace with substring"); - TEST(t_remove_at_head(), "remove at head"); - TEST(t_remove_in_between(), "remove in between"); - TEST(t_remove_at_tail(), "remove at tail"); - TEST(t_pop_empty_array(), "pop with empty array"); - TEST(t_pop_non_empty_array(), "pop with non-empty array"); - TEST(t_split_empty_string(), "split empty string"); - TEST(t_split_single_item(), "split single item"); - TEST(t_split_multiple_items(), "split multiple items"); - TEST(t_split_whitespace_only(), "split whitespace only"); - TEST(t_split_multiple_consecutive_whitespaces(), "split multiple consecutive whitespaces"); - TEST(t_detach(), "detach"); return test_done(); } From patchwork Wed Jul 24 14:55:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ren=C3=A9_Scharfe?= X-Patchwork-Id: 13741065 Received: from mout.web.de (mout.web.de [212.227.15.3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A72C41CAA1 for ; Wed, 24 Jul 2024 14:55:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=212.227.15.3 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721832954; cv=none; b=d93ILbLmZJBAYf2yuTJvgwlfw5/lmk9BGyQ3cLML5br6Zyg8vXB00sFGwo3BXMAGgyaWlgXv9Q6cpyQa3UKuhejXMYWS1Hyb19ouKxeQQMXabebtfwQHPoy2LC7Hvm42kVwSFoWWl7MXXxbUUZGLeYNMgg1V4/qqu+b+uyZ6sFQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721832954; c=relaxed/simple; bh=drkeA6Sv3wtBP0CemUwWGF89r/xsi/dMoOI0g0WkalA=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=iCB7O8EKTKKdWgsEKbjLWTn/1bEy4n/TAadqCAcZTY4czUMfU1291GUh9a+alqTGeEYNRfrXbhfdQJOXw8JW6kmHZcKgREJoWdzIX8EqOTv83m8NZ6e2YtBU8e7WF2rdd413Z+njAg0kqVaFIb8rUXOYIdqDXXXaO9GQ6S53qMA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=web.de; spf=pass smtp.mailfrom=web.de; dkim=pass (2048-bit key) header.d=web.de header.i=l.s.r@web.de header.b=konQO/PE; arc=none smtp.client-ip=212.227.15.3 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=web.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=web.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=web.de header.i=l.s.r@web.de header.b="konQO/PE" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=web.de; s=s29768273; t=1721832942; x=1722437742; i=l.s.r@web.de; bh=9UUNj+ssLcqeIn16ObzSImh7ZHEU2slYp0ve932h9lM=; h=X-UI-Sender-Class:Message-ID:Date:MIME-Version:Subject:From:To: Cc:References:In-Reply-To:Content-Type:Content-Transfer-Encoding: cc:content-transfer-encoding:content-type:date:from:message-id: mime-version:reply-to:subject:to; b=konQO/PEQbjLQInD3yoYDIbeH/pplE6AWgpU2qyjfQwX9M61vhgr3JpbCWQPajME KlM2lLCIjXpY0CGLsqO8RWM3meQyEqvx6LsnMvyrZJPekx13F8rMA5JqbxvMzVAg8 Rw9VnUZNoU1SQ3sxOauDAY7AEnq0HEvZDcHthok9ZwN3kDXyvDPb1au5U8tGi2TxL EkRQuJRKwSEu6/+MXAZHNq+C3L4pZsDJHZPrRUF7ccfWxmYm7Libnes5E6En855Sp L+DotoVdRwq/lhLbY8WzfA026p3cy7++eexQMMUWfH92ZGgDmLfHICURDUQ5eItEq nRy1mzKMvVBHowD4Zg== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Received: from [192.168.178.29] ([91.47.153.221]) by smtp.web.de (mrweb006 [213.165.67.108]) with ESMTPSA (Nemesis) id 1M59n6-1sXjKc3ORo-005tFi; Wed, 24 Jul 2024 16:55:42 +0200 Message-ID: Date: Wed, 24 Jul 2024 16:55:42 +0200 Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: [PATCH v3 7/7] t-strbuf: use for_test From: =?utf-8?q?Ren=C3=A9_Scharfe?= To: Git List Cc: Phillip Wood , Josh Steadmon , Junio C Hamano References: <85b6b8a9-ee5f-42ab-bcbc-49976b30ef33@web.de> <73465c3d-1be0-456b-9471-f875e819c566@web.de> Content-Language: en-US In-Reply-To: <73465c3d-1be0-456b-9471-f875e819c566@web.de> X-Provags-ID: V03:K1:wzdAaI5gW5WY4PcNdEFsmPB0hpe8Awe2k4sClwR/tpDA791VbID fFvkOVSC/0rREXZHEHakaV8Ld9LAlI6b1rC7q04WokSMLvo+/0gGowh/29wfy3nEDUDm0zP bdurGaDy4N2elVcKRMAd5HEnIZ9A0cbLWWsaklz3KGmDxGzIPbLiOzM4L3DYz60sPVQAUGK ihFVNzreCdCalDv/fwwfA== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:b0UHleiQ41E=;4tOt7FMOs5nqqm2bsMasBKnpAew dxQjgNOiCONMehgyf3iefib5r5jBvPY6GxUzYbcis/Sunzb52u7NT0rndb6iy7Dk+SIuhrg+9 K2QYrYW3sbitUwPRDkV529m/UjbPOzOT4HxMhwUSyYjMfLSX8Vdgb8f87qIa3Gr4Lp9WWX9Md wUnDus0GY520392HN8Idq6ys59/qlir+0+ZFmU4S/Om0Togh7WxeXGPYBT9C1yCuQiYBA9zRH +ajKogq1AVS7A/30bi7oMiYcYdobMp/6Bu1LeoBkIiV31IWZrLmoAA1fK6TdTYjZhwv6U8B9Q FmFF21AYylY9nhXVPZDVEzfO4cUvKByFcIag/mXgcmsUxIGGtUwc3+HXCgOuzazK/6V7qc/uD KLhP6mauPds6o0+HXycu5PGbw3WwxqfvAXAD6UjP7pYm9uILgCQMJJPUPmCf3ogIWz3DHmD7H NZ5rP6zrHPR5aGIdD/QnT35Fc8oAIapB9XwP5iQVGsv3AU3l2/iVmlO2EX1tFlC2ciBAlh/sM cWkHZe5i+kgUpAnjnOfBhJ5+gKvKpVgqfWZS59FhKX/4PNwT+XD9fRndNK0TnnjFwF6V39ySP tYspCcNaVmnnOC8LRTe4Yr68B/uGcllhnLqBFNIqY3DgGU5oR6IOH3+NmP0dhkY0b7w4EaqN+ TFrUA0QAjIRytuND+skhVzN1XvI28jBdJv0RRfD2x6Z4XAWbJtZqlcZ1HtKX+dL9ZwkVIRG7v cRaTscnz4SJZqtBwJBSKEKWsNUsjWrscGr7vOdKiQ2Aa1WdK7N7o4og3AxBRQH55I7FObRt0U tpVH+wzeZh0WbdvOFasrBPsQ== 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 for_test 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 --- t/unit-tests/t-strbuf.c | 127 ++++++++++++++++++++-------------------- 1 file changed, 65 insertions(+), 62 deletions(-) -- 2.45.2 diff --git a/t/unit-tests/t-strbuf.c b/t/unit-tests/t-strbuf.c index 6027dafef7..1e4a7885c9 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 */ @@ -45,31 +19,8 @@ static int assert_sane_strbuf(struct strbuf *buf) return check_uint(buf->len, <, buf->alloc); } -static void t_static_init(void) +static void t_addch(struct strbuf *buf, int ch) { - struct strbuf buf = STRBUF_INIT; - - check_uint(buf.len, ==, 0); - check_uint(buf.alloc, ==, 0); - check_char(buf.buf[0], ==, '\0'); -} - -static void t_dynamic_init(void) -{ - struct strbuf buf; - - strbuf_init(&buf, 1024); - check(assert_sane_strbuf(&buf)); - check_uint(buf.len, ==, 0); - check_uint(buf.alloc, >=, 1024); - check_char(buf.buf[0], ==, '\0'); - strbuf_release(&buf); -} - -static void t_addch(struct strbuf *buf, const void *data) -{ - const char *p_ch = data; - const char ch = *p_ch; size_t orig_alloc = buf->alloc; size_t orig_len = buf->len; @@ -85,9 +36,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 +55,71 @@ 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"); - 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"); + for_test ("static initialization works") { + struct strbuf buf = STRBUF_INIT; + + if (!check_uint(buf.len, ==, 0) || + !check_uint(buf.alloc, ==, 0) || + !check_char(buf.buf[0], ==, '\0')) + test_skip_all("STRBUF_INIT is broken"); + } + + for_test ("dynamic initialization works") { + struct strbuf buf; + + strbuf_init(&buf, 1024); + check(assert_sane_strbuf(&buf)); + check_uint(buf.len, ==, 0); + check_uint(buf.alloc, >=, 1024); + check_char(buf.buf[0], ==, '\0'); + strbuf_release(&buf); + } + + for_test ("strbuf_addch adds char") { + struct strbuf sb = STRBUF_INIT; + + t_addch(&sb, 'a'); + t_release(&sb); + } + + for_test ("strbuf_addch adds NUL char") { + struct strbuf sb = STRBUF_INIT; + + t_addch(&sb, '\0'); + t_release(&sb); + } + + for_test ("strbuf_addch appends to initial value") { + struct strbuf sb = STRBUF_INIT; + + t_addstr(&sb, "initial value"); + t_addch(&sb, 'a'); + t_release(&sb); + } + + for_test ("strbuf_addstr adds string") { + struct strbuf sb = STRBUF_INIT; + + t_addstr(&sb, "hello there"); + t_release(&sb); + } + + for_test ("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(); }