From patchwork Tue Feb 25 10:10:41 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Seyi Kuforiji X-Patchwork-Id: 13989718 Received: from mail-ej1-f45.google.com (mail-ej1-f45.google.com [209.85.218.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 35C842144BF for ; Tue, 25 Feb 2025 10:12:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740478369; cv=none; b=mOSsM9jwwCLvj2pyJs/ffDEeLSp9uosk5dVLL+0WJ8QjBtGkdhShsadzvSGFrwH40C3MOr0TnI4/WX0FpndiySrcVuM5YUi6qmxkeBKlV5wM0V5gzz8nI8eKdc1Ca9o7bIRGxWLwZor5/R43Ky+Obm6i+t/eGMbalwsn4DdM9Bk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740478369; c=relaxed/simple; bh=z/7qh82HOxr+eTGS8LG2e7fRjIhCK+gtZkezu3nPB6w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uK7IYA8nJzWnKA80WMJ6eR/vSFxCqg8uQtihNcSxQH7MSPmUPE9Nf3CwmF0vNVjQQOQok3hkWFDi4h1opUy+UGHQYh2Rk65l4ljgpx0ZXHF8Rnl4x6KIF7i3+SbRqomYFdHXPhUxnUOfly/bM+RPw4IP9xCEL/CWOu+m64sAVcU= 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=Wtna9pXQ; arc=none smtp.client-ip=209.85.218.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="Wtna9pXQ" Received: by mail-ej1-f45.google.com with SMTP id a640c23a62f3a-aaec111762bso282896966b.2 for ; Tue, 25 Feb 2025 02:12:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1740478365; x=1741083165; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=N1rDoIkE5BaBfNgNXuAus4sQ3UcMOZiG7yh9Co8hrVQ=; b=Wtna9pXQxMbGa7O4A1Fq5L4EVocJDOBN8HDIcIfHI+vCsye23xScb4H71DD2mAyT7S YZ593dc3UAoXE01EKm2p8PjRmEAKR+t/vyGAJyqcOA6z5zu2JfU9EEnHPrKb+VqRMDz6 m6T9QzOFLUZQTgusb++W82HhIMDMAwyIzdos1uZEX1ePL0JTi7bgZtV/Z7iQQDdIYMnQ nEkMKdVBQbCHiV7SuLHCGVd2qJ8spAl2JJp0RNZ1thzKXyZ2ZcyZxUNgS7cjL7tve3sO easZv6Gjv0+eERQDRg2o9y0MqNVubK371lOXjEAMwZVOWv9ztaS5AxqHZIcQw92a2rxw u15Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740478365; x=1741083165; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=N1rDoIkE5BaBfNgNXuAus4sQ3UcMOZiG7yh9Co8hrVQ=; b=SOTGo73uD3ovZo1XnX6lepWJcFMDngNDsT7SCFgUVIG1KxbEcQcKbnxdlKFsabnxyT K87gXbRtVwoPu6G83VSyxiAa3CB7yjB/RDQO3EFAxREi6KfuUgNDWldF0MDHSyKn10br Mgem8PJtIQPRk0aTt4VbX46mo+hr5lNAavUPjzsT1kxMuMr5sJm0mhB+aAPuOqcGVRIn NRX4OtYjvGGx+9ZLP35XuhJy7NKQqdiw1qjsFpZKvHDM1zU85BOpOJ/FQovWDVCxc8wA YU2jkv3ca0mC/9tiGJyz0T3cJ40EO+36VtbyZ/BOg9+RZmSPCbgCdbtT3EYb0WGWj0PV d8Ig== X-Gm-Message-State: AOJu0YxALjAGpvhhlbJmX8j+7LRwXpZYQkJ4EBhz20Eh/FHiRsvMFc++ AMaUpR8nJOy1yrf2m1vgVIKmsMRt987x49oMRHoGYIXErKguYFBIDs19iGWBgZ0HZw== X-Gm-Gg: ASbGncs8Q8xaC5phWZD0k9oEDdT/1d+LTg8dcsLXNGELXqRzR99Isl71BqK9yDbi2qz LZOaOD+cGDQ36LIuUKQhe275f2gAX9/RnYONQoKO43yWThvfvOZ9tmx3luILrUJSKn3x2fxunEq DZdzBajRMocHVbBern5DZHAryQhSfRTcnZwMr/tVcdusbbwjGyH+stf/QiFzWrdoYFBXlB24h5F Qq/886oBfSFLd6+6NqJgjMYBI8Rsz4udN1Zujhc/HU6106r/8UFcm10nuX8hMCYrqoNmEwyqxQi 7ZCZsUbRwMkglFIk5QyZPqgcrfDKjq/UZ7QcOSk= X-Google-Smtp-Source: AGHT+IEtTDVxi/qh/mNGQO4TpLjShToHaujX32vNcHJQGGSSr/CyOcUIa+BdXtwg4Vi1mInrmtTdOQ== X-Received: by 2002:a17:906:32db:b0:abb:b092:dae0 with SMTP id a640c23a62f3a-abc099debf0mr1478238666b.11.1740478365087; Tue, 25 Feb 2025 02:12:45 -0800 (PST) Received: from localhost.localdomain ([41.217.53.197]) by smtp.googlemail.com with ESMTPSA id a640c23a62f3a-abed20b7513sm115120566b.177.2025.02.25.02.12.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Feb 2025 02:12:44 -0800 (PST) From: Seyi Kuforiji To: git@vger.kernel.org Cc: ps@pks.im, phillip.wood@dunelm.org.uk, Seyi Kuforiji Subject: [PATCH v3 1/4] t/unit-tests: implement clar specific oid helper functions Date: Tue, 25 Feb 2025 11:10:41 +0100 Message-ID: <20250225101044.84210-2-kuforiji98@gmail.com> X-Mailer: git-send-email 2.47.0.86.g15030f9556 In-Reply-To: <20250225101044.84210-1-kuforiji98@gmail.com> References: <20250224152704.70289-1-kuforiji98@gmail.com> <20250225101044.84210-1-kuforiji98@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 `get_oid_arbitrary_hex()` and `init_hash_algo()` are both required for oid-related tests to run without errors. In the current implementation, both functions are defined and declared in the `t/unit-tests/lib-oid.{c,h}` which is utilized by oid-related tests in the homegrown unit tests structure. Adapt functions in lib-oid.{c,h} to use clar. Both these functions become available for oid-related test files implemented using the clar testing framework, which requires them. This will be used by subsequent commits. Mentored-by: Patrick Steinhardt Signed-off-by: Seyi Kuforiji --- Makefile | 2 +- t/meson.build | 2 +- t/unit-tests/lib-oid.c | 32 +++++++++++--------------------- t/unit-tests/lib-oid.h | 9 ++++++--- t/unit-tests/unit-test.c | 2 ++ 5 files changed, 21 insertions(+), 26 deletions(-) diff --git a/Makefile b/Makefile index bcf5ed3f85..81799488f0 100644 --- a/Makefile +++ b/Makefile @@ -1365,6 +1365,7 @@ CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X) CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES)) CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/clar/clar.o CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/unit-test.o +CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/lib-oid.o UNIT_TEST_PROGRAMS += t-oid-array UNIT_TEST_PROGRAMS += t-oidmap @@ -1381,7 +1382,6 @@ UNIT_TEST_PROGRAMS += t-trailer UNIT_TEST_PROGRAMS += t-urlmatch-normalization UNIT_TEST_PROGS = $(patsubst %,$(UNIT_TEST_BIN)/%$X,$(UNIT_TEST_PROGRAMS)) UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/test-lib.o -UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/lib-oid.o UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/lib-reftable.o # xdiff and reftable libs may in turn depend on what is in libgit.a diff --git a/t/meson.build b/t/meson.build index 780939d49f..862cf1cfd4 100644 --- a/t/meson.build +++ b/t/meson.build @@ -14,6 +14,7 @@ clar_test_suites = [ clar_sources = [ 'unit-tests/clar/clar.c', 'unit-tests/unit-test.c', + 'unit-tests/lib-oid.c' ] clar_decls_h = custom_target( @@ -68,7 +69,6 @@ foreach unit_test_program : unit_test_programs unit_test = executable(unit_test_name, sources: [ 'unit-tests/test-lib.c', - 'unit-tests/lib-oid.c', 'unit-tests/lib-reftable.c', unit_test_program, ], diff --git a/t/unit-tests/lib-oid.c b/t/unit-tests/lib-oid.c index 8f0ccac532..e0b3180f23 100644 --- a/t/unit-tests/lib-oid.c +++ b/t/unit-tests/lib-oid.c @@ -1,9 +1,9 @@ -#include "test-lib.h" +#include "unit-test.h" #include "lib-oid.h" #include "strbuf.h" #include "hex.h" -int init_hash_algo(void) +int cl_setup_hash_algo(void) { static int algo = -1; @@ -11,42 +11,32 @@ int init_hash_algo(void) const char *algo_name = getenv("GIT_TEST_DEFAULT_HASH"); algo = algo_name ? hash_algo_by_name(algo_name) : GIT_HASH_SHA1; - if (!check(algo != GIT_HASH_UNKNOWN)) - test_msg("BUG: invalid GIT_TEST_DEFAULT_HASH value ('%s')", - algo_name); + cl_assert(algo != GIT_HASH_UNKNOWN); } return algo; } -static int get_oid_arbitrary_hex_algop(const char *hex, struct object_id *oid, +static void cl_parse_oid(const char *hex, struct object_id *oid, const struct git_hash_algo *algop) { - int ret; size_t sz = strlen(hex); struct strbuf buf = STRBUF_INIT; - if (!check(sz <= algop->hexsz)) { - test_msg("BUG: hex string (%s) bigger than maximum allowed (%lu)", - hex, (unsigned long)algop->hexsz); - return -1; - } + cl_assert(sz <= algop->hexsz); strbuf_add(&buf, hex, sz); strbuf_addchars(&buf, '0', algop->hexsz - sz); - ret = get_oid_hex_algop(buf.buf, oid, algop); - if (!check_int(ret, ==, 0)) - test_msg("BUG: invalid hex input (%s) provided", hex); + cl_assert_equal_i(get_oid_hex_algop(buf.buf, oid, algop), 0); strbuf_release(&buf); - return ret; } -int get_oid_arbitrary_hex(const char *hex, struct object_id *oid) + +void cl_parse_any_oid(const char *hex, struct object_id *oid) { - int hash_algo = init_hash_algo(); + int hash_algo = cl_setup_hash_algo(); - if (!check_int(hash_algo, !=, GIT_HASH_UNKNOWN)) - return -1; - return get_oid_arbitrary_hex_algop(hex, oid, &hash_algos[hash_algo]); + cl_assert(hash_algo != GIT_HASH_UNKNOWN); + cl_parse_oid(hex, oid, &hash_algos[hash_algo]); } diff --git a/t/unit-tests/lib-oid.h b/t/unit-tests/lib-oid.h index 4e77c04bd2..4031775104 100644 --- a/t/unit-tests/lib-oid.h +++ b/t/unit-tests/lib-oid.h @@ -5,6 +5,7 @@ /* * Convert arbitrary hex string to object_id. + * * For example, passing "abc12" will generate * "abc1200000000000000000000000000000000000" hex of length 40 for SHA-1 and * create object_id with that. @@ -12,14 +13,16 @@ * algo is not allowed. The hash algo is decided based on GIT_TEST_DEFAULT_HASH * environment variable. */ -int get_oid_arbitrary_hex(const char *s, struct object_id *oid); + +void cl_parse_any_oid (const char *s, struct object_id *oid); /* * Returns one of GIT_HASH_{SHA1, SHA256, UNKNOWN} based on the value of * GIT_TEST_DEFAULT_HASH environment variable. The fallback value in the * absence of GIT_TEST_DEFAULT_HASH is GIT_HASH_SHA1. It also uses - * check(algo != GIT_HASH_UNKNOWN) before returning to verify if the + * cl_assert(algo != GIT_HASH_UNKNOWN) before returning to verify if the * GIT_TEST_DEFAULT_HASH's value is valid or not. */ -int init_hash_algo(void); + +int cl_setup_hash_algo(void); #endif /* LIB_OID_H */ diff --git a/t/unit-tests/unit-test.c b/t/unit-tests/unit-test.c index fa8818842a..5af645048a 100644 --- a/t/unit-tests/unit-test.c +++ b/t/unit-tests/unit-test.c @@ -1,5 +1,7 @@ #include "unit-test.h" +#include "hex.h" #include "parse-options.h" +#include "strbuf.h" #include "string-list.h" #include "strvec.h" From patchwork Tue Feb 25 10:10:42 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Seyi Kuforiji X-Patchwork-Id: 13989719 Received: from mail-ed1-f46.google.com (mail-ed1-f46.google.com [209.85.208.46]) (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 BE1C9264A92 for ; Tue, 25 Feb 2025 10:12:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740478373; cv=none; b=aWYLBNz6Ffu+NS8mvwgjev5vCiAXiuDCtsYaQrQeLPE26X2hWu0K8Yfl4UPFV2Ozuk4x5Dxmm2fkXgF2XwynAvxKaMvP6UUczLAwExB3iBQYvvsMXEtn5VRJRZvyHbKJEH+ypkE95bR5VLpvMFBd6rDUZA7jqPj6JH223Z+YzvA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740478373; c=relaxed/simple; bh=A01EJzzZiByvriqE51xBYmWYBxsYKlH+ASS/hzAjoro=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QFywVngbrWQgiKHn7JusSPwg32zfAl1TmZ79nwMEu0UtZJUROQ9b8MBhjbgk+Ja1x1rTKb5fza79CfSQ/cxpSfP25u+aRNSr2Fqp0R+WSUKlVg0cj9cpzIn5GZyCqblF1m7UYkXAkGpm15ud6xJeb0zO8N3cQZV8PQbI2bohrP0= 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=lss8mzFq; arc=none smtp.client-ip=209.85.208.46 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="lss8mzFq" Received: by mail-ed1-f46.google.com with SMTP id 4fb4d7f45d1cf-5ded1395213so9074516a12.2 for ; Tue, 25 Feb 2025 02:12:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1740478370; x=1741083170; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=WO1c+28Pbxy1nqeTNAkDEuYLGqNpuqYXvjNj66FccDg=; b=lss8mzFqdBE9hoEpRh3hh/afISVsaKQJfUyP4FhO04s2oRLdb2w4H1yMaWhsVGE20/ oHO0bfwKaY2FuusZtCKKBkoHI1Sru8/q0wrQitxKAGyWrKkIep3fGgZbD39vRwipTGOF 3WzEzt+F3Fk4hKZjZEiJIbAfYsZHrMhJZ1J3GhpO9rnzaxGpuiG70FIZgEfqSNmPou2S zqH+Fm6ccJx7QZuUq4gqvzRmizzMXW5+2O8iCxHM4o2HanKR3ec2wNcobhJ5FZvp0JtV 2/T1R1Tu5r/fMiQsMUY98r4cgwp7VSKRHjnqsAC4Ye4mOhqibjrsB0jiO1+sgTOOPDO0 Rr2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740478370; x=1741083170; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=WO1c+28Pbxy1nqeTNAkDEuYLGqNpuqYXvjNj66FccDg=; b=XxkjYhR5xcgelKLQEkGR3b+OwLTA86M60VcSeKxRsw2fdAQErkBmy4jY1EnNB4E+il mTFR3UkMmRUFwLxSJT+GsaxXDlsa5livOwqNJYd9/ELhgjlWadf8KvRRHy+qn2Da752k uzhv6gYGV62vx8lKsgtLZb6M3Ie638XcN8l99css8d3tjvv9sXQnwd5alWXDHlHEj+uS TsM+qCyJ/cTBkkKomSfIkUsAZLp3nUDHpbSs7Eu/ZlcSY4NdjCGBKNroH6svvtg8WGO5 +XoPIDKEL+tokXrt4J8hhiJ8iMDi+IUJeIxK9g2ly9k2YP7QkyR9TCxw2WLvTx/t5XID ynVw== X-Gm-Message-State: AOJu0YyvfNgaSE9plWWULzuK5HuqOoEe7hLTz1eqGRVASh8c+AYEul2h NYzOiQMUKFkjGda0ASGgjyJWwWg0LRI5H5cTqlJvmgsZbbIHqIUD6cQCVQlRkO1h0g== X-Gm-Gg: ASbGncvR97qn+8wia7NZTrYtvEUKwtRA07JEC6s5VdKr8PuFLBXK0BnZr2+GBwD2S6i rlxl9u2kACYR45mgyJPylhe3fFQU+3PJZooAbZjk7BTZocz6OyQ8wblZByDK1uSAc+Cx4XcbsCS r+LTHZ3T254mSJncekP4b2SiPKU37TmaeCSXwWRO2fbEbHxmaMM3BVrws1NKMi0olFYOWWgt1hz +YIadj5yxXuJsSP71LhBGXCxqH7vMqvcj1jdV3HWjBlEmFQFWWLSK2qLAmePNsGrgFk7/pKKdT7 QYA55tWd2cH5DaLJVuklTpvMvdbjS7dXtdk38dQ= X-Google-Smtp-Source: AGHT+IFtx2nmPXwjrzziCZG+pNlacdoJvWpnMJsgxFSK9Pe7Os1tzKEKi7S2xBHnlX5YLYpFq1tPlQ== X-Received: by 2002:a17:907:1ca4:b0:ab6:d575:9540 with SMTP id a640c23a62f3a-abed1076c86mr272828666b.50.1740478369412; Tue, 25 Feb 2025 02:12:49 -0800 (PST) Received: from localhost.localdomain ([41.217.53.197]) by smtp.googlemail.com with ESMTPSA id a640c23a62f3a-abed20b7513sm115120566b.177.2025.02.25.02.12.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Feb 2025 02:12:48 -0800 (PST) From: Seyi Kuforiji To: git@vger.kernel.org Cc: ps@pks.im, phillip.wood@dunelm.org.uk, Seyi Kuforiji Subject: [PATCH v3 2/4] t/unit-tests: convert oid-array test to use clar test framework Date: Tue, 25 Feb 2025 11:10:42 +0100 Message-ID: <20250225101044.84210-3-kuforiji98@gmail.com> X-Mailer: git-send-email 2.47.0.86.g15030f9556 In-Reply-To: <20250225101044.84210-1-kuforiji98@gmail.com> References: <20250224152704.70289-1-kuforiji98@gmail.com> <20250225101044.84210-1-kuforiji98@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Adapt oid-array test script to clar framework by using clar assertions where necessary. Remove descriptions from macros to reduce redundancy, and move test input arrays to global scope for reuse across multiple test functions. Introduce `test_oid_array__initialize()` to explicitly initialize the hash algorithm. These changes streamline the test suite, making individual tests self-contained and reducing redundant code. Mentored-by: Patrick Steinhardt Signed-off-by: Seyi Kuforiji --- Makefile | 2 +- t/meson.build | 2 +- t/unit-tests/{t-oid-array.c => u-oid-array.c} | 125 +++++++++--------- 3 files changed, 66 insertions(+), 63 deletions(-) rename t/unit-tests/{t-oid-array.c => u-oid-array.c} (34%) diff --git a/Makefile b/Makefile index 81799488f0..c9b0320fcb 100644 --- a/Makefile +++ b/Makefile @@ -1356,6 +1356,7 @@ CLAR_TEST_SUITES += u-example-decorate CLAR_TEST_SUITES += u-hash CLAR_TEST_SUITES += u-hashmap CLAR_TEST_SUITES += u-mem-pool +CLAR_TEST_SUITES += u-oid-array CLAR_TEST_SUITES += u-prio-queue CLAR_TEST_SUITES += u-reftable-tree CLAR_TEST_SUITES += u-strbuf @@ -1367,7 +1368,6 @@ CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/clar/clar.o CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/unit-test.o CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/lib-oid.o -UNIT_TEST_PROGRAMS += t-oid-array UNIT_TEST_PROGRAMS += t-oidmap UNIT_TEST_PROGRAMS += t-oidtree UNIT_TEST_PROGRAMS += t-reftable-basics diff --git a/t/meson.build b/t/meson.build index 862cf1cfd4..1b17a53acc 100644 --- a/t/meson.build +++ b/t/meson.build @@ -4,6 +4,7 @@ clar_test_suites = [ 'unit-tests/u-hash.c', 'unit-tests/u-hashmap.c', 'unit-tests/u-mem-pool.c', + 'unit-tests/u-oid-array.c', 'unit-tests/u-prio-queue.c', 'unit-tests/u-reftable-tree.c', 'unit-tests/u-strbuf.c', @@ -49,7 +50,6 @@ clar_unit_tests = executable('unit-tests', test('unit-tests', clar_unit_tests) unit_test_programs = [ - 'unit-tests/t-oid-array.c', 'unit-tests/t-oidmap.c', 'unit-tests/t-oidtree.c', 'unit-tests/t-reftable-basics.c', diff --git a/t/unit-tests/t-oid-array.c b/t/unit-tests/u-oid-array.c similarity index 34% rename from t/unit-tests/t-oid-array.c rename to t/unit-tests/u-oid-array.c index 45b59a2a51..e48a433f21 100644 --- a/t/unit-tests/t-oid-array.c +++ b/t/unit-tests/u-oid-array.c @@ -1,22 +1,19 @@ #define USE_THE_REPOSITORY_VARIABLE -#include "test-lib.h" +#include "unit-test.h" #include "lib-oid.h" #include "oid-array.h" #include "hex.h" -static int fill_array(struct oid_array *array, const char *hexes[], size_t n) +static void fill_array(struct oid_array *array, const char *hexes[], size_t n) { for (size_t i = 0; i < n; i++) { struct object_id oid; - if (!check_int(get_oid_arbitrary_hex(hexes[i], &oid), ==, 0)) - return -1; + cl_parse_any_oid(hexes[i], &oid); oid_array_append(array, &oid); } - if (!check_uint(array->nr, ==, n)) - return -1; - return 0; + cl_assert_equal_i(array->nr, n); } static int add_to_oid_array(const struct object_id *oid, void *data) @@ -34,30 +31,22 @@ static void t_enumeration(const char **input_args, size_t input_sz, actual = OID_ARRAY_INIT; size_t i; - if (fill_array(&input, input_args, input_sz)) - return; - if (fill_array(&expect, expect_args, expect_sz)) - return; + fill_array(&input, input_args, input_sz); + fill_array(&expect, expect_args, expect_sz); oid_array_for_each_unique(&input, add_to_oid_array, &actual); - if (!check_uint(actual.nr, ==, expect.nr)) - return; - - for (i = 0; i < actual.nr; i++) { - if (!check(oideq(&actual.oid[i], &expect.oid[i]))) - test_msg("expected: %s\n got: %s\n index: %" PRIuMAX, - oid_to_hex(&expect.oid[i]), oid_to_hex(&actual.oid[i]), - (uintmax_t)i); - } + cl_assert_equal_i(actual.nr, expect.nr); + + for (i = 0; i < actual.nr; i++) + cl_assert(oideq(&actual.oid[i], &expect.oid[i])); oid_array_clear(&actual); oid_array_clear(&input); oid_array_clear(&expect); } -#define TEST_ENUMERATION(input, expect, desc) \ - TEST(t_enumeration(input, ARRAY_SIZE(input), expect, ARRAY_SIZE(expect)), \ - desc " works") +#define TEST_ENUMERATION(input, expect) \ + t_enumeration(input, ARRAY_SIZE(input), expect, ARRAY_SIZE(expect)); static void t_lookup(const char **input_hexes, size_t n, const char *query_hex, int lower_bound, int upper_bound) @@ -66,61 +55,75 @@ static void t_lookup(const char **input_hexes, size_t n, const char *query_hex, struct object_id oid_query; int ret; - if (!check_int(get_oid_arbitrary_hex(query_hex, &oid_query), ==, 0)) - return; - if (fill_array(&array, input_hexes, n)) - return; + cl_parse_any_oid(query_hex, &oid_query); + fill_array(&array, input_hexes, n); ret = oid_array_lookup(&array, &oid_query); - if (!check_int(ret, <=, upper_bound) || - !check_int(ret, >=, lower_bound)) - test_msg("oid query for lookup: %s", oid_to_hex(&oid_query)); + cl_assert(ret <= upper_bound); + cl_assert(ret >= lower_bound); oid_array_clear(&array); } -#define TEST_LOOKUP(input_hexes, query, lower_bound, upper_bound, desc) \ - TEST(t_lookup(input_hexes, ARRAY_SIZE(input_hexes), query, \ - lower_bound, upper_bound), \ - desc " works") +#define TEST_LOOKUP(input_hexes, query, lower_bound, upper_bound) \ + t_lookup(input_hexes, ARRAY_SIZE(input_hexes), query, \ + lower_bound, upper_bound); -static void setup(void) +void test_oid_array__initialize(void) { /* The hash algo is used by oid_array_lookup() internally */ - int algo = init_hash_algo(); - if (check_int(algo, !=, GIT_HASH_UNKNOWN)) - repo_set_hash_algo(the_repository, algo); + int algo = cl_setup_hash_algo(); + repo_set_hash_algo(the_repository, algo); } -int cmd_main(int argc UNUSED, const char **argv UNUSED) +static const char *arr_input[] = { "88", "44", "aa", "55" }; +static const char *arr_input_dup[] = { "88", "44", "aa", "55", + "88", "44", "aa", "55", + "88", "44", "aa", "55" }; +static const char *res_sorted[] = { "44", "55", "88", "aa" }; + +void test_oid_array__enumerate_unique(void) { - const char *arr_input[] = { "88", "44", "aa", "55" }; - const char *arr_input_dup[] = { "88", "44", "aa", "55", - "88", "44", "aa", "55", - "88", "44", "aa", "55" }; - const char *res_sorted[] = { "44", "55", "88", "aa" }; - const char *nearly_55; + TEST_ENUMERATION(arr_input, res_sorted); +} + +void test_oid_array__enumerate_duplicate(void) +{ + TEST_ENUMERATION(arr_input_dup, res_sorted); +} + +void test_oid_array__lookup(void) +{ + TEST_LOOKUP(arr_input, "55", 1, 1); +} - if (!TEST(setup(), "setup")) - test_skip_all("hash algo initialization failed"); +void test_oid_array__lookup_non_existent(void) +{ + TEST_LOOKUP(arr_input, "33", INT_MIN, -1); +} + +void test_oid_array__lookup_duplicates(void) +{ + TEST_LOOKUP(arr_input_dup, "55", 3, 5); +} - TEST_ENUMERATION(arr_input, res_sorted, "ordered enumeration"); - TEST_ENUMERATION(arr_input_dup, res_sorted, - "ordered enumeration with duplicate suppression"); +void test_oid_array__lookup_non_existent_dup(void) +{ + TEST_LOOKUP(arr_input_dup, "66", INT_MIN, -1); +} - TEST_LOOKUP(arr_input, "55", 1, 1, "lookup"); - TEST_LOOKUP(arr_input, "33", INT_MIN, -1, "lookup non-existent entry"); - TEST_LOOKUP(arr_input_dup, "55", 3, 5, "lookup with duplicates"); - TEST_LOOKUP(arr_input_dup, "66", INT_MIN, -1, - "lookup non-existent entry with duplicates"); +void test_oid_array__lookup_almost_dup(void) +{ + const char *nearly_55; - nearly_55 = init_hash_algo() == GIT_HASH_SHA1 ? + nearly_55 = cl_setup_hash_algo() == GIT_HASH_SHA1 ? "5500000000000000000000000000000000000001" : "5500000000000000000000000000000000000000000000000000000000000001"; - TEST_LOOKUP(((const char *[]){ "55", nearly_55 }), "55", 0, 0, - "lookup with almost duplicate values"); - TEST_LOOKUP(((const char *[]){ "55", "55" }), "55", 0, 1, - "lookup with single duplicate value"); - return test_done(); + TEST_LOOKUP(((const char *[]){ "55", nearly_55 }), "55", 0, 0); +} + +void test_oid_array__lookup_single_dup(void) +{ + TEST_LOOKUP(((const char *[]){ "55", "55" }), "55", 0, 1); } From patchwork Tue Feb 25 10:10:43 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Seyi Kuforiji X-Patchwork-Id: 13989720 Received: from mail-ej1-f41.google.com (mail-ej1-f41.google.com [209.85.218.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 8483F264A92 for ; Tue, 25 Feb 2025 10:12:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740478378; cv=none; b=C19zH4WbZLSaA0NAyf7+FAoeysPggmYVwHdfRf0n2A6Dmgz8rGLcMwzy5QgFWnYb/8Pf1JdIQLhXdy77rsxc0ocRMgReYUDiHmdIWwxlI/I7x0gYQG1G+9V0cmn+eQChoj/Ei+bQS7GyTzXLpF3hfZdYlhVR97Z5JE5H4TE3M14= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740478378; c=relaxed/simple; bh=2I3Sxy0+gRaej2+g2kj7a9a7YacCAzgqKhviw2Gw+iw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BTIoFqljz2qKBXIMFVezY+LbZNQEPMU5qk0tAXzGFa07/Sb80J9VEx0ycYRBEJEkFFqE//K0Dh2uxwm2CqI7XMcBsOZO6vnvIHtwOxayHmvShd4I8kp7gEhhkemnaw1q+5zZTgVMLv5TM7ng9BGlnJIuHrLrvu50vCx0Za8b7gE= 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=d6vWv5a1; arc=none smtp.client-ip=209.85.218.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="d6vWv5a1" Received: by mail-ej1-f41.google.com with SMTP id a640c23a62f3a-abbac134a19so827838266b.0 for ; Tue, 25 Feb 2025 02:12:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1740478375; x=1741083175; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=52pGapUxpqKwFzQKL/kANoHc4ZzG6RaXxnCvaVWUCvI=; b=d6vWv5a1xtMgBNoiNueBRVdgWbGD2Fy5oLnFDQoVx+LVzz9+2LiSF5hfb87eSy6MK3 OzjlLJWKxtzbkM9SWOeQd5tZue7gE3mh/3a7HQOzTN2n9Ag2jLQhtWsUjAffq4pGuFzJ RyyJ9UxAPVvyFOZs6RbINg0xi0XhgvceQt4W20uTrfTWD/q2XLrWfzTgmWg0SO+5eUQN bgx3qkgovgW45Byyqly7SnIDs+yZUSHZsAORlXMr8wQFeVE7Bm8ipKtoBHfJbPErd4Tq YkfZc7bWYkazCATyXjJ1Z+XQamDm8LFIvvbQNjJEKedzK24XbCegOOB64ioE6X/INzus 6c0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740478375; x=1741083175; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=52pGapUxpqKwFzQKL/kANoHc4ZzG6RaXxnCvaVWUCvI=; b=iab9vwubz91xml1JXaVDzMdigl3+ay+JDsV4BkAMt+tEpEBH7+oYl7133Ji8EoTEAH 5lMO/j8mJEUEigzMcS6o/9RwY97QHpTJUXni82WJ2g3vfShuHs9kX8Ht1PkoLKt2NJAd tuqAP8QbBv1uDWzBHDyH4IrjmpWCkkzEXVdrlNsZcXWMPuBHLj4q65O71Tlnx9XMFN9j 0AA0a+ZWp9v58d+bN20JjTOsHwnSyC/sXF43Z7U4mPhvomUKFivi2zoFJuP//XIdLIdi yJWb2hKXzwcq/arkYe3taQbQBckC5XWAZ3cCGEyNdBiQZ/7uqAU5K5QmYhOyUVTq1ZNo o4xg== X-Gm-Message-State: AOJu0YxIjUrvD5NLjuvqz5WlBkuV7qH/9OT9cZr12B21BfJf7oB6z+PF jWedca0+38K450ho6knKS+vfoSMUsn8QaQ0Oe8WZmdIC419BtJQwSArpOG/aDg6xAw== X-Gm-Gg: ASbGnct9rPcvFK+kd5YD8qYFNOBywEXf5auRh4Kb3mSE+tGQ0IJHAGN/GHrLfUvLVOU H6DvlH/vISCRd3CygKS49NtTJwudQt9s30sDjVsgO+DFpaHoJNg2zPpn8IXtUBuu4OpojdXSagD nWcWooTkajgAtEuhF/FvdRsLnUFrTeiVANthBtHg+6WaM1PUFQubhuUN8WGFh2O2hQOsTc1gfE+ y79Sycx/u4vkGJ/iCigFd0rByCM9JdOjXoYsiOlGuNCWiW1Klvf/U6ygsydOBvInCX3JH18gfAH IHG2v4BW/wXMJlEseYHgP5KoGMRjoP9jHtoPj/8= X-Google-Smtp-Source: AGHT+IFATHcYBdOe9dw6ifx1KGl2fWPCBDYrgoWIp5sRGTUpP1za6SEK/7TEsZWss9ZUpsAGtfdGbA== X-Received: by 2002:a17:906:809:b0:abe:dc92:817c with SMTP id a640c23a62f3a-abedc928304mr123760466b.51.1740478374493; Tue, 25 Feb 2025 02:12:54 -0800 (PST) Received: from localhost.localdomain ([41.217.53.197]) by smtp.googlemail.com with ESMTPSA id a640c23a62f3a-abed20b7513sm115120566b.177.2025.02.25.02.12.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Feb 2025 02:12:54 -0800 (PST) From: Seyi Kuforiji To: git@vger.kernel.org Cc: ps@pks.im, phillip.wood@dunelm.org.uk, Seyi Kuforiji Subject: [PATCH v3 3/4] t/unit-tests: convert oidmap test to use clar test framework Date: Tue, 25 Feb 2025 11:10:43 +0100 Message-ID: <20250225101044.84210-4-kuforiji98@gmail.com> X-Mailer: git-send-email 2.47.0.86.g15030f9556 In-Reply-To: <20250225101044.84210-1-kuforiji98@gmail.com> References: <20250224152704.70289-1-kuforiji98@gmail.com> <20250225101044.84210-1-kuforiji98@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Adapt oidmap test script to clar framework by using clar assertions where necessary. `cl_parse_any_oid()` ensures the hash algorithm is set before parsing. This prevents issues from an uninitialized or invalid hash algorithm. Introduce 'test_oidmap__initialize` handles the to set up of the global oidmap map with predefined key-value pairs, and `test_oidmap__cleanup` frees the oidmap and its entries when all tests are completed. The test loops through all entries to detect multiple errors. With this change, it stops at the first error encountered, making it easier to address it. Mentored-by: Patrick Steinhardt Signed-off-by: Seyi Kuforiji --- Makefile | 2 +- t/meson.build | 2 +- t/unit-tests/{t-oidmap.c => u-oidmap.c} | 153 +++++++++--------------- 3 files changed, 56 insertions(+), 101 deletions(-) rename t/unit-tests/{t-oidmap.c => u-oidmap.c} (32%) diff --git a/Makefile b/Makefile index c9b0320fcb..e4e85e6007 100644 --- a/Makefile +++ b/Makefile @@ -1357,6 +1357,7 @@ CLAR_TEST_SUITES += u-hash CLAR_TEST_SUITES += u-hashmap CLAR_TEST_SUITES += u-mem-pool CLAR_TEST_SUITES += u-oid-array +CLAR_TEST_SUITES += u-oidmap CLAR_TEST_SUITES += u-prio-queue CLAR_TEST_SUITES += u-reftable-tree CLAR_TEST_SUITES += u-strbuf @@ -1368,7 +1369,6 @@ CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/clar/clar.o CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/unit-test.o CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/lib-oid.o -UNIT_TEST_PROGRAMS += t-oidmap UNIT_TEST_PROGRAMS += t-oidtree UNIT_TEST_PROGRAMS += t-reftable-basics UNIT_TEST_PROGRAMS += t-reftable-block diff --git a/t/meson.build b/t/meson.build index 1b17a53acc..d5b83cdb72 100644 --- a/t/meson.build +++ b/t/meson.build @@ -5,6 +5,7 @@ clar_test_suites = [ 'unit-tests/u-hashmap.c', 'unit-tests/u-mem-pool.c', 'unit-tests/u-oid-array.c', + 'unit-tests/u-oidmap.c', 'unit-tests/u-prio-queue.c', 'unit-tests/u-reftable-tree.c', 'unit-tests/u-strbuf.c', @@ -50,7 +51,6 @@ clar_unit_tests = executable('unit-tests', test('unit-tests', clar_unit_tests) unit_test_programs = [ - 'unit-tests/t-oidmap.c', 'unit-tests/t-oidtree.c', 'unit-tests/t-reftable-basics.c', 'unit-tests/t-reftable-block.c', diff --git a/t/unit-tests/t-oidmap.c b/t/unit-tests/u-oidmap.c similarity index 32% rename from t/unit-tests/t-oidmap.c rename to t/unit-tests/u-oidmap.c index b22e52d08b..dc805b7e3c 100644 --- a/t/unit-tests/t-oidmap.c +++ b/t/unit-tests/u-oidmap.c @@ -1,4 +1,4 @@ -#include "test-lib.h" +#include "unit-test.h" #include "lib-oid.h" #include "oidmap.h" #include "hash.h" @@ -18,102 +18,85 @@ static const char *const key_val[][2] = { { "11", "one" }, { "22", "two" }, { "33", "three" } }; -static void setup(void (*f)(struct oidmap *map)) +static struct oidmap map; + +void test_oidmap__initialize(void) { - struct oidmap map = OIDMAP_INIT; - int ret = 0; + oidmap_init(&map, 0); for (size_t i = 0; i < ARRAY_SIZE(key_val); i++){ struct test_entry *entry; FLEX_ALLOC_STR(entry, name, key_val[i][1]); - if ((ret = get_oid_arbitrary_hex(key_val[i][0], &entry->entry.oid))) { - free(entry); - break; - } - entry = oidmap_put(&map, entry); - if (!check(entry == NULL)) - free(entry); + cl_parse_any_oid(key_val[i][0], &entry->entry.oid); + cl_assert(oidmap_put(&map, entry) == NULL); } +} - if (!ret) - f(&map); +void test_oidmap__cleanup(void) +{ oidmap_free(&map, 1); } -static void t_replace(struct oidmap *map) +void test_oidmap__replace(void) { struct test_entry *entry, *prev; FLEX_ALLOC_STR(entry, name, "un"); - if (get_oid_arbitrary_hex("11", &entry->entry.oid)) - return; - prev = oidmap_put(map, entry); - if (!check(prev != NULL)) - return; - check_str(prev->name, "one"); + cl_parse_any_oid("11", &entry->entry.oid); + prev = oidmap_put(&map, entry); + cl_assert(prev != NULL); + cl_assert_equal_s(prev->name, "one"); free(prev); FLEX_ALLOC_STR(entry, name, "deux"); - if (get_oid_arbitrary_hex("22", &entry->entry.oid)) - return; - prev = oidmap_put(map, entry); - if (!check(prev != NULL)) - return; - check_str(prev->name, "two"); + cl_parse_any_oid("22", &entry->entry.oid); + prev = oidmap_put(&map, entry); + cl_assert(prev != NULL); + cl_assert_equal_s(prev->name, "two"); free(prev); } -static void t_get(struct oidmap *map) +void test_oidmap__get(void) { struct test_entry *entry; struct object_id oid; - if (get_oid_arbitrary_hex("22", &oid)) - return; - entry = oidmap_get(map, &oid); - if (!check(entry != NULL)) - return; - check_str(entry->name, "two"); - - if (get_oid_arbitrary_hex("44", &oid)) - return; - check(oidmap_get(map, &oid) == NULL); - - if (get_oid_arbitrary_hex("11", &oid)) - return; - entry = oidmap_get(map, &oid); - if (!check(entry != NULL)) - return; - check_str(entry->name, "one"); + cl_parse_any_oid("22", &oid); + entry = oidmap_get(&map, &oid); + cl_assert(entry != NULL); + cl_assert_equal_s(entry->name, "two"); + + cl_parse_any_oid("44", &oid); + cl_assert(oidmap_get(&map, &oid) == NULL); + + cl_parse_any_oid("11", &oid); + entry = oidmap_get(&map, &oid); + cl_assert(entry != NULL); + cl_assert_equal_s(entry->name, "one"); } -static void t_remove(struct oidmap *map) +void test_oidmap__remove(void) { struct test_entry *entry; struct object_id oid; - if (get_oid_arbitrary_hex("11", &oid)) - return; - entry = oidmap_remove(map, &oid); - if (!check(entry != NULL)) - return; - check_str(entry->name, "one"); - check(oidmap_get(map, &oid) == NULL); + cl_parse_any_oid("11", &oid); + entry = oidmap_remove(&map, &oid); + cl_assert(entry != NULL); + cl_assert_equal_s(entry->name, "one"); + cl_assert(oidmap_get(&map, &oid) == NULL); free(entry); - if (get_oid_arbitrary_hex("22", &oid)) - return; - entry = oidmap_remove(map, &oid); - if (!check(entry != NULL)) - return; - check_str(entry->name, "two"); - check(oidmap_get(map, &oid) == NULL); + cl_parse_any_oid("22", &oid); + entry = oidmap_remove(&map, &oid); + cl_assert(entry != NULL); + cl_assert_equal_s(entry->name, "two"); + cl_assert(oidmap_get(&map, &oid) == NULL); free(entry); - if (get_oid_arbitrary_hex("44", &oid)) - return; - check(oidmap_remove(map, &oid) == NULL); + cl_parse_any_oid("44", &oid); + cl_assert(oidmap_remove(&map, &oid) == NULL); } static int key_val_contains(struct test_entry *entry, char seen[]) @@ -121,8 +104,7 @@ static int key_val_contains(struct test_entry *entry, char seen[]) for (size_t i = 0; i < ARRAY_SIZE(key_val); i++) { struct object_id oid; - if (get_oid_arbitrary_hex(key_val[i][0], &oid)) - return -1; + cl_parse_any_oid(key_val[i][0], &oid); if (oideq(&entry->entry.oid, &oid)) { if (seen[i]) @@ -134,48 +116,21 @@ static int key_val_contains(struct test_entry *entry, char seen[]) return 1; } -static void t_iterate(struct oidmap *map) +void test_oidmap__iterate(void) { struct oidmap_iter iter; struct test_entry *entry; char seen[ARRAY_SIZE(key_val)] = { 0 }; int count = 0; - oidmap_iter_init(map, &iter); + oidmap_iter_init(&map, &iter); while ((entry = oidmap_iter_next(&iter))) { - int ret; - if (!check_int((ret = key_val_contains(entry, seen)), ==, 0)) { - switch (ret) { - case -1: - break; /* error message handled by get_oid_arbitrary_hex() */ - case 1: - test_msg("obtained entry was not given in the input\n" - " name: %s\n oid: %s\n", - entry->name, oid_to_hex(&entry->entry.oid)); - break; - case 2: - test_msg("duplicate entry detected\n" - " name: %s\n oid: %s\n", - entry->name, oid_to_hex(&entry->entry.oid)); - break; - default: - test_msg("BUG: invalid return value (%d) from key_val_contains()", - ret); - break; - } - } else { - count++; + if (key_val_contains(entry, seen) != 0) { + cl_failf("Unexpected entry: name = %s, oid = %s", + entry->name, oid_to_hex(&entry->entry.oid)); } + count++; } - check_int(count, ==, ARRAY_SIZE(key_val)); - check_int(hashmap_get_size(&map->map), ==, ARRAY_SIZE(key_val)); -} - -int cmd_main(int argc UNUSED, const char **argv UNUSED) -{ - TEST(setup(t_replace), "replace works"); - TEST(setup(t_get), "get works"); - TEST(setup(t_remove), "remove works"); - TEST(setup(t_iterate), "iterate works"); - return test_done(); + cl_assert_equal_i(count, ARRAY_SIZE(key_val)); + cl_assert_equal_i(hashmap_get_size(&map.map), ARRAY_SIZE(key_val)); } From patchwork Tue Feb 25 10:10:44 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Seyi Kuforiji X-Patchwork-Id: 13989721 Received: from mail-ej1-f42.google.com (mail-ej1-f42.google.com [209.85.218.42]) (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 483AC25EF8A for ; Tue, 25 Feb 2025 10:13:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740478383; cv=none; b=sHN8En3/zmJnVpwfGcSt4S5NIS3tLRTOfJTOm4CZWYrGpE3Bj/+7OukvBB8dw1g7NXCBSey+bVhKJl5stok86+7Qq84x0qnapFgSB1FR6HljMlvOfH8J+/dtYeNhT/C5Wt50D2Kj2Hait0MhJtDdVjJQ6SWqHPUlnjAWs2hAKUU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740478383; c=relaxed/simple; bh=Jy8UAsmv0M9Ianz70QW37WeM/FMtgRFue80XQrAXUSY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=MoCM5Xh6Bd+h3x+Nzp1OfhJ1VHMk3eV485jTWMLtMg5vfC90xBfzCtVTTWj2GTZkS522dUKKqk6CITMIdiTK/9m4Kaxi2IiD5LBQcQ77tDdtI2Z2PRexoqED3cnuWjOEwko/ptvzghgQX4yMVhYj3QO9tir0WjzZrKmmVML4470= 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=KK3chy4P; arc=none smtp.client-ip=209.85.218.42 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="KK3chy4P" Received: by mail-ej1-f42.google.com with SMTP id a640c23a62f3a-abbdc4a0b5aso992183266b.0 for ; Tue, 25 Feb 2025 02:13:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1740478379; x=1741083179; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=0NgQ/SeUGRi4k7g9nIO1t+BNpYgEGEVWcRY2g0N2lSM=; b=KK3chy4PhEu3vwjTW7ki+YkUE7LcPht0eBbPylk/UKbT5ifNNV5Rr0jK5mCp2gjA/G O+Qg7IlyeStzE6PEAUu3iT2I9zo/xSRjLtHkn4AZPR50wpM9+p36z48Ga/I6LM4y0BeR 9YnjbaHRDGCbPO+CeTl9XRAD42IHQUNyCY3N17oVBLcPP98ohQlSvP7u/8iySi4CdC/m S6wUGapumbZ4qwwA/U4P9AVzJgvQAZNIuNPO/Xu4/mZ/QSiboGnJk6HUbOD3UJl0tyrU VO+Azu4OWFOAM4W+Vek9Zvm1zVqePL6JkToy0+bxM28x8XG7UDGoA3CWxRPakDkGmdh5 ItIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740478379; x=1741083179; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0NgQ/SeUGRi4k7g9nIO1t+BNpYgEGEVWcRY2g0N2lSM=; b=g6B9nEYN4RRU5/3WbGtjGfB9zHMZRgJqbCx9OvFPGQy0E5/X/fLTfdE2KQwK0GZK7B JT5mjTGYC9a874JXL5v51Y7pXXHlQ3UQDE0Mt1Ai0CZb3VDwJUjzIS3Gjjp8ktZhdEnP 1qPChlUpI0z4sfThZRqsY1dEtGnEIkLcT8t6AsNrSHLtKqIAPwuZ4RNftJUoI46A17Lm dA/nee9PUr468jBvMk5OqNb6YGju/bpswgmjBamS0Tl73SRsfQ/PmvHxebtDlvQKa/Mx xz9/9ndBLf7ifiIAY4YOtx7nQWlbTz3Ns+/mX4n59gL00kCIx227Iss04DzGxbz4bNyw Ss6g== X-Gm-Message-State: AOJu0Yxu0oIVrkziS6bZy3qaBRl9guAoIRINdNzviy9Ysq9SiN8/JRGk 9+/ArBHFwmmuT7ZhvZifdSOoPkLPWrIiL+CAko1pNwXtQCZOsOQX/ILE4yjwyg6rWA== X-Gm-Gg: ASbGncsvnkPABkMTn+VFIiMxoY7KK97b7CGw5URkVPzFnjR9gI3QF7ZhbPO8f8SsNTU zO0C7SBrvuxtM4LmGdskG1Fb3Br76jffOvNot2wDDc4/NRQ07CQ9IKs5NtkXLvSstKFDk4AUZuw 4X4SUzE8GGQNsf7dIBvni1Rx7RW5icwkRE8yaVXvn13XtQm0f+xK+u9+r/I7Mkbw1aXL3MgO9X3 iD89aD/IBiRBbUnLgEYg2UjXZ/ceJAXJFTbavAFE9NTtn09Pc+LIeaJMoSVG1qjQUYj+5EeCzv4 dDabmGhtMUdOVCiPBtKMObSs4OnS6fxLIGmi5Nw= X-Google-Smtp-Source: AGHT+IGBJyyrF2oHUL2dDXXghi4jo3qtU+U9XVP6PpcQULVQhvzhd+4812Yy+7L5DzWt8pJaB/pYCg== X-Received: by 2002:a17:907:a317:b0:ab7:46c4:a7be with SMTP id a640c23a62f3a-abbeda28000mr1874241766b.2.1740478379164; Tue, 25 Feb 2025 02:12:59 -0800 (PST) Received: from localhost.localdomain ([41.217.53.197]) by smtp.googlemail.com with ESMTPSA id a640c23a62f3a-abed20b7513sm115120566b.177.2025.02.25.02.12.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Feb 2025 02:12:58 -0800 (PST) From: Seyi Kuforiji To: git@vger.kernel.org Cc: ps@pks.im, phillip.wood@dunelm.org.uk, Seyi Kuforiji Subject: [PATCH v3 4/4] t/unit-tests: convert oidtree test to use clar test framework Date: Tue, 25 Feb 2025 11:10:44 +0100 Message-ID: <20250225101044.84210-5-kuforiji98@gmail.com> X-Mailer: git-send-email 2.47.0.86.g15030f9556 In-Reply-To: <20250225101044.84210-1-kuforiji98@gmail.com> References: <20250224152704.70289-1-kuforiji98@gmail.com> <20250225101044.84210-1-kuforiji98@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Adapt oidtree test script to clar framework by using clar assertions where necessary. `cl_parse_any_oid()` ensures the hash algorithm is set before parsing. This prevents issues from an uninitialized or invalid hash algorithm. Introduce 'test_oidtree__initialize` handles the to set up of the global oidtree variable and `test_oidtree__cleanup` frees the oidtree when all tests are completed. With this change, `check_each` stops at the first error encountered, making it easier to address it. Mentored-by: Patrick Steinhardt Signed-off-by: Seyi Kuforiji --- Makefile | 2 +- t/meson.build | 2 +- t/unit-tests/{t-oidtree.c => u-oidtree.c} | 79 +++++++++-------------- 3 files changed, 34 insertions(+), 49 deletions(-) rename t/unit-tests/{t-oidtree.c => u-oidtree.c} (45%) diff --git a/Makefile b/Makefile index e4e85e6007..2b134efc70 100644 --- a/Makefile +++ b/Makefile @@ -1358,6 +1358,7 @@ CLAR_TEST_SUITES += u-hashmap CLAR_TEST_SUITES += u-mem-pool CLAR_TEST_SUITES += u-oid-array CLAR_TEST_SUITES += u-oidmap +CLAR_TEST_SUITES += u-oidtree CLAR_TEST_SUITES += u-prio-queue CLAR_TEST_SUITES += u-reftable-tree CLAR_TEST_SUITES += u-strbuf @@ -1369,7 +1370,6 @@ CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/clar/clar.o CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/unit-test.o CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/lib-oid.o -UNIT_TEST_PROGRAMS += t-oidtree UNIT_TEST_PROGRAMS += t-reftable-basics UNIT_TEST_PROGRAMS += t-reftable-block UNIT_TEST_PROGRAMS += t-reftable-merged diff --git a/t/meson.build b/t/meson.build index d5b83cdb72..91699917ff 100644 --- a/t/meson.build +++ b/t/meson.build @@ -6,6 +6,7 @@ clar_test_suites = [ 'unit-tests/u-mem-pool.c', 'unit-tests/u-oid-array.c', 'unit-tests/u-oidmap.c', + 'unit-tests/u-oidtree.c', 'unit-tests/u-prio-queue.c', 'unit-tests/u-reftable-tree.c', 'unit-tests/u-strbuf.c', @@ -51,7 +52,6 @@ clar_unit_tests = executable('unit-tests', test('unit-tests', clar_unit_tests) unit_test_programs = [ - 'unit-tests/t-oidtree.c', 'unit-tests/t-reftable-basics.c', 'unit-tests/t-reftable-block.c', 'unit-tests/t-reftable-merged.c', diff --git a/t/unit-tests/t-oidtree.c b/t/unit-tests/u-oidtree.c similarity index 45% rename from t/unit-tests/t-oidtree.c rename to t/unit-tests/u-oidtree.c index a38754b066..e6eede2740 100644 --- a/t/unit-tests/t-oidtree.c +++ b/t/unit-tests/u-oidtree.c @@ -1,10 +1,12 @@ -#include "test-lib.h" +#include "unit-test.h" #include "lib-oid.h" #include "oidtree.h" #include "hash.h" #include "hex.h" #include "strvec.h" +static struct oidtree ot; + #define FILL_TREE(tree, ...) \ do { \ const char *hexes[] = { __VA_ARGS__ }; \ @@ -16,8 +18,7 @@ static int fill_tree_loc(struct oidtree *ot, const char *hexes[], size_t n) { for (size_t i = 0; i < n; i++) { struct object_id oid; - if (!check_int(get_oid_arbitrary_hex(hexes[i], &oid), ==, 0)) - return -1; + cl_parse_any_oid(hexes[i], &oid); oidtree_insert(ot, &oid); } return 0; @@ -27,10 +28,8 @@ static void check_contains(struct oidtree *ot, const char *hex, int expected) { struct object_id oid; - if (!check_int(get_oid_arbitrary_hex(hex, &oid), ==, 0)) - return; - if (!check_int(oidtree_contains(ot, &oid), ==, expected)) - test_msg("oid: %s", oid_to_hex(&oid)); + cl_parse_any_oid(hex, &oid); + cl_assert_equal_i(oidtree_contains(ot, &oid), expected); } struct expected_hex_iter { @@ -44,19 +43,11 @@ static enum cb_next check_each_cb(const struct object_id *oid, void *data) struct expected_hex_iter *hex_iter = data; struct object_id expected; - if (!check_int(hex_iter->i, <, hex_iter->expected_hexes.nr)) { - test_msg("error: extraneous callback for query: ('%s'), object_id: ('%s')", - hex_iter->query, oid_to_hex(oid)); - return CB_BREAK; - } - - if (!check_int(get_oid_arbitrary_hex(hex_iter->expected_hexes.v[hex_iter->i], - &expected), ==, 0)) - ; /* the data is bogus and cannot be used */ - else if (!check(oideq(oid, &expected))) - test_msg("expected: %s\n got: %s\n query: %s", - oid_to_hex(&expected), oid_to_hex(oid), hex_iter->query); + cl_assert(hex_iter->i < hex_iter->expected_hexes.nr); + cl_parse_any_oid(hex_iter->expected_hexes.v[hex_iter->i], + &expected); + cl_assert_equal_s(oid_to_hex(oid), oid_to_hex(&expected)); hex_iter->i += 1; return CB_CONTINUE; } @@ -75,48 +66,42 @@ static void check_each(struct oidtree *ot, const char *query, ...) strvec_push(&hex_iter.expected_hexes, arg); va_end(hex_args); - if (!check_int(get_oid_arbitrary_hex(query, &oid), ==, 0)) - return; + cl_parse_any_oid(query, &oid); oidtree_each(ot, &oid, strlen(query), check_each_cb, &hex_iter); - if (!check_int(hex_iter.i, ==, hex_iter.expected_hexes.nr)) - test_msg("error: could not find some 'object_id's for query ('%s')", query); + if (hex_iter.i != hex_iter.expected_hexes.nr) + cl_failf("error: could not find some 'object_id's for query ('%s')", query); + strvec_clear(&hex_iter.expected_hexes); } -static void setup(void (*f)(struct oidtree *ot)) +void test_oidtree__initialize(void) { - struct oidtree ot; - oidtree_init(&ot); - f(&ot); - oidtree_clear(&ot); } -static void t_contains(struct oidtree *ot) +void test_oidtree__cleanup(void) { - FILL_TREE(ot, "444", "1", "2", "3", "4", "5", "a", "b", "c", "d", "e"); - check_contains(ot, "44", 0); - check_contains(ot, "441", 0); - check_contains(ot, "440", 0); - check_contains(ot, "444", 1); - check_contains(ot, "4440", 1); - check_contains(ot, "4444", 0); + oidtree_clear(&ot); } -static void t_each(struct oidtree *ot) +void test_oidtree__contains(void) { - FILL_TREE(ot, "f", "9", "8", "123", "321", "320", "a", "b", "c", "d", "e"); - check_each(ot, "12300", "123", NULL); - check_each(ot, "3211", NULL); /* should not reach callback */ - check_each(ot, "3210", "321", NULL); - check_each(ot, "32100", "321", NULL); - check_each(ot, "32", "320", "321", NULL); + FILL_TREE(&ot, "444", "1", "2", "3", "4", "5", "a", "b", "c", "d", "e"); + check_contains(&ot, "44", 0); + check_contains(&ot, "441", 0); + check_contains(&ot, "440", 0); + check_contains(&ot, "444", 1); + check_contains(&ot, "4440", 1); + check_contains(&ot, "4444", 0); } -int cmd_main(int argc UNUSED, const char **argv UNUSED) +void test_oidtree__each(void) { - TEST(setup(t_contains), "oidtree insert and contains works"); - TEST(setup(t_each), "oidtree each works"); - return test_done(); + FILL_TREE(&ot, "f", "9", "8", "123", "321", "320", "a", "b", "c", "d", "e"); + check_each(&ot, "12300", "123", NULL); + check_each(&ot, "3211", NULL); /* should not reach callback */ + check_each(&ot, "3210", "321", NULL); + check_each(&ot, "32100", "321", NULL); + check_each(&ot, "32", "320", "321", NULL); }