From patchwork Thu Feb 20 08:29:55 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Seyi Kuforiji X-Patchwork-Id: 13983477 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 1702C1EB5C1 for ; Thu, 20 Feb 2025 08:30:11 +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=1740040213; cv=none; b=JFQ8syDk6J0KSPiKlVcS+4zv6N/ODS58pZIQzpR88UmJ06jwiHrBceKbxUK65XRuyeULC4VJT5e24G92niT3TO3ITN9+NumhoyjYCCnJDFddJtdWujOIGgIpNCQzZsCjhMB0mhQ1jAmgmmGzW/t+sFwU9J5pvYCIm3lBPX4dz4g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740040213; c=relaxed/simple; bh=qWGfasLMZtQ5KSysSkSmO82av0z2MRy2vgLBbeyiFNo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=l4aOGgdJ93/SqZNLd69ijdmzf7GFDU+brQNKcBB8yS0lfNAhVa8edyaJh14bzvdUGN/8WhPTVsT7szQaqVUKiEamHuz6jKMhiK5VAsCHagCSqYLx6ZpthtgnNCxqX1L0sXNcrFWCzz7yL9X3s7roIkacWOZf31fGQymuaXBCki0= 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=m/qAUKLU; 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="m/qAUKLU" Received: by mail-ej1-f45.google.com with SMTP id a640c23a62f3a-abb97e15bcbso124596866b.0 for ; Thu, 20 Feb 2025 00:30:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1740040210; x=1740645010; 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=XFsVhNE7usTqD/kbJ6QEgxQEFOfvh9nTaAXkr5/PEa8=; b=m/qAUKLUgLTMTC/ODwX2xlTPD3zpmCdHQJ3IjzBi5/Z9eX3+jUXhIFwB6i2QAWrJlI kPcgWC0aA7lpwwcSJ8Ho9NqyYMKXMyBIDzMGMzr/Yh1orSDjXrBS2v4TXI11eOtfZebt CzVHGW5kLKG/uNRMTiKWJ0XM7yc3xlHFjfASA1KVjdoACPGvGVS/q+thE5SXrJiu2U57 wpXsEN0eOHBunq1PFZNlb3HJwUxb/Iyt2Pzvj8aXLVmAR9TBG8SoO4LbqkrL64gwXXBf i4tt+ogsai4FKRTPuigmPCosu9nOs6Ki7YGMZTwd/tRkD2p/MDhOpLapkeG92yvlOEgX 727w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740040210; x=1740645010; 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=XFsVhNE7usTqD/kbJ6QEgxQEFOfvh9nTaAXkr5/PEa8=; b=klutXQbfJKQwjreKCRorL9lIhgIYkftdxCQsoD4Y9eT24hXxQdphxPsxsIYa9mSI1G lhmhmSjq2nomGxohtQYun7ZaTQsvlIr8K8Cd3zHOsCKKqX8S0H+v2AEWEQjtgbSndxx4 Fd/5YU6rabzEjYcjP1xjfR/PwtjZELjPpnxiDzhGE1Ew8q58PFap+uiPh4SN2SarA6V6 WtFvhHOSJvDR51dQUYDSGqbnuYqCVro8W76+e5mUyu9AnSkJZhcXOJbpViBuWj/PvbqB oPxwScN0odQt+Q0yKjo9WGTVP8hE9oDGsH/HfghxOXIIb1eZ0sy6g2cHa450rlLPnU6p rMew== X-Gm-Message-State: AOJu0Yz7QnOpRRUCn/sMOudzQWeT0YUOzu1hrpVi/y4RXBD9xjzCFqds //gH+B4gYnY2+fstCK909kGnUdYClKTBeY3xVYxnhIOKvCUnADeBCE43wOLX78vT5A== X-Gm-Gg: ASbGncvffsphnjCf78feV0G6vMZbGDGhRMsG/uSux+nHVumnf5e5ziobvHV4Xi6I56C YD/GFvuEjHJjHhwiI6hTJkzmn80WpRXcEpWO0kZOYaS/VNN++7t1iplS0TTHaHSr6iICUU72vz/ aumi2jqB843eBmAGATVdNB9G463OOV3WL3E6Zg3Jk29a36FU8hzWIm48gg0oOqnDWU0ZGo4SCr3 1BswqJmg3AxU+hpSRYET3Hb/1zSgCyy3gCS9pzDdhvreekw63eyGbWXIzm4E+NOExs4AAsMrwde 7SO8MVtddjLO+OlJA9TriEFGc5k= X-Google-Smtp-Source: AGHT+IES92wZp9UHTCg7ZWU33z/a+cfeygS8gZIxnlFfQlcZ4RuDVgogV/x9cJWSZBiI2SqV6sDcWw== X-Received: by 2002:a17:906:32c4:b0:ab6:6018:df18 with SMTP id a640c23a62f3a-abbccd0052emr551192866b.6.1740040209435; Thu, 20 Feb 2025 00:30:09 -0800 (PST) Received: from localhost.localdomain ([154.118.9.41]) by smtp.googlemail.com with ESMTPSA id a640c23a62f3a-abbdf53d765sm230533966b.39.2025.02.20.00.30.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Feb 2025 00:30:09 -0800 (PST) From: Seyi Kuforiji To: git@vger.kernel.org Cc: ps@pks.im, phillip.wood@dunelm.org.uk, Seyi Kuforiji Subject: [PATCH 1/5] t/unit-tests: implement oid helper functions in unit-tests.{c,h} Date: Thu, 20 Feb 2025 09:29:55 +0100 Message-ID: <20250220082959.10854-2-kuforiji98@gmail.com> X-Mailer: git-send-email 2.47.0.86.g15030f9556 In-Reply-To: <20250220082959.10854-1-kuforiji98@gmail.com> References: <20250220082959.10854-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. Implement equivalent functions in unit-tests.{c,h}. 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 --- t/unit-tests/unit-test.c | 42 ++++++++++++++++++++++++++++++++++++++++ t/unit-tests/unit-test.h | 19 ++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/t/unit-tests/unit-test.c b/t/unit-tests/unit-test.c index fa8818842a..13d54f192a 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" @@ -62,3 +64,43 @@ int cmd_main(int argc, const char **argv) strvec_clear(&args); return ret; } + +int init_hash_algo(void) +{ + static int algo = -1; + + if (algo < 0) { + const char *algo_name = getenv("GIT_TEST_DEFAULT_HASH"); + algo = algo_name ? hash_algo_by_name(algo_name) : GIT_HASH_SHA1; + + cl_assert(algo != GIT_HASH_UNKNOWN); + } + return algo; +} + +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; + + 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); + cl_assert_equal_i(ret, 0); + + strbuf_release(&buf); +} + + +void cl_parse_any_oid(const char *hex, struct object_id *oid) +{ + int hash_algo = init_hash_algo(); + + cl_assert(hash_algo != GIT_HASH_UNKNOWN); + cl_parse_oid(hex, oid, &hash_algos[hash_algo]); +} diff --git a/t/unit-tests/unit-test.h b/t/unit-tests/unit-test.h index 85e5d6a948..ebed51212f 100644 --- a/t/unit-tests/unit-test.h +++ b/t/unit-tests/unit-test.h @@ -8,3 +8,22 @@ snprintf(desc, sizeof(desc), fmt, __VA_ARGS__); \ clar__fail(__FILE__, __func__, __LINE__, "Test failed.", desc, 1); \ } while (0) + +/* + * 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. + * WARNING: passing a string of length more than the hexsz of respective hash + * algo is not allowed. The hash algo is decided based on GIT_TEST_DEFAULT_HASH + * environment variable. + */ +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 + * 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); From patchwork Thu Feb 20 08:29:56 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Seyi Kuforiji X-Patchwork-Id: 13983478 Received: from mail-ed1-f52.google.com (mail-ed1-f52.google.com [209.85.208.52]) (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 2C6501EB9ED for ; Thu, 20 Feb 2025 08:30:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740040219; cv=none; b=UpUcJ35lrRZzIfv6/sIaN5Sk3JEIZCftwGpX5o1q9FJykVCLf04hv4Ykhs9KBxiCjw4jQOAPEt0YzC8DKVxyiNwwgSAvZQfjzWZvVIPX4nnXFKmVb8S//hm6VoNqh5f2fJIYz2EqTNpDtYnYHe+83CWxdndM6DKerJUiml3iYIU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740040219; c=relaxed/simple; bh=Be3KJTWu3uvyXcSjaqWEvMLRjjdwMqKHoc/BlKEw/Ck=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=sceJ+ytQ4x/liIBBtzv60q/cfjFLJKULsero+xaFBjWdeWStKfCHRnqzQ2zoNwYD0aoFxTu4tlvwQy6CLt5mNMg5zXvO50qhKtJ6f1SMf9tPFc7I/MjfGR7bRqhf3gY82mMcIsaIzlWw6d6JJvEPJ/y4JuDMHlTOYIdYARC/vqU= 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=X2talzlS; arc=none smtp.client-ip=209.85.208.52 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="X2talzlS" Received: by mail-ed1-f52.google.com with SMTP id 4fb4d7f45d1cf-5e08064b4ddso865890a12.1 for ; Thu, 20 Feb 2025 00:30:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1740040213; x=1740645013; 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=KtN+XstNeI7gMandsxKaPbJcGHFunT4zd9h1h42MJ+A=; b=X2talzlSjMJqs6eq3btSyGWmVcduBz3uDNt8aNhFZa2GII/4rWO64ORq1zXEQycUO8 AvZ2cOFaOGxiE/+Vv2/TuShWA9yi2asVoI1VVcR1OddTL4H9lnl39ZoN/Vdf4p90IEUF W/stS8st1yqEhZDJosAdNkOY6k3iXutLlf6QJTwzXuh2UO7AAkqF4AhVZ30OiVjvMJWc ZoB1wOkF6I/X/rfFXbNEqVT62VFrm+3Y8T40CB+oPKfDFJ92Y5qx7SD6xXyi0P51j8CC Ttwqf4mqDJARNOJm1avMTM0udtwAVd3hLJPr30UbJtNK9IExV7hnFFRQeIl9IHQ2/5Sd NCJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740040213; x=1740645013; 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=KtN+XstNeI7gMandsxKaPbJcGHFunT4zd9h1h42MJ+A=; b=rZbgxgLmYMj5UZtpVBkq8j2a1f5P79uuG2+Tbf6EUOkGw7UIA66p92TUfFFQHR4a3g nwyLpj9+psmjiltZMEggCUikqCSmzbz9/Uxw6cIM1BcF2MmGOUabjD7xBPKaU5tWjB0N 3X52j+rgLKW+FzzenrJX83L2JSXCljDdUzcx7uba1XFLnXZSyRsqWvOXGJjJV4jbY78e MM4xk0UWO84PxI39u4963J44ecdM2CMRx9OptfwM5hq0BRvKMyXAiAs/dpfJX5cCL2Ie kg1PFC2eFw/xJlnaNbqYZ6mZkudw63zstsvlrOg4DNwWM+lc6HiH/64FqO66M2jKgtqG 7j4g== X-Gm-Message-State: AOJu0Yz98VsyVW63Z8rG6sSkeVBC3xHcwzlHWuG8rZ3yhtUY0RJUNyDn omlJP1eYXs+YurBzlu4hPZy2hX6GiYVwNvHUM9HqA0D7KvXaPqeKQlnkMF0IUGo81w== X-Gm-Gg: ASbGncv7z98ygoGSZq/ZzrMi5U79jc9+Ra9Yw1Tk/xa6u6Jbvpkot61Xuo9WFWSlpUD 5mrgDXmepYFI3wyq7lJnk93Hf50Jr/Y3T1M5U+1qrlRBvRF6zyFwQnFO8IcI3xMXsofvlbgys1N To5z3OTdh4e0PAFXBw3KKhAdIl1Z5WYDyzfDE6TM9H6ix978rB/dSeGI5kzNilYVwY0X3kTpFjs Wvx2JwdCePkc5Pp+PycK5XCNY9mj9iG7KwVTYqA35GPc444X+ouTZZ1m7m6BZ2+IhhRrSRZxDgb zzpTweG0B2VdPqu910b2CKmAYz8= X-Google-Smtp-Source: AGHT+IF0YLhABO9SfXLxGvbrnIi0C4GwW568u0utQdHBc3WPn2dq+Ue198PmYcg07tn3ZxqQkuEvWg== X-Received: by 2002:a17:907:d204:b0:abb:b249:4410 with SMTP id a640c23a62f3a-abbb249464amr1247096966b.39.1740040212987; Thu, 20 Feb 2025 00:30:12 -0800 (PST) Received: from localhost.localdomain ([154.118.9.41]) by smtp.googlemail.com with ESMTPSA id a640c23a62f3a-abbdf53d765sm230533966b.39.2025.02.20.00.30.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Feb 2025 00:30:12 -0800 (PST) From: Seyi Kuforiji To: git@vger.kernel.org Cc: ps@pks.im, phillip.wood@dunelm.org.uk, Seyi Kuforiji Subject: [PATCH 2/5] t/unit-tests: convert oid-array test to use clar Date: Thu, 20 Feb 2025 09:29:56 +0100 Message-ID: <20250220082959.10854-3-kuforiji98@gmail.com> X-Mailer: git-send-email 2.47.0.86.g15030f9556 In-Reply-To: <20250220082959.10854-1-kuforiji98@gmail.com> References: <20250220082959.10854-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} | 123 +++++++++--------- 3 files changed, 65 insertions(+), 62 deletions(-) rename t/unit-tests/{t-oid-array.c => u-oid-array.c} (35%) diff --git a/Makefile b/Makefile index bcf5ed3f85..f8e061365b 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 @@ -1366,7 +1367,6 @@ 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 -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 780939d49f..93410a8545 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', @@ -48,7 +49,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 35% rename from t/unit-tests/t-oid-array.c rename to t/unit-tests/u-oid-array.c index 45b59a2a51..6d173dc004 100644 --- a/t/unit-tests/t-oid-array.c +++ b/t/unit-tests/u-oid-array.c @@ -1,22 +1,18 @@ #define USE_THE_REPOSITORY_VARIABLE -#include "test-lib.h" -#include "lib-oid.h" +#include "unit-test.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 +30,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 +54,76 @@ 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); + cl_assert(algo != GIT_HASH_UNKNOWN); + 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 ? "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 Thu Feb 20 08:29:57 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Seyi Kuforiji X-Patchwork-Id: 13983479 Received: from mail-ej1-f47.google.com (mail-ej1-f47.google.com [209.85.218.47]) (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 BEF9C1EB19A for ; Thu, 20 Feb 2025 08:30:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740040220; cv=none; b=eRR/FJLoyZyi7Us5jRbHvrJ88mrmT3kueXcXSp6EHw0J9v3cfafwCh52JUkV8DxZqO51eg/ciVydujdXYv+QWpNQDzY9qoSF1+I+nLy41OxfLQEvYDIuegvKqxGiIfcSeYltb+XAP/a1DN575fsmo4hfeiSTmbfqpN7jkD0sdFc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740040220; c=relaxed/simple; bh=3Jq2RZvMnDlzpfaMH1TSZOQiU4+wNouwJS4B/hytduQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fwob/heittn8pTKFvtbKyRYNm93btF18xa0qe09sUjCFSh/fTCkHQngs6ur55bkv3eX7v+Wcef3W1IFmM7G0Ju7BxTPRZ9KFKuKYbOcmBmuDAoFBYW6gbGlqis7JaJrUDPxAkv8fNxG/GF0IXUsO+7bs+/b4GSPuINKmKmqVYr4= 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=gt+L2gYW; arc=none smtp.client-ip=209.85.218.47 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="gt+L2gYW" Received: by mail-ej1-f47.google.com with SMTP id a640c23a62f3a-aaf0f1adef8so138504966b.3 for ; Thu, 20 Feb 2025 00:30:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1740040217; x=1740645017; 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=w0saPmNXMvuBHkwP86QbxEMbqf8OPcq/bqWcgcbYAWc=; b=gt+L2gYWC7HKtTjNJiGNAbHLVUp2c/Tih0IpERUGOpMV6B62JElWA4LBfNPO6HdnyB QZ2K8d6/2l+3iV968vjXlc45b10cnsqUGigecDaw8dlP88GH4l1/x1l3+HNHluDdiipC +JxbI3D0bFiyrKErf7PtrFmpy6MLCS0LUPVeZY7Z0PCSNTvr4GV6BNgfkHIXFg1G/uJ5 G+LnnQdc1Pd6rhOHcDrBBeu0LXrKLg9KQDNrhm9m6w0qX15K73QQDq3AbStRgjlVrJcO Q9UyGSmTHiTlNLHXU9ERyPgkvmWdQzDJi8aywUrAhwuhwkfVPYsaaIYLP9DdRdNv6uaO ElZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740040217; x=1740645017; 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=w0saPmNXMvuBHkwP86QbxEMbqf8OPcq/bqWcgcbYAWc=; b=Rm7UjeQSXziO3UolpN3Bsw6F51hiG+6Bdf4rj5l24BWq4VuaeQiPmwyPDc+cse09nM BTT5D4niQgCnU2KRx5vdEblbAj640/k1XVDp6UheoKynipdMpjiE/LCLdwV6Fb8ut0o2 xxgtwMFBU0FS3744+HahxN0YsWtCeCRTXY0xCANzQmTq9dqTyn04goxVxD/i6EhzN8jk 6vBP9vw9kaIiTrnep+jAhemnFZ4U60cyFuP5U2W26ZiHnvmmAT1qBtHpIQNanT2/WpNV rFe2IywEEQELqO3ITjeE+HAW/hshxYsW1lGZ8l8HNdpZN7dghGKVukhQ+si6naa+dUe3 ZeFg== X-Gm-Message-State: AOJu0YwvoeblWG4VAOtCqX4Born+HPng8zPA5iU8XozaLPe/iKCCj/WL 1puCPwoUCFX/ovSw2zx5WXLZljW6SXWpeolgPZU1ixfvtlROPVFBX4kiyfxnuWtlYA== X-Gm-Gg: ASbGnctzv7wye/L2hEh67zYCHpx0hT52d090+5N/Nnio6X8VjpiCoFUk8COy3v7Lpmg rkSyrSHHFRp+oT4mlSAVA8aM7TM6ORC0k7vlZZEdoN75MRJRqp2BkHctosmIMSiM5oXY3KjzxqW dYvShuPTEXC1JWN1BslNzxmi0ItjwAAb7MYL/sYDgIn0rwKywVfr3A3ScZe5u+ItaEcsl0gdkbb B/u9dyXsLsRsdzlxSZdq0hG2XCBHH2EBP9PrBKGaLHUnM5/07FxHWh0rPzuRURA7lDfu6HIl3MY NcWxJmdFLxoOtC8IYbyiYVqIUdI= X-Google-Smtp-Source: AGHT+IGUqIt7zxfZ0eGTL/O5mFiOfHSK7GFIYxrNthtxQG+90AUhhdIjjNeGjM6PmPK2yCxhDN6RgA== X-Received: by 2002:a17:906:1bb2:b0:aba:f6ff:d38a with SMTP id a640c23a62f3a-abbccf0b152mr680303766b.29.1740040216551; Thu, 20 Feb 2025 00:30:16 -0800 (PST) Received: from localhost.localdomain ([154.118.9.41]) by smtp.googlemail.com with ESMTPSA id a640c23a62f3a-abbdf53d765sm230533966b.39.2025.02.20.00.30.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Feb 2025 00:30:16 -0800 (PST) From: Seyi Kuforiji To: git@vger.kernel.org Cc: ps@pks.im, phillip.wood@dunelm.org.uk, Seyi Kuforiji Subject: [PATCH 3/5] t/unit-tests: convert oidmap test to use clar Date: Thu, 20 Feb 2025 09:29:57 +0100 Message-ID: <20250220082959.10854-4-kuforiji98@gmail.com> X-Mailer: git-send-email 2.47.0.86.g15030f9556 In-Reply-To: <20250220082959.10854-1-kuforiji98@gmail.com> References: <20250220082959.10854-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` handles the necessary checks needed for the test to run smoothly. 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. This streamlines 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-oidmap.c => u-oidmap.c} | 153 ++++++++---------------- 3 files changed, 54 insertions(+), 103 deletions(-) rename t/unit-tests/{t-oidmap.c => u-oidmap.c} (32%) diff --git a/Makefile b/Makefile index f8e061365b..58a6af1eb0 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 @@ -1367,7 +1368,6 @@ 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 -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 93410a8545..f9e0ae15df 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', @@ -49,7 +50,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..e40740db5c 100644 --- a/t/unit-tests/t-oidmap.c +++ b/t/unit-tests/u-oidmap.c @@ -1,5 +1,4 @@ -#include "test-lib.h" -#include "lib-oid.h" +#include "unit-test.h" #include "oidmap.h" #include "hash.h" #include "hex.h" @@ -18,102 +17,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 +103,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 +115,18 @@ 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++; - } + cl_assert_equal_i(key_val_contains(entry, seen), 0); + 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 Thu Feb 20 08:29:58 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Seyi Kuforiji X-Patchwork-Id: 13983480 Received: from mail-ed1-f53.google.com (mail-ed1-f53.google.com [209.85.208.53]) (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 D8A741EB19A for ; Thu, 20 Feb 2025 08:30:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740040224; cv=none; b=onSdwih7kNGLMjZaYzO9CDUgaaO4f2RNMTbaprQdCILOxh8VoWkhqOtO/DDDSri5PzpELgtaTH0tKkiu2SVcxUQdMzSaN9LXvAcmTsgWoTKTN4s7PiIps1+98zNaf4xSI+epc/vIon6C6nK81jWwvBK+FDHvFZKaxpCGy9YFMA4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740040224; c=relaxed/simple; bh=r08WGRJR85V8Ugbppb01qn/Cn8xBlRgMH8Qc94B5YxY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=C4Dyku1S1hnC8hpFtGFudweUiJfmAQzGcqx+IdeGSKVqJlTPYsgFBpLT69nCkKuEQaGMDWVY/Qf/PX3M4IeDKCtoh91NJfEEAUhYyiyWhFAtu8OYvOzba9CO+H7AyZA86yM+AH6wXnXl9hPjCDhy5qYN4Jil3Knp2qwWGv+zmV8= 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=VVOr3yvl; arc=none smtp.client-ip=209.85.208.53 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="VVOr3yvl" Received: by mail-ed1-f53.google.com with SMTP id 4fb4d7f45d1cf-5ded46f323fso824258a12.1 for ; Thu, 20 Feb 2025 00:30:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1740040221; x=1740645021; 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=tMNe0KNVZ0ezW/BXclTfZ4dcGv1NO3f0HWS3/rdroXA=; b=VVOr3yvlPkz0lUROXG1c8AMpqKSKdVEf+w7aAlJRc0qMsSK9kx9zNueI2/0p+NKSNB 0N2vimOjZHd5rWdqslIGLmilo+0obOIBDVTS8Evts9US1nXlTy6XgWSDs8GO/6MCen3l TSur1fQOeWbw97BpwOdJB8Y082qXAbh/xaKzvcoxP6v7q80pFynYbVHeAI71dD0kvqFX p6JgDIE03RQoS9CMrY8MTXJsZ4AAbl6Q7IOhrZhvqMfZWL+JJchS3CVzPbvA7ALWAZ1K BNs046P2zpjsqHf5/dvIRxhjkRYJvGR7OOuIPfUBhP17yLJU6yHkfoKf0knUQ2K49/lr Nx/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740040221; x=1740645021; 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=tMNe0KNVZ0ezW/BXclTfZ4dcGv1NO3f0HWS3/rdroXA=; b=P8wVWyiQa8pi1Rg8ZvbKTKjAKtAq3MLqinLcwkdTVWbPPlm0iTKncP65QGKevdID8T LRHLQ9VGFCmwtJmH82t0z1HLOZggiWv/gu7OKvt8ls7tvoWk2EMU5W831B2+VEDtfFHS wbLEVJ5jpbzS5MD4/hnjOhu8QWy3qzeKXI+X9k+DuEcYbUZhoPe8y92gz0ZIqXKdtuFC GcJCxutoFKOIlTfIjdMjChy+Sy5PAfTTY1H1UtHWiIDDJ1HYPd9I6dNJfg7DJpyUzJMv Ro+qcxiFISxRESar66LhyxEshC8nS1qLgLxwlmtvZVoFJa85BvWoAorMSHu6hvo3W2TO arnw== X-Gm-Message-State: AOJu0YwUMz7p/y/Exlaoz2Gj+XU9UlboTBNgvxFcTIYG6cDOCCx7GR8q c4VEFpTU+cjL0Okm1A9dgqyh61B2xjNxguEhgupba1rOP1z+ItGEJZiPXdlOZ9qJ4A== X-Gm-Gg: ASbGncvQ3RNpleXheeWIQqfr6vZdhPO/pwpsFXnsyJEgV67iLLnqJs88fjnEGCd7kZl g+ptUItBZbKh0pf9kGmhWEdP1tq6FVTo0mq8w409Ek+6pbgjklaXjnHaskNR6Z0HsselD5qD5wi MSVlQLhXJDEojnlD40b1BxW1UnmYW0aGY9aZCYUsWJ3vqfmfgHyQpGHcSlcWQJ58kxBjuYktHb8 BT4/2ARxO4BaDmuO74dxYTLJmN/sn2Jy5SzIGDWqcVLnxXBsDk0cpv2qaSiHJcB3mZNkZfWActC 6FSacaog97DoJ/1Al/PkRAIq6qI= X-Google-Smtp-Source: AGHT+IF7gA3IWByoPBDVf2DJC+w7Qn1aJ4skQ3gPfkK1T1CpPAQ7SqEkv7OSIeAv3YlxZJDzlsRUGw== X-Received: by 2002:a17:907:6d0a:b0:ab7:e8d6:3b12 with SMTP id a640c23a62f3a-abbccc51169mr721332366b.1.1740040220548; Thu, 20 Feb 2025 00:30:20 -0800 (PST) Received: from localhost.localdomain ([154.118.9.41]) by smtp.googlemail.com with ESMTPSA id a640c23a62f3a-abbdf53d765sm230533966b.39.2025.02.20.00.30.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Feb 2025 00:30:19 -0800 (PST) From: Seyi Kuforiji To: git@vger.kernel.org Cc: ps@pks.im, phillip.wood@dunelm.org.uk, Seyi Kuforiji Subject: [PATCH 4/5] t/unit-tests: convert oidtree test to use clar Date: Thu, 20 Feb 2025 09:29:58 +0100 Message-ID: <20250220082959.10854-5-kuforiji98@gmail.com> X-Mailer: git-send-email 2.47.0.86.g15030f9556 In-Reply-To: <20250220082959.10854-1-kuforiji98@gmail.com> References: <20250220082959.10854-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` handles the necessary checks needed for the test to run smoothly. 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. This streamlines 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-oidtree.c => u-oidtree.c} | 78 +++++++++-------------- 3 files changed, 32 insertions(+), 50 deletions(-) rename t/unit-tests/{t-oidtree.c => u-oidtree.c} (44%) diff --git a/Makefile b/Makefile index 58a6af1eb0..feb01702c7 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 @@ -1368,7 +1369,6 @@ 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 -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 f9e0ae15df..0b412a7c16 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', @@ -50,7 +51,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 44% rename from t/unit-tests/t-oidtree.c rename to t/unit-tests/u-oidtree.c index a38754b066..de6f6bd292 100644 --- a/t/unit-tests/t-oidtree.c +++ b/t/unit-tests/u-oidtree.c @@ -1,10 +1,11 @@ -#include "test-lib.h" -#include "lib-oid.h" +#include "unit-test.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 +17,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 +27,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 +42,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 +65,40 @@ 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); + cl_assert_equal_i(hex_iter.i, hex_iter.expected_hexes.nr); 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); } From patchwork Thu Feb 20 08:29:59 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Seyi Kuforiji X-Patchwork-Id: 13983481 Received: from mail-ej1-f50.google.com (mail-ej1-f50.google.com [209.85.218.50]) (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 3032E1EDA1C for ; Thu, 20 Feb 2025 08:30:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740040228; cv=none; b=tcFgI9akxsO2i1+0l3/zJVi7kQNZlz4JwnTCzJWhGkdJJKCi/w1Sq4sJ9jLkTJSTKcY1CPaWKjbpX6QyB8iSjMt8ilGecl5i5vmGaiiEEjKZuhu1C+zDsf3W+CVHlitNdos5E6lL50ZGy8TvKu/JqQw1mcVMnZBGM8jqopHuarc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740040228; c=relaxed/simple; bh=k4BoABB5by+iiofaE/PHK7FGScfahVzUesUSAZZAYzE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mfJI9Nj5laMzveQYtBPAOIMTTKTlc8xriZ9x6tlJ38FYaOGuEvrO3D8pG86tqLfrgZkR09jgAY1sNokq4g6PVcQ5fCkjT644mTOBflPxqXeQ39/2lStt1GCZa/rwnyuxUQ4L4Km1CkALmYyb4/aL7ASUIcXjrTpmeFH/h37O798= 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=YrDamdTH; arc=none smtp.client-ip=209.85.218.50 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="YrDamdTH" Received: by mail-ej1-f50.google.com with SMTP id a640c23a62f3a-ab7430e27b2so119106066b.3 for ; Thu, 20 Feb 2025 00:30:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1740040224; x=1740645024; 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=p+8q52LtX47sRUIbNGIeZa/PiPJPDFPMyNOagzmri7I=; b=YrDamdTHPzHabo0dQDJEdZ3lmC6EdWvD+U2lxw3Scc/nPi1s9nglZ61RmGVJ/Ne40J Cj6tq2YgJowas55TnijGlX6efziuh9sWuAxIXjwfFsm4NBX69BnA2YmP4n/AcsGpS7N7 BAgrLwHnkgckyFlUaI+RaWuPc8o9bM4L8NRYNTOVx1r5ukz+e0MErphYXVvQp7Rt/h4t jr2352g0ySvDeNb/8gJ3j5oh4/QSveB63TOyXpWVsuHjnp8YJ2hVvSl3t3GVbBdb/8e8 O+BKe/AtNYijDBfmpFj6CQ99RpXov0/+Dg3aj9W70dlCqbSKSELPnmi28Ydbz1kD9okp RXwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740040224; x=1740645024; 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=p+8q52LtX47sRUIbNGIeZa/PiPJPDFPMyNOagzmri7I=; b=b3defNs+RgVJRx0eAw2JvQHlY+jskD30aYVAB9j9RRrlGJ78ME5PHdBHH3Oq5cyLhM PsIHqUUtiZ4765Q+BS821ngpBQSZcXtxcQZbCdAsty+wkCG9iu79RAw3bOmBZfg5cZBM if7MegEugJKiecf50nWc1W8qvEnjAgFt72n4TneoFGSbOnrgTId2HWRsubfOoIbKOyZO T6wlf102EddaraR639C/DG73jqic/6A9l3QXPiIoUtY7vIICxIDFZBsr8iHhLCGVUwl8 Ew7j/fYgDJjYEimoWSSbjnFdybXrY14FpZPAB+6W5PgT8dRCZ0q1eVKo5HfNl6bzG29F SCFA== X-Gm-Message-State: AOJu0Yy1HS3mL51M6laF5EISt8ZUbVCJrstZS8ruuqCeh57AquQ+nVbh E5xW1YYhMrEKkc8/Z+833Fh3MKcLiPPS/sS0Kn+J+d71AsomRijdhDuMJBI6/hu6uA== X-Gm-Gg: ASbGncsn+Y4bA6ZNBcI3kp01745EgM4wqxxlKFdcugY3q3fDKUs1fX/yUf3Cvho3HDI p2TjIsHbd8vnZG4R3HR2w/uJOLbiqjWUGxojOw22IzoD+Za3O+uR4yd+TSeMlCemUcX3BqCKqgH 8KMRHEd4UhUrHbPJojaZEOO6m9tm03oiZdJl/aU/i219br73NokW4ZhQI/9HvwvcqnR9wd7E4Zr uE/KGeCSyXI7vjmsgvcaDLdfRPLXLN0tNjip0ETOJdhjXusr6AtWaTuOYhzmwIvBvsjRikGzYqu 1xIKncDQ4hpGamXe+TnhCXBfBVo= X-Google-Smtp-Source: AGHT+IFXVv2YIW3OchsbOgbI1Ftm3gUfiF8cOg/fNqYg++N/10X8rQL+MkDgwaH1BGJhldtphmqF+g== X-Received: by 2002:a17:907:da4:b0:ab7:6d59:3b4c with SMTP id a640c23a62f3a-abb70aa65bcmr1678631966b.21.1740040223897; Thu, 20 Feb 2025 00:30:23 -0800 (PST) Received: from localhost.localdomain ([154.118.9.41]) by smtp.googlemail.com with ESMTPSA id a640c23a62f3a-abbdf53d765sm230533966b.39.2025.02.20.00.30.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Feb 2025 00:30:23 -0800 (PST) From: Seyi Kuforiji To: git@vger.kernel.org Cc: ps@pks.im, phillip.wood@dunelm.org.uk, Seyi Kuforiji Subject: [PATCH 5/5] t/unit-tests: remove lib-oid.{c,h,o} Date: Thu, 20 Feb 2025 09:29:59 +0100 Message-ID: <20250220082959.10854-6-kuforiji98@gmail.com> X-Mailer: git-send-email 2.47.0.86.g15030f9556 In-Reply-To: <20250220082959.10854-1-kuforiji98@gmail.com> References: <20250220082959.10854-1-kuforiji98@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The `lib-oid.c`, `lib-oid.h`, and `lib-oid.o files` are no longer needed since their equivalent functions have been implemented in unit-test.c and unit-test.h. This removes redundant code and ensures all unit test-related functionality is consolidated in a single location. Drop references to lib-oid from our `Makefile`, and `meson.build` files to prevent build errors due to missing files. Mentored-by: Patrick Steinhardt Signed-off-by: Seyi Kuforiji --- Makefile | 1 - t/meson.build | 1 - t/unit-tests/lib-oid.c | 52 ------------------------------------------ t/unit-tests/lib-oid.h | 25 -------------------- 4 files changed, 79 deletions(-) delete mode 100644 t/unit-tests/lib-oid.c delete mode 100644 t/unit-tests/lib-oid.h diff --git a/Makefile b/Makefile index feb01702c7..6afa6587ba 100644 --- a/Makefile +++ b/Makefile @@ -1381,7 +1381,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 0b412a7c16..c1c4aa32aa 100644 --- a/t/meson.build +++ b/t/meson.build @@ -68,7 +68,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 deleted file mode 100644 index 8f0ccac532..0000000000 --- a/t/unit-tests/lib-oid.c +++ /dev/null @@ -1,52 +0,0 @@ -#include "test-lib.h" -#include "lib-oid.h" -#include "strbuf.h" -#include "hex.h" - -int init_hash_algo(void) -{ - static int algo = -1; - - if (algo < 0) { - 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); - } - return algo; -} - -static int get_oid_arbitrary_hex_algop(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; - } - - 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); - - strbuf_release(&buf); - return ret; -} - -int get_oid_arbitrary_hex(const char *hex, struct object_id *oid) -{ - int hash_algo = init_hash_algo(); - - if (!check_int(hash_algo, !=, GIT_HASH_UNKNOWN)) - return -1; - return get_oid_arbitrary_hex_algop(hex, oid, &hash_algos[hash_algo]); -} diff --git a/t/unit-tests/lib-oid.h b/t/unit-tests/lib-oid.h deleted file mode 100644 index 4e77c04bd2..0000000000 --- a/t/unit-tests/lib-oid.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef LIB_OID_H -#define LIB_OID_H - -#include "hash.h" - -/* - * 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. - * WARNING: passing a string of length more than the hexsz of respective hash - * 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); -/* - * 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 - * GIT_TEST_DEFAULT_HASH's value is valid or not. - */ -int init_hash_algo(void); - -#endif /* LIB_OID_H */