From patchwork Sat Jun 29 15:35:31 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: 13716958 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 5CFA11DFE8 for ; Sat, 29 Jun 2024 15:35:39 +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=1719675342; cv=none; b=uAfj4NzoofFcvguEHfqbubYmTj7SW3Js5MCAe1o96XwhIMh/OHsw2BbVoA8mh2CjtsFOFrUV3qr2qLA5xMtS1bwOXiEjmsBKY7HZT4pNyek1l633RU5p0pyDfoVhGt4Pw7KrG+c9qHpM1yCaCk0OASYXc042VJGEXaMZ7TGc3vo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719675342; c=relaxed/simple; bh=YyK3jzp7ErpZe2/uMplGBhSUT2DtitcUTovgN0Iyat4=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=a7SBH582NP8StDSU5msVK8vTfQWTGlUvphST6JveHifyaGe4kbBZEpnHeaT1MB/dKrLDqMKezc+kymVdDFlVaULG0fMpw9jRCWv8KpxyQLyWFCdlcRKQBqWWyQTbw2urZQRObU0TtDZP6rqEcvEb9mkvaez/WAlHGsGOYMXOyjs= 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=iJW62I1R; 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="iJW62I1R" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=web.de; s=s29768273; t=1719675332; x=1720280132; 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=iJW62I1R1luOc1HSnuVzdx6JHU35hSnGvQhbAdPlXUjXHZWfPHrp9/Qu+WhAT6cA WAUf3rUfjN3SfTrwh6FrmFXboySf0YFzenyKc4EhMf5Txm5DX02naqgIkhIhMjWPI bZumq9W8fwlGW4fRHwCvBLHXyofS/34JsH9gaok/2EsvmApo6mAGrzTnjbD96mChT C4C7oriPEkaUwCor7AtQCsBzcWI3gWVufXeEHIEESK/t64GqWVwNbdgMpvgmiAuqR 3egPKloN8fBJAJmV+sNzcf96fdIiYyxCAakYMtesuO93LtNPV0+zoJzNCLGYn1g6x jZ2UJUg/MKMk89YJ1w== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Received: from [192.168.178.29] ([79.203.19.151]) by smtp.web.de (mrweb106 [213.165.67.124]) with ESMTPSA (Nemesis) id 1N4vRm-1sG3cW1s4S-00uSE1; Sat, 29 Jun 2024 17:35:32 +0200 Message-ID: Date: Sat, 29 Jun 2024 17:35:31 +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 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 References: <85b6b8a9-ee5f-42ab-bcbc-49976b30ef33@web.de> Content-Language: en-US In-Reply-To: <85b6b8a9-ee5f-42ab-bcbc-49976b30ef33@web.de> X-Provags-ID: V03:K1:IySmVKi/O39Le73uMjy7cdg0Xq437iptjlJNFl9kDHkLZhBjIzI dDQmQJXcVjo/NlIZCv4qU6q/pnKlFq5G0iirFZgXEJa31fiT2NiEDQi0OMr23FMVbBf6gI7 2mhq13oK+2CqJAATnO780+Uk7ETnquRRL799kYgG5X1/jH+KfChKxb58ADrHxNcHyTL6+kQ bnEFuKzRnlQbnbA1RC+gw== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:QIe9S8Ddtp4=;1O117hPdEIZlM0Q3XzAre/Saglx +07lfIi8M4fr3bn9b9Nd2Lyt7TrAdI3wiYgJ0m3FlRdcvlidG0foxhH1PfX4y1+KsX387uyIq 7CcmrejjDHVvgvqpZZymJyKLGbVQL9mJTM4kue/8C2N2YowBGIyuf6ciKv9Y5IaJLXzFohKxW 4MYXdUMnNE2JZ8DhJr+8CO/gUcxSsgrKJamU3sOYwkSe07eUBihvw/G8gICwfYG1M9JTBppr2 B/FpVQxliXZlz5q99+fJyKrg7VbBvpp3uwulK+M4047ivt2EI1PUPcvHgXfhq3HL6I8IsFgN8 YsYn+m9OvITBargikmXUOGwLW3pIxG49GJF711umjqjcQLACJ8YdIXMjq/Qk0knB5tnJKKklr f+LvZKWFzI5HZQU0p3TZfSiCwD/2fNwELnCH3UR+RdaXyt1iIJT6MGqfofbVdcBlU+m9rB6hW kx6Kapi7O2qtgHdmoHKg7L/18CqV4SpHGPpZ/Wtb3I5HEtEXUxpiM+tfMh8RWEfYb6SFZt6K4 uxfsUY4upybwB3H81xieKeCXoZh0KhAoTliEPx8VpXDqiSTW+K05BoMdlhC/Zdy1OvC5Eb8EN zNfmeEGe8TAzGOm3GL01FmmfLMIPv3KS8vmm4s/2SAPMljz9lxLB+UninAaAuFfhnjkFLoXk5 THkotXDItzJgGNb7fE+/5NpLkPjEh4YcrHqdwazL1veMpzyqGi9H9AL+wsVZEekfVIYVvpPQn Z6JNd24WAvguVvuMOU6/vnixi8jjhvhL+rZgFU2j7Sz4C+9tOi4RQztuPdhIneexiOYPoOdJ9 0lcamnE89u93oCwY701bfg5aWW7O9Ym7JshAm8u3/+GuM= 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 Sat Jun 29 15:43:30 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: 13716959 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 87851548E1 for ; Sat, 29 Jun 2024 15:43:39 +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=1719675822; cv=none; b=ZrwveBDxZqiFPO1d/p0kW28nqJz5quR0hH/lG2kPODlhEaPZ7hfx3o6E2rZmkvIZSixuLQn096yzOCbjUvWjc2fn+y9og9shA89ePHa/C0+FNYiHgPnuDDr06kV7eWTx1jnqkcirEkzWw6ZkKFl1cITB47zvkIL6IgEgiSLQo0I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719675822; c=relaxed/simple; bh=YagDGeAKeaNi6n/5iUNHxp9D3X0jc6/m3qGlN9kzldc=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=AXgRbRceYoN0YayvpRIVyHZZWRKaNB/F+5T5kcFfuXMw478zO83CeJx/LLPU12ipMhI0a5hYm9gt9tGfsoJ0RfNnPWZf9AwOjTBbMfpPisS1YH+ySHreOi4BVQOznYYIYYZ284rxBMUgh5Aa3AmshlIlHGzx4fcfpEFYssvt25Y= 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=TlUsCvmg; 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="TlUsCvmg" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=web.de; s=s29768273; t=1719675811; x=1720280611; i=l.s.r@web.de; bh=5UTXkzhLz+SAaZ89rV8LL96uMktc4A5U3RQRvZ0uXGM=; 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=TlUsCvmgd7j2bOVNKjpNyq2bU8lbgW9FyNCN3wLS4knZ6CHhgxeZ/JkieGLuk9CV NIY2mde8MheIpeg9WN6QPO0hqqFt+8wZvc/0c0ds8fcpiVm70SCvnIM0+Lois0VsW fbi+xMo+7ycsFb/fpSII1XkSdZ2KIO7TRaggYTa+7yAHRYpq0ekgSnIhcMyLbPiFc Os9vBGMOjThCctyf3rLT6Gxvmnepsn3jqzj3aydLGqi5ER0ifplem1PNMy0i1OYHB 0666fhZIARgASthf59/C4FpFOunWJ5kultwHXCq4kGO2qI4n38hgMFLwCD13oYAAF e5VtWvnf0WgTqZeYPg== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Received: from [192.168.178.29] ([79.203.19.151]) by smtp.web.de (mrweb106 [213.165.67.124]) with ESMTPSA (Nemesis) id 1MzkOR-1sAsi212O1-016KIQ; Sat, 29 Jun 2024 17:43:31 +0200 Message-ID: <8175f239-8d4e-49f7-ae0d-dba7df8c365d@web.de> Date: Sat, 29 Jun 2024 17:43:30 +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 2/6] unit-tests: add TEST_RUN From: =?utf-8?q?Ren=C3=A9_Scharfe?= To: Git List Cc: Phillip Wood , Josh Steadmon References: <85b6b8a9-ee5f-42ab-bcbc-49976b30ef33@web.de> Content-Language: en-US In-Reply-To: <85b6b8a9-ee5f-42ab-bcbc-49976b30ef33@web.de> X-Provags-ID: V03:K1:Fyvu8Y4w7h+gSXZ8rOJ/xIeAIpmWb6oIMgb2fU78LAjJGq59Wx/ 7cDCTBBchyXFzSPmDHI9l2K2q8H5SBinZeM5qzExJmpfHrsoOOysBRw1W9F4QrXSVKdfr0i tr4q3PoPRzXrjyODKsp9WixPFHhwk4njrUz3fJFdOEP7lGpdL40gnv9W0TrssXrTd5+L8e8 sL53IZp0HiuAy/oknl1Og== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:99vhO0vwV3s=;A5jtXjVIWg1ttl5kO9gDuK1iHGw TyVkYYhUOLSwr50PQOcI6kXV3rAYFc+bztyUCoKrd0AuZ1IWeSHK2+FEdslrLQrqFnlP/qHPA Vwp+Djo5EIas8FWfcYgKL94l27MzIZo0Psj9gf/quSuqCG7AeLd1puUt0Ad5n6q5KU+nGEsdA SnWVZKkEGmlzyWHt4MMKJqMHVLrZjZpV8Pu62lPaIXcjqfy95jhwPWBKTWsebqmWu03Dwd0Wy Ci5RPy2YPggFX7w00ETaJfywzGBK/v8upnP5/hAz7eruEoR0b4aZAWbSRPA4A8l/MGIWHkk/z Tsl1Mna2bgawnfHSwguqg7Gkih4l7E5po1/Zt7fmtnR7Wh+WQD+V4eeFGKBrdG4ZwwaWTwlwS 1Wp7pmdaMg6dPLYp4GqaFXW6kZM3yCzl99sNuY4NKAeydt/z/Fy3Vg4klkL1c2XvwIUReeu8W q7jQXZcODLG6N/PgFf+tIN/xhITfVi/6IF4e8W145MbKJJBEph1o4QC/SMrjArTgUh6TvnuEc Lbgqws0xL+DyVWMQutnaO24WHng/Y6wH8unIQcktWu2wCUIMoQe6FngPxmvZyDvkRrHByR3tz JgnjJpAPr06QPOAiHp2tVcYd+xTdSwdlHJ2IElZmKW3FLPFt/Fr8IBjJmN2NlBMPYrDItVJp/ PLE6JPYEyDrSV7fBJ1wD0ODkO8ZQmtF6iHWcnA1VQ3Zu9BJ4wFrbSv9VY2DHd3dL4/4s3GP/K g897xeXsMsG76Eht25JGbAJu+eODn9q7wO9YVWMyo3sUq4azdWv0IL1YIPvq8Vz+ooc4aT+m7 Libm8iROb86HSn4e6j9yUsIs1j5P34ZrzNZpg1B7je1dc= The macro TEST only allows defining a test that consists of a single expression. Add the new macro, TEST_RUN, which provides a way to define unit tests that are made up of one or more statements. A test started with it implicitly ends when the next test is started or test_done() is called. TEST_RUN allows defining self-contained tests en bloc, a bit like test_expect_success does for regular tests. Unlike TEST it does not require defining wrapper functions for test statements. No public method is provided for ending a test explicitly, yet; let's see if we'll ever need one. Signed-off-by: René Scharfe --- t/helper/test-example-tap.c | 33 +++++++++++++++++++++++++++++ t/t0080/expect | 35 ++++++++++++++++++++++++++++++- t/unit-tests/test-lib.c | 42 +++++++++++++++++++++++++++++++++++-- t/unit-tests/test-lib.h | 8 +++++++ 4 files changed, 115 insertions(+), 3 deletions(-) -- 2.45.2 diff --git a/t/helper/test-example-tap.c b/t/helper/test-example-tap.c index d072ad559f..7b02177a9f 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"); + if (TEST_RUN("TEST_RUN passing test")) + check_int(1, ==, 1); + if (TEST_RUN("TEST_RUN failing test")) + check_int(1, ==, 2); + if (TEST_RUN("TEST_RUN passing TEST_TODO()")) + TEST_TODO(check(0)); + if (TEST_RUN("TEST_RUN failing TEST_TODO()")) + TEST_TODO(check(1)); + if (TEST_RUN("TEST_RUN test_skip()")) { + check(0); + test_skip("missing prerequisite"); + check(1); + } + if (TEST_RUN("TEST_RUN test_skip() inside TEST_TODO()")) + TEST_TODO((test_skip("missing prerequisite"), 1)); + if (TEST_RUN("TEST_RUN TEST_TODO() after failing check")) { + check(0); + TEST_TODO(check(0)); + } + if (TEST_RUN("TEST_RUN failing check after TEST_TODO()")) { + check(1); + TEST_TODO(check(0)); + check(0); + } + if (TEST_RUN("TEST_RUN messages from failing string and char comparison")) { + check_str("\thello\\", "there\"\n"); + check_str("NULL", NULL); + check_char('a', ==, '\n'); + check_char('\\', ==, '\''); + } + if (TEST_RUN("TEST_RUN test with no checks")) + ; /* nothing */ + return test_done(); } diff --git a/t/t0080/expect b/t/t0080/expect index 0cfa0dc6d8..92526e1b06 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 - TEST_RUN passing test +# check "1 == 2" failed at t/helper/test-example-tap.c:98 +# left: 1 +# right: 2 +not ok 21 - TEST_RUN failing test +not ok 22 - TEST_RUN passing TEST_TODO() # TODO +# todo check 'check(1)' succeeded at t/helper/test-example-tap.c:102 +not ok 23 - TEST_RUN 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 - TEST_RUN test_skip() # SKIP +# skipping test - missing prerequisite +ok 25 - TEST_RUN test_skip() inside TEST_TODO() # SKIP +# check "0" failed at t/helper/test-example-tap.c:111 +not ok 26 - TEST_RUN TEST_TODO() after failing check +# check "0" failed at t/helper/test-example-tap.c:117 +not ok 27 - TEST_RUN 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 - TEST_RUN messages from failing string and char comparison +# BUG: test has no checks at t/helper/test-example-tap.c:125 +not ok 29 - TEST_RUN test with no checks +1..29 diff --git a/t/unit-tests/test-lib.c b/t/unit-tests/test-lib.c index 3c513ce59a..fc50755fae 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 *desc; + char *location; } ctx = { .lazy_plan = 1, .result = RESULT_NONE, @@ -123,9 +125,45 @@ void test_plan(int count) ctx.lazy_plan = 0; } -int test_done(void) +static void test__run_maybe_end(void) { + if (ctx.running) { + assert(ctx.location); + assert(ctx.desc); + test__run_end(0, ctx.location, "%s", ctx.desc); + FREE_AND_NULL(ctx.location); + FREE_AND_NULL(ctx.desc); + } assert(!ctx.running); + assert(!ctx.location); + assert(!ctx.desc); +} + +int test__run(const char *location, const char *format, ...) +{ + va_list ap; + char *desc; + + test__run_maybe_end(); + + va_start(ap, format); + desc = xstrvfmt(format, ap); + va_end(ap); + + if (test__run_begin()) { + test__run_end(1, location, "%s", desc); + free(desc); + return 0; + } else { + ctx.location = xstrdup(location); + ctx.desc = desc; + return 1; + } +} + +int test_done(void) +{ + test__run_maybe_end(); if (ctx.lazy_plan) test_plan(ctx.count); @@ -169,7 +207,7 @@ void test_skip_all(const char *format, ...) int test__run_begin(void) { - assert(!ctx.running); + test__run_maybe_end(); ctx.count++; ctx.result = RESULT_NONE; diff --git a/t/unit-tests/test-lib.h b/t/unit-tests/test-lib.h index 2de6d715d5..6df40e3b12 100644 --- a/t/unit-tests/test-lib.h +++ b/t/unit-tests/test-lib.h @@ -21,6 +21,13 @@ */ void test_plan(int count); +/* + * Start a test, returns 1 if the test was actually started or 0 if it + * was skipped. The test ends when the next test starts or test_done() + * is called. + */ +#define TEST_RUN(...) test__run(TEST_LOCATION(), __VA_ARGS__) + /* * test_done() must be called at the end of main(). It will print the * plan if plan() was not called at the beginning of the test program @@ -156,6 +163,7 @@ extern union test__tmp test__tmp[2]; int test__run_begin(void); __attribute__((format (printf, 3, 4))) int test__run_end(int, const char *, const char *, ...); +int test__run(const char *location, const char *format, ...); void test__todo_begin(void); int test__todo_end(const char *, const char *, int); From patchwork Sat Jun 29 15:44:18 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: 13716960 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 4EDA184FAD for ; Sat, 29 Jun 2024 15:44:26 +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=1719675869; cv=none; b=HQXgrxmL6Ns6L+BI7tdmXDIU0FF9+sUh70wERaUjJq+xsYtvdm4dODbUPgkM3o3qS/XHjySYPsMd5Iim3ZKM5YxtpodZAP6s//3tcWzGbE/kT1BidOWPJmqXDNRwIXp31hkE0hHaPpOzXOveuUYAAq7ij1JOGwRxNpftNDhExfI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719675869; c=relaxed/simple; bh=iqx0pCWgACmPsxQ7vLGynXXixnAjOjYS4ISm4xml0Jg=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=aMDFGKOcsDQVxrI++p6WSglc5GJaIzMeyzMikowMe4gavVD/4bPDuO38M1JSSTLeGXcU/6DU3CZZV227n5ISUU1WtMvCa6H7nnGHj90WvKZjHCa9IWD77V79SLhdOthYpkWKKc0uBHVupazxl6D5ZWMysXgnY25yZlZxWYmsZTE= 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=c/EYdvhE; 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="c/EYdvhE" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=web.de; s=s29768273; t=1719675859; x=1720280659; i=l.s.r@web.de; bh=gaGP69e46uktmhFyFF30ONan6Siv+QKziM27MOkx05I=; 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=c/EYdvhEzXkxC3So70UN8tuR/g0IadG+uCVBg+Mm8Rym5QuUXSho6+zd7h9diMMR Zeh4KA6ySHAk+ieSMtNqJzhGOMeFnisjgX6WlPYwgogcjrJpWiWLkdQdCqQVqk8iP FXl52VhEdfHQzIxGhcuuBdsOFOPwd+SopDqatN4xGgdVrhimmxn1V+n+XGWWK01IT TADwxpgPfNYb2HUJnK1hVT3YtGgt+G8zsYymL6YC6Xn6qgfDqTGS0z3Gee0b76bqB UKWfAh2FWLMnbZ9Laer0CkVQnZTNc1Hmuv0SW0LvTOG1rDOpQ807UUDy7uu5Ivbzx nnBxmISeZIDvN2G/TA== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Received: from [192.168.178.29] ([79.203.19.151]) by smtp.web.de (mrweb106 [213.165.67.124]) with ESMTPSA (Nemesis) id 1N79RG-1sMfSp0lTw-00wfAa; Sat, 29 Jun 2024 17:44:19 +0200 Message-ID: Date: Sat, 29 Jun 2024 17:44:18 +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 3/6] t-ctype: use TEST_RUN From: =?utf-8?q?Ren=C3=A9_Scharfe?= To: Git List Cc: Phillip Wood , Josh Steadmon References: <85b6b8a9-ee5f-42ab-bcbc-49976b30ef33@web.de> Content-Language: en-US In-Reply-To: <85b6b8a9-ee5f-42ab-bcbc-49976b30ef33@web.de> X-Provags-ID: V03:K1:V42wwa8gA90Eaui3oJ1UMupleU+WcM+JpoisQdLRcwCA35gvHig EF0Iv4FxzaDf2QXpGlqUwbqSnxDmaGpVdwNrN1hMVkZiYWJXHtOJ0Uy9lVJ2J4YS0nwtSwC pE/2vqCRrZRgds8uZOfVQ3jbEgPOzsmf7KDw+M3jiRsk2WnM1GyC6D9s7LHiGE/ql7eVoPJ c26+4w0PIvLO8iuY22irg== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:Z/rvNYLlkK4=;Hl19SwDNswem9Paq6K0nNy/jwHu 6dk3pFSjJaDbhCGij/OR7Sq6sqyPZk77OT6bW1vtGZYKsUFLwVAqdJq2nlsZU3uWwcZtxHauM 8ymxzz6JFZOdfW6s0lPPCRtpL8olPeZX2rNgl9Z5wx5QV4BnCBzTDoEDqGsGVYLcSxb+RA+0d GMtaNMbd/XjLCpbqnWDkGMO1zeVa5FdsZnLcgoE5fPXQlCwOGY4KPgPRNa1JlHtn8GoAN+cmQ Cpb1fWHesfnpjVb25dUHF+NK7JcCpRSO6f301SZC0UFlzPpt/QgSUq+d0sJ1Pr+qn4p0tBO5Y BkZKhsHyEct9iLOGzn2S0A/2FlohyGu/VFfQCGQxxYnSXUS6i0Hg8jQYYd+9LnT0llyZZan+e xcR1BS3T8mgqMvDz5HivlgLD0J1Q5iwjQR4xnchu6TnhcTk+OPs9aJr9XLWJV+GRUfbufu2IZ HP0baO6z27rf7XKARjrY2koz3p2v4Ic1rIGupNy8+XciHI7r8wCvTEVMcST68dEvgLIquxBwf h6V1OlLtT4RAGqV+Et3/RE+9Vn1nEcwloj8ESHZ7s3X8gjKMAEJFIWiaqYppyYhEHebmoSeTD HLInz1XuACsaELJyJ80R7lfe44On9cDKeLZ/US6pX+k6z6td0zBFnb5zbBuMuiKNLHXZHLaAN DwQLxKzrDSlIQ6FrGd5Gt6w2Jg73cLbXQmAMeqddRN59kHM3UltRBLFNrGgytjN8RLEbIWkjI h+XCkC5yStoH5iymtNlwB2aiUMlK+EBsraowJIPWjn04BXiiYo3mM0JfU/Cik8n92DVAB27pg avB7mpEE5DChx1sQ6HE0XhDArO0/LCicJKtRJHagkbEzs= Use the macro TEST_RUN instead of the internal functions test__run_begin() and test__run_end(). 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..04a66dc105 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_RUN(#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 Sat Jun 29 15:45:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ren=C3=A9_Scharfe?= X-Patchwork-Id: 13716961 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 6099A4C99 for ; Sat, 29 Jun 2024 15:46:03 +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=1719675967; cv=none; b=G/+ZjRIo27D8VnuH8xAbXtiiGTfBtWRmT4tBcit3jV+rN1mjql6xVnerKvDbgyHg5crrYplpETAf1fw5tX+uC5+agShOqnxIWe7eACLtiKjttodLrKKNAxlhYULTuAqumu0ObLhcrT/eN1ENjF1WLK7kK70MHzI9ZB8tpSHcr1Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719675967; c=relaxed/simple; bh=rDPD7D7Zh9lnPSvgNjKMiDIMFezZXO7J6Hh6Wi6yDmc=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=GmHBtteWMLZQPYT7U2wmVmzD+8IyGolSPio7HjPZ6pwlt8S3PBneHlq2Bh4h2sV0vQyw7ZEFfjTp75GTz8vhEsksxcHK39bBjP9TRoZh6PYjKy1LmnUZ0cVqF+Ryk07uiQLw4CoeJZnNT4F8qDZns5u/hSWBQVKgIOyUmEhPaTc= 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=pJhhr18I; 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="pJhhr18I" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=web.de; s=s29768273; t=1719675956; x=1720280756; i=l.s.r@web.de; bh=AWTi6rLJAjnB2ZvDPtVywAj7JaK5Y2DAOO16XOlv09w=; 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=pJhhr18Is8TVe8eqyGWSvWC5MogT1VENz4jXLWKLb7vzD/ZMC85YU7Sco+lTuQnO IdmcpxQPgS45onWwzCQYsSPOWnZ3lfhx2+C6V04Sp5JHC13zS0S40PXbx/abkly6L /Qzo7WxrgRqjcCk5/nrnw7gOogKKBP4/wSG/PtUOFNb/4mw39BRTBs1XsjLV7Gq2q KUTbDVMqUEPvK92QrwIpLaeehj9xmAsX3yr7FiyNc/49pT6QsCMyxYL4ooGmrKRzu KLOClnjDg1IFu82uq8WBDmnRsAJHcKcg0xsB6PHCrIBFhYlpJ/+iTVbZTfaXeLzwX O9oJgxE/dIB93QzcvA== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Received: from [192.168.178.29] ([79.203.19.151]) by smtp.web.de (mrweb106 [213.165.67.124]) with ESMTPSA (Nemesis) id 1Mm9Zi-1rxHzo24Aq-00bgUW; Sat, 29 Jun 2024 17:45:56 +0200 Message-ID: <16223dec-c117-4f8f-958f-bfc407882b4b@web.de> Date: Sat, 29 Jun 2024 17:45:56 +0200 Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: [PATCH 4/6] t-reftable-basics: use TEST_RUN From: =?utf-8?q?Ren=C3=A9_Scharfe?= To: Git List Cc: Phillip Wood , Josh Steadmon References: <85b6b8a9-ee5f-42ab-bcbc-49976b30ef33@web.de> Content-Language: en-US In-Reply-To: <85b6b8a9-ee5f-42ab-bcbc-49976b30ef33@web.de> X-Provags-ID: V03:K1:mSSRVcEsH8ntGYGRtJU3AGOctxnYRCBkFsSQa3mGLatGFSPrLGa ffleUfjgfCAFmyiZMbLUKYa2VKSxkPOkJd0hiRYLdBdXj8BGu0zZpCBBKEKs9hzLIt9SBzZ NHshpH8IweEx2HRD9+nnePZvKe3kjcMKP3xJuZ2uOq100RnawI5RcpnE5iYb48vaEcku6uT Cb7DnmYLeX4tiBcbfjyTg== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:NPErNEtb4m4=;YfTm/2YfGcdwAJjQsEylF7uUjwU kLmL9Ylx/k+aM018EU2kSQrGHBsg4bjrWywH8jqJ6zyM8EwEQksJ/QNTSAq4/Wf1fBu+cAAt/ 70mI/fzoy9sEZuDaye+Vfnr+mEDendasfxodOKPICd0kylXLXQZD7ZxjdRIcyjxCZ+HUofR2w PZZ406vtmwM23QiisdyeZram6ezqHod9UulfF2ETzr6IihumFT+QpA6UsYFwasLRLCRmoR05Z NkHXZNYhJ5hVSRx9Xhb+fvvNXZRfgIXLTEZXTvhfmFan5syN090CYXE7mxi/+8nYdgp861LuE hoTao0cjMnjHJDPVvixyL3OYLZrWDkU6m3tNuddiGCiTqaNs49U4DwYD8flv94YKZ7KA8ZwzV o3vdpk63ITAGgKe5DHiv8uRCIlSriZG6qW0WRxO0RRZXNh6Rjmtje5qn0lD4kpT9/AsFwblM5 UKlevyZsICOxuvrmanizWo7nybP1vZTVgihtlxlMvYFfzJ7Ux9EgWaRxvED8nxbvaOdl/zR5T lcRdlCO8zCzLLEdXlvFpoY907km9sLxvW/rstdr4yJqtA0PH2MyLOyyNqpfidN7QGInS7f2kx GiCQr8vq76iBPds1Xv6k+O+z6hRanzWQtlxWVNh2qdwmVgDJ5hvqg3P/wBBzJKNrnEGoimdU6 S8GG5zBmPRkWECdzpXXCaABxuCN+IX0N9Kf13NRo/m6CWjdJ2CEFdclMu/zMFbNPuaRvVg5y0 TJQjIQiZ7LNHcIN5/2+GNGJqR49WF3UJi+R/gaSrnxE8PbB1xLyMqfHIIqOxJhUFi5H0/o476 duicSBX062qyZEWqxDiVOaiYu17cigZYU2jYl203y0mQE= 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 TEST_RUN 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..e6e789e741 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_RUN("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_RUN("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_RUN("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_RUN("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_RUN("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_RUN("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_RUN("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_RUN("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 Sat Jun 29 15:46: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: 13716962 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 B53BD131E4B for ; Sat, 29 Jun 2024 15:46:57 +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=1719676020; cv=none; b=LWsqLoQN5/uqoCA509//u0i5N5XSTBToIdYsYd/uimpmgeK4V2dkGMb9NpmN16TSP0519Y+AZQxESFjDrtrr9N4l2dfzSh8YVfUsddCOK2U3KfPOp16x34hi8B2Ttui0c6ZSFM222FmdZkwjX0e/j0qEWl2u8sAoWTqXgx7x3s0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719676020; c=relaxed/simple; bh=D4mang5fnCrNO2LM0qspuTpZ7p/Ex3GMVFJHGjk0XQ4=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=hmFE1cT/g8lnBHgrNzIy8dLAmDGIqYPV04Cmt3yL2u0RXAXTT+dM4pZ7Xv8yf44cpTQ2Oc8FIriNw8SfFX7bh0Xece9Avpx1/vgjmqF0ePcXZ79AF+o0SI8QWiR5freMNbt05uAfe45orH7jpeyi/91QEkVA4wLHtwQN6PQJK+E= 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=Y33IhuAG; 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="Y33IhuAG" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=web.de; s=s29768273; t=1719676009; x=1720280809; i=l.s.r@web.de; bh=ViSyAcXAEqyUcuXfZuqO4BsXPfAN4tHNRoZrz17EUd4=; 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=Y33IhuAGxOMeV8PrTy3cuK1jXhjPkH3ku9mEv8euaYUVDaRDCP9QqoULO0e6lgts PPAL7NnS1jo4yt8QW2t7wyhyd0ObkQ8sG6saq6exEL02rKnG0AeREle8R8heoIkDg uEUuLExi8RFGXTXJJ+3Fk47QH7F7ePZJf9qAsCDUTwzjTqfhiKzjutnu30p/5KN78 nK+29BG+UMSXgPELndq6AmSaflTZOhs+ALGIDlJeytqSYeMAuAv4Dx+PYZfgHCZ0M Dm1BUA4KIivzfYQ4wFsIuHT2Wm/WqAbBP/qKKPeuzxZASFvqlW4noW1fpGp05Mbeg vpJ3BIn3l6iYDvVN5g== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Received: from [192.168.178.29] ([79.203.19.151]) by smtp.web.de (mrweb105 [213.165.67.124]) with ESMTPSA (Nemesis) id 1MnFyI-1ryNwK3sG4-00gxs5; Sat, 29 Jun 2024 17:46:48 +0200 Message-ID: <27cdab5f-15b3-45fb-89e1-530a6c0817a6@web.de> Date: Sat, 29 Jun 2024 17:46: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 5/6] t-strvec: use TEST_RUN From: =?utf-8?q?Ren=C3=A9_Scharfe?= To: Git List Cc: Phillip Wood , Josh Steadmon References: <85b6b8a9-ee5f-42ab-bcbc-49976b30ef33@web.de> Content-Language: en-US In-Reply-To: <85b6b8a9-ee5f-42ab-bcbc-49976b30ef33@web.de> X-Provags-ID: V03:K1:uTDR4orKkWstzlQjqPW7uW5fmxF88zNlonaB0fRfNGnLrZops3I N3/gnTIXNqx8Z12MT3/wwWPVBA++R97wnF96wLFkxXIX7R61ZuJ8NW7+PkaAoSW5SsG5Gef V+sI5cHRw0ilmdX5lJporhnTv45zyxGOF+92C1N7XinRPs/JWOFOb39U0ECTcp/QlSBL8Fk kj4wCCp/lf8jcz06zSEcg== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:+idnqPVG+Pw=;dJADAhJlAvp9qTr1rLBYqF1iDKE SVS6w3D/03WJIfNaMbj2AhmkD8OHHzvPvguoaJZGwmHPYRpMhsSBiRFHMO1yqQRHVc2+r+Grq aWd+53wp7ijj+ISYoU6s5FB3TAFAwuEmvATRiWvaNbBFs+RYlJsy/vMEoxYkhtRRrTn15ZKBo VhkPXzBbM//x8F/iRbtYeLAXpi5pP2hMvr3ZqRnhGBc/Au21hwN86oribLQWYxmKrFOSlQyF2 U6FJZgZ8Ip8Pft4BXTuhR+/g8iWVwkgBGGd1DC50QrVCjysD8KOUey/UZ+hvnvEzmrzgOAeyd flBr3KaRwkbTLeip2FMBxRJAog7B6J7ESENaHGELdwJ4OnLDKBtq+RXsblqVW1fdjcZgobxZI 2C1lSJQ7mSEEE97boPKLbbp0rHAYqVAbk+BjfUrqOwtDtZyhiMVShLXmxszK6+Zj+viBSCBdc vmPfDzkxm6rNQLdxvEmuUj4P+dQfS6KXs8RRrJgymXdNubU+apM9axm0E2JIu595t7ixeyUzz 45vmjgUQDTdXWYu+lRJEzsl/blsymX1oI7HOjAuR5a3sHUEMx16PfWM5D5KF1UmiV03Knu2F5 Ysni+/LfZ7TdNCkfNYz6zinm/MubtpWoVJFoi8APtVqQy9XBhAaU/u19PFRR0cIQEoBVxu0Tc 4DnUCsUmvT4L7Jo09/UFseG2+NkZVUs6PanZIly5vtS7ZtNGS5aepvh8rf2tzOC6DxIPbgw1x E656Caj5hBrAmknDJzoL0/BNpBdlIEqiTaJMji6JXlqjvsmB+K/JD/VtPaN8QyHD5y1y9m4QQ z+MlHVArUZGiSGqOrapvgPMIusJinDElTNOi4ZDumFji0= 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 TEST_RUN 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..00ff7d4ae8 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_RUN("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_RUN("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_RUN("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_RUN("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_RUN("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_RUN("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_RUN("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_RUN("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_RUN("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_RUN("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_RUN("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_RUN("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_RUN("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_RUN("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_RUN("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_RUN("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_RUN("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_RUN("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_RUN("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_RUN("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_RUN("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_RUN("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 Sat Jun 29 15:47:34 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: 13716963 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 0D38E78C88 for ; Sat, 29 Jun 2024 15:47:42 +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=1719676065; cv=none; b=E+tgFIfb6AugzltnXoOPQcqCJ1P8MCiGIfte3b+B/bY3YKrdHZ5c2kHOlAmeGrBduq/sXMoKnIYsJmz6blDx5WA8f8pw/U8UAliCihxxzyCTZPpHYcN7hZItZ40cA/0D8WPln4cKGgFJWyOVXyH4XghSMz1BzJnlfds0ZBDM3+U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719676065; c=relaxed/simple; bh=Dq0699fMSmt4XV5KWLE+PRNPz7CS0bWvg40BI6GncCk=; h=Message-ID:Date:MIME-Version:Subject:From:To:Cc:References: In-Reply-To:Content-Type; b=pYq41TVVsTFZRljUfHsCXYac1fIfNySsoSZ2NqXMcFpwHEFnkez4gUV8vhvMszPiDxLZONu+sWgohKr14c/tJV4v9QXiP97D+cS2bNtAkDNcJh7oD1zJ3gV1BpvcEEjQrvs4bWaL+bbMXlrGt5zaBKRnPuDAFecfJTgypJBjLsw= 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=ZurXRdR2; 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="ZurXRdR2" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=web.de; s=s29768273; t=1719676055; x=1720280855; i=l.s.r@web.de; bh=TLXDMpNoNVuhLOZMC2iTAu/dbeP4BPiwDZP3IdR5eRo=; 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=ZurXRdR21cTuWGCB6jydJ7DLQHbAMyAMRHX3DLKGYmJh5Vt1GtBjc7G85G5NggRo A9/r5bAeeryglD07KTvu1++263R2nV8UZmARmLlfmNgpJzUKJ+t0kfbG1ejbEFwi/ OAZiNXnFaWaGcT1hMQLbuCCyMCTywxqlkQDh19B1q3Zr8o1BnVn5WNNhfhYvXTkgP Agvocbaa34GTVrj0NMN67JyMP7WS8d7+9fAkWc/KBIKPGOM6oWHTt9CiPXIiPvqMA KiXsF/vdVwSsCHGiCOfz4cLb2XFRHmso0srs7eXWXg+/RDOzXafr2u7tQlUGzeX94 XtjG7ttAxlZkvwYthw== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Received: from [192.168.178.29] ([79.203.19.151]) by smtp.web.de (mrweb106 [213.165.67.124]) with ESMTPSA (Nemesis) id 1MsJSy-1s8w1t0dbu-010WUx; Sat, 29 Jun 2024 17:47:35 +0200 Message-ID: <1bf053ae-957e-4e9a-90f0-11cc76848ce9@web.de> Date: Sat, 29 Jun 2024 17:47:34 +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 6/6] t-strbuf: use TEST_RUN From: =?utf-8?q?Ren=C3=A9_Scharfe?= To: Git List Cc: Phillip Wood , Josh Steadmon References: <85b6b8a9-ee5f-42ab-bcbc-49976b30ef33@web.de> Content-Language: en-US In-Reply-To: <85b6b8a9-ee5f-42ab-bcbc-49976b30ef33@web.de> X-Provags-ID: V03:K1:0F3GHx5dsdvvXDWDxvWEL7sBmbvevd+bvsvKMCXMzrV9vE7nILg 3Y1MXSFNCQEWZdmOfqtFujQiaPBIPC1nnN75rqq7+8o9g6K+N/zOEWRpwsH2mCnxl/6gl0e 4dui8iJyCkZCUwYsvP4WPoY+lR9pzN4zoO7bXTKdxt2EExQ24hpNr5S0ZHf2+atGXUncPtk 5Vz9+tHFyHL1ZRNMmuWXA== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:yCtiQVOcSBs=;61CxcOA61bEJXUw5eBJLyKFWGFC gxhV5L7gWrQngsUWoKfVeFN/lToOpR4rLbIEUpfnyER860usQIEWAi5csTjnKt5WencyNQvZO VQ+Zq9IGlmuKph7aWEEyxqgbVOvcxD12xGhka4eyA4dbnSm3ZIGpoelrSWA3KrDP2gHR6BXRA n0G1D3nWiywFbSebdo+WbO2OzgetPQjft/72NqpqTg0xO1RXxzdxt1i7Z91vp+u4wRAj1BdMX TAe9zugXpu9FGtY06OdHGvJl+XpykOLzVA9xpaNWbcgoVQdbnwLVw0sNqYQrnkWuuGpsuVIT2 U0Xd8IPD1YOfb2iKT6RlMiaTeVhabIqMBuXgGxffvkoADUtYHocxhUY7d5cn0WGYyPxjvFOoy rVlTJ8haEge4XoUWJbxP+e8+AfvzADbEXuluMBus67HAL9AHa1A9zoZ8ugd8muLPz21r/DFgU ZMhsl77UWERPHzOsxx12/WhEiWrNjXeuC3kBdUxigApAVP1f8iXEIE1XRYeDQIxtirL+ulgJy lYFACSX2y+PNK9Q3a8Kb3uZAUi2r5A8W0ei3bKX+v3QjZnv/K2edMOb5xG3tLLIv6ZMqtUUiB 8+LkZZxprq+KChxjI79jtZ/nsV0vKEWUov8C3TNsIJjjXbbwal1hqAuvX98G7fvWM5oTCpy1x CW77EcezaandSFjXVFA/pa59MrOjRHnl/3NswGN1A3zTl1uXgYMYBSYZVnSF7W+lv4NeU56dl Pj9vMaO89JppMarm89GO8Dy4bT+7gz7vLD7QF4jj85wnM/Z4qKXWBNLGzmKSm8QoQIifjUe/y Iqhm9UR9055i+Hk6B9ewfTpWwOE89SYfgNVup1EaZyZlc= The macro TEST takes a single expression. If a test requires multiple statements then they need to be placed in a function that's called in the TEST expression. The functions setup() and setup_populated() here are used for that purpose and take another function as an argument, making the control flow hard to follow. Remove the overhead of these functions by using TEST_RUN instead. Move their duplicate post-condition checks into a new helper, t_release(), and let t_addch() and t_addstr() accept properly typed input parameters instead of void pointers. Use the fully checking t_addstr() for adding initial values instead of only doing only a length comparison -- there's no need for skipping the other checks. This results in test cases that look much more like strbuf usage in production code, only with checked strbuf functions replaced by checking wrappers. Signed-off-by: René Scharfe --- t/unit-tests/t-strbuf.c | 79 +++++++++++++++++++++-------------------- 1 file changed, 41 insertions(+), 38 deletions(-) -- 2.45.2 diff --git a/t/unit-tests/t-strbuf.c b/t/unit-tests/t-strbuf.c index 6027dafef7..c8e39ddda7 100644 --- a/t/unit-tests/t-strbuf.c +++ b/t/unit-tests/t-strbuf.c @@ -1,32 +1,6 @@ #include "test-lib.h" #include "strbuf.h" -/* wrapper that supplies tests with an empty, initialized strbuf */ -static void setup(void (*f)(struct strbuf*, const void*), - const void *data) -{ - struct strbuf buf = STRBUF_INIT; - - f(&buf, data); - strbuf_release(&buf); - check_uint(buf.len, ==, 0); - check_uint(buf.alloc, ==, 0); -} - -/* wrapper that supplies tests with a populated, initialized strbuf */ -static void setup_populated(void (*f)(struct strbuf*, const void*), - const char *init_str, const void *data) -{ - struct strbuf buf = STRBUF_INIT; - - strbuf_addstr(&buf, init_str); - check_uint(buf.len, ==, strlen(init_str)); - f(&buf, data); - strbuf_release(&buf); - check_uint(buf.len, ==, 0); - check_uint(buf.alloc, ==, 0); -} - static int assert_sane_strbuf(struct strbuf *buf) { /* Initialized strbufs should always have a non-NULL buffer */ @@ -66,10 +40,8 @@ static void t_dynamic_init(void) strbuf_release(&buf); } -static void t_addch(struct strbuf *buf, const void *data) +static void t_addch(struct strbuf *buf, int ch) { - const char *p_ch = data; - const char ch = *p_ch; size_t orig_alloc = buf->alloc; size_t orig_len = buf->len; @@ -85,9 +57,8 @@ static void t_addch(struct strbuf *buf, const void *data) check_char(buf->buf[buf->len], ==, '\0'); } -static void t_addstr(struct strbuf *buf, const void *data) +static void t_addstr(struct strbuf *buf, const char *text) { - const char *text = data; size_t len = strlen(text); size_t orig_alloc = buf->alloc; size_t orig_len = buf->len; @@ -105,18 +76,50 @@ static void t_addstr(struct strbuf *buf, const void *data) check_str(buf->buf + orig_len, text); } +static void t_release(struct strbuf *sb) +{ + strbuf_release(sb); + check_uint(sb->len, ==, 0); + check_uint(sb->alloc, ==, 0); +} + int cmd_main(int argc, const char **argv) { if (!TEST(t_static_init(), "static initialization works")) test_skip_all("STRBUF_INIT is broken"); TEST(t_dynamic_init(), "dynamic initialization works"); - TEST(setup(t_addch, "a"), "strbuf_addch adds char"); - TEST(setup(t_addch, ""), "strbuf_addch adds NUL char"); - TEST(setup_populated(t_addch, "initial value", "a"), - "strbuf_addch appends to initial value"); - TEST(setup(t_addstr, "hello there"), "strbuf_addstr adds string"); - TEST(setup_populated(t_addstr, "initial value", "hello there"), - "strbuf_addstr appends string to initial value"); + + if (TEST_RUN("strbuf_addch adds char")) { + struct strbuf sb = STRBUF_INIT; + t_addch(&sb, 'a'); + t_release(&sb); + } + + if (TEST_RUN("strbuf_addch adds NUL char")) { + struct strbuf sb = STRBUF_INIT; + t_addch(&sb, '\0'); + t_release(&sb); + } + + if (TEST_RUN("strbuf_addch appends to initial value")) { + struct strbuf sb = STRBUF_INIT; + t_addstr(&sb, "initial value"); + t_addch(&sb, 'a'); + t_release(&sb); + } + + if (TEST_RUN("strbuf_addstr adds string")) { + struct strbuf sb = STRBUF_INIT; + t_addstr(&sb, "hello there"); + t_release(&sb); + } + + if (TEST_RUN("strbuf_addstr appends string to initial value")) { + struct strbuf sb = STRBUF_INIT; + t_addstr(&sb, "initial value"); + t_addstr(&sb, "hello there"); + t_release(&sb); + } return test_done(); }