From patchwork Sun Jul 21 06:15:48 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: 13737941 Received: from mout.web.de (mout.web.de [217.72.192.78]) (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 BDAC1443D for ; Sun, 21 Jul 2024 06:15:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.72.192.78 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721542561; cv=none; b=U42z8wEtzvJeVy2BXX4lFJPlH4SMMZRTl7FfSl2lteQKtK+kx8FGV9IA9Nbuf1oy4+iHUaddmMf9U4n9bHA8grPEjH4xOUjbG0VfEb0OHXcMTgHabzu6KKSG6AIUmBs1MK+SsO5jGYPQ7TJ6vDRddyWf2RaIj7tAvaGaBahHEXU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721542561; c=relaxed/simple; bh=YyK3jzp7ErpZe2/uMplGBhSUT2DtitcUTovgN0Iyat4=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=VQZppJ5mA0XHt5qdltEhDxcWJXG7+Id5eVl9Tg1OLbTxI9ZSOP7bNk45XZ334cWav+ezh0TY2vv+Q8HDEE7EXqZ/QN+m+z0wToYIqk2qjqhDpNcg4f8ZpNe9Eo0TSSQRhoOOs0+9/lbxH4ZK5RLDUKg/CDlV7HKjWWH0xZ6CCZ0= 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=bjYmvWbT; arc=none smtp.client-ip=217.72.192.78 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="bjYmvWbT" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=web.de; s=s29768273; t=1721542549; x=1722147349; i=l.s.r@web.de; bh=vChOqHfkyfoog8JInnCFkAZ6GOnV8Wik2MQxV326SVk=; 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=bjYmvWbTtJXmV5dRxCU0coB+7RUNJ987COZIWkZc8rDzoSAN166LkWwyFTPRIzBY RDajT433XVk1+EOoRn6EqF4oQUMXw9z1MVPhSoz2E0Hd/1wmI2CvtzvQyJ1gyPJaI pzUl/RlZzTlBvJs8UlPH0xqvsGdafh9X8jQOa0l44/8H6as6q2sNEFyPPqFEW5Kny nU4ph0ElDPQoSiM/8z5qiUkvb+pVHD61NdGB/srBos45MlsF2th0JyljDZmi47efy 0ubh8Dk9MrhLGfv5VhM/pHE2Nfg5dW19FIrHdrnpdwEzquln0A0nG6QUH+TZpeuyf cb2pK4DkcTEPTBgyiQ== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Received: from [192.168.178.29] ([91.47.153.221]) by smtp.web.de (mrweb106 [213.165.67.124]) with ESMTPSA (Nemesis) id 1MdwNg-1rvWXi3T8w-00be5V; Sun, 21 Jul 2024 08:15:48 +0200 Message-ID: Date: Sun, 21 Jul 2024 08:15:48 +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 v2 1/6] t0080: move expected output to a file 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> Content-Language: en-US In-Reply-To: X-Provags-ID: V03:K1:bq29t4BrIXky7JFYV8sVpTcOMsIUSHuqXTJsCHyb/SY1WTSamFm 8+KMm7rILEfPwHRfSYnvgaxsEoMORNuASNmOgYCaNmPglxn009T3fhsmgFBVv33ZC1Nhjhq u/gyk5S7CpCg9p3jMFDopSuQ6ShKFljgAFM66JldRTVredAVP0JIIb38Sb2+YNfyZe4d50E 55zDbS5UjJRw1wUobLtQQ== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:K6ZdSgU0U3A=;WOCBhSvJhGutpmQHwvag2bNa/IE lQh3yKTuV+fXJGHvLBTVngS2H+s67VhF3gYdPXV0+KEaWFUtfVv7SPqI4nwtcl6TqL5C0c6Bo /qJIYJ/Jyfu4LUH2zxpt6KModKlbVQvk3zNh+aJhwyk0O1ULHobkPG5VOAwGv1gelR21C6sjY q31ErPePmQsc807p3XvYeuTmCAa44jC1OXE3ywhwTcssm6CFV8FmVOC2x0onaapRdkeFjS2oW yliUIOzpPIPkGiCZkgPpQcaMAB4D4Hdnww3f6WDkja2H90xkDOwGD5lwH4sUG6LxiyA+re4Ye ilX5Cd33SwX9t6dHg066QlSMDnj5vaJVwn7ZG/iPhVhtUl58VCdtyf0ogTaFMAmfeNnfRdzbh 1GWvDRJ50mzBOOFauoeVP8Y4d50Ge9is4h3xi7I0Z2fzxG3C37fOviBRumWG3nJDNpWCrCTX0 IdWqCh0yzdxES952uSjktxHYLht1jN8M1LXhyndTjdxSkcHtvY1GJ3iPVTjpk5i7f76WEqZBU yHLkZaXopfhSjUOc2FYP7BRVGSoBdo73NSITDSV80CR52bkJjRNjiv3SCLn5irWgXDttRfMug PUOD4fDsSx7lJIU3p2r+BUQ5x1cE66fKUgZVY5Ck5ee6T3Fr84N+Z56EoBR0RCy+B4cpR+33G uO0+18SjPCgga4cs6HuMm1gugU+vC3nsyhrcpGWbO0R2+jx421pVpL7mvLQxlfKnaVfLUgxuc BmBIVOTF7uWIQIXL54fTlzDfL3elfQuc5uDqNqu49NQQmA/CLOFTfXTx82mQeKCeqRC+hcNSO /LADQOzifyN1NC4Yt+Ity/kw== Provide the expected output of "test-tool example-tap" verbatim instead of as a here-doc, to avoid distractions due to quoting, variables containing quotes and indentation. Signed-off-by: René Scharfe --- t/t0080-unit-test-output.sh | 48 +------------------------------------ t/t0080/expect | 43 +++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 47 deletions(-) create mode 100644 t/t0080/expect -- 2.45.2 diff --git a/t/t0080-unit-test-output.sh b/t/t0080-unit-test-output.sh index 7bbb065d58..93f2defa19 100755 --- a/t/t0080-unit-test-output.sh +++ b/t/t0080-unit-test-output.sh @@ -6,54 +6,8 @@ TEST_PASSES_SANITIZE_LEAK=true . ./test-lib.sh test_expect_success 'TAP output from unit tests' ' - cat >expect <<-EOF && - ok 1 - passing test - ok 2 - passing test and assertion return 1 - # check "1 == 2" failed at t/helper/test-example-tap.c:77 - # left: 1 - # right: 2 - not ok 3 - failing test - 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 - 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 - ok 9 - test_skip() # SKIP - ok 10 - skipped test returns 1 - # skipping test - missing prerequisite - ok 11 - test_skip() inside TEST_TODO() # SKIP - ok 12 - test_skip() inside TEST_TODO() returns 1 - # check "0" failed at t/helper/test-example-tap.c:49 - not ok 13 - TEST_TODO() after failing check - ok 14 - TEST_TODO() after failing check returns 0 - # check "0" failed at t/helper/test-example-tap.c:57 - not ok 15 - failing check after TEST_TODO() - ok 16 - failing check after TEST_TODO() returns 0 - # check "!strcmp("\thello\\\\", "there\"\n")" failed at t/helper/test-example-tap.c:62 - # left: "\011hello\\\\" - # right: "there\"\012" - # 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} - 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 - ok 19 - test with no checks returns 0 - 1..19 - EOF - ! test-tool example-tap >actual && - test_cmp expect actual + test_cmp "$TEST_DIRECTORY"/t0080/expect actual ' test_done diff --git a/t/t0080/expect b/t/t0080/expect new file mode 100644 index 0000000000..0cfa0dc6d8 --- /dev/null +++ b/t/t0080/expect @@ -0,0 +1,43 @@ +ok 1 - passing test +ok 2 - passing test and assertion return 1 +# check "1 == 2" failed at t/helper/test-example-tap.c:77 +# left: 1 +# right: 2 +not ok 3 - failing test +ok 4 - failing test and assertion return 0 +not ok 5 - passing TEST_TODO() # TODO +ok 6 - passing TEST_TODO() returns 1 +# 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 '1' at t/helper/test-example-tap.c:33 +ok 9 - test_skip() # SKIP +ok 10 - skipped test returns 1 +# skipping test - missing prerequisite +ok 11 - test_skip() inside TEST_TODO() # SKIP +ok 12 - test_skip() inside TEST_TODO() returns 1 +# check "0" failed at t/helper/test-example-tap.c:49 +not ok 13 - TEST_TODO() after failing check +ok 14 - TEST_TODO() after failing check returns 0 +# check "0" failed at t/helper/test-example-tap.c:57 +not ok 15 - failing check after TEST_TODO() +ok 16 - failing check after TEST_TODO() returns 0 +# check "!strcmp("\thello\\", "there\"\n")" failed at t/helper/test-example-tap.c:62 +# left: "\011hello\\" +# right: "there\"\012" +# check "!strcmp("NULL", NULL)" failed at t/helper/test-example-tap.c:63 +# left: "NULL" +# right: NULL +# 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 +ok 19 - test with no checks returns 0 +1..19 From patchwork Sun Jul 21 06:21:36 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: 13737942 Received: from mout.web.de (mout.web.de [212.227.17.12]) (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 AC14146BA for ; Sun, 21 Jul 2024 06:21:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=212.227.17.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721542917; cv=none; b=cOSB6j6Z2RvmlRPwo4TWURSRWeZQaLNktNmTXHNKQO6LhLEE4RXPcI1xKCQFkBsEa2F2VYzoQo7YzXlxaquj22SFLs7alTYR/AuxfePHRH3N3G2U67Zjui3lbef9oSyDZK8DwLWG4EL1w725bthkeAyfSN35L00WJ79q/6uwpII= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721542917; c=relaxed/simple; bh=dupydwXUlCLTQ+HOiYuoQMBVFY6Zsde9HK+hkCxdaE4=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=NbWCTKNNxlFqS6t7CjC0oZuatWxRUQXCGX84GvvYfTrJbBGJVXQwS2+EnEOm+WCpDLOrRpZH30bii6XEjucYgqwf3UUnL6G3uyxQ+ayGFJABR9RBNR921saj0nIfme0md8IRj+ikJTF/sooA6MuExKK6YKbHy0/uOf5aAtMxa2Y= 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=adpsIWrd; arc=none smtp.client-ip=212.227.17.12 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="adpsIWrd" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=web.de; s=s29768273; t=1721542896; x=1722147696; i=l.s.r@web.de; bh=BsdGOclOgA+AAnq2AZigRN/T38TxorFYa5NOYfprbAw=; 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=adpsIWrdrjilgyjAs1yrMAataZIb9M55nbRwZ40pO5XlLcUnPy/4du9mIL+U9G9d KJp12+FXoAODO6MsYEJnVU34JFL6QD01+L8nKyROxNpaeqNZipxJYURzmVF7/wE8d 5MY8Rv+WXwowa6teU51PG55bZ8pJDnHZfE9bqMYC/XeS+K8rEWkayQmkhkseI0Ycn K2iZ9dWN4W3oyV1ZOq7DvbYTRB0z6Y7CFjryODewOx3Bo1rDpdNb7bZYWsFjSf30G C3bOeQJmxzX0uPNRshcmMnvwzdnZTa/WEjjiI+bbp5wbcOvDmqTpbmEMlmIsLSseU tC6AfsmAZxb6OQFHkQ== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Received: from [192.168.178.29] ([91.47.153.221]) by smtp.web.de (mrweb106 [213.165.67.124]) with ESMTPSA (Nemesis) id 1M8Bw1-1sRrZj2d2B-0083b2; Sun, 21 Jul 2024 08:21:36 +0200 Message-ID: <2dff757d-3c5d-4923-97df-26bcb1c21230@web.de> Date: Sun, 21 Jul 2024 08:21:36 +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 v2 2/6] 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> Content-Language: en-US In-Reply-To: X-Provags-ID: V03:K1:e51r28F2E11nfh59sqCtUF5jHbzDvaG6TxyKLwAXtvldi9PnpPJ PB631aqNwCSoGyV83jWdW6krInC2zTO0VBuIEq8PkghESgmad5MTyq1pCZYa2qwagaCo0+B Aryoyh4zvvz6pG8mznuh5OjXUpDrT0t2ewqsQNZnH3gvV6iN586GRSW02J1NG8rxal4IUP9 jYyhEJRkGJyHXUMXdbehw== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:b7xvAIDZtmY=;Fg2fRTV20/0+DB7FL0yt5zc7oII WMlPtjuN7Sxp/nc5gWx7m6qQQqOkNDbL7I7WO1t6QkNYTkZVFQ3ApYIpCbKVaRBa/53kmDM/F yUlU2R8hJUTO0nqcTWvVpRnK0wSmjwblWOy5p3fGzKBXmXrcED5LofC9EWXNdqbPqVfSbQW7L Y08Eiea+kq9qmVhmfrW9nMqNYjFLz7wCz2EjLinsS2VfIaXO7BAp/NgX14lII9EgtGXDkNRb+ 4FbvjspeR7ob4OJD/xHVeAJGJZM6TyuXOWAPqnXdUak5a+HPdkbNreRqy5idQI/D4qOrlHVHg uJ+n5x6NLmlmb5+8oa1kpoBqrKKgkMMafrqyG9dg8R9MMgcSOjOaiaXdM0zUyyzQRxrGv2Pqi lSzgGMW+PkFjbL7CWotTSzHM/a2BTZAhZgIZmOA4BVm6Mun/ozrd27Wcwe/olxUSf1S0mtAbm Y8AuEa8VYSFfA6eT55IAJY4lfOl3fHYcIZ+Vp+PjcAko49WSf4ndzaGjh/Is9928jrqvScYdt OYQ2k7VdhdZXySDPCQEGJtMY3TUVbu23R7EqfusDFwk0EqBRAs/3CydtNEyFbEKoR6fuuVaNE T+Opp6wp55iD+75pjV7sdRGwPYQtW703ImneZimnsAVJRf1WIQ1uBaiq7AxYAWY4VQttId6Jg MPW25Y0XLfikpJAVwkHDBBiGImOT7BKHiWiRD089QKFjvJiH8irYXQ0oOSm2LV8XpQuWkz8ps 7yO+lYLj82m0hCvf1f8rbFwYk+gZAUno23zs6yerxVMjmWPXpua/+xqQtmN9RmMKQy31eSUF3 LY8z9AIP4UYb6H1dofdUSX0Q== 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. Helped-by: Phillip Wood Signed-off-by: René Scharfe Signed-off-by: René Scharfe --- .clang-format | 2 ++ t/helper/test-example-tap.c | 33 +++++++++++++++++++++++++++++++++ t/t0080/expect | 35 ++++++++++++++++++++++++++++++++++- t/unit-tests/test-lib.h | 19 +++++++++++++++++++ 4 files changed, 88 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 d072ad559f..51d5e6e75b 100644 --- a/t/helper/test-example-tap.c +++ b/t/helper/test-example-tap.c @@ -92,5 +92,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/expect b/t/t0080/expect index 0cfa0dc6d8..583f41b8c9 100644 --- a/t/t0080/expect +++ b/t/t0080/expect @@ -40,4 +40,37 @@ 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 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:98 +# 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:102 +not ok 23 - for_test failing TEST_TODO() +# check "0" failed at t/helper/test-example-tap.c:104 +# skipping test - missing prerequisite +# skipping check '1' at t/helper/test-example-tap.c:106 +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:111 +not ok 26 - for_test TEST_TODO() after failing check +# check "0" failed at t/helper/test-example-tap.c:117 +not ok 27 - for_test failing check after TEST_TODO() +# check "!strcmp("\thello\\", "there\"\n")" failed at t/helper/test-example-tap.c:120 +# left: "\011hello\\" +# right: "there\"\012" +# check "!strcmp("NULL", NULL)" failed at t/helper/test-example-tap.c:121 +# left: "NULL" +# right: NULL +# check "'a' == '\n'" failed at t/helper/test-example-tap.c:122 +# left: 'a' +# right: '\012' +# check "'\\' == '\''" failed at t/helper/test-example-tap.c:123 +# 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:125 +not ok 29 - for_test test with no checks +1..29 diff --git a/t/unit-tests/test-lib.h b/t/unit-tests/test-lib.h index 2de6d715d5..12afd47ac9 100644 --- a/t/unit-tests/test-lib.h +++ b/t/unit-tests/test-lib.h @@ -14,6 +14,25 @@ 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. + * 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 Sun Jul 21 06:22:36 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: 13737943 Received: from mout.web.de (mout.web.de [212.227.17.12]) (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 D34C7443D for ; Sun, 21 Jul 2024 06:22:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=212.227.17.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721542970; cv=none; b=lMoldUSkPkYRa3ANAdAMs4OzvFl2RNR8YyZ6ZuFoT878QTBM62F+hwgQiQ1lZhOq15I0vJIHmDunVt/WnuL5+IKFeHkaCTOe+TloAOYtlQLlKmVjiwbTKx32hq0PZ6wpCfd1fZCE2szIxrjv1QzrlVyFIRHIGiq7mK2Sxlq8b38= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721542970; c=relaxed/simple; bh=438lcOvmlew/vUJkbq2Wo+GsT0Zjvn85P8sXcjgd4ew=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=Gzw8mLcBJi7GDrNZlsNvejTZBek2Xa1Be2CJlaF1CyncZznXuTf3PeUKc2l/zSX7OThEsJFEjaLmJFz+bAeUNWjVm9gOqq6xt2Hm3VsRo/YJ33XEyR6qlHgnLy5xdXUxQrzYsplGm/oz1qEbGIGXic9QlewGRL8D7rtV4ij2I5k= 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=TDSq8y3/; arc=none smtp.client-ip=212.227.17.12 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="TDSq8y3/" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=web.de; s=s29768273; t=1721542957; x=1722147757; 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=TDSq8y3/q4KRcLuBQ7hrPAbwluNTRtBvnTAjvWsrPPTQRixfgP4+2qF248km+pJy +1+BYSwXBCAJEn81WqYnV6hVImet2yhzP6Ty8w7KOYPZt6H1yosoMJKV+9P9QH4XG 80TEJvswynJ+KW0N6DSeD4TrWksMjCFLLUJL13bGohOWitqbp2vqLQ8IVd+t7iRKk UvMk95GJ+oLigtRGiFQjDmt93hVHnEdoeRRyj+vePCgJGlMpJE+qmvZGrkr7R0Gsw flLmmLn0HPyOsk+MygHk+Bf+U+RbrGnDApL6xtuhrnJsJUZOAj+ms09w+LM5dETmb kpJx1fuBQBfcwXYjnw== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Received: from [192.168.178.29] ([91.47.153.221]) by smtp.web.de (mrweb105 [213.165.67.124]) with ESMTPSA (Nemesis) id 1MZjIb-1srCN006YZ-00WYTx; Sun, 21 Jul 2024 08:22:37 +0200 Message-ID: Date: Sun, 21 Jul 2024 08:22:36 +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 v2 3/6] 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> Content-Language: en-US In-Reply-To: X-Provags-ID: V03:K1:CQkCLgtF+U0WYmWlDqV9UIbTdrUj8ktjs5Mc/2KnvSaoUj8PqbU 0l6mUA/pd9Apsoqb9stxrpCulcpX2FhaluMD3U8TQTIbmlU4GorbyHqfQbaStnnuO+c5WaN mM8rGyl/M6AIc5iIl0mTWhN0r1uW1mHWo5wehmOUjsAA6xdRjeOJO+2ZJHr0IORw5dxpHHw ab50PBGHU3t5/Umtw64NA== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:3e+OszmtUl8=;fkfrsca39cH/YNJlp+EcgOCu0bz hG720F0NFlEZBLS0pmw5I3oPcKCkQ2nqGTM4ki7/J7a5UUj6XKg7kNaToV5TQAEm00fktkA5v aPLM6HZQ78k7NZervuS+SlWCDtVHz5rZYpUbYhHgqCDDyqPW2QS+FzxTFasIk1JHvPa5g043i hjUTOpeRS0eOaoKsbdmYP2wvt+poYDpoLAF749CO8N15QJc2hXKMIhhj+0BJdEVXCz4RIRe5h 4eDLat/BTHYU6hqPK1FzCKZ6FrI0TVbAuLsstJ6K1zwDe8+qD8tfC4s5onhLoXW2veh4L2cLL +E9V+JnQ7xVe9Tqfjc6BDR3N3nlSuilFC/pEMNg+RlTp5yZfbMTaltce5KryKH2Pem7cxkkCV glkn5BMfbnM1r0pO95XaGJs4u3y8i9YwjrwdKOvc+wGTtSs5VVFfyexFnIGpW5EgpUDezIYdL wicBzBMCV8Gm3l4KZh5fGqz57fwtdxlnCrHf5fSMP5zYAUwVl6GrIf9e1hnLkiBMzRWekm45W QXQD3zRXbar7BgN+hE3SM4XOA3L8ek0LY+QCfsA9hLABx49pVera+t56saPGbbrZv/hSo7Tjb Od+/vK9lIvfLzJC+0c0nEDIGdUEsxFkz1UKcGwwZTyTCcPhskS2Tp74HYrXNhre2NJ2+Hbg2h 4YGZTuMiG9AR0U5PzuwP0kxrgi5BS6Qob24Hum1bynjEovn2hlTCRkZISEwvcd7ejwN7EfkQJ 3L/MQC/+hrWEGh+iaMC2Um2Q3BhEPhcNSjT9pSarL36ye4TCPge4PIr4+0/QiaS+B5qXQngPk 9xOTFSpZHO2yNwFxMtijOUlA== 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 Sun Jul 21 06:23:59 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: 13737944 Received: from mout.web.de (mout.web.de [212.227.17.11]) (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 DD9CC443D for ; Sun, 21 Jul 2024 06:24:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=212.227.17.11 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721543058; cv=none; b=lJ/b7cHJFjeNk58sr5/Qe0qj6baQGyItrka+cFxIlnoxyx+cF0CejPDdb1qHalVcNDYbhZudZf1AaWAZ+CT5BqNdArVEdYT7bxp4chfzbrnLenh4fo6LOfB/r8Q2LmMqjPw6CkDOJCqXhVnyHn/62WE6cBJkRVtbL7GcxivpdfI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721543058; 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=NBNed+iP5aH4sRSQp69o1CTqxoeN03yOjl/hvk+txbTU6RVKyZM2HkNhoXQIde49mRBvW0koTRr4VVrMGoZxA9BSycmskHYxwKk3UAI4e8UNv3u8sNz6XrPzB1TJCMYcNZQgGgB7tXJEtiYWI0fQsX59E5AKO5c/mL4poCaM1t8= 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=bxjqs2/B; arc=none smtp.client-ip=212.227.17.11 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="bxjqs2/B" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=web.de; s=s29768273; t=1721543039; x=1722147839; 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=bxjqs2/Brl3fSei6zhf+IE8iO8OwhoxNHaVOvIhzkUQ65GFtt0QCAtg0Htwfifxv rGb65+pt2v4opPCHwMl1MG1pMp6IGJ6r7W95KPgwy4kWLpnOMmlYa/PRLPHbp35e4 sGcdhRVO66PkVXtAo8g85QL4LUdtQCi/6glsuSgBQYyyLIeTG+vfE7ss9Qjq3tFW1 JxkJWAOsrqT0ZPIezwmddX/eAoyo8m8AaJrqARnZScF2E8AQS3JKvvmxIAKSa/oeY YZOAmDRd3wtpU1813/MrY1hqGs0bryx47d+myn48+0RTUHiM2UmLT681I4YU7GQKK ZVCtMDMHyjDdMkUR+A== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Received: from [192.168.178.29] ([91.47.153.221]) by smtp.web.de (mrweb106 [213.165.67.124]) with ESMTPSA (Nemesis) id 1MIL4Y-1sYhlD2YFQ-00G2Xh; Sun, 21 Jul 2024 08:23:59 +0200 Message-ID: Date: Sun, 21 Jul 2024 08:23:59 +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 v2 4/6] 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> Content-Language: en-US In-Reply-To: X-Provags-ID: V03:K1:RzNCUUvfwyUcXoPA9I/3hYGxGwxU8pHZC/DK1hvDQKHmy0KXH9j a9hmna+AWdalnClJfLPGC6IXVeMuF8iAMLU+HNSaWQrd88zyYfBnYVri9XvyS4WHQFOeGOG bpmAWIriQqM0rvmnbxDtpM7nrBkhaJHAHzMRuoswOB5xCTi5a4WnqdGIddbr/vop4pv8EAO 1+MSooqOQB3ixOnkfV5Tw== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:S4LsU8K9n3k=;s9HuJ+jOJqx/8KLswv9V40Y+GKV jkXz7YVe+fptWwGtkyg2oqFTxYRamKurUyZnwJW/C5dCND+d8fYpKWwYkn/JSOok8CAVagAkp WzM9CEPWIKNd+QnOhb/ENXnsp/Nev1GaGunjwNVlQfO1g9aidlRJ/FhfUkItwqUPl51qBzmri JChryUtU5OhQSxyP8VLlfGONuGxBBfbDQm3WY24xJ2NKJoG4X85WjWAifWv1NLQccsJ7e9P7D fTaDXOyjfRHlT78pYqW/LaXibiVLJVwCyWDwMlpG33vtEfp1i/f+tlL5cyzH4RMNlD+BlUX0G 5kgqmBj4k050fuQMxS5A6QYCcCUpftE4zd95D2QnlIjVWsdijvXJN2wCPMmCMDnSs3x74wBT5 sQ1zbKS+fgG3AL87imaIFlgJ0ebtTWG4+P9vdND1mUmq+GQ8lMwOAG/zIOtMtT4VDayzQ6jnr alroM3kC2Lc1rki70LmVCoZZ+et0TqgSsSL5tn9DLtbewKb5Fz0W84gzw5sw7t/DpL/4emlpS QvJtDWUlLB16cCHfXpu8zuVWyNZvcYusQCzElpcyzI5D+K6jLy+WkLNat95XnhtRbsNCz/egI sY950LmyjGu5DB3JGq8/DvSlNhlUx1igLI6DXRvoU/CGdVtfFvuDDNGrprFlIsUpSGQCyG4F5 b6AHML4aDeA6kASbZ7JgPOB0Kko5oxYjdk2aq0vzo1zfI0nyu38XMGAm8KXGxzWdOMAxONcXF CBT8pbIL0WGFwqnE7476zENXziWp0AcwGHuOREzE81aV5gSEF198LODxDXilNXgShFe29S25c b0V/4TARQm5jg00YV3/Z5mbw== 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 Sun Jul 21 06:24:50 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: 13737945 Received: from mout.web.de (mout.web.de [212.227.17.11]) (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 8D3C9443D for ; Sun, 21 Jul 2024 06:25:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=212.227.17.11 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721543103; cv=none; b=nQQMHLj4jZNvCDE1evGLXqvK7XBXPU4LPPCCIu2HSmyyddEkyFN+oBB1wm1WPeW+FFWGyvR/odgwhjkVlRltvA0GLb3VAOQB3Mqk6nsIWWjDI7hUR/LL5TiUDqkNJAv0It9UdDii608s1kY75vM/9rTIp33II6PsmfM0VqC+DXE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721543103; 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=k2tu16do2BK3wtPFmyJN9L0PLPX7EPpCnBTCULqmH0j/FLcGwu57nRhWqIPVrE2mg/0HYTvo4LBTG2uMS5UmH/jU2odPKGDwTi4oK68ycN35IXsyYA7yc8zGJ/XNUqx8Tos+Z5XtS+qMRfUe60QXVVjm7s2XlrpBVF7l62ltDa0= 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=hzQWOLSD; arc=none smtp.client-ip=212.227.17.11 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="hzQWOLSD" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=web.de; s=s29768273; t=1721543091; x=1722147891; 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=hzQWOLSDeb5hlviK/RA3n44VJuxuhCSw8Dzbrbk8KS9jayS6lBZbNV2GiAmaSUOn G9I8+iqlJ4LdgmHEA+6LFrHjdnrVmP4I7SH7UZ9jIBon3pVRAZ+wuWUJ+Yzi9pWO3 aGBS74o6UIBRjBbIVgLfLW0Jw7eXpIC/HMtlg0ceqcf5uZPF5+8oGqt1S51XUfgo0 MWlV5IjzjikVGU2GJDcgnq0VkeTRp8OWLO+6WMJhtHLZMFtPSHiM2ZmZOfmabxZNO QrZ7n2WeOzye8koJ3rBo11hkStnirF5hibs9Y0EE14AVTaqVx8nSIfTfiy2HDL8B6 IiEG81RiQ66hAkYCVw== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Received: from [192.168.178.29] ([91.47.153.221]) by smtp.web.de (mrweb106 [213.165.67.124]) with ESMTPSA (Nemesis) id 1N1LsH-1sL8J748OR-0117Qx; Sun, 21 Jul 2024 08:24:51 +0200 Message-ID: Date: Sun, 21 Jul 2024 08:24:50 +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 v2 5/6] 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> Content-Language: en-US In-Reply-To: X-Provags-ID: V03:K1:4z4cbeVK8P2sIkVrcvvnCfmIXTNWq+Ia4P0qmazFu3yGSqwcVg8 ijN37PcZs4mL5XfxPbvga/R5vVk1Yu7fwAOKJ/uKV/O3g4A4vHOE4Fxa8vK8pMzbncn1Q3i DnDsoG6YwMwmYL0Q/5GcMa28PUUxmg12BpTyS4AoBZKKKmts5DUKwB8IuEiOllfOZgeXj2O 0TBdIgxNl4DBauo/cA6GA== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:GpmfmmDayLU=;/cLQ6nI7SWf0oa4CTn6AYofBkQo 6iorh4r05mos8+9Dw0LKvucml7baiOjF62tiLyBq+fg3ONP5uYTCxZfmW8D51V1XL6//fjp+1 y4p+qvj0hptGgjLKFoxJxXY3dS4XKf8fYVQDq9pw7G0fnqGnOMED2qCZqx4YqEAiUZbLtmjN5 DQPRaSL94thyGbIzKc7ALmQWsB+mhGcDteh0Nwhp9bzlCaXPvLR2iXWdJn/WBUdVXvq9dX6EZ pELV3HL1WSWfCHU2Gku+ht9FTL8N6LgnC/rUEGYtCJF9B1LUfORjwUJRRe5A/+jXN7aoCjlhV 0s6Uar0s9T5jWEUsXVEkg83O7wvIjIJT1j3vHUeFx0aNJ0/asMVE7vy0U8Jd3SIzNmes6qTgE wTt8oZuJEUSRoT9Y9mflwqmxgXu04kMyZp072PfYYw467XufmPAFAZ5WvFDH8Hy589FMutiqt +OugMC8OB/huNdT42wb/PrZwiEzkdeCkb/cYG0CxQKpm/XESJ3yGJZS3fH6yZPlowrLYTugpm CUavgSwbJNndvBWrNHP+m0BM6nhwXC2wm9aOH7RqEFQBaNQ2rL4IElao6kOZaR1O1dWOOH/Y5 rKHgcXGirfX0XHJ2Lm3ssl6r6ZGjaV9GAJunaPQ0kjX5Vxl713vBESEMnUbFGp3nL8tzYkXBY vgaH4gVV6VDsk2Brf19W1MLQpV3NJ2jdqo9PrzCK24kD9uDLr0ak1p+aCRsMBm7x4EZn9dkWM 8UWXpknaL5M8N/uNsw89SoXk3o4yynA6QXsnZqHjVTPxMslz4mJ7OsDCnxJB7befeaNOyOHHE WmMLK2ndXodS9PhztadDPsYQ== 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 Sun Jul 21 06:26:16 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: 13737946 Received: from mout.web.de (mout.web.de [217.72.192.78]) (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 BA734539A for ; Sun, 21 Jul 2024 06:26:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.72.192.78 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721543189; cv=none; b=tyrJ/ONKjMtobQj0sE10M1xsRdKckWK+AmAZFo2fu/x4O/OwpFVqXOf1qrc0KB33qWBod/LHiE/byZNiQDonAdv9iSckTS8YQHYEBoWLoRRT6xel2eUhfjZg62VXWa4LXrEQW8Cn28WiEXQjNbw25ag4pab5oIl7i9Ei/o+cnJg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721543189; c=relaxed/simple; bh=NH0xhiZeYjyAbANGJFRegjsDaLD3qj8V1LcMpOE46js=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=RB/5aCXFxRKpjzsIhx8rOBX+X2ty+AH5UtasZyrLSfW5yhFtdkabeefqnGgsMZbCXcysDgpdmrvd2BW6bi3LtjbtLofi98ZsSL+0CmaJojBqYLB4P7Nz/8zdck6xkAt3V2CCCJaEVSUw6gYt4mYvOfwz6g0gs/DFaheuz9b+Zh0= 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=Uzz+Fmym; arc=none smtp.client-ip=217.72.192.78 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="Uzz+Fmym" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=web.de; s=s29768273; t=1721543177; x=1722147977; i=l.s.r@web.de; bh=D+q/D9n1r07ldtcZ4xTGsSWpAaMg0Mb0hujX0q2CZAU=; 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=Uzz+FmymNTsg+zSBs4m3CwSRzHTd/lvLQdIn6QoffYK8XGqEZPJNFo08w249gkAL +FWF+bYS0jw6trBcX9/AKLPF7oVALPeniVd/gN7hy4wBnwvQrHdCIDSBia98X3eBR ybb1P783Z/2b4bILVxk2SdCOHuUR4eT90bYVw6xYRdg/kr7GxgAg3zi3O1SZjSE19 tsL8z6R67Z5rKMJiYzOMs2UfF4fkaFcw04Kp4m+K8XP56Deq51Wcih7oPhrni1s71 8bRVWAVF96BRgdU5jRAG1yhL/56+X5+t19/8ZnYgZv0Mq5qBWu4i8tfDQegXX99+2 02ixSlH0B+WIDoVKfQ== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Received: from [192.168.178.29] ([91.47.153.221]) by smtp.web.de (mrweb105 [213.165.67.124]) with ESMTPSA (Nemesis) id 1M5QqN-1sUbD21Wlh-0017GR; Sun, 21 Jul 2024 08:26:17 +0200 Message-ID: Date: Sun, 21 Jul 2024 08:26:16 +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 v2 6/6] 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> Content-Language: en-US In-Reply-To: X-Provags-ID: V03:K1:muWYbUUTq/59excXTqVbB4JpY17mUw9zgXPlCEY9ergrefk0d6M JUWzox48Ey5bs0Fl09nbepFWnJ04riFqWtP9fKUXXzhcjx3JSVMzSH6Oas3F0cGp9tB6KKk KZrY19OsojmFg8xX5AnAoaDve25xRm6l0hRIGG3HyNuRuRrFqSsKm6oz8mlFBNFIFirpJrY Ut/L1X6w1ea/sSVIRNy0A== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:4q+YS02GtlY=;PbnKve6TFpdpnetT9JHChZhPDer c5cIKrDvj51564tZzv6ve8D8f+Rfzs5vy+MTkcgjcKigBEE3ePBdcOxAw80P3PZ55jUAX6YKI 4ltTlqLNu5Qs7DMt3k+rWTXdDkziSlJ154nqKVDP6pMaqp4rSSJIV41+h+MogedS4uSQY2tiC bzVZKW/A0+j++WdgKGaf6xqOO6CnwKGSpaMWM+Xrib8Y46mFmLBQBZ6OrzjJZyZpbSO9ijWNy /muzaGZg3h4RBBB8Z22kjW0reCtgg49i7CTtql3zfFKQ3mfM9ahk5BWSgLYWmgpaaSTLjR4z6 +C0/08VqSzYbptG2v2QHhrGX5KhEK9Gr7w7npDRpGsiIK5eBEwTQ1xTZGFATGEOIeDhXZ6rpn 9M4lnknmYCm3jqNbrKBvrlG5pKW75pYBrvVIQ+wP505SO/x3ocstAszmlpJTTQa7rdo7XKihL HxgrCFS0owyc3STeHVBWMwuII6ishMpnsLN8ok2fAZOkkW47cO7cIC2VmWJfJVgLNeW0nFI/L 4pDOQjqEELNyIHLwoUHb+JnNlLbjFtProbf6eIVrviSH0yTHtazgjXifHph/eJSDLzESrqJko lDYQm6uAq6M1IaIOt0fFoEcl74bINIcbVwg5FyBgV7Gkqodc0o5AoJZnir9w6romwX+wfeBxB DWam0uk3dL5chcaxyb0qg3dyEBzfPBFBDEmikxEE8FASG7kdTqOIbvCa8l81SwzWTf/JPFWeg eRYXJpCdZ/2QioRbJj7Ift0aDE/ueVFJj+ceQksNwvI+OQ+TM7S5eFzC4HiSHighpC7mAzPr/ 4pwhG4AwjR5RVta15bDkK25A== 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 | 122 ++++++++++++++++++++-------------------- 1 file changed, 60 insertions(+), 62 deletions(-) -- 2.45.2 diff --git a/t/unit-tests/t-strbuf.c b/t/unit-tests/t-strbuf.c index 6027dafef7..82cae4cbe3 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,66 @@ 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(); }