From patchwork Mon Feb 19 04:45:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philip Peterson X-Patchwork-Id: 13562164 Received: from mail-wr1-f41.google.com (mail-wr1-f41.google.com [209.85.221.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 229B03C26 for ; Mon, 19 Feb 2024 04:45:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708317945; cv=none; b=gJptFhV2UATXxtF6/qQD8OyeTi8IV0z/P5VImEzj92s73z/7Ov9Cj2pOwmJi6JivyLO23oFHR0grZs+POqWL2bVVh/TB3HU5Fp5qV/f+OpJcsJz4UC9yMCbNX6rxpNAzpDGOM6r2vK3yWZuW+Sev0amQkFkEjNJDkX5ZCtY0P+E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708317945; c=relaxed/simple; bh=0VZQZ4uO2euqBbzwRlk8/twwEFaLOKHkoJMEPCjyLVo=; h=Message-ID:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=Udhd79bQUW06Zl86eEnhsGkuR6MVPmdbx0tK2w9CIZNKvCIMtS3hqeM6ZDaOok4FqI5faR6+iwPEsgeCbEiFeQAmgWdLQlVD73F4dgJNsvHKtTJZIoRkFdq7ZypeTYFVG/OcH2DqMZRFI16xDUeTrpI6B4UyvSdP9wWUtThjol8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=MwJknduC; arc=none smtp.client-ip=209.85.221.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="MwJknduC" Received: by mail-wr1-f41.google.com with SMTP id ffacd0b85a97d-33d18931a94so1693699f8f.1 for ; Sun, 18 Feb 2024 20:45:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708317941; x=1708922741; darn=vger.kernel.org; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date :message-id:reply-to; bh=4EdHh2OzjiRjZwya1ZQbkXFQUUSOrqE5ZRIvk8EBr9M=; b=MwJknduCHHbIwIoU7oL4tLXztozSetp6CWT27JWYbc2rsAdKksF5c1vdn0RAm26UQu ERZkbf/ZJX6qzrOk5v5atNmkMnghVG5nQG5Oy+C0u0h7CoFP8mmWX5Q0EbeBgRIwftUU xiESGlFg7/8Ygvt7d4oNYL0hPqK5vqdFdYO2TfXTtvG2gfI3I3UnuhnOsoiKN+j7wy9t v20rmYJzYL6/N546NhPxHJ1ujrRzi8ebI9x5T7zWcgTO04awMEHFBTjNMfHO8eB1fyby SmvPIZyvUJGuSGZDD9r0OKQ/JskoipNAjs7ihGikBbjeJPvUaZvSD/NRDrYmKs5tlJcr 0Wsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708317941; x=1708922741; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=4EdHh2OzjiRjZwya1ZQbkXFQUUSOrqE5ZRIvk8EBr9M=; b=sfWH0U3SppKoQG3lGV91HaxScupdR+tV4h2kfp7vT0IMTo+WtAncc2GzAr99JrP+BY YPCglrDpn5dDJIAp0eMKWBUAwGHGl4OJG3go3Buo2qicuH50wAzH6bv3wuNU6Bf+/Hul jGTsaG5J6t3DKZiuNizin3i0QRbg/OH4yeEd58olA79572+dP4t7pLRhiZXiAmR2KjC4 9AY7msC2x0jQDlMEYNuLu+A+AgnoEHFdcf/KNHvZTQwHwcIkX8eNtdNcdDeEs/aZGHHG e3Fp7rIxE+RNStld6BZY3C4LV/oBVFLnNZDagrnKvukVrsT91K+CQK8s0PovOIF9lXei I1hQ== X-Gm-Message-State: AOJu0YxfHky/+CTsR5cdUGDn+9lFwjUTz1cDIBhrtcrooKqR4YrswltZ ohYRoIUsSbDu8Asqvrmo58Iesr0wT2MK23dpqXQvg93oLCc/tbuatsAr1y1N X-Google-Smtp-Source: AGHT+IGDLpkPODCYl6ADPWmtXqEvqkK/Nfpe5sl7GWzSs+YREbdxGlu2emmvmn2Jg5LbgkLEedQgrw== X-Received: by 2002:a5d:4a49:0:b0:33d:11a2:42 with SMTP id v9-20020a5d4a49000000b0033d11a20042mr6114993wrs.47.1708317940766; Sun, 18 Feb 2024 20:45:40 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id y21-20020a05600c20d500b0041044e130bfsm9951557wmm.33.2024.02.18.20.45.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Feb 2024 20:45:39 -0800 (PST) Message-ID: <2c60c4406d4eb1307a32f23604f3ef8e34ad56d6.1708317938.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Mon, 19 Feb 2024 04:45:37 +0000 Subject: [PATCH 1/2] apply: add unit tests for parse_range and rename to parse_fragment_range Fcc: Sent Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 To: git@vger.kernel.org Cc: Philip Peterson , Philip Peterson From: Philip Peterson From: Philip Peterson This patchset makes the parse_range function in apply be non-internal linkage in order to expose to the unit testing framework. In so doing, because there is another function called parse_range, I gave this one a more specific name, parse_fragment_range. Other than that, this commit adds several test cases (positive and negative) for the function. Signed-off-by: Philip Peterson --- Makefile | 1 + apply.c | 8 ++--- apply.h | 4 +++ t/unit-tests/t-apply.c | 67 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 t/unit-tests/t-apply.c diff --git a/Makefile b/Makefile index 15990ff3122..369092aedfe 100644 --- a/Makefile +++ b/Makefile @@ -1339,6 +1339,7 @@ THIRD_PARTY_SOURCES += compat/regex/% THIRD_PARTY_SOURCES += sha1collisiondetection/% THIRD_PARTY_SOURCES += sha1dc/% +UNIT_TEST_PROGRAMS += t-apply UNIT_TEST_PROGRAMS += t-basic UNIT_TEST_PROGRAMS += t-mem-pool UNIT_TEST_PROGRAMS += t-strbuf diff --git a/apply.c b/apply.c index 7608e3301ca..199a1150df6 100644 --- a/apply.c +++ b/apply.c @@ -1430,8 +1430,8 @@ static int parse_num(const char *line, unsigned long *p) return ptr - line; } -static int parse_range(const char *line, int len, int offset, const char *expect, - unsigned long *p1, unsigned long *p2) +int parse_fragment_range(const char *line, int len, int offset, const char *expect, + unsigned long *p1, unsigned long *p2) { int digits, ex; @@ -1530,8 +1530,8 @@ static int parse_fragment_header(const char *line, int len, struct fragment *fra return -1; /* Figure out the number of lines in a fragment */ - offset = parse_range(line, len, 4, " +", &fragment->oldpos, &fragment->oldlines); - offset = parse_range(line, len, offset, " @@", &fragment->newpos, &fragment->newlines); + offset = parse_fragment_range(line, len, 4, " +", &fragment->oldpos, &fragment->oldlines); + offset = parse_fragment_range(line, len, offset, " @@", &fragment->newpos, &fragment->newlines); return offset; } diff --git a/apply.h b/apply.h index 7cd38b1443c..bbc5e3caeb5 100644 --- a/apply.h +++ b/apply.h @@ -187,3 +187,7 @@ int apply_all_patches(struct apply_state *state, int options); #endif + + +int parse_fragment_range(const char *line, int len, int offset, const char *expect, + unsigned long *p1, unsigned long *p2); diff --git a/t/unit-tests/t-apply.c b/t/unit-tests/t-apply.c new file mode 100644 index 00000000000..ff0abfb2e0b --- /dev/null +++ b/t/unit-tests/t-apply.c @@ -0,0 +1,67 @@ +#include "test-lib.h" +#include "apply.h" + +#define FAILURE -1 + +static void setup_static(const char *line, int len, int offset, + const char *expect, int assert_result, + unsigned long assert_p1, + unsigned long assert_p2) +{ + unsigned long p1 = 9999; + unsigned long p2 = 9999; + int result = parse_fragment_range(line, len, offset, expect, &p1, &p2); + check_int(result, ==, assert_result); + check_int(p1, ==, assert_p1); + check_int(p2, ==, assert_p2); +} + +int cmd_main(int argc, const char **argv) +{ + char* text; + int expected_result; + + /* Success */ + text = "@@ -4,4 +"; + expected_result = 9; + TEST(setup_static(text, strlen(text), 4, " +", expected_result, 4, 4), + "well-formed range"); + + text = "@@ -4 +8 @@"; + expected_result = 7; + TEST(setup_static(text, strlen(text), 4, " +", expected_result, 4, 1), + "non-comma range"); + + /* Failure */ + text = "@@ -X,4 +"; + expected_result = FAILURE; + TEST(setup_static(text, strlen(text), 4, " +", expected_result, 9999, 9999), + "non-digit range (first coordinate)"); + + text = "@@ -4,X +"; + expected_result = FAILURE; + TEST(setup_static(text, strlen(text), 4, " +", expected_result, 4, 1), // p2 is 1, a little strange but not catastrophic + "non-digit range (second coordinate)"); + + text = "@@ -4,4 -"; + expected_result = FAILURE; + TEST(setup_static(text, strlen(text), 4, " +", expected_result, 4, 4), + "non-expected trailing text"); + + text = "@@ -4,4"; + expected_result = FAILURE; + TEST(setup_static(text, strlen(text), 4, " +", expected_result, 4, 4), + "not long enough for expected trailing text"); + + text = "@@ -4,4"; + expected_result = FAILURE; + TEST(setup_static(text, strlen(text), 7, " +", expected_result, 9999, 9999), + "not long enough for offset"); + + text = "@@ -4,4"; + expected_result = FAILURE; + TEST(setup_static(text, strlen(text), -1, " +", expected_result, 9999, 9999), + "negative offset"); + + return test_done(); +} From patchwork Mon Feb 19 04:45:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philip Peterson X-Patchwork-Id: 13562165 Received: from mail-wr1-f45.google.com (mail-wr1-f45.google.com [209.85.221.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BFD4D1CD0A for ; Mon, 19 Feb 2024 04:45:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708317945; cv=none; b=KnpnCLLWGzfh0JG8KkCI/S89XDb6lV4YuufFMKGXyRVwlyDSIeBrwgXUBRq+ySUte2perserGR+OL7VyYy/pgmAbnZ4+uvhkqAPzXaEB5po2oyOMPgeUZstcVDkdl7IuwvATvGpVP+SRCqoBYT2Y6cxdpj5GuIRTz5D2bsgNcS0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708317945; c=relaxed/simple; bh=6sK8cFM1K+huDUz0oGZzjH3TutaA5miELh4O6mS6SxM=; h=Message-ID:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=e6JmymNvh1ufeBRxZRlogGS82ngL6wKpFiBwqK9GCNXBTjQWBc/9SVXUgInWYCU7wCMZLXcf1ho2GtTY0pe1w7diEx2npqKcuBTGc+EB1cK1g+fnWcteWW/0xKpBBkhSxyEbMY1aNzA9wc+Vk/9H55xqabicqAad8tdWou8WW58= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=hz8JZoB/; arc=none smtp.client-ip=209.85.221.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="hz8JZoB/" Received: by mail-wr1-f45.google.com with SMTP id ffacd0b85a97d-33d28468666so950912f8f.0 for ; Sun, 18 Feb 2024 20:45:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708317941; x=1708922741; darn=vger.kernel.org; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date :message-id:reply-to; bh=9x3Wi6mlfR6ezpJBtGlzg/O83HS01kI1ADdS3Vtpz60=; b=hz8JZoB/oju3Jdq4u/3a3vsZQKZtu3Ro1zXgspdMfk8K3QIfowqQeK+b6L0wxf72gk TlZgRawTNQOM6OVE7EZ2nSWE6MuLAd7RGkCD9NHFtRCPpUXEb//N2gayMT6mxZkJmAP/ Y08f/DWCvKzI2IPQrypoYWE/tFt1UCte91MfqNvJtPOGyXqSl5ij7POc9BIvFoNRmL+M sSEyvUWnDu6MQo0L4YxdV7785HrWYYaxE4qW66yMV1zF6CWPpQFiu2/NG7jvfRsECptG X+70lZvyLAzrJrlJjB76UmSKCwvMBTyIY3dgmnvaCMRCJwgkZt/FOx5vIqlgu0NA5Aj/ 1Q0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708317941; x=1708922741; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9x3Wi6mlfR6ezpJBtGlzg/O83HS01kI1ADdS3Vtpz60=; b=YfLc1QXGYLzBeqHy3sQd6wQlShT7ujOY42ivfPdHCPBdG3CVl8lOdZVyQO7Kqlpz4r zFlHkgRdslIxeojlfIq2r04Ydfj8yA5vRy/mFmfU5cjTG3ZSrLgDLcFSTniQ4owTmYy4 q3y8EvUGjrpwy/82ZMhoe9R0cNg/qpDELDkyOMfi/jt5KzhMmFegfh+PqJo5yuH2bAp7 lDoWMO0OTZgx+22KT1w5i/3iVvBGhYyQufPfGO7Xoy3riCvTAjjJxO1BpAHZ9Hj/WDYY xa4R+UuLoeTXn8c7THXYPJO0Ci1WyjkxUOxlTisp1xQxnQfBB4o0RBHiKLKD+6CHUHmn i5Qg== X-Gm-Message-State: AOJu0YxbGlej34qnAG7DVDhb+KLQtXEJ9HtvU7zFw7d915DmFbDfiQBd 4dBl5hUsgihd+1Grezi1G7EvypGgkMBnr/WGd5sSQW5LZHujXAGqWl422SRC X-Google-Smtp-Source: AGHT+IFkYDq1AzXt6bHRjGfGPA1azXP7GpXZ8KDHUNGlQpgW5o7vfaRiJiCjcT86bwg8U8se39BKug== X-Received: by 2002:a5d:4dd2:0:b0:33d:52c6:874 with SMTP id f18-20020a5d4dd2000000b0033d52c60874mr524947wru.58.1708317941280; Sun, 18 Feb 2024 20:45:41 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id v4-20020a5d6784000000b0033d39626c27sm4574286wru.76.2024.02.18.20.45.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Feb 2024 20:45:41 -0800 (PST) Message-ID: <7dab12ab7b8af3e6a0778fc1a01dd1479990bcff.1708317938.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Mon, 19 Feb 2024 04:45:38 +0000 Subject: [PATCH 2/2] apply: rewrite unit tests with structured cases Fcc: Sent Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 To: git@vger.kernel.org Cc: Philip Peterson , Philip Peterson From: Philip Peterson From: Philip Peterson The imperative format was a little hard to read, so I rewrote the test cases in a declarative style by defining a common structure for each test case and its assertions. Signed-off-by: Philip Peterson --- t/unit-tests/t-apply.c | 123 ++++++++++++++++++++++++++--------------- 1 file changed, 78 insertions(+), 45 deletions(-) diff --git a/t/unit-tests/t-apply.c b/t/unit-tests/t-apply.c index ff0abfb2e0b..2b78624b690 100644 --- a/t/unit-tests/t-apply.c +++ b/t/unit-tests/t-apply.c @@ -3,65 +3,98 @@ #define FAILURE -1 -static void setup_static(const char *line, int len, int offset, - const char *expect, int assert_result, - unsigned long assert_p1, - unsigned long assert_p2) +typedef struct test_case { + const char *line; + const char *expect_suffix; + int offset; + unsigned long expect_p1; + unsigned long expect_p2; + int expect_result; +} test_case; + +static void setup_static(struct test_case t) { unsigned long p1 = 9999; unsigned long p2 = 9999; - int result = parse_fragment_range(line, len, offset, expect, &p1, &p2); - check_int(result, ==, assert_result); - check_int(p1, ==, assert_p1); - check_int(p2, ==, assert_p2); + int result = parse_fragment_range(t.line, strlen(t.line), t.offset, t.expect_suffix, &p1, &p2); + check_int(result, ==, t.expect_result); + check_int(p1, ==, t.expect_p1); + check_int(p2, ==, t.expect_p2); } int cmd_main(int argc, const char **argv) { - char* text; - int expected_result; - - /* Success */ - text = "@@ -4,4 +"; - expected_result = 9; - TEST(setup_static(text, strlen(text), 4, " +", expected_result, 4, 4), - "well-formed range"); + TEST(setup_static((struct test_case) { + .line = "@@ -4,4 +", + .offset = 4, + .expect_suffix = " +", + .expect_result = 9, + .expect_p1 = 4, + .expect_p2 = 4 + }), "well-formed range"); - text = "@@ -4 +8 @@"; - expected_result = 7; - TEST(setup_static(text, strlen(text), 4, " +", expected_result, 4, 1), - "non-comma range"); + TEST(setup_static((struct test_case) { + .line = "@@ -4 +8 @@", + .offset = 4, + .expect_suffix = " +", + .expect_result = 7, + .expect_p1 = 4, + .expect_p2 = 1 + }), "non-comma range"); - /* Failure */ - text = "@@ -X,4 +"; - expected_result = FAILURE; - TEST(setup_static(text, strlen(text), 4, " +", expected_result, 9999, 9999), - "non-digit range (first coordinate)"); + TEST(setup_static((struct test_case) { + .line = "@@ -X,4 +", + .offset = 4, + .expect_suffix = " +", + .expect_result = FAILURE, + .expect_p1 = 9999, + .expect_p2 = 9999 + }), "non-digit range (first coordinate)"); - text = "@@ -4,X +"; - expected_result = FAILURE; - TEST(setup_static(text, strlen(text), 4, " +", expected_result, 4, 1), // p2 is 1, a little strange but not catastrophic - "non-digit range (second coordinate)"); + TEST(setup_static((struct test_case) { + .line = "@@ -4,X +", + .offset = 4, + .expect_suffix = " +", + .expect_result = FAILURE, + .expect_p1 = 4, + .expect_p2 = 1 // A little strange this is 1, but not end of the world + }), "non-digit range (second coordinate)"); - text = "@@ -4,4 -"; - expected_result = FAILURE; - TEST(setup_static(text, strlen(text), 4, " +", expected_result, 4, 4), - "non-expected trailing text"); + TEST(setup_static((struct test_case) { + .line = "@@ -4,4 -", + .offset = 4, + .expect_suffix = " +", + .expect_result = FAILURE, + .expect_p1 = 4, + .expect_p2 = 4 + }), "non-expected trailing text"); - text = "@@ -4,4"; - expected_result = FAILURE; - TEST(setup_static(text, strlen(text), 4, " +", expected_result, 4, 4), - "not long enough for expected trailing text"); + TEST(setup_static((struct test_case) { + .line = "@@ -4,4", + .offset = 4, + .expect_suffix = " +", + .expect_result = FAILURE, + .expect_p1 = 4, + .expect_p2 = 4 + }), "not long enough for expected trailing text"); - text = "@@ -4,4"; - expected_result = FAILURE; - TEST(setup_static(text, strlen(text), 7, " +", expected_result, 9999, 9999), - "not long enough for offset"); + TEST(setup_static((struct test_case) { + .line = "@@ -4,4", + .offset = 7, + .expect_suffix = " +", + .expect_result = FAILURE, + .expect_p1 = 9999, + .expect_p2 = 9999 + }), "not long enough for offset"); - text = "@@ -4,4"; - expected_result = FAILURE; - TEST(setup_static(text, strlen(text), -1, " +", expected_result, 9999, 9999), - "negative offset"); + TEST(setup_static((struct test_case) { + .line = "@@ -4,4", + .offset = -1, + .expect_suffix = " +", + .expect_result = FAILURE, + .expect_p1 = 9999, + .expect_p2 = 9999 + }), "negative offset"); return test_done(); }