From patchwork Tue Jul 30 14:05: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: 13747441 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 68D9419F49E for ; Tue, 30 Jul 2024 14:06:06 +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=1722348369; cv=none; b=vE2Pboqav0sf8+QHu85wXtPdvy9eyfW8hWQpz7qQ9zyyg+H86Oaj9l+zrxaLZpifWuf0liT70vm7Ec7R3Ei8xQLsPC3FP34ifO3CvCFrVWCp93rluY3IyJyXJ7zM2wp6RnKPWyNL8uI2vU20AmmFnt6726c4CVNq8s9PnmP9+bM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722348369; c=relaxed/simple; bh=r2jp93goIRdT+BIB/XSbWAf0Q9mQ5Yc6bm0JF7E5LuE=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=JQZb2jSY8QK+4qei0zM9Dc6YvOMpQShpVtBvKpVslcqm11AwxQcg7mLYowC2t7UB+qY0XCCrTdolQH71VdX6lDmH/Qkh87h8CHclQBcOiMSufvkeSdWTX4Xjto9ntw/+3detMOhGsGzHT54YeuUOP6Z3LUVIuJpQU9E6nDMcqG4= 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=mQ51gKfZ; 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="mQ51gKfZ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=web.de; s=s29768273; t=1722348359; x=1722953159; i=l.s.r@web.de; bh=Jd4MUHctimRAoX9giffvGW6MUWwrJEmcC6U21ditm6w=; 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=mQ51gKfZ6HhXcWRw2uDtS9StisvmR28SVMKPmr9ODMqq2ifnztijxQdY7GdVy25l LynExAD4jvZ6ztRfofwEucoBiaEXezy1j5HEBzGQI7SmtbphwzVgReD3AU3zV7hWy hyagutEdSchG64fUxEymPqkUTCxXHi65Ylb1sSupURphAIcBYjjETisHxMYMbidtZ B3wUFY2ibXfO2DKv0geNeaGEJb0W9Od/QVwV+JyOFe1+bZ67ElQO0OtgvsYJqKCkK 1PzaXGA6LIuauFczyx5/0sOF5n8jQBRsHgDrOajsnyCSFiZIKmvf04HHrRsgYErsa UaWLZpkRCOTg1eW7DA== 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 1MV2Ka-1sj94j0mqE-00X8kN; Tue, 30 Jul 2024 16:05:59 +0200 Message-ID: <23498c37-1e30-45a9-92ae-d31a05778fca@web.de> Date: Tue, 30 Jul 2024 16:05: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 v4 1/6] t0080: use here-doc test body From: =?utf-8?q?Ren=C3=A9_Scharfe?= To: Git List Cc: Phillip Wood , Josh Steadmon , Kyle Lippincott References: <85b6b8a9-ee5f-42ab-bcbc-49976b30ef33@web.de> <077a178e-eb30-45ff-b653-a514bfd33077@web.de> Content-Language: en-US In-Reply-To: <077a178e-eb30-45ff-b653-a514bfd33077@web.de> X-Provags-ID: V03:K1:MYeOvxeeVMq5CT8eedAPsxZz06gP47DXWUSS0xp064xwQ+LH8hm N/nE8pFYhazGWqtark828zSS6AY0C+m9r2It6ClkL/DFAWS/CCgPStxvAefH8I6R3F9ka2A SOSnf2DXImluc1jZtteluHzYl9CmMwLNZU/e0KoMUTK+9tB0UB+TsWs0bAWjM05p4hQrkCz HqRP43cjvLW/hdt3hdY3w== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:d9Ngw+kIsCE=;MmDXIOC9W52JGE30thPrQwabCal k2Wul2+91lroJlaA4b3Bj8+V27bc/0axruIm00exIhorZRyv+zqA0ILM6Xf7H5Xo9llfKMGEL DtnQ6Vx4dmor/DJmnJmbu+yu1eq+jnZ4cY87dWsUvHfHSwPf+SaFd749qPX0S6isP6BL08UQ9 5EK5J02RuqBFddsPbFAtShWX62HVvPqket9unacof9y6Mng9fAZEPUZzn0tD/o1SQWRUTtMSr iOao3pk6QqL2XpAiIkqGZazZk2Vk5M6d3USKI/4ih9g3/8A/YCvYRAcgeOmnYdhFBlxArIISa llMZ9Tq3lfypuOIoh5x8qnwN8D7jdgyBcAKt6V0EenY+RWY47N+XFGSJRtaPtJrXr9WfPBskZ CwX4Bm17pNBoDTL8aYprbz38ppr+vVgYx0NflcPYPUSx1Z9FsfV+ceRgGlm0Bi21CkEFhOZqh 8/K2LtgbZ4KPV3YwI5EbshFfZN2TXwaIG3MXaaqs7zX1pqro/RgFlIk97/oaInZG+EjMlf/zO e0VqkCMMZmEe64+ZRo8KXwfg99w/LLTs/tFx4BpBeNhc9kcPvvOPnb8h4oUntscVwh/Oz8tVq ItiTJmsu+H9WvzwbSaUl4lp8p6zUW9csg4PnV1cOU3K6JgoiWzulkO6F9Z59hF0ANIgFVTHnn 6R+cX1tDMjKWKp6+NhA5AAV0Am8/g4jSIfuiRV0sAJxsGc2aqst1EfpyLfSJfrS3L0e8zC1yr LjaHEr1HbpfZKShUlvUvN7JSMt4bq8p+cHFFMETc3mpMEWaJojAFrPtE29AJ1K0hiPv77G3/5 nXDwwO8G/esRVConOBLqEQRw== 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. Signed-off-by: René Scharfe --- t/t0080-unit-test-output.sh | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) -- 2.46.0 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 Tue Jul 30 14:07:00 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: 13747442 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 B30E119F49E for ; Tue, 30 Jul 2024 14:07:08 +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=1722348431; cv=none; b=bN+yTTCLNVMjsigrtbGgkfrHe/438Ryx5VYz0L+VVyK+gweZgyteYH/iaiK9QD25V0FITS0u3sz/gERjf37CzHap2nfPui2+nPSPzHbT1Qt51PAev0upatlxi7hpfc6/bFRBZPrTDtQFTJ14uEYZeKOnRDKiZuoZdvLPuavtmew= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722348431; c=relaxed/simple; bh=CeMUahlXNhDiH93HWnZ+v5IXpOTKtlTALGqMCUmzOjc=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=AxTStBzGi+E1fk8AcrtuWTzGTaAErH1Jyrg2iYQgNyNdvfY1i/Dcl6+ubvTTHP0jwsk5M39CYQE2P4+zBsOOKuI6m+Plrt/zgzkYSfgWEXeQsnGBnXvhs2MPfXKcu3irwUFwFwYTD3RwpEvFY+ISTVNkDd3IX6ZUQqdzdpELAys= 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=wnVItC4c; 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="wnVItC4c" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=web.de; s=s29768273; t=1722348420; x=1722953220; i=l.s.r@web.de; bh=zRkQAjOa/f2lAkTjrn+U4hvvk7cBL/hkIlAC2hWStK0=; 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=wnVItC4cE9oPcb3MDucK1aNi8Qcc10La2d/d/IyJ5hsdBlkAoYb3yF/gf8pyeT6v RuffsiFacnh8MzfwGH2tYRwdUUsSH4oSMbp3WRDoZgn/F4E5hu/aeKx1/nHB7s+P0 SeWNT7OvCz8z+LD+ZQ/cygDXI9UL4ShCSGAReF3yFp9AbxucTwRaa0YjlxV/54avZ aIPqe6s1izXK3Ialn7icFedzxJz1g3xH58fRSCG0SMa4pTUtygZhbBnex200sS61u m7KITM5gCNO2eFtEncImEW36MsXChN8TPNf8rXKUfkGbxiOkcdMLX3nA3PNPVn2v9 H3yMNXSwygs3NWyUsQ== 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 1MLzn1-1sqbMz2bqb-00QISk; Tue, 30 Jul 2024 16:07:00 +0200 Message-ID: <4b27cee7-98eb-41ac-a68b-44f42e15a5d2@web.de> Date: Tue, 30 Jul 2024 16:07:00 +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 v4 2/6] 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 , Kyle Lippincott References: <85b6b8a9-ee5f-42ab-bcbc-49976b30ef33@web.de> <077a178e-eb30-45ff-b653-a514bfd33077@web.de> Content-Language: en-US In-Reply-To: <077a178e-eb30-45ff-b653-a514bfd33077@web.de> X-Provags-ID: V03:K1:o2dHgSF1UZ7bj1YMY6VKtdx+3CHrfq/FwVePaTgPPN4TAiGrbt/ H+c+TMVU32XtaTUIliFuARAtJ+8dV0vOWLe1jvXnXw0CaKI8/JIkTKyiblcQHmtEvABrD9a ylkCwUJhkXQ6FM/hypnri3hcSmICp6sJ3LplrIQL3jWFvnftc7O7Ddr7/TPaRKw7TUaYGpR e3/IOYm7eLA6/PG9PGUyg== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:N/1roOzHAGE=;XvfYnJZ6blou5ig/NEXp7L0nobJ B5h6y995MuefGiwuzCqDVpx1FVDvxI+QN6/Rbj4/C7JbcW5Sl6mvnk/x9ktXYgZfUlGVBgW6S 25Z3+cPbkKgIo9ascPOdbUC58FjPkPa8N1dr8Vq/sQpsud3eTGE+z5+HBzkyg0Szg1QRossYU tpW30SNbUOHBy+ysbKkw63ETE4FKFFbQ5YSbwMYr8a1AVIm+VqX8QLNFvbFey2xcPfhKTN4R2 1EPuAEJcyUfsBuylB0mc3CdOuSG1aj2bRg8la+ejmk9JqkZ35sd4GF3+Ax7cTHPR39G6fqIFc UPdS4NBrODJZo7I+2tfOv7Ey8YX7rGO/LLxNunKZgQnqb7b1hkgWHRc3nNwtoSJ1B2sPG9rfK nZOH54HI8vbIh8Oj1/hAUoeir0RFFN1qxMstCppVZsp99fKHZhMrYb4EGOnSmzr2VJ7y2kr7N 4BhoY71ARaQZZDugWXPkcVoJBuO8ancMON79eOVn0heEdt64dXY5Sl1mLcFJNnqR9SZJLsGpI 9ba6Muusdrr19cLj9K5VNWekHwrmjvmquljkjF4bPHBYjs1KS7wiXu046oX2eKdkrfus2pG6M iDgZ9XUFeGbE6jpHIOPeJWwPniEeSWe3NGPcYv+2mORDMGO7m8bKekfxIJV40OjyIjjsdCLt4 ZJdoz2f8pdY+JiJOkQG0oqSxuQlTzGadYwrrPJI4aHIqDg9pABzoFJEgmheP4XNDAbX3nkcWQ lu27AnULZKLwdXq2YEikr9uPdE/cx1jGOYk+ntHy2P+0DUQXbzSKHz1RzffTwixrw2DWLMkXc g4kPLSmLw1DGh/Ub9YJ6Zx7Q== 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 --- 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.46.0 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 Tue Jul 30 14:08:19 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: 13747443 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 2F86519F49E for ; Tue, 30 Jul 2024 14:08:28 +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=1722348511; cv=none; b=LvWYvtQq+98UldM2ZAgUBDXB2VNjn+VtL187OQ9JwkMTdPWJWxwbN7fi/uNgvv225T8PBJdcOYCxOlu/qKOiIbE9Wo/AYKlBrSytzMkEETwC7J9JIK+QsmNi40LX/cL6ILxhZeWgXbZv0hjG/1Y4plfkxbgSkQU4YgO2pBCkT2o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722348511; c=relaxed/simple; bh=7nox1Wpe5DG1MaLpo5JM326nkMrErWS8GdkcpCm/ZcM=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=qhZyR7ywpAthJNa6auSDe4BHLOaJqyclNt+qgKD+esfK5hH2WddGilSEenGV9KCoNKtKPgaK7AScQQCqoWhuerwi4KFVw8ucc4wfc13ja51/kSwTm7QPzx/Eh7wHUbWweg3ju5/YCVSlnId/8WabRrQfiOW5g29KyTkdUeQsmuE= 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=eq48DDYu; 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="eq48DDYu" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=web.de; s=s29768273; t=1722348500; x=1722953300; i=l.s.r@web.de; bh=bzR1SlmrkMSKI7HPa1MSeK04+ukez0ojHH/x1hAo3v0=; 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=eq48DDYu/t/FO38pX3Xix0UlsMT6ZGe93Vug8VsePxzIbCwEwtWFYyGRyMPC3iZL 2rFTSkCYBSxfvJF+ZGoPcg44w9EADjhF7Ln8PTTpwB9jBTd1iFY5ggwIYTpmaHIQw +w4qiuWpMixSPKlI5trof9LUGRIu1IwSIn1eEalS6Ov4gbn6BF7nP476J9ikk47VX T+O1OImccAwDTQhhAgeBBSWZXV6E5ghcSQsYYyFDsglcVBI9zSXA9sdjGwSaey16g KWxD9MyRNN0I9kr0tqZa7l0Z1oyIpLh/T176aiwmpxfY5u7BNIL/73dpy8i351sVs SPhBXSNd2I6Y8KDBOg== 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 1MMGyK-1sryni13gM-00JqBI; Tue, 30 Jul 2024 16:08:20 +0200 Message-ID: Date: Tue, 30 Jul 2024 16:08:19 +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 v4 3/6] unit-tests: add if_test From: =?utf-8?q?Ren=C3=A9_Scharfe?= To: Git List Cc: Phillip Wood , Josh Steadmon , Kyle Lippincott References: <85b6b8a9-ee5f-42ab-bcbc-49976b30ef33@web.de> <077a178e-eb30-45ff-b653-a514bfd33077@web.de> Content-Language: en-US In-Reply-To: <077a178e-eb30-45ff-b653-a514bfd33077@web.de> X-Provags-ID: V03:K1:qy4EX6n5EtiiBND2qvilFR3s2K0Dkmwj/qZjjxleOKwOcFUW+zm rozHj3m9UHMsnLC2vxjiM87TJG+QeVwbBtey90mTtsk8j4YsuijYLKE64cLBz+f3Mm/AiGZ GmFFAXML1pYJzjLGRc31wi7FrDVkrJ7onQF7ptjw8i8R/ZLQ7bXnVhXUgxv8uBQ/EwuT1Lt wAhDF2uHV/DT8qXadII2Q== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:RH2yAvSelXs=;o4+oesZitlq4e/GEbwvZ8zmOzE+ PpBaq12Ci+/moZK/kTPmaZbAa/TaqMmg6+AKUK3ewZ+T9dwB3P5AQNgCrtvHysihHSMpVwRGJ brbMtkiDr8hmW4uJadI7h2z9QzsLw4Ffpqr1Y7qbgn1YGZeP4SGeRPOF0RqLKCSdKeES9mQE+ Qnyf4WQNuBtFHvDDXFINiSzNua+vk5xMUfgmhhewGbdbuNyofGWuNawWkFxJswGDYpFjdEmbI Bydt5T9tNoF6ic84QDlI7a/+adTsShXrfUKwxYYnWYczBwMTohXZR8pFWwVByW0Ux2ABWdhp9 SH0Y4mxsIuGayKzOV9E+dqYizzprSQWVT6pORl4eSxx1faMkPFAKt1XRdkJ58bB3aHR4QTa86 6YYNIw0L6RRagdAkHVuapbRJhhoqJvrYNm1tetEDtB2si7jNjKJGzx+YVkK/MJBaXegIOS6s7 net1rdJ2m4eYp22XGYuM1mH0Y/JYcfplaBPLNVbZzIHuIaMMuNsYl9qEOtVuD08dBzlUq7xVW N/X1imgUC5x8GD7Acfnm9CitOmePwPkE8kgjk+6OtVspUP7K4z+DxBAskQpSfOXjA5DlRH834 tKv7piyjmTneIndyxDppPabYgPmy0/nUhO/MstcEz4cTpGisALOESDOxEKYLyRAsLWBhhjm4h icF3qXsx2KY1TFaTPfMryi7xo+OrBEb4p69LZEuFxWAMsUMu8WcfP12ZDqh6DRyjwzUyC1lJk CHz6HYyNMDrttE7zmYLDdDu6NWkc3lJRaRNEz50AHJ2J4F7/NwyXMiKeB2YY95CQJ5BJhnLXO frR6Sl2lBLch7xpO0Fz7tMug== The macro TEST only allows defining a test that consists of a single expression. Add a new macro, if_test, which provides a way to define unit tests that are made up of one or more statements. if_test allows defining self-contained tests en bloc, a bit like test_expect_success does for regular tests. It acts like a conditional; the test body is executed if test_skip_all() had not been called before. Signed-off-by: René Scharfe --- .clang-format | 5 +++++ t/helper/test-example-tap.c | 33 +++++++++++++++++++++++++++++++++ t/t0080-unit-test-output.sh | 35 ++++++++++++++++++++++++++++++++++- t/unit-tests/test-lib.c | 29 +++++++++++++++++++++++++++++ t/unit-tests/test-lib.h | 20 ++++++++++++++++++++ 5 files changed, 121 insertions(+), 1 deletion(-) -- 2.46.0 diff --git a/.clang-format b/.clang-format index 6408251577..4c6d317508 100644 --- a/.clang-format +++ b/.clang-format @@ -169,6 +169,11 @@ ForEachMacros: - 'strmap_for_each_entry' - 'strset_for_each_entry' +# A list of macros that should be interpreted as conditionals instead of as +# function calls. +IfMacros: + - 'if_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..914af88e0a 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"); + if_test ("if_test passing test") + check_int(1, ==, 1); + if_test ("if_test failing test") + check_int(1, ==, 2); + if_test ("if_test passing TEST_TODO()") + TEST_TODO(check(0)); + if_test ("if_test failing TEST_TODO()") + TEST_TODO(check(1)); + if_test ("if_test test_skip()") { + check(0); + test_skip("missing prerequisite"); + check(1); + } + if_test ("if_test test_skip() inside TEST_TODO()") + TEST_TODO((test_skip("missing prerequisite"), 1)); + if_test ("if_test TEST_TODO() after failing check") { + check(0); + TEST_TODO(check(0)); + } + if_test ("if_test failing check after TEST_TODO()") { + check(1); + TEST_TODO(check(0)); + check(0); + } + if_test ("if_test messages from failing string and char comparison") { + check_str("\thello\\", "there\"\n"); + check_str("NULL", NULL); + check_char('a', ==, '\n'); + check_char('\\', ==, '\''); + } + if_test ("if_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..3c369c88e2 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 - if_test passing test + # check "1 == 2" failed at t/helper/test-example-tap.c:100 + # left: 1 + # right: 2 + not ok 21 - if_test failing test + not ok 22 - if_test passing TEST_TODO() # TODO + # todo check 'check(1)' succeeded at t/helper/test-example-tap.c:104 + not ok 23 - if_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 - if_test test_skip() # SKIP + # skipping test - missing prerequisite + ok 25 - if_test test_skip() inside TEST_TODO() # SKIP + # check "0" failed at t/helper/test-example-tap.c:113 + not ok 26 - if_test TEST_TODO() after failing check + # check "0" failed at t/helper/test-example-tap.c:119 + not ok 27 - if_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 - if_test messages from failing string and char comparison + # BUG: test has no checks at t/helper/test-example-tap.c:127 + not ok 29 - if_test test with no checks + 1..29 EOF ! test-tool example-tap >actual && diff --git a/t/unit-tests/test-lib.c b/t/unit-tests/test-lib.c index 989dc758e6..fa1f95965c 100644 --- a/t/unit-tests/test-lib.c +++ b/t/unit-tests/test-lib.c @@ -16,6 +16,8 @@ static struct { unsigned running :1; unsigned skip_all :1; unsigned todo :1; + char location[100]; + char description[100]; } ctx = { .lazy_plan = 1, .result = RESULT_NONE, @@ -125,6 +127,8 @@ void test_plan(int count) int test_done(void) { + if (ctx.running && ctx.location[0] && ctx.description[0]) + test__run_end(1, ctx.location, "%s", ctx.description); assert(!ctx.running); if (ctx.lazy_plan) @@ -167,13 +171,38 @@ void test_skip_all(const char *format, ...) va_end(ap); } +void test__run_describe(const char *location, const char *format, ...) +{ + va_list ap; + int len; + + assert(ctx.running); + assert(!ctx.location[0]); + assert(!ctx.description[0]); + + xsnprintf(ctx.location, sizeof(ctx.location), "%s", + make_relative(location)); + + va_start(ap, format); + len = vsnprintf(ctx.description, sizeof(ctx.description), format, ap); + va_end(ap); + if (len < 0) + die("unable to format message: %s", format); + if (len >= sizeof(ctx.description)) + BUG("ctx.description too small to format %s", format); +} + int test__run_begin(void) { + if (ctx.running && ctx.location[0] && ctx.description[0]) + test__run_end(1, ctx.location, "%s", ctx.description); assert(!ctx.running); ctx.count++; ctx.result = RESULT_NONE; ctx.running = 1; + ctx.location[0] = '\0'; + ctx.description[0] = '\0'; return ctx.skip_all; } diff --git a/t/unit-tests/test-lib.h b/t/unit-tests/test-lib.h index 2de6d715d5..f15dceb29e 100644 --- a/t/unit-tests/test-lib.h +++ b/t/unit-tests/test-lib.h @@ -14,6 +14,23 @@ 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 + * conditional; the test body is expected as a statement or block after + * the closing parenthesis. The description for each test should be + * unique. E.g.: + * + * if_test ("something else %d %d", arg1, arg2) { + * prepare(); + * test_something_else(arg1, arg2); + * cleanup(); + * } + */ +#define if_test(...) \ + if (test__run_begin() ? \ + (test__run_end(0, TEST_LOCATION(), __VA_ARGS__), 0) : \ + (test__run_describe(TEST_LOCATION(), __VA_ARGS__), 1)) + /* * Print a test plan, should be called before any tests. If the number * of tests is not known in advance test_done() will automatically @@ -153,6 +170,9 @@ union test__tmp { extern union test__tmp test__tmp[2]; +__attribute__((format (printf, 2, 3))) +void test__run_describe(const char *, const char *, ...); + int test__run_begin(void); __attribute__((format (printf, 3, 4))) int test__run_end(int, const char *, const char *, ...); From patchwork Tue Jul 30 14:10:09 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: 13747444 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 48A3C1A0AFE for ; Tue, 30 Jul 2024 14:10:19 +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=1722348622; cv=none; b=R4k0VMmZAMO6kWeMjj4YYySKGWoFXZtio+ZbOHJLlRXJY6l9lQV+2ppVtpnXRahW2snnzXBi9Nn0kLhESACIp/tyk9t68wnLMU0vxHTXPOkw5smISP+2NyIMSxN2Qq7LU2VSTv3Xe3Cex9botmg7fYtYvBg4PlYpUgwKI5iYTb0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722348622; c=relaxed/simple; bh=VQuGozWgtncEtWDYDjKZVyjD2Uim76r9Syw/4hbxugU=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=ltKfb12LOjgf5kKaT5B+aeGs2fTwQeS2fNivyVAL0nCY6xVs8Qad8x8BrIArc5MjU3L2fQoO3d5mqfVLfMss/RNxO9NDN1226cSeWGe7i2xVJdDKrwxza1GlHL5fGaobhNTwJB6xQcYb3EB2+dB4XSs7V8igKhfd90O3j8cKjKg= 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=ggnEke4c; 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="ggnEke4c" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=web.de; s=s29768273; t=1722348610; x=1722953410; i=l.s.r@web.de; bh=dQJDQ5JWlC4O3nIB5XFWeRb4OOTCHpwa4OP76DXDJWk=; 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=ggnEke4cAzlDa4K+JYUlL4/y8LXHDOFufkfzAGzeRz9mRWcMxfmGMVYiNM5f9yHt T3+eb9N0WxP8x9lITcl1qsHpPYw0gdjKumAUkw1Q0nk92dhdLwRdWdfNvZePlQmtG QhOJkEHCki/EXYNUtHmbySSM43q7tijsEjCHcNv8YpRCZj/MGouQ0MJ65HUf2r97x 5jqHCy82aJ2H76r90yAZAYDADGRKpQsXL2Qsp7qwcKzvSLJ/Ca1mOFTagDP34KI2P pQYSsuN2TIm3M4HNIppiZrIXa6KJcHnh+l7BLtamsOi07WvnwW9YzJQG0N69bwFlC +/ArtOdXcyWA0clFsw== 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 1MjgX3-1rpGbS2dwJ-00nqiY; Tue, 30 Jul 2024 16:10:09 +0200 Message-ID: Date: Tue, 30 Jul 2024 16:10:09 +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 v4 4/6] t-ctype: use if_test From: =?utf-8?q?Ren=C3=A9_Scharfe?= To: Git List Cc: Phillip Wood , Josh Steadmon , Kyle Lippincott References: <85b6b8a9-ee5f-42ab-bcbc-49976b30ef33@web.de> <077a178e-eb30-45ff-b653-a514bfd33077@web.de> Content-Language: en-US In-Reply-To: <077a178e-eb30-45ff-b653-a514bfd33077@web.de> X-Provags-ID: V03:K1:yWwy2VDQTvGAta3GApoRhYMf+o9WIq5flNDfoOLCLk6KOtuuBiC mQ+He/6OaICEorhm61l3IBavGCNntk7tYhwefjCQfxXdU6nx+qw4mfatkVaEkQZ/CNdyATe 3NsAXhUgsLsxwVejvH8gyZCW0r4By6II1Yc/byeNLeihqdpOcQW5E+gnmwOcfYrgYwGgveA Ulu0YSzWsSfHTxtqw1r6w== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:A8H6Hs/0DaU=;qX2Vi4580uBm0DW5KzmRwzJTcWC USQT1jpSIrsKuSrMDFtgjefgA4agr9wLuqC85+rYLytSjTJfRNdpQOvZgbsIImvsREKrvbp7A WMUsEfY6PGU1ikFo0gslHCS13cCY9dEDiFuiNU7blBxA+i1aww5ZfvmEy/ctdy98GZKiQ2XPY 6mgMvTa2efKXagdmklXA5uj00uNg5k5aS8M1XaNXdZc4MIB4kNc+zRR8M4zPfUvUXDiaTm7DU LWqDSyvROJixf2JX8so5ysZGnvBMRf3wr+ZEIPp35kQdSB6siA+l4CgR7etm1/tmMP4iROyx0 hZOs5Uui9SHIB15pB1fV1MspN0KGXhws3rMUAhqbq/pacf1E4uDF8Imh+zupctYpJB/sjkMjM cFuX6j3O+LjtQlxKmf7Y3d9EQLDPX5+Cb/oXG536YlLFJdxoJ25htPzVQJNjUXqtb1Dgf+mjM fy3w5iegXmhzuqW+sANCIbRAVbGaaTOPcsxEzr+0PJjEeARmTvC/1Qdq3220L2WhVp/6GYNpg hHG5xpjmWfWTBAg5rUH+nFIYq33zEAcd+0Z4t014x2pXqMzcdE9piKJR4+eWoKDY5z4K5NgjX KcyRgyUaLIbwf13C1b1hAsM0+7C4f2p5A2TFE036z3rUk8NxxlLBSoGoIgGhJFZdO2UYu7fV7 8gWguZ14qIaZWfWRFR7oTIbiyB6VCllANETSeu/E9JptCgHLQjbLSAAxvzRxONdARb1zAyZ9F JOIWYRH/kgPF4gXzi4SfSWUP6pG7Jm0SoQI+84A+FVk6egTcBkg53tq81mnG1proxCcbHFwTv pmCM9/gCnF0wnvs6+g30jMrA== Use the documented macro if_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.46.0 diff --git a/t/unit-tests/t-ctype.c b/t/unit-tests/t-ctype.c index d6ac1fe678..e28a7f50f9 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) { \ + if_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 Tue Jul 30 14:10: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: 13747459 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 EBA261A2576 for ; Tue, 30 Jul 2024 14:11:07 +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=1722348670; cv=none; b=WDRqgJmaIk8knoZpBYC9ceBvysULBTAvHYhp0AjZegL41Ycs2OlED53pa6LF1OH95C3Xh29dODu6NxlwUQv4iQGBZhySF+zw6oC+XmQaHI91deeGgZD591NYJWvGRGmvqi8ec2vzaS1E1B7CjHvxlgoPEvmiFXIG7gmUCKkD90c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722348670; c=relaxed/simple; bh=23dLN2TBH1DrPuXJh41PXdy48UUOEO6FbQ2EHmgHigs=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=FIcr5TgonGjJqpzIW7rWYWMqRgfID4OiMMV6DUXzVVXdj9pZOQSRJMJGd/i9Dy+XI/BAwRN/bEpLg5l9eYNftI4bLT3OFhduwN7Vhe5s+q5dMmsazPVBMe7VkkxWvTzXUyrGQlYAw+kE2JyNRwLsE+3wXXMHkunkdJqc3DbT6X4= 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=py36Z8kq; 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="py36Z8kq" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=web.de; s=s29768273; t=1722348659; x=1722953459; i=l.s.r@web.de; bh=MVtYYBjWvvgr5tfMlqcJ0DIeNRGpuZdRwZT+r6vJ0bY=; 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=py36Z8kqecReh/qeAd8NaknUZvtF/mUxscqUF6MsiCtM7OyhGFF5gm0OA2ew9xmM P4MrxKZNSN/0xqnWU2eZVNWyTjLNiAppAlMy6+quLw09NpO6ZzJIUjqoVCXl3qRY+ ZRZ4jsO2G159rUTz8NtttqezmKZG5v17ymLTYPDZAanxTnun6D/0KS5Ay5y9gtekn YmLBrXuQja9g5OF3lmzsbf5OMspwb998CMESz9BgdMjiwz6FprVSqYVyBYLWVW26r zKBVP9sKCC7YxaO6IVgOuxUUIl2MqXo8XxZMvtjYozkxvg0EeC31ffceUfmEhMNvh qovJkS3+XLLKZtA4Tg== 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 1MJWoU-1so8Yl1nR2-00NpXU; Tue, 30 Jul 2024 16:10:59 +0200 Message-ID: Date: Tue, 30 Jul 2024 16:10: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 v4 5/6] t-reftable-basics: use if_test From: =?utf-8?q?Ren=C3=A9_Scharfe?= To: Git List Cc: Phillip Wood , Josh Steadmon , Kyle Lippincott References: <85b6b8a9-ee5f-42ab-bcbc-49976b30ef33@web.de> <077a178e-eb30-45ff-b653-a514bfd33077@web.de> Content-Language: en-US In-Reply-To: <077a178e-eb30-45ff-b653-a514bfd33077@web.de> X-Provags-ID: V03:K1:sD9pFQNU6tQjhZfu0ntZo36+r8N12bqSlWDgHpYbritbcv09Zib tYnxEa+YdGw5baUb8lh8UQHC32eDr0zKSBPR/NtS4c//CiCzwrCmyM+4ZVqhyyhYrcEaeqF D/sMbJvjJVgiJFvX87FsKiRZvAvecIzFr0X18qLwzXgy4DBSLdpMeGeSVeYV0sSYSoouSTm 9waWq8NU/ua57IbUAtiwQ== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:jhWsnOxJo4o=;CiRMWO0IrR/2A1B0j3WQv6ABWAV kN1Cvlz27qKami58g6NvL6pSR3BjaHDmCFqzHsPGLIdwEDRLPg6oZEPbKO5R8qA4VxUmvD6i9 091uvEtX4sfPGwE7ga4BVyyoOEscBA5HTm8g2eeIuVG2TRM0CRla8lkIwZDZ0hYi+oTyJqJI5 +Dm6s6sz5jh690Ddak5Hgt/+e9FR4cybPu3WNvZpAnGXJOGGoAgvhiGj052LS50Nn5Cn2iFBY MYJ9oiVSEG9SNJhX5b4tpkhTP6++rR9jC6anjzm3lMD6J/bgxi481GQsllNEzX4Q1aLByZdKM FPWvbv0u4Loo4FZtJsWgUlAlCUMivaORQMCUq/HvKJN2RJHkFET1bXPqC+djAhZ7x6+2FAk2+ 6ecmtL76bE7ObQshxrVDkPZi0gS6VAHhY9htfuK3rhw0UxVS+FXLiQe7PnrFS6ZOsQeCD8NL0 ksDb1PnpzbiBmIJ+KD0zNZiAVzDUdQWQkUrqnDkiRgiwjA4Cl4Z/3alvDI+HzA3857sXc0Jnd Q3wvGdp9gWki3YbEVmu3rHEvvyJK2Mk2yGwaNIkd9Cs0nFbTAYNGfLRUaRfnF0Nv+lHtKu/q1 URGQ5s4G+wWnduc98+tPut+9KNExp92wHRNuAVg/tOhCbXMzY+MJ1C2iUeq6wJmdE/Bxf07tM FaBG2HHSZ1J8I3kVeeccfAjIuWEEZq29OduuDLIp1YlpWwwrUeUFP9PVVUOz2Nsz8ccr61bea BFDRhUkWUadJmbNBc6NezS7nWJBTwCcxe4B26KLMm/qbIeX3k19TbBLS8HgMydxG8RaH/tiZy qslaDrrbQgL5EDFMwPomy1aA== 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 if_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.46.0 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..1dd60ab5f0 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, + if_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 }; + if_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)); -} + if_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); -} + if_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); + if_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); -} + if_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"); + if_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); + } + + if_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 Tue Jul 30 14:12: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: 13747460 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 842D91A08D0 for ; Tue, 30 Jul 2024 14:12:45 +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=1722348768; cv=none; b=aajixOp3Ln/f+K5K7MiQZGrxXr7YUQndJ12LuZsMV5Tth8Ze5U8ET4A7tCgMOECEPlJlb31bkgBTy9U6r9rRq+g+YgDoRx6eB7RXb/hRgpic9dVyQ+edfcbjcEY9KdwNBzulxAKfekGTsZ4W2vx9P97aeWyC/y4UQavb6421spI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722348768; c=relaxed/simple; bh=K/7RTRR48IPyVaA0xadWvcnA4/X6wrurtj/j0O4/xNg=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=szfMUTHC+pTRPrafdC3TyNlx3Jr/d8RCZ+ItcCYWABIGkyfGt5eUvj4h+ThYGcC7Z4Azht8u86rzz4eptk+t8aAGsjs5xf8rtfPMpzsOaye3BwXd2mvQWB9zp1Ln6sVCoGvo3MnoF1pEPWRrKAHohFbAwM4Q4Rv/0C9umpzVfg8= 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=aNGhpjYL; 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="aNGhpjYL" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=web.de; s=s29768273; t=1722348757; x=1722953557; i=l.s.r@web.de; bh=kltu9+m8isvVUz7CTPV00moo1ZYcPOa93v+db86W7BQ=; 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=aNGhpjYLWqcDGLRpEf01IpIk9RvjxXjZqNJZxv2Vvv5OeC4pPOiSDb9mXWLSDTkC qIKkZAp09P8+bFBnVkcq/enOsdpFIzYIMD9oOf1xXMg0dEAjTBXsBzvqfAXlcc72k vrlGf+dfF3s67L06bxLbzK0JCfVaK36T75cjZgKdUMgFnVMrUttpwLfg0k9bsNXF4 tRJyWbyuXGo4sA0LCRQN4U571tvcjVLnRYBdN+LGV7bu60yyzofEovufP39h2lMQH jDEO5kuVaWJFs2XUENJhkP+L8ssxkU8nwYDt6Mywb0umUc4KgFHKo/dAc/dxz/cl9 trRymRcIdE2bup5zOQ== 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 1M9qd5-1seYLY01Cf-007VL2; Tue, 30 Jul 2024 16:12:37 +0200 Message-ID: Date: Tue, 30 Jul 2024 16:12: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 v4 6/6] t-strvec: use if_test From: =?utf-8?q?Ren=C3=A9_Scharfe?= To: Git List Cc: Phillip Wood , Josh Steadmon , Kyle Lippincott References: <85b6b8a9-ee5f-42ab-bcbc-49976b30ef33@web.de> <077a178e-eb30-45ff-b653-a514bfd33077@web.de> Content-Language: en-US In-Reply-To: <077a178e-eb30-45ff-b653-a514bfd33077@web.de> X-Provags-ID: V03:K1:ub83y7Me/q1jAqO+i8OKmpTivGMU55oHlvyPyMd1X5MW38xuzX9 MxeYRn8KlnIgauKuuz92FNbBdUb68PIzsOYNrrvo5/+MaZoloiBNRB6ZhZeEYnAl7ic6lt0 nJNN0pdLtJZ/u1Kz6w3A9aorzvhLivI3C90YP/O/5ER5KUzbf/Qr62Jxs0PJPLsWt9uBSLO 1EQc+hkjvor8I9YoYV+XQ== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:l9uP3JxXtpI=;C34ugWa3m1znCfvKggrx3wfsHgf r5hqqsBDyG5Y48n1sBFQhEcosVB06IAXA0n8ntLiNUFoSptEvFssIGdSPEz5FkIAlbNhvWmP7 NANUCpTXQQPaA+ttv++99UaaQxflLkyqDfK3FHSRiqq2Nh8b5JkqRmiSu90cK8R8yGswCnrQ8 q1u6zTPBhOqT1s66a8h3TuPckRGfXfo3SZ0IkeIJhVnPIR/EdUzAHTCS2uibNXbT9xWvyuzrx +BErMqYOw4Jf990u1WLD9KbN39+jz0xzELwlkKyyLLm9PjCuGihSWwPBUqG76rmKYamxFGm/A La8YzywbomstC47fWV23B04elTI9mTNaHj3AbR2xrUIbMA5eJ2s1h1F9palQXTAuGmS9vjrjy mfH1lDUPaLcWL4vTgtC43CVkiathI9lEVjWVtpV1vBGYX22QpCYuyLJpgNO5k9en6+/tNVlWM SIvlkUtIwp/qVGCbSgH9CUd4txToorFNnLra7kryI7tOBLDbbgl/pky7JzhX9R1+Lbdk2m7is TxBe9MgWfg8KAw84wr4FNbXDCCcl2UB/qSNZaUUmDreqyzb7T2VaBebOvth16oLIS+jjnhLlv uK38DUv1TTu3AdiJoGSWyTxv9HqDR+5Ayat3mouKMDNIejdEmpH2UZnDAupvYW10boHOpjGtQ xx87ruJhahGvcO5TCRXjwUWiLZpMnOdF1vmbGnc8+RGtNXkWfZ+QNDGmunrDS1kJiNBYUCwSp Wxn3Knz+gxSHQTW6ZM3uxSdeNSzD5c27TfKzzck7LCK1WRtRd8TtEu+DJsrQuLbfiSGG9AX48 0Bo8vMqiTdwnCwymBMNR6GmQ== 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 if_test instead. Signed-off-by: René Scharfe --- t/unit-tests/t-strvec.c | 356 ++++++++++++++++++---------------------- 1 file changed, 156 insertions(+), 200 deletions(-) -- 2.46.0 diff --git a/t/unit-tests/t-strvec.c b/t/unit-tests/t-strvec.c index d4615ab06d..557e9c835c 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); -} + if_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); -} + if_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); -} + if_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; + if_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); -} + if_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); -} + if_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; + if_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); -} + if_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); -} + if_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); -} + if_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); -} + if_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); -} + if_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); -} + if_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); -} + if_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); -} + if_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); -} + if_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); -} + if_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); -} + if_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); -} + if_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); -} + if_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); -} + if_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; + if_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(); }