From patchwork Thu Jan 30 09:13:31 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Seyi Kuforiji X-Patchwork-Id: 13954360 Received: from mail-ed1-f45.google.com (mail-ed1-f45.google.com [209.85.208.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 B46861B4F21 for ; Thu, 30 Jan 2025 09:13:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738228434; cv=none; b=qyzBcAfOzmUEAfkwfMCZJ0T9zz0yyRGweE9oNqxUTb/bst8OIzekI/sICpZ1dZFQNrfvLuIyLQK9PuBnCn9ZxJGxMOpaJokcFeWgl2bgBP0nE63PPS6nmxe86OtmGfpucVrjewuaR0TcU0wXV9lGxwqTdshLzZ2wJgyzAJStWTQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738228434; c=relaxed/simple; bh=UgNTkQj40msqh+R/SNKGNOqbZ6wmSpJune27bgM+/s4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OLladfLnF7E66KXRv6YMX0LDE/38cg4vOJMeFjeIZDE3N+2nbdgbSSZ7ilbus627xqbU7+MMOOpxiUhX7xnlOZaOQKd6yLguMCNfA3OJahog6QT51GXGkx7WSW/y8rSf4ddo16xlJO/j0hlhg6VX9jGrqefCyPUToPr8ESoFmVU= 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=GIIG8O94; arc=none smtp.client-ip=209.85.208.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="GIIG8O94" Received: by mail-ed1-f45.google.com with SMTP id 4fb4d7f45d1cf-5dc75f98188so315764a12.2 for ; Thu, 30 Jan 2025 01:13:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1738228431; x=1738833231; 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=cHt6Aan/+mIR1lo66eP2Rr0CLNK8OTEJCD2RdeBRRKQ=; b=GIIG8O944Hi9lpwU/RsY1dsmjjZaWHWy+QUMIpJXWj4LRK/lW4ODN5m1Yu+1txFc/1 wfARRV0PraEA3Ovz7bfd77ZD+GzAEdfG49AdqO1VvfU0JEI/aAbu08go7as2In61qKkY BcurBEgX/Hhvm85YKaptxuWvEP/58O9XAoTG8cWciYI8Cxo2NBrLxHlGHGibU+NCEvjO 4AM53zsY6nFGBbf28qFUZGsu6JnnIT8IAJCgESjFflDCkmG/ffTsheKLix0blYsGGY8d yP/5tLFyUvGYOzMxNtPkYamqYkj8DSDWOrNtgTq98OkQ23bKnvyOFHIN+drJmZik0DXG hrsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738228431; x=1738833231; 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=cHt6Aan/+mIR1lo66eP2Rr0CLNK8OTEJCD2RdeBRRKQ=; b=bYx22yL8ILXaLApzRBOMPu2R6kkD/XQEc0xmKQSNzqXpCqApHvAT60LDnfnBtT56l6 w937lmKGO9dCsmypwlSdLvEO5ja1npqc0Qq7RPaeq0wUx7g9EgMDshu9YZpVucLFl7Q7 V4gMaEjF5sr6arOI3fL22XBsHOwe3QGzzzlp8jnFmYiEmiUL53KMjmj0FEogio/kICfA rergacQf8ntJqb3RFAnyhuFM7KsULQZM14t1ebitdiLHMxbWdN/PCE0uAUHf9usU19DO eYRK8E/SRYWDhkf3ORvgTn9WMcrOHKTEGzQOSD6EWen0foneHEZAq5Pei8HlKOkfPj5X n1PQ== X-Gm-Message-State: AOJu0YzCwDvRsLJA84lKidwAlezmEaxtL/rguyuHJ8OoJ0q2uoFDcNru d0qcQuaubYSNDsUOswIg9ATfIW7JuTuE72nRvWruF3lV0kFxOWkfpzZOK1WZIZIPBg== X-Gm-Gg: ASbGncuzzMg/CD3E8ycgNWRzc6D4TMvb0a+IFpnJDlsFgAfWUgXxLlbr1Sa18FFLe8R ErysoQ3tuuEwsijJvsig5MeUHeyrmGc+HvQJBgYjKt8UnfXpZJ2CEmfFYqFFQZmxwRAFGOZlXsV wrUwPhj8s8tkcCZNNeR+kaHVGhWSlj0hVyVKN4F2L9vqret4+i8of1mCKvUP57v96aODfWL9980 UMRSuQZcNx01UNNfyV/OHF/5kp8q8cfMprQQIsoHqjnIRnXymfSkyWBrkpwBFR3OXt3fgGhvzae ojZoAuuZSNitK01ri+0s6AUfLfy2Tw== X-Google-Smtp-Source: AGHT+IG+UmZ48475shKcbxoByRQ3bCgORr/kqs1gDuvBJf4igG55cgTUftRX+xYmdMMiwvSK71iNZA== X-Received: by 2002:a05:6402:3589:b0:5dc:5c18:6cc with SMTP id 4fb4d7f45d1cf-5dc5efa8af0mr5795536a12.3.1738228430424; Thu, 30 Jan 2025 01:13:50 -0800 (PST) Received: from localhost.localdomain ([154.120.90.178]) by smtp.googlemail.com with ESMTPSA id 4fb4d7f45d1cf-5dc723e9ee4sm742249a12.25.2025.01.30.01.13.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Jan 2025 01:13:49 -0800 (PST) From: Seyi Kuforiji To: git@vger.kernel.org Cc: ps@pks.im, phillip.wood@dunelm.org.uk, Seyi Kuforiji Subject: [PATCH 1/4] t/unit-tests: convert hashmap test to clar framework Date: Thu, 30 Jan 2025 10:13:31 +0100 Message-ID: <20250130091334.39922-2-kuforiji98@gmail.com> X-Mailer: git-send-email 2.47.0.86.g15030f9556 In-Reply-To: <20250130091334.39922-1-kuforiji98@gmail.com> References: <20250130091334.39922-1-kuforiji98@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Adapts hashmap test script to clar framework by using clar assertions where necessary. Test functions are created as both standalone and inline to test different test cases. Mentored-by: Patrick Steinhardt Signed-off-by: Seyi Kuforiji --- Makefile | 2 +- t/meson.build | 2 +- t/unit-tests/{t-hashmap.c => u-hashmap.c} | 226 +++++++++++----------- 3 files changed, 114 insertions(+), 116 deletions(-) rename t/unit-tests/{t-hashmap.c => u-hashmap.c} (60%) diff --git a/Makefile b/Makefile index b0cdc1fd4a..2d9dad119a 100644 --- a/Makefile +++ b/Makefile @@ -1339,6 +1339,7 @@ THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/clar/% CLAR_TEST_SUITES += u-ctype CLAR_TEST_SUITES += u-hash +CLAR_TEST_SUITES += u-hashmap CLAR_TEST_SUITES += u-mem-pool CLAR_TEST_SUITES += u-prio-queue CLAR_TEST_SUITES += u-reftable-tree @@ -1349,7 +1350,6 @@ CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/clar/clar.o CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/unit-test.o UNIT_TEST_PROGRAMS += t-example-decorate -UNIT_TEST_PROGRAMS += t-hashmap UNIT_TEST_PROGRAMS += t-oid-array UNIT_TEST_PROGRAMS += t-oidmap UNIT_TEST_PROGRAMS += t-oidtree diff --git a/t/meson.build b/t/meson.build index 14fea8dddf..af597f9804 100644 --- a/t/meson.build +++ b/t/meson.build @@ -1,6 +1,7 @@ clar_test_suites = [ 'unit-tests/u-ctype.c', 'unit-tests/u-hash.c', + 'unit-tests/u-hashmap.c', 'unit-tests/u-mem-pool.c', 'unit-tests/u-prio-queue.c', 'unit-tests/u-reftable-tree.c', @@ -45,7 +46,6 @@ test('unit-tests', clar_unit_tests) unit_test_programs = [ 'unit-tests/t-example-decorate.c', - 'unit-tests/t-hashmap.c', 'unit-tests/t-oid-array.c', 'unit-tests/t-oidmap.c', 'unit-tests/t-oidtree.c', diff --git a/t/unit-tests/t-hashmap.c b/t/unit-tests/u-hashmap.c similarity index 60% rename from t/unit-tests/t-hashmap.c rename to t/unit-tests/u-hashmap.c index 83b79dff39..6b6d22005a 100644 --- a/t/unit-tests/t-hashmap.c +++ b/t/unit-tests/u-hashmap.c @@ -1,4 +1,4 @@ -#include "test-lib.h" +#include "unit-test.h" #include "hashmap.h" #include "strbuf.h" @@ -83,23 +83,23 @@ static void t_replace(struct hashmap *map, unsigned int ignore_case) struct test_entry *entry; entry = alloc_test_entry("key1", "value1", ignore_case); - check_pointer_eq(hashmap_put_entry(map, entry, ent), NULL); + cl_assert_equal_p(hashmap_put_entry(map, entry, ent), NULL); entry = alloc_test_entry(ignore_case ? "Key1" : "key1", "value2", ignore_case); entry = hashmap_put_entry(map, entry, ent); - if (check(entry != NULL)) - check_str(get_value(entry), "value1"); + cl_assert(entry != NULL); + cl_assert_equal_s(get_value(entry), "value1"); free(entry); entry = alloc_test_entry("fooBarFrotz", "value3", ignore_case); - check_pointer_eq(hashmap_put_entry(map, entry, ent), NULL); + cl_assert_equal_p(hashmap_put_entry(map, entry, ent), NULL); entry = alloc_test_entry(ignore_case ? "FOObarFrotz" : "fooBarFrotz", "value4", ignore_case); entry = hashmap_put_entry(map, entry, ent); - if (check(entry != NULL)) - check_str(get_value(entry), "value3"); + cl_assert(entry != NULL); + cl_assert_equal_s(get_value(entry), "value3"); free(entry); } @@ -122,20 +122,18 @@ static void t_get(struct hashmap *map, unsigned int ignore_case) for (size_t i = 0; i < ARRAY_SIZE(key_val); i++) { entry = alloc_test_entry(key_val[i][0], key_val[i][1], ignore_case); - check_pointer_eq(hashmap_put_entry(map, entry, ent), NULL); + cl_assert_equal_p(hashmap_put_entry(map, entry, ent), NULL); } for (size_t i = 0; i < ARRAY_SIZE(query); i++) { entry = get_test_entry(map, query[i][0], ignore_case); - if (check(entry != NULL)) - check_str(get_value(entry), query[i][1]); - else - test_msg("query key: %s", query[i][0]); + cl_assert(entry != NULL); + cl_assert_equal_s(get_value(entry), query[i][1]); } - check_pointer_eq(get_test_entry(map, "notInMap", ignore_case), NULL); - check_int(map->tablesize, ==, 64); - check_int(hashmap_get_size(map), ==, ARRAY_SIZE(key_val)); + cl_assert_equal_p(get_test_entry(map, "notInMap", ignore_case), NULL); + cl_assert_equal_i(map->tablesize, 64); + cl_assert_equal_i(hashmap_get_size(map), ARRAY_SIZE(key_val)); } static void t_add(struct hashmap *map, unsigned int ignore_case) @@ -165,39 +163,19 @@ static void t_add(struct hashmap *map, unsigned int ignore_case) hashmap_for_each_entry_from(map, entry, ent) { - int ret; - if (!check_int((ret = key_val_contains( - key_val, seen, - ARRAY_SIZE(key_val), entry)), - ==, 0)) { - switch (ret) { - case 1: - test_msg("found entry was not given in the input\n" - " key: %s\n value: %s", - entry->key, get_value(entry)); - break; - case 2: - test_msg("duplicate entry detected\n" - " key: %s\n value: %s", - entry->key, get_value(entry)); - break; - } - } else { - count++; - } + int ret = key_val_contains(key_val, seen, + ARRAY_SIZE(key_val), entry); + cl_assert(ret == 0); + count++; } - check_int(count, ==, 2); + cl_assert_equal_i(count, 2); } - for (size_t i = 0; i < ARRAY_SIZE(seen); i++) { - if (!check_int(seen[i], ==, 1)) - test_msg("following key-val pair was not iterated over:\n" - " key: %s\n value: %s", - key_val[i][0], key_val[i][1]); - } + for (size_t i = 0; i < ARRAY_SIZE(seen); i++) + cl_assert_equal_i(seen[i], 1); - check_int(hashmap_get_size(map), ==, ARRAY_SIZE(key_val)); - check_pointer_eq(get_test_entry(map, "notInMap", ignore_case), NULL); + cl_assert_equal_i(hashmap_get_size(map), ARRAY_SIZE(key_val)); + cl_assert_equal_p(get_test_entry(map, "notInMap", ignore_case), NULL); } static void t_remove(struct hashmap *map, unsigned int ignore_case) @@ -211,24 +189,25 @@ static void t_remove(struct hashmap *map, unsigned int ignore_case) for (size_t i = 0; i < ARRAY_SIZE(key_val); i++) { entry = alloc_test_entry(key_val[i][0], key_val[i][1], ignore_case); - check_pointer_eq(hashmap_put_entry(map, entry, ent), NULL); + cl_assert_equal_p(hashmap_put_entry(map, entry, ent), NULL); } for (size_t i = 0; i < ARRAY_SIZE(remove); i++) { entry = alloc_test_entry(remove[i][0], "", ignore_case); removed = hashmap_remove_entry(map, entry, ent, remove[i][0]); - if (check(removed != NULL)) - check_str(get_value(removed), remove[i][1]); + cl_assert(removed != NULL); + cl_assert_equal_s(get_value(removed), remove[i][1]); free(entry); free(removed); } entry = alloc_test_entry("notInMap", "", ignore_case); - check_pointer_eq(hashmap_remove_entry(map, entry, ent, "notInMap"), NULL); + cl_assert_equal_p(hashmap_remove_entry(map, entry, ent, "notInMap"), NULL); free(entry); - check_int(map->tablesize, ==, 64); - check_int(hashmap_get_size(map), ==, ARRAY_SIZE(key_val) - ARRAY_SIZE(remove)); + cl_assert_equal_i(map->tablesize, 64); + cl_assert_equal_i(hashmap_get_size(map), + ARRAY_SIZE(key_val) - ARRAY_SIZE(remove)); } static void t_iterate(struct hashmap *map, unsigned int ignore_case) @@ -242,38 +221,21 @@ static void t_iterate(struct hashmap *map, unsigned int ignore_case) for (size_t i = 0; i < ARRAY_SIZE(key_val); i++) { entry = alloc_test_entry(key_val[i][0], key_val[i][1], ignore_case); - check_pointer_eq(hashmap_put_entry(map, entry, ent), NULL); + cl_assert_equal_p(hashmap_put_entry(map, entry, ent), NULL); } hashmap_for_each_entry(map, &iter, entry, ent /* member name */) { - int ret; - if (!check_int((ret = key_val_contains(key_val, seen, - ARRAY_SIZE(key_val), - entry)), ==, 0)) { - switch (ret) { - case 1: - test_msg("found entry was not given in the input\n" - " key: %s\n value: %s", - entry->key, get_value(entry)); - break; - case 2: - test_msg("duplicate entry detected\n" - " key: %s\n value: %s", - entry->key, get_value(entry)); - break; - } - } + int ret = key_val_contains(key_val, seen, + ARRAY_SIZE(key_val), + entry); + cl_assert(ret == 0); } - for (size_t i = 0; i < ARRAY_SIZE(seen); i++) { - if (!check_int(seen[i], ==, 1)) - test_msg("following key-val pair was not iterated over:\n" - " key: %s\n value: %s", - key_val[i][0], key_val[i][1]); - } + for (size_t i = 0; i < ARRAY_SIZE(seen); i++) + cl_assert_equal_i(seen[i], 1); - check_int(hashmap_get_size(map), ==, ARRAY_SIZE(key_val)); + cl_assert_equal_i(hashmap_get_size(map), ARRAY_SIZE(key_val)); } static void t_alloc(struct hashmap *map, unsigned int ignore_case) @@ -284,17 +246,17 @@ static void t_alloc(struct hashmap *map, unsigned int ignore_case) char *key = xstrfmt("key%d", i); char *value = xstrfmt("value%d", i); entry = alloc_test_entry(key, value, ignore_case); - check_pointer_eq(hashmap_put_entry(map, entry, ent), NULL); + cl_assert_equal_p(hashmap_put_entry(map, entry, ent), NULL); free(key); free(value); } - check_int(map->tablesize, ==, 64); - check_int(hashmap_get_size(map), ==, 51); + cl_assert_equal_i(map->tablesize, 64); + cl_assert_equal_i(hashmap_get_size(map), 51); entry = alloc_test_entry("key52", "value52", ignore_case); - check_pointer_eq(hashmap_put_entry(map, entry, ent), NULL); - check_int(map->tablesize, ==, 256); - check_int(hashmap_get_size(map), ==, 52); + cl_assert_equal_p(hashmap_put_entry(map, entry, ent), NULL); + cl_assert_equal_i(map->tablesize, 256); + cl_assert_equal_i(hashmap_get_size(map), 52); for (int i = 1; i <= 12; i++) { char *key = xstrfmt("key%d", i); @@ -302,27 +264,27 @@ static void t_alloc(struct hashmap *map, unsigned int ignore_case) entry = alloc_test_entry(key, "", ignore_case); removed = hashmap_remove_entry(map, entry, ent, key); - if (check(removed != NULL)) - check_str(value, get_value(removed)); + cl_assert(removed != NULL); + cl_assert_equal_s(value, get_value(removed)); free(key); free(value); free(entry); free(removed); } - check_int(map->tablesize, ==, 256); - check_int(hashmap_get_size(map), ==, 40); + cl_assert_equal_i(map->tablesize, 256); + cl_assert_equal_i(hashmap_get_size(map), 40); entry = alloc_test_entry("key40", "", ignore_case); removed = hashmap_remove_entry(map, entry, ent, "key40"); - if (check(removed != NULL)) - check_str("value40", get_value(removed)); - check_int(map->tablesize, ==, 64); - check_int(hashmap_get_size(map), ==, 39); + cl_assert(removed != NULL); + cl_assert_equal_s("value40", get_value(removed)); + cl_assert_equal_i(map->tablesize, 64); + cl_assert_equal_i(hashmap_get_size(map), 39); free(entry); free(removed); } -static void t_intern(void) +void test_hashmap__intern(void) { const char *values[] = { "value1", "Value1", "value2", "value2" }; @@ -330,32 +292,68 @@ static void t_intern(void) const char *i1 = strintern(values[i]); const char *i2 = strintern(values[i]); - if (!check(!strcmp(i1, values[i]))) - test_msg("strintern(%s) returns %s\n", values[i], i1); - else if (!check(i1 != values[i])) - test_msg("strintern(%s) returns input pointer\n", - values[i]); - else if (!check_pointer_eq(i1, i2)) - test_msg("address('%s') != address('%s'), so strintern('%s') != strintern('%s')", - i1, i2, values[i], values[i]); - else - check_str(i1, values[i]); + cl_assert_equal_s(i1, values[i]); + cl_assert(i1 != values[i]); + cl_assert_equal_p(i1, i2); } } -int cmd_main(int argc UNUSED, const char **argv UNUSED) +void test_hashmap__replace_case_sensitive(void) +{ + setup(t_replace, 0); +} + +void test_hashmap__replace_case_insensitive(void) +{ + setup(t_replace, 1); +} + +void test_hashmap__get_case_sensitive(void) +{ + setup(t_get, 0); +} + +void test_hashmap__get_case_insensitive(void) +{ + setup(t_get, 1); +} + +void test_hashmap__add_case_sensitive(void) +{ + setup(t_add, 0); +} + +void test_hashmap__add_case_insensitive(void) +{ + setup(t_add, 1); +} + +void test_hashmap__remove_case_sensitive(void) +{ + setup(t_remove, 0); +} + +void test_hashmap__remove_case_insensitive(void) +{ + setup(t_remove, 1); +} + +void test_hashmap__iterate_case_sensitive(void) +{ + setup(t_iterate, 0); +} + +void test_hashmap__iterate_case_insensitive(void) +{ + setup(t_iterate, 1); +} + +void test_hashmap__alloc_case_sensitive(void) +{ + setup(t_alloc, 0); +} + +void test_hashmap__alloc_case_insensitive(void) { - TEST(setup(t_replace, 0), "replace works"); - TEST(setup(t_replace, 1), "replace (case insensitive) works"); - TEST(setup(t_get, 0), "get works"); - TEST(setup(t_get, 1), "get (case insensitive) works"); - TEST(setup(t_add, 0), "add works"); - TEST(setup(t_add, 1), "add (case insensitive) works"); - TEST(setup(t_remove, 0), "remove works"); - TEST(setup(t_remove, 1), "remove (case insensitive) works"); - TEST(setup(t_iterate, 0), "iterate works"); - TEST(setup(t_iterate, 1), "iterate (case insensitive) works"); - TEST(setup(t_alloc, 0), "grow / shrink works"); - TEST(t_intern(), "string interning works"); - return test_done(); + setup(t_alloc, 1); } From patchwork Thu Jan 30 09:13:32 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Seyi Kuforiji X-Patchwork-Id: 13954361 Received: from mail-ed1-f47.google.com (mail-ed1-f47.google.com [209.85.208.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 A833E1B4243 for ; Thu, 30 Jan 2025 09:13:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738228438; cv=none; b=qySO3M3dCScx9K1DIER6dPCJ9WS5/L1pQnYbQTzbZJ5DP3p4CvJ+EFbZsJIhOlov0/gikZlBwqByrA2XrS2HQtwPYojRv/E9bEvotKzcI4CQtL5f07SBFNkwx/T9j0is85fJGVLrBNWlhjLN3W1YwCKIHowCBVNsyBxAfJPg1pk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738228438; c=relaxed/simple; bh=jfTH4JEv1ZTwEjAiZIiJ0Ym9Mv56yiRN8sblrkutTY8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CzXvLfGscvNpzJJqojS+Bkgq3Oyeq92TdVxOwTLVPjNn1rwa4CkSnyjsksC+apwt9Tswno5NBOPT5Tl6Q2HdU/YKexpt8ru6UcPcw7IyWp15QezbmY4cAJgsW9TpBJILA7JYJa9CCp+Ty99+1jbYJH85tC6V37HbPVImscPwWX4= 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=J+0UC/oo; arc=none smtp.client-ip=209.85.208.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="J+0UC/oo" Received: by mail-ed1-f47.google.com with SMTP id 4fb4d7f45d1cf-5db689a87cbso937869a12.3 for ; Thu, 30 Jan 2025 01:13:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1738228435; x=1738833235; 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=46ZJLy13d8XyVS+Tm2//4aBH5tveKhmyIuJs4p8Kc98=; b=J+0UC/ooqDtC6I37/YEq+HVbQjPwCT+vELycVNk5Lgs/JOUMYFTqD3NCrc78rqaC8j zhvXcCVXOupdhlhIFeOr2iA/V3z31xmZs/xtTa8/mtvBKSt9+1QknxiaPlLlEXbJc2+M NUFFdEZloIJDi+UH3GSyH2jtIKRZSx716VbzBkJyO+IjGNKLDP/9p+0s/55ibYFPhVc4 ygNPlV3n2IAtFXBBD9Hl86ttmb/H0A/AiCjEYa/nTRGrrEIdYmGZG7JN7s8jlnNwDkDm 7N9AI2JvF3BSptUryiWQzlk7E2DDbB1Srnw4MBwWDMK0ITS+fFPrOcrVT9ob0ja2D492 KZWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738228435; x=1738833235; 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=46ZJLy13d8XyVS+Tm2//4aBH5tveKhmyIuJs4p8Kc98=; b=mNR5zcAfI+wJWVdehSJy1yajSQMDY0b4immFV9697vhpcIAUI2vFTUDA6+X4LQg5HO lh5YuTxAJpOf9VUTLsyXQfkkDELjLoB/cQU8EkZwy+4que4Bm1y2Zjrzs/FS/t3HJ8Bd FV4w/Yb8LT5BHBtz/Ppw8sEgtyoTeKvt2ROL4DLdcTSYyn7QNMUIFM85ymg/KuNR5qf+ K30wmRZW718zM181htD1QrIj+opEs8JQC8gxDjuZP+q6lNFPmLIk1W9GE8ZcUFUuEHpt 2ZIt4g4YfefeL/Z0ufPTxOzSrnPFZtJuOZL+ynolloGFcykntDXukFKmqhS8ca562cBW iXlA== X-Gm-Message-State: AOJu0Yyl1sp380FkYTIjmpwrzg4hbrvUnVoRISnmDz+Oyidt/HeFpCE5 8zel2bI9yQyRrq9xjuLOLWsacTG2HmIoRh3zVcURvr8YxvqRnCgpE/8bPnwmLMyz8A== X-Gm-Gg: ASbGnctzoG/9T0zbfIq4qVR7nGPMXZ2VkmOXHkjtwQ5sTM9iOdI6FSLH/cHSNNu+O+i 5H6miakv77QNSjc52PEULrJ4YZgaMO7JgPIeMweb764qLObmZoQHoJf3xp2KLyd4coRvNIONazR GEcym0jdzriEa2WOWvkf5anMmbGc23Ni8MY3qMts3WfueHrZqSBU/3b6RjCDibST9n91TJpuxG7 abUaxnFDocEVJLSPgTbIXn1anVWBd0lL86Lfoj5+e1rjFPLRKJkEZ7KGE4W2RLRoZwrGUwhFDbn +F7Xt3Iw+NYCxaqL2BhvLFxDJ+BSSg== X-Google-Smtp-Source: AGHT+IEdSG4y6SqzpWxIjRgxENFtQqkIjXHH/Jfj+qtD4ozkVb+tawVYw2uEZDCX5eTbM2gbhP/TlQ== X-Received: by 2002:a05:6402:2791:b0:5db:faf8:e06d with SMTP id 4fb4d7f45d1cf-5dc5efb8ccemr6530302a12.7.1738228434524; Thu, 30 Jan 2025 01:13:54 -0800 (PST) Received: from localhost.localdomain ([154.120.90.178]) by smtp.googlemail.com with ESMTPSA id 4fb4d7f45d1cf-5dc723e9ee4sm742249a12.25.2025.01.30.01.13.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Jan 2025 01:13:54 -0800 (PST) From: Seyi Kuforiji To: git@vger.kernel.org Cc: ps@pks.im, phillip.wood@dunelm.org.uk, Seyi Kuforiji Subject: [PATCH 2/4] t/unit-tests: adapt example decorate test to clar framework Date: Thu, 30 Jan 2025 10:13:32 +0100 Message-ID: <20250130091334.39922-3-kuforiji98@gmail.com> X-Mailer: git-send-email 2.47.0.86.g15030f9556 In-Reply-To: <20250130091334.39922-1-kuforiji98@gmail.com> References: <20250130091334.39922-1-kuforiji98@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Adapts example decorate test script to clar framework by using clar assertions where necessary. Test functions are created as standalone to test different test cases. Mentored-by: Patrick Steinhardt Signed-off-by: Seyi Kuforiji --- Makefile | 2 +- t/meson.build | 2 +- t/unit-tests/t-example-decorate.c | 74 ------------------------------ t/unit-tests/u-example-decorate.c | 76 +++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 76 deletions(-) delete mode 100644 t/unit-tests/t-example-decorate.c create mode 100644 t/unit-tests/u-example-decorate.c diff --git a/Makefile b/Makefile index 2d9dad119a..732d765f1c 100644 --- a/Makefile +++ b/Makefile @@ -1338,6 +1338,7 @@ THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/% THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/clar/% CLAR_TEST_SUITES += u-ctype +CLAR_TEST_SUITES += u-example-decorate CLAR_TEST_SUITES += u-hash CLAR_TEST_SUITES += u-hashmap CLAR_TEST_SUITES += u-mem-pool @@ -1349,7 +1350,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-example-decorate UNIT_TEST_PROGRAMS += t-oid-array UNIT_TEST_PROGRAMS += t-oidmap UNIT_TEST_PROGRAMS += t-oidtree diff --git a/t/meson.build b/t/meson.build index af597f9804..c7e08eca6f 100644 --- a/t/meson.build +++ b/t/meson.build @@ -1,5 +1,6 @@ clar_test_suites = [ 'unit-tests/u-ctype.c', + 'unit-tests/u-example-decorate.c', 'unit-tests/u-hash.c', 'unit-tests/u-hashmap.c', 'unit-tests/u-mem-pool.c', @@ -45,7 +46,6 @@ clar_unit_tests = executable('unit-tests', test('unit-tests', clar_unit_tests) unit_test_programs = [ - 'unit-tests/t-example-decorate.c', 'unit-tests/t-oid-array.c', 'unit-tests/t-oidmap.c', 'unit-tests/t-oidtree.c', diff --git a/t/unit-tests/t-example-decorate.c b/t/unit-tests/t-example-decorate.c deleted file mode 100644 index bfc776e223..0000000000 --- a/t/unit-tests/t-example-decorate.c +++ /dev/null @@ -1,74 +0,0 @@ -#define USE_THE_REPOSITORY_VARIABLE - -#include "test-lib.h" -#include "object.h" -#include "decorate.h" -#include "repository.h" - -struct test_vars { - struct object *one, *two, *three; - struct decoration n; - int decoration_a, decoration_b; -}; - -static void t_add(struct test_vars *vars) -{ - void *ret = add_decoration(&vars->n, vars->one, &vars->decoration_a); - - check(ret == NULL); - ret = add_decoration(&vars->n, vars->two, NULL); - check(ret == NULL); -} - -static void t_readd(struct test_vars *vars) -{ - void *ret = add_decoration(&vars->n, vars->one, NULL); - - check(ret == &vars->decoration_a); - ret = add_decoration(&vars->n, vars->two, &vars->decoration_b); - check(ret == NULL); -} - -static void t_lookup(struct test_vars *vars) -{ - void *ret = lookup_decoration(&vars->n, vars->one); - - check(ret == NULL); - ret = lookup_decoration(&vars->n, vars->two); - check(ret == &vars->decoration_b); - ret = lookup_decoration(&vars->n, vars->three); - check(ret == NULL); -} - -static void t_loop(struct test_vars *vars) -{ - int objects_noticed = 0; - - for (size_t i = 0; i < vars->n.size; i++) { - if (vars->n.entries[i].base) - objects_noticed++; - } - check_int(objects_noticed, ==, 2); -} - -int cmd_main(int argc UNUSED, const char **argv UNUSED) -{ - struct object_id one_oid = { { 1 } }, two_oid = { { 2 } }, three_oid = { { 3 } }; - struct test_vars vars = { 0 }; - - vars.one = lookup_unknown_object(the_repository, &one_oid); - vars.two = lookup_unknown_object(the_repository, &two_oid); - vars.three = lookup_unknown_object(the_repository, &three_oid); - - TEST(t_add(&vars), - "Add 2 objects, one with a non-NULL decoration and one with a NULL decoration."); - TEST(t_readd(&vars), - "When re-adding an already existing object, the old decoration is returned."); - TEST(t_lookup(&vars), - "Lookup returns the added declarations, or NULL if the object was never added."); - TEST(t_loop(&vars), "The user can also loop through all entries."); - - clear_decoration(&vars.n, NULL); - - return test_done(); -} diff --git a/t/unit-tests/u-example-decorate.c b/t/unit-tests/u-example-decorate.c new file mode 100644 index 0000000000..3a457d41fc --- /dev/null +++ b/t/unit-tests/u-example-decorate.c @@ -0,0 +1,76 @@ +#define USE_THE_REPOSITORY_VARIABLE + +#include "unit-test.h" +#include "object.h" +#include "decorate.h" +#include "repository.h" + +struct test_vars { + struct object *one, *two, *three; + struct decoration n; + int decoration_a, decoration_b; +}; + +static struct test_vars vars; + +void test_example_decorate__add(void) +{ + void *ret = add_decoration(&vars.n, vars.one, &vars.decoration_a); + cl_assert(ret == NULL); + ret = add_decoration(&vars.n, vars.two, NULL); + cl_assert(ret == NULL); +} + +void test_example_decorate__readd(void) +{ + void *ret; + + cl_assert(add_decoration(&vars.n, vars.one, &vars.decoration_a) == NULL); + cl_assert(add_decoration(&vars.n, vars.two, NULL) == NULL); + + ret = add_decoration(&vars.n, vars.one, NULL); + cl_assert(ret == &vars.decoration_a); + ret = add_decoration(&vars.n, vars.two, &vars.decoration_b); + cl_assert(ret == NULL); +} + +void test_example_decorate__lookup(void) +{ + void *ret; + + add_decoration(&vars.n, vars.two, &vars.decoration_b); + add_decoration(&vars.n, vars.one, NULL); + + ret = lookup_decoration(&vars.n, vars.two); + cl_assert(ret == &vars.decoration_b); + ret = lookup_decoration(&vars.n, vars.one); + cl_assert(ret == NULL); +} + +void test_example_decorate__loop(void) +{ + int objects_noticed = 0; + + add_decoration(&vars.n, vars.one, &vars.decoration_a); + add_decoration(&vars.n, vars.two, &vars.decoration_b); + + for (size_t i = 0; i < vars.n.size; i++) { + if (vars.n.entries[i].base) + objects_noticed++; + } + cl_assert_equal_i(objects_noticed, 2); +} + +void test_example_decorate__initialize(void) +{ + struct object_id one_oid = { { 1 } }, two_oid = { { 2 } }, three_oid = { { 3 } }; + + vars.one = lookup_unknown_object(the_repository, &one_oid); + vars.two = lookup_unknown_object(the_repository, &two_oid); + vars.three = lookup_unknown_object(the_repository, &three_oid); +} + +void test_example_decorate__cleanup(void) +{ + clear_decoration(&vars.n, NULL); +} From patchwork Thu Jan 30 09:13:33 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Seyi Kuforiji X-Patchwork-Id: 13954362 Received: from mail-ed1-f45.google.com (mail-ed1-f45.google.com [209.85.208.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 087F51B4228 for ; Thu, 30 Jan 2025 09:13:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738228442; cv=none; b=E1L247OxehOc/47jrHGauaqHmipXCEK/680xZSdeAYIARaci/hT2zs4CLrGTuNdjPpSJZgUa0HSYfMIej0eHvOp5VXY/M6hXOW1kPLnndu6WtGdgtXtn+13j6zEdSuTFhBvQ2JptKBesdv0g13BubgayvBuBM6972VNVJt27eaw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738228442; c=relaxed/simple; bh=kxiWGlfHVqJJc7kjiFJ752FOiU6AHsrviRaskebe9eQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ezZKWq3XNXtvaKhIFwA5JgZ1izqJsDyQvRQxUSdbRoMmoT3JSWJC+4liEYOGjC+GBT4bnFnibsna8eekttRy16gh0e1VdT/biGlvIcLMrdufI/P6D8txVPIoNelQag0nhXrLYOn/rb+uP3eOSf/MCxr0fW2xFPU7B1e+YmyxwHE= 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=EXu0vq0k; arc=none smtp.client-ip=209.85.208.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="EXu0vq0k" Received: by mail-ed1-f45.google.com with SMTP id 4fb4d7f45d1cf-5d3f28881d6so689916a12.1 for ; Thu, 30 Jan 2025 01:13:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1738228438; x=1738833238; 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=i0SxbEBeNdokXNPpE6tqi9lrmzHFgZuRVqqdScJgHcU=; b=EXu0vq0kqEhZlvb3DIV0UTvwABAvkIlT0OAdjd20B/10ik/v9W76tFsC66E2lm9iTV KUICRof3dg1uA271qasVbSVBwabOFxlwAZWQDPoFmnEWpn1+0/LoBxP5RLOdKn78lfhA 0tb2l/hVha4r8zGRO7Mdb+KZk+c9hPDCu7pPmnYDVzqIF0ImgZCMj9JNk6dqdxBXFxba +rO0baCUEttLD3zW9Uh2zkFAYPwzoc58Pa/9T7tCqZnNWKaQocjTT39AzhTwNk94shOw Xd7OgB/38mezc4BLT4XqEwhSnzMvZp3to7Poc+LhYxOnf+CVcMWf0RCj56DlpPJ10E2V 6fUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738228438; x=1738833238; 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=i0SxbEBeNdokXNPpE6tqi9lrmzHFgZuRVqqdScJgHcU=; b=tQSoGEXiyyPX4xO1VvLP0jF90BC+RSvDR3YhF249286Vqo2uKX6sdAYzq7kZF2jA/+ Quq9FKwm0bP59nCa1qVgHDiM50i5SWF9/yMtPTzif8Jh5Ms/LvytCT5+X4y7O0zN/hXh 0DdY5nDqUK6zxOvl/GbE2hzQXKc17hGvGaZGV/HvzZYj8KGS9RuVotObpwO1lOyHzi6N wDa7VVBc59Cb+86XZKRhwXGoMD+qN9zvd4CaS/LPa/zUkvJW1fCQyldMHkBOLjVYkhh3 mKpl62P0aAYYLj4rfPU5T/J/If+z0rql08ooxxqGt2eCd3PabLbBiSwOySVI0BDgwTY+ RyYA== X-Gm-Message-State: AOJu0YwXSiQx+iJfHSgmBoEEOaGFGZS/AyV4ZRwWpaEA2/NcYWQJF1sh G+mW8LQO4s4yHYtspR7cZA5vfrW80CDp9USftjqH6G6bEiQimqOgqq8XL7Bk+P0JQQ== X-Gm-Gg: ASbGnctquZoUaBE6+fmZBjylvUEzU+9onWjAfjJnyUNqSfVxr2Z81OXXtz/G0SqreCp xPwKe4w00JLE+P5MyEOgXKMOmd+g2xKaH5u77oEKbE9wihzDs16fUJ7oqUXIe59D9DsilfDWgoG kgAUzA/BqIRhlHJeupWUSsm4TuRcOxDmWUBm2TVFJlLIHijXOnEx66jr0DIpwNXJx1mkQagFOs1 CaZtgo3w1vTWW4cJrznErWLgqdkbjHokNbJm+wV/KzpifwLxjFkafFWppZccAxmVgnVroDRAEpL uUuUzQmHT9BS0785HXzGlfzBLCOHOA== X-Google-Smtp-Source: AGHT+IE3rClwh0ScdmXvpxbO2FVk6Uu9d5A6tZJ1iYj+i8wxo6Et7uNH0cU9O7ry8+I71f9yGgIfcw== X-Received: by 2002:a05:6402:358e:b0:5da:1219:c81 with SMTP id 4fb4d7f45d1cf-5dc5efc5d27mr14484094a12.16.1738228437832; Thu, 30 Jan 2025 01:13:57 -0800 (PST) Received: from localhost.localdomain ([154.120.90.178]) by smtp.googlemail.com with ESMTPSA id 4fb4d7f45d1cf-5dc723e9ee4sm742249a12.25.2025.01.30.01.13.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Jan 2025 01:13:57 -0800 (PST) From: Seyi Kuforiji To: git@vger.kernel.org Cc: ps@pks.im, phillip.wood@dunelm.org.uk, Seyi Kuforiji Subject: [PATCH 3/4] t/unit-tests: convert strbuf test to clar framework Date: Thu, 30 Jan 2025 10:13:33 +0100 Message-ID: <20250130091334.39922-4-kuforiji98@gmail.com> X-Mailer: git-send-email 2.47.0.86.g15030f9556 In-Reply-To: <20250130091334.39922-1-kuforiji98@gmail.com> References: <20250130091334.39922-1-kuforiji98@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Adapt strbuf test script to clar framework by using clar assertions where necessary. Test functions are created as standalone to test different test cases. Mentored-by: Patrick Steinhardt Signed-off-by: Seyi Kuforiji --- Makefile | 2 +- t/meson.build | 2 +- t/unit-tests/t-strbuf.c | 122 ---------------------------------------- t/unit-tests/u-strbuf.c | 121 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 123 insertions(+), 124 deletions(-) delete mode 100644 t/unit-tests/t-strbuf.c create mode 100644 t/unit-tests/u-strbuf.c diff --git a/Makefile b/Makefile index 732d765f1c..358193597f 100644 --- a/Makefile +++ b/Makefile @@ -1344,6 +1344,7 @@ CLAR_TEST_SUITES += u-hashmap CLAR_TEST_SUITES += u-mem-pool CLAR_TEST_SUITES += u-prio-queue CLAR_TEST_SUITES += u-reftable-tree +CLAR_TEST_SUITES += u-strbuf CLAR_TEST_SUITES += u-strvec CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X) CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES)) @@ -1361,7 +1362,6 @@ UNIT_TEST_PROGRAMS += t-reftable-reader UNIT_TEST_PROGRAMS += t-reftable-readwrite UNIT_TEST_PROGRAMS += t-reftable-record UNIT_TEST_PROGRAMS += t-reftable-stack -UNIT_TEST_PROGRAMS += t-strbuf UNIT_TEST_PROGRAMS += t-strcmp-offset UNIT_TEST_PROGRAMS += t-trailer UNIT_TEST_PROGRAMS += t-urlmatch-normalization diff --git a/t/meson.build b/t/meson.build index c7e08eca6f..6cb72842b1 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-prio-queue.c', 'unit-tests/u-reftable-tree.c', + 'unit-tests/u-strbuf.c', 'unit-tests/u-strvec.c', ] @@ -57,7 +58,6 @@ unit_test_programs = [ 'unit-tests/t-reftable-readwrite.c', 'unit-tests/t-reftable-record.c', 'unit-tests/t-reftable-stack.c', - 'unit-tests/t-strbuf.c', 'unit-tests/t-strcmp-offset.c', 'unit-tests/t-trailer.c', 'unit-tests/t-urlmatch-normalization.c', diff --git a/t/unit-tests/t-strbuf.c b/t/unit-tests/t-strbuf.c deleted file mode 100644 index 3f4044d697..0000000000 --- a/t/unit-tests/t-strbuf.c +++ /dev/null @@ -1,122 +0,0 @@ -#include "test-lib.h" -#include "strbuf.h" - -/* wrapper that supplies tests with an empty, initialized strbuf */ -static void setup(void (*f)(struct strbuf*, const void*), - const void *data) -{ - struct strbuf buf = STRBUF_INIT; - - f(&buf, data); - strbuf_release(&buf); - check_uint(buf.len, ==, 0); - check_uint(buf.alloc, ==, 0); -} - -/* wrapper that supplies tests with a populated, initialized strbuf */ -static void setup_populated(void (*f)(struct strbuf*, const void*), - const char *init_str, const void *data) -{ - struct strbuf buf = STRBUF_INIT; - - strbuf_addstr(&buf, init_str); - check_uint(buf.len, ==, strlen(init_str)); - f(&buf, data); - strbuf_release(&buf); - check_uint(buf.len, ==, 0); - check_uint(buf.alloc, ==, 0); -} - -static int assert_sane_strbuf(struct strbuf *buf) -{ - /* Initialized strbufs should always have a non-NULL buffer */ - if (!check(!!buf->buf)) - return 0; - /* Buffers should always be NUL-terminated */ - if (!check_char(buf->buf[buf->len], ==, '\0')) - return 0; - /* - * Freshly-initialized strbufs may not have a dynamically allocated - * buffer - */ - if (buf->len == 0 && buf->alloc == 0) - return 1; - /* alloc must be at least one byte larger than len */ - return check_uint(buf->len, <, buf->alloc); -} - -static void t_static_init(void) -{ - struct strbuf buf = STRBUF_INIT; - - check_uint(buf.len, ==, 0); - check_uint(buf.alloc, ==, 0); - check_char(buf.buf[0], ==, '\0'); -} - -static void t_dynamic_init(void) -{ - struct strbuf buf; - - strbuf_init(&buf, 1024); - check(assert_sane_strbuf(&buf)); - check_uint(buf.len, ==, 0); - check_uint(buf.alloc, >=, 1024); - check_char(buf.buf[0], ==, '\0'); - strbuf_release(&buf); -} - -static void t_addch(struct strbuf *buf, const void *data) -{ - const char *p_ch = data; - const char ch = *p_ch; - size_t orig_alloc = buf->alloc; - size_t orig_len = buf->len; - - if (!check(assert_sane_strbuf(buf))) - return; - strbuf_addch(buf, ch); - if (!check(assert_sane_strbuf(buf))) - return; - if (!(check_uint(buf->len, ==, orig_len + 1) && - check_uint(buf->alloc, >=, orig_alloc))) - return; /* avoid de-referencing buf->buf */ - check_char(buf->buf[buf->len - 1], ==, ch); - check_char(buf->buf[buf->len], ==, '\0'); -} - -static void t_addstr(struct strbuf *buf, const void *data) -{ - const char *text = data; - size_t len = strlen(text); - size_t orig_alloc = buf->alloc; - size_t orig_len = buf->len; - - if (!check(assert_sane_strbuf(buf))) - return; - strbuf_addstr(buf, text); - if (!check(assert_sane_strbuf(buf))) - return; - if (!(check_uint(buf->len, ==, orig_len + len) && - check_uint(buf->alloc, >=, orig_alloc) && - check_uint(buf->alloc, >, orig_len + len) && - check_char(buf->buf[orig_len + len], ==, '\0'))) - return; - check_str(buf->buf + orig_len, text); -} - -int cmd_main(int argc UNUSED, const char **argv UNUSED) -{ - if (!TEST(t_static_init(), "static initialization works")) - test_skip_all("STRBUF_INIT is broken"); - TEST(t_dynamic_init(), "dynamic initialization works"); - TEST(setup(t_addch, "a"), "strbuf_addch adds char"); - TEST(setup(t_addch, ""), "strbuf_addch adds NUL char"); - TEST(setup_populated(t_addch, "initial value", "a"), - "strbuf_addch appends to initial value"); - TEST(setup(t_addstr, "hello there"), "strbuf_addstr adds string"); - TEST(setup_populated(t_addstr, "initial value", "hello there"), - "strbuf_addstr appends string to initial value"); - - return test_done(); -} diff --git a/t/unit-tests/u-strbuf.c b/t/unit-tests/u-strbuf.c new file mode 100644 index 0000000000..fec3c768d2 --- /dev/null +++ b/t/unit-tests/u-strbuf.c @@ -0,0 +1,121 @@ +#include "unit-test.h" +#include "strbuf.h" + +/* wrapper that supplies tests with an empty, initialized strbuf */ +static void setup(void (*f)(struct strbuf*, const void*), + const void *data) +{ + struct strbuf buf = STRBUF_INIT; + + f(&buf, data); + strbuf_release(&buf); + cl_assert_equal_i(buf.len, 0); + cl_assert_equal_i(buf.alloc, 0); +} + +/* wrapper that supplies tests with a populated, initialized strbuf */ +static void setup_populated(void (*f)(struct strbuf*, const void*), + const char *init_str, const void *data) +{ + struct strbuf buf = STRBUF_INIT; + + strbuf_addstr(&buf, init_str); + cl_assert_equal_i(buf.len, strlen(init_str)); + f(&buf, data); + strbuf_release(&buf); + cl_assert_equal_i(buf.len, 0); + cl_assert_equal_i(buf.alloc, 0); +} + +static void assert_sane_strbuf(struct strbuf *buf) +{ + /* Initialized strbufs should always have a non-NULL buffer */ + cl_assert(buf->buf != NULL); + /* Buffers should always be NUL-terminated */ + cl_assert(buf->buf[buf->len] == '\0'); + /* + * Freshly-initialized strbufs may not have a dynamically allocated + * buffer + */ + if (buf->len == 0 && buf->alloc == 0) + return; + /* alloc must be at least one byte larger than len */ + cl_assert(buf->len < buf->alloc); +} + +void test_strbuf__static_init(void) +{ + struct strbuf buf = STRBUF_INIT; + + cl_assert_equal_i(buf.len, 0); + cl_assert_equal_i(buf.alloc, 0); + cl_assert(buf.buf[0] == '\0'); +} + +void test_strbuf__dynamic_init(void) +{ + struct strbuf buf; + + strbuf_init(&buf, 1024); + assert_sane_strbuf(&buf); + cl_assert_equal_i(buf.len, 0); + cl_assert(buf.alloc >= 1024); + cl_assert(buf.buf[0] == '\0'); + strbuf_release(&buf); +} + +static void t_addch(struct strbuf *buf, const void *data) +{ + const char *p_ch = data; + const char ch = *p_ch; + size_t orig_alloc = buf->alloc; + size_t orig_len = buf->len; + + assert_sane_strbuf(buf); + strbuf_addch(buf, ch); + assert_sane_strbuf(buf); + cl_assert_equal_i(buf->len, orig_len + 1); + cl_assert(buf->alloc >= orig_alloc); + cl_assert(buf->buf[buf->len] == '\0'); +} + +static void t_addstr(struct strbuf *buf, const void *data) +{ + const char *text = data; + size_t len = strlen(text); + size_t orig_alloc = buf->alloc; + size_t orig_len = buf->len; + + assert_sane_strbuf(buf); + strbuf_addstr(buf, text); + assert_sane_strbuf(buf); + cl_assert_equal_i(buf->len, orig_len + len); + cl_assert(buf->alloc >= orig_alloc); + cl_assert(buf->buf[buf->len] == '\0'); + cl_assert_equal_s(buf->buf + orig_len, text); +} + +void test_strbuf__add_single_char(void) +{ + setup(t_addch, "a"); +} + +void test_strbuf__add_empty_char(void) +{ + setup(t_addch, ""); +} + +void test_strbuf__add_multi_char(void) +{ + setup_populated(t_addch, "initial value", "a"); +} + +void test_strbuf__add_single_str(void) +{ + setup(t_addstr, "hello there"); +} + +void test_strbuf__add_multi_str(void) +{ + setup_populated(t_addstr, "initial value", "hello there"); +} From patchwork Thu Jan 30 09:13:34 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Seyi Kuforiji X-Patchwork-Id: 13954363 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 ABDD61AF0BA for ; Thu, 30 Jan 2025 09:14:04 +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=1738228446; cv=none; b=EVy7JsMDBfcH/EwgUZvpJFsnibW5IE0204iNTTBVY99J4+6Z9LL47nPD9FeMEo5eUb/ZK5DL5kCumMkuUIB1dtb4dv57i1jA7E/yuqYgYfaMmZ5QlCtY85yQQAT1ckYA3vD8QZWfhUjXOICWYrhKuQ1sNyfHRwwMaQPKTRRRlEI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738228446; c=relaxed/simple; bh=702jeXmUPylhU+WEiB3HlXCL2hZl27qbN/CM3OT7SSQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=IdLvPj1kBcA4YUyLxuv5ART36kU0uAmoqEKwoVtgBlYQKq7cno3vzyvEQXMFJlx2w+Sa801F+rv4wiiLzrZbCFn8JBXj2RNi4yo2kd8xKHtLB2QZba1KlSD2U2k6DntcTG3oe3AbUr/PbUu+GCajN1+l/bweR1QvExfKmIqS09E= 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=SMNfFU1q; 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="SMNfFU1q" Received: by mail-ej1-f41.google.com with SMTP id a640c23a62f3a-aaedd529ba1so84368866b.1 for ; Thu, 30 Jan 2025 01:14:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1738228443; x=1738833243; 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=5oCyTA0uGOjzFWATG+1/OHeBJHmQkYy6F9RxFloHXTs=; b=SMNfFU1q336fqS7BsYu1FEKacc+mA6lf2gOqmNLqLpeLvlQqUqtgVveISZKq9Efn7M gEe3L1NB31cTx0DZdM7Siqq8AWpMoyMndrjjvQdGHY9O8Y9sk/txnXlpwQE3fFHQ7kJ5 UXIQdXPuilEJlqNhZ4/A+doW5gl94Dkrfqc+5dd6fwsHfpgldhov/TNqtQuRGZtDx0TC A1vhGFhXphFVayPManG6f0pQU/gBALm6HzpcQNGWa7oZNJj+Z9aZuTGJhPxdkhGx05mY npX1Opn5unkuNTquL7zrgwvDxbJyRpL2E9qy9f4JiAg4yjL5PyhuOGDBe8G0fpzXr8sF bDuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738228443; x=1738833243; 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=5oCyTA0uGOjzFWATG+1/OHeBJHmQkYy6F9RxFloHXTs=; b=fnl7/bFB0pwSGT4JdQasU8Xn4W4LdRPlhJP4zk9p86zNl7UQmx7EBC5NzcxLnsYb59 vioVrdKHswpUffHsWqcH6ypyr/4eGLwYCNJZDSEvLAsGIipBSbuf5BokW03h6q98BSrQ sgDCEtwQ4/oOggnLVNHOFnHezCW+PIRhhwqpR/iLK9ehFHJeZedLHTiSV6DQgJNUA1yt tj/c9047hVxHXHkfJX6CxHNtuu+Lorkz7eJ9WaCE1z/WVAzsWToX1ar/cD5H1guHeB1s mFaKBhz5m38WV+8FQNNmRVvBIJl7Jw39SrdYhTCh2Q87J9jEbx1pKLi/OevkkoT/8ggm jT6g== X-Gm-Message-State: AOJu0YxcqjBbn7xAx5MQws2UaZtgBs4MXytv8uiiNMcxCQG0mO8sON0n lGF514sZj3rz0lMh6HTNX3haikJfAidFWxo1svGBE+cq+EF5+vwPIfzo06Er+9kG0g== X-Gm-Gg: ASbGncsohpVtZdC7Mbso3Zz1Np9K8D2Gtgp8paND/pm+QzipgjFR9p5Y/xseotuxT9J Nq0RlabtlbG4SPWoAnMPaogvwevaTfcb3uFzsNI3FLzFIXRRsjBWN/1gF5PN1EEXQ3mZIfVAlhc 2EJWGd13J09jPzq5fyV6xFCeOSKsO9DXu6ZcDkCwDENBaIifrt0nd+ciu+mQ/12F/RYAGjVak5R csc0nyK2JfS+ZO/dkC4Y8Zm/3U8QruePfYh5QFpfcAuxYYn7g3VQYXG7FPt/HkEfroZi8HA+zqd izhi8deNGf7DJfj70EDTJ5XyPvehLg== X-Google-Smtp-Source: AGHT+IFOhLQs4g4IFaLqodhecsp0vg11iUX5Ojn4RfKk8XYMxqVGPRoRD2Rb+ryTiK33pBYYYYgRVw== X-Received: by 2002:a05:6402:358e:b0:5da:1219:c81 with SMTP id 4fb4d7f45d1cf-5dc5efc5d27mr14484703a12.16.1738228442381; Thu, 30 Jan 2025 01:14:02 -0800 (PST) Received: from localhost.localdomain ([154.120.90.178]) by smtp.googlemail.com with ESMTPSA id 4fb4d7f45d1cf-5dc723e9ee4sm742249a12.25.2025.01.30.01.13.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Jan 2025 01:14:01 -0800 (PST) From: Seyi Kuforiji To: git@vger.kernel.org Cc: ps@pks.im, phillip.wood@dunelm.org.uk, Seyi Kuforiji Subject: [PATCH 4/4] t/unit-tests: convert strcmp-offset test to clar framework Date: Thu, 30 Jan 2025 10:13:34 +0100 Message-ID: <20250130091334.39922-5-kuforiji98@gmail.com> X-Mailer: git-send-email 2.47.0.86.g15030f9556 In-Reply-To: <20250130091334.39922-1-kuforiji98@gmail.com> References: <20250130091334.39922-1-kuforiji98@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Adapt strcmp-offset test script to clar framework by using clar assertions where necessary. Test functions are created as standalone to test different test cases. Mentored-by: Patrick Steinhardt Signed-off-by: Seyi Kuforiji --- Makefile | 2 +- t/meson.build | 2 +- t/unit-tests/t-strcmp-offset.c | 35 -------------------------- t/unit-tests/u-strcmp-offset.c | 45 ++++++++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 37 deletions(-) delete mode 100644 t/unit-tests/t-strcmp-offset.c create mode 100644 t/unit-tests/u-strcmp-offset.c diff --git a/Makefile b/Makefile index 358193597f..76b5de4fdd 100644 --- a/Makefile +++ b/Makefile @@ -1345,6 +1345,7 @@ CLAR_TEST_SUITES += u-mem-pool CLAR_TEST_SUITES += u-prio-queue CLAR_TEST_SUITES += u-reftable-tree CLAR_TEST_SUITES += u-strbuf +CLAR_TEST_SUITES += u-strcmp-offset CLAR_TEST_SUITES += u-strvec CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X) CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES)) @@ -1362,7 +1363,6 @@ UNIT_TEST_PROGRAMS += t-reftable-reader UNIT_TEST_PROGRAMS += t-reftable-readwrite UNIT_TEST_PROGRAMS += t-reftable-record UNIT_TEST_PROGRAMS += t-reftable-stack -UNIT_TEST_PROGRAMS += t-strcmp-offset UNIT_TEST_PROGRAMS += t-trailer UNIT_TEST_PROGRAMS += t-urlmatch-normalization UNIT_TEST_PROGS = $(patsubst %,$(UNIT_TEST_BIN)/%$X,$(UNIT_TEST_PROGRAMS)) diff --git a/t/meson.build b/t/meson.build index 6cb72842b1..3935782bbb 100644 --- a/t/meson.build +++ b/t/meson.build @@ -7,6 +7,7 @@ clar_test_suites = [ 'unit-tests/u-prio-queue.c', 'unit-tests/u-reftable-tree.c', 'unit-tests/u-strbuf.c', + 'unit-tests/u-strcmp-offset.c', 'unit-tests/u-strvec.c', ] @@ -58,7 +59,6 @@ unit_test_programs = [ 'unit-tests/t-reftable-readwrite.c', 'unit-tests/t-reftable-record.c', 'unit-tests/t-reftable-stack.c', - 'unit-tests/t-strcmp-offset.c', 'unit-tests/t-trailer.c', 'unit-tests/t-urlmatch-normalization.c', ] diff --git a/t/unit-tests/t-strcmp-offset.c b/t/unit-tests/t-strcmp-offset.c deleted file mode 100644 index 6880f21161..0000000000 --- a/t/unit-tests/t-strcmp-offset.c +++ /dev/null @@ -1,35 +0,0 @@ -#include "test-lib.h" -#include "read-cache-ll.h" - -static void check_strcmp_offset(const char *string1, const char *string2, - int expect_result, uintmax_t expect_offset) -{ - size_t offset; - int result = strcmp_offset(string1, string2, &offset); - - /* - * Because different CRTs behave differently, only rely on signs of the - * result values. - */ - result = (result < 0 ? -1 : - result > 0 ? 1 : - 0); - - check_int(result, ==, expect_result); - check_uint((uintmax_t)offset, ==, expect_offset); -} - -#define TEST_STRCMP_OFFSET(string1, string2, expect_result, expect_offset) \ - TEST(check_strcmp_offset(string1, string2, expect_result, \ - expect_offset), \ - "strcmp_offset(%s, %s) works", #string1, #string2) - -int cmd_main(int argc UNUSED, const char **argv UNUSED) -{ - TEST_STRCMP_OFFSET("abc", "abc", 0, 3); - TEST_STRCMP_OFFSET("abc", "def", -1, 0); - TEST_STRCMP_OFFSET("abc", "abz", -1, 2); - TEST_STRCMP_OFFSET("abc", "abcdef", -1, 3); - - return test_done(); -} diff --git a/t/unit-tests/u-strcmp-offset.c b/t/unit-tests/u-strcmp-offset.c new file mode 100644 index 0000000000..7e8e9acf3c --- /dev/null +++ b/t/unit-tests/u-strcmp-offset.c @@ -0,0 +1,45 @@ +#include "unit-test.h" +#include "read-cache-ll.h" + +static void check_strcmp_offset(const char *string1, const char *string2, + int expect_result, uintmax_t expect_offset) +{ + size_t offset; + int result = strcmp_offset(string1, string2, &offset); + + /* + * Because different CRTs behave differently, only rely on signs of the + * result values. + */ + result = (result < 0 ? -1 : + result > 0 ? 1 : + 0); + + cl_assert_equal_i(result, expect_result); + cl_assert_equal_i((uintmax_t)offset, expect_offset); +} + +void test_strcmp_offset__empty(void) +{ + check_strcmp_offset("", "", 0, 0); +} + +void test_strcmp_offset__equal(void) +{ + check_strcmp_offset("abc", "abc", 0, 3); +} + +void test_strcmp_offset__different(void) +{ + check_strcmp_offset("abc", "def", -1, 0); +} + +void test_strcmp_offset__mismatch(void) +{ + check_strcmp_offset("abc", "abz", -1, 2); +} + +void test_strcmp_offset__different_length(void) +{ + check_strcmp_offset("abc", "abcdef", -1, 3); +}