From patchwork Fri Aug 9 11:05:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chandra Pratap X-Patchwork-Id: 13758702 Received: from mail-oi1-f173.google.com (mail-oi1-f173.google.com [209.85.167.173]) (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 4EC6D19306A for ; Fri, 9 Aug 2024 11:13:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723202026; cv=none; b=NcK87zuZM0s+PVRvTLikm6hxi+XEn4oj+89fANjPxGMRe9Kwbx4HPiZtT0s5ALOyjJJleb2CAN+7OWqjrok3uLfjT1W3oZR4H/JDQlrCTj1NpF6M1nlWE5BoXErDyAc2CSOn/0AYBIMvqEUl/Mjfu1GvrdCGfTY3ZUdGuF9Oshc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723202026; c=relaxed/simple; bh=stqmcweIMPMEas1OCJSOLfDrAW5QfFO9g8qE4FHE+hc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=eha85gkz+xUWE0rqNyhOURPI+3VvSNBFQoNwS1F0yp6WNx59gL4JBzwwDwXRkw1xQxnIL5Xf7QGAAqX4g1Pt2UXzyBkNnuqAeA51sXjIOPr0UBQBLjgKEft9FiBKk7D3F9tCjQJGeCdFN1zfb9vW3J0kLgdxXg3aXA4dWN0PMAQ= 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=Z/SFS5Lh; arc=none smtp.client-ip=209.85.167.173 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="Z/SFS5Lh" Received: by mail-oi1-f173.google.com with SMTP id 5614622812f47-3db50abf929so1386615b6e.2 for ; Fri, 09 Aug 2024 04:13:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723202023; x=1723806823; 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=SAUWDxzybE8j+ucUI5K6++zQJVuVePlW53lS1vKvI08=; b=Z/SFS5Lhu7T4teGimDHHZ/cPL4nvPlX+MD0V4VNq5MJEZLezQu0TDD8GFjUghB8P4z wTm77aLlDZOVgmilm1nt8Eiti44OtO1lZGBpKuI6Ox6KPky1canAUpR7UeG06n8aYIIS yDNrYvHnpUSjtH4bSHOeDBk+9X4uYdxIKrJ9LTiazaTRjaFjVgzjCDMxAOHmBU1SbAna yt9fe8H9r1R9fIBNXcj0f+5anuHULEBSknbf5b76gYXYFpyxJzIiONnu8h+sXFX0TmLJ ng726Ko7pUo4jfpdf46Omh/2l46pQ3D069RjvJURIY1dUoTpzzKaujFbDe08SHWfuUSl 6DoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723202023; x=1723806823; 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=SAUWDxzybE8j+ucUI5K6++zQJVuVePlW53lS1vKvI08=; b=Dyen/WOQmbHL2Ja10IUqzYS7vPgpGbUyfejtUvYrFseFFcKbdi/C3CE/OkQ4tR5R4h lxqWw0g2oKJg+/HJy9Z0+vTX1TBXmVkriFu71bD0lUtHwA3UiB/xSUm4lO/Vvyb+bc30 bqfvxga0AsQr9Lh+rCH4sM6ViL0AfZwjaLYpJObOI+WsvWJxyammxzoUNRjGPWEz8BhW IXFhPr/mc5B78TBJpvghZwALoBHhczjuVOry0ldqTtsPkMUBg+RpmCF+q3cwoKuLBzIw sy8s7iEtvxE+TShGOK5rG5YEZN4j94yzEYUrNsBvC7v4/ZabD9c5xmqGTm5WZSwk3pst 6Nww== X-Gm-Message-State: AOJu0YyY2uOnP+pm1+opjhP7wQf0FEu6y7YATh7mVTGczupzuBz/uILj w4b+Yc2cgY0nJfip8etGsoULbT/MngmixgOkA001jy7zVq5r6ZPEBK7DAJ5L X-Google-Smtp-Source: AGHT+IHeN3IcwGDr9NikQhV2U8iztUGBH9KF1XZxgoxoZuIWQD2TNSykt0X6cyIKZehaNJJU21I17A== X-Received: by 2002:a05:6870:5688:b0:261:86d:89e2 with SMTP id 586e51a60fabf-26c62f1ee09mr1401709fac.36.1723202022538; Fri, 09 Aug 2024 04:13:42 -0700 (PDT) Received: from Ubuntu.. ([117.99.60.199]) by smtp.googlemail.com with ESMTPSA id d2e1a72fcca58-710cb2e74d1sm2490377b3a.171.2024.08.09.04.13.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Aug 2024 04:13:42 -0700 (PDT) From: Chandra Pratap To: git@vger.kernel.org Cc: Chandra Pratap , Patrick Steinhardt , Christian Couder Subject: [PATCH v2 1/4] t: move reftable/readwrite_test.c to the unit testing framework Date: Fri, 9 Aug 2024 16:35:41 +0530 Message-ID: <20240809111312.4401-2-chandrapratap3519@gmail.com> X-Mailer: git-send-email 2.45.GIT In-Reply-To: <20240809111312.4401-1-chandrapratap3519@gmail.com> References: <20240807141608.4524-1-chandrapratap3519@gmail.com> <20240809111312.4401-1-chandrapratap3519@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 reftable/readwrite_test.c exercises the functions defined in reftable/reader.{c,h} and reftable/writer.{c,h}. Migrate reftable/readwrite_test.c to the unit testing framework. Migration involves refactoring the tests to use the unit testing framework instead of reftable's test framework and renaming the tests to align with unit-tests' naming conventions. Since some tests in reftable/readwrite_test.c use the functions set_test_hash(), noop_flush() and strbuf_add_void() defined in reftable/test_framework.{c,h} but these files are not #included in the ported unit test, copy these functions in the new test file. While at it, ensure structs are 0-initialized with '= { 0 }' instead of '= { NULL }'. Mentored-by: Patrick Steinhardt Mentored-by: Christian Couder Signed-off-by: Chandra Pratap --- Makefile | 2 +- reftable/reftable-tests.h | 1 - t/helper/test-reftable.c | 1 - .../unit-tests/t-reftable-readwrite.c | 418 +++++++++--------- 4 files changed, 210 insertions(+), 212 deletions(-) rename reftable/readwrite_test.c => t/unit-tests/t-reftable-readwrite.c (70%) diff --git a/Makefile b/Makefile index 3863e60b66..76e4d1c1ec 100644 --- a/Makefile +++ b/Makefile @@ -1341,6 +1341,7 @@ UNIT_TEST_PROGRAMS += t-oidtree UNIT_TEST_PROGRAMS += t-prio-queue UNIT_TEST_PROGRAMS += t-reftable-basics UNIT_TEST_PROGRAMS += t-reftable-merged +UNIT_TEST_PROGRAMS += t-reftable-readwrite UNIT_TEST_PROGRAMS += t-reftable-record UNIT_TEST_PROGRAMS += t-strbuf UNIT_TEST_PROGRAMS += t-strcmp-offset @@ -2682,7 +2683,6 @@ REFTABLE_OBJS += reftable/writer.o REFTABLE_TEST_OBJS += reftable/block_test.o REFTABLE_TEST_OBJS += reftable/dump.o REFTABLE_TEST_OBJS += reftable/pq_test.o -REFTABLE_TEST_OBJS += reftable/readwrite_test.o REFTABLE_TEST_OBJS += reftable/stack_test.o REFTABLE_TEST_OBJS += reftable/test_framework.o REFTABLE_TEST_OBJS += reftable/tree_test.o diff --git a/reftable/reftable-tests.h b/reftable/reftable-tests.h index d5e03dcc1b..7d955393d2 100644 --- a/reftable/reftable-tests.h +++ b/reftable/reftable-tests.h @@ -13,7 +13,6 @@ int basics_test_main(int argc, const char **argv); int block_test_main(int argc, const char **argv); int pq_test_main(int argc, const char **argv); int record_test_main(int argc, const char **argv); -int readwrite_test_main(int argc, const char **argv); int stack_test_main(int argc, const char **argv); int tree_test_main(int argc, const char **argv); int reftable_dump_main(int argc, char *const *argv); diff --git a/t/helper/test-reftable.c b/t/helper/test-reftable.c index 9d378427da..d371e9f9dd 100644 --- a/t/helper/test-reftable.c +++ b/t/helper/test-reftable.c @@ -8,7 +8,6 @@ int cmd__reftable(int argc, const char **argv) block_test_main(argc, argv); tree_test_main(argc, argv); pq_test_main(argc, argv); - readwrite_test_main(argc, argv); stack_test_main(argc, argv); return 0; } diff --git a/reftable/readwrite_test.c b/t/unit-tests/t-reftable-readwrite.c similarity index 70% rename from reftable/readwrite_test.c rename to t/unit-tests/t-reftable-readwrite.c index f411abfe9c..235e3d94c7 100644 --- a/reftable/readwrite_test.c +++ b/t/unit-tests/t-reftable-readwrite.c @@ -6,37 +6,48 @@ license that can be found in the LICENSE file or at https://developers.google.com/open-source/licenses/bsd */ -#include "system.h" - -#include "basics.h" -#include "block.h" -#include "blocksource.h" -#include "reader.h" -#include "record.h" -#include "test_framework.h" -#include "reftable-tests.h" -#include "reftable-writer.h" +#include "test-lib.h" +#include "reftable/reader.h" +#include "reftable/blocksource.h" +#include "reftable/reftable-error.h" +#include "reftable/reftable-writer.h" static const int update_index = 5; -static void test_buffer(void) +static void set_test_hash(uint8_t *p, int i) +{ + memset(p, (uint8_t)i, hash_size(GIT_SHA1_FORMAT_ID)); +} + +static ssize_t strbuf_add_void(void *b, const void *data, size_t sz) +{ + strbuf_add(b, data, sz); + return sz; +} + +static int noop_flush(void *arg) +{ + return 0; +} + +static void t_buffer(void) { struct strbuf buf = STRBUF_INIT; - struct reftable_block_source source = { NULL }; - struct reftable_block out = { NULL }; + struct reftable_block_source source = { 0 }; + struct reftable_block out = { 0 }; int n; uint8_t in[] = "hello"; strbuf_add(&buf, in, sizeof(in)); block_source_from_strbuf(&source, &buf); - EXPECT(block_source_size(&source) == 6); + check_int(block_source_size(&source), ==, 6); n = block_source_read_block(&source, &out, 0, sizeof(in)); - EXPECT(n == sizeof(in)); - EXPECT(!memcmp(in, out.data, n)); + check_int(n, ==, sizeof(in)); + check(!memcmp(in, out.data, n)); reftable_block_done(&out); n = block_source_read_block(&source, &out, 1, 2); - EXPECT(n == 2); - EXPECT(!memcmp(out.data, "el", 2)); + check_int(n, ==, 2); + check(!memcmp(out.data, "el", 2)); reftable_block_done(&out); block_source_close(&source); @@ -52,9 +63,9 @@ static void write_table(char ***names, struct strbuf *buf, int N, }; struct reftable_writer *w = reftable_new_writer(&strbuf_add_void, &noop_flush, buf, &opts); - struct reftable_ref_record ref = { NULL }; + struct reftable_ref_record ref = { 0 }; int i = 0, n; - struct reftable_log_record log = { NULL }; + struct reftable_log_record log = { 0 }; const struct reftable_stats *stats = NULL; REFTABLE_CALLOC_ARRAY(*names, N + 1); @@ -73,7 +84,7 @@ static void write_table(char ***names, struct strbuf *buf, int N, (*names)[i] = xstrdup(name); n = reftable_writer_add_ref(w, &ref); - EXPECT(n == 0); + check_int(n, ==, 0); } for (i = 0; i < N; i++) { @@ -89,27 +100,25 @@ static void write_table(char ***names, struct strbuf *buf, int N, log.value.update.message = (char *) "message"; n = reftable_writer_add_log(w, &log); - EXPECT(n == 0); + check_int(n, ==, 0); } n = reftable_writer_close(w); - EXPECT(n == 0); + check_int(n, ==, 0); stats = reftable_writer_stats(w); for (i = 0; i < stats->ref_stats.blocks; i++) { int off = i * opts.block_size; - if (off == 0) { - off = header_size( - (hash_id == GIT_SHA256_FORMAT_ID) ? 2 : 1); - } - EXPECT(buf->buf[off] == 'r'); + if (!off) + off = header_size((hash_id == GIT_SHA256_FORMAT_ID) ? 2 : 1); + check(buf->buf[off] == 'r'); } - EXPECT(stats->log_stats.blocks > 0); + check(stats->log_stats.blocks > 0); reftable_writer_free(w); } -static void test_log_buffer_size(void) +static void t_log_buffer_size(void) { struct strbuf buf = STRBUF_INIT; struct reftable_write_options opts = { @@ -140,14 +149,14 @@ static void test_log_buffer_size(void) } reftable_writer_set_limits(w, update_index, update_index); err = reftable_writer_add_log(w, &log); - EXPECT_ERR(err); + check(!err); err = reftable_writer_close(w); - EXPECT_ERR(err); + check(!err); reftable_writer_free(w); strbuf_release(&buf); } -static void test_log_overflow(void) +static void t_log_overflow(void) { struct strbuf buf = STRBUF_INIT; char msg[256] = { 0 }; @@ -177,12 +186,12 @@ static void test_log_overflow(void) memset(msg, 'x', sizeof(msg) - 1); reftable_writer_set_limits(w, update_index, update_index); err = reftable_writer_add_log(w, &log); - EXPECT(err == REFTABLE_ENTRY_TOO_BIG_ERROR); + check_int(err, ==, REFTABLE_ENTRY_TOO_BIG_ERROR); reftable_writer_free(w); strbuf_release(&buf); } -static void test_log_write_read(void) +static void t_log_write_read(void) { int N = 2; char **names = reftable_calloc(N + 1, sizeof(*names)); @@ -190,13 +199,13 @@ static void test_log_write_read(void) struct reftable_write_options opts = { .block_size = 256, }; - struct reftable_ref_record ref = { NULL }; + struct reftable_ref_record ref = { 0 }; int i = 0; - struct reftable_log_record log = { NULL }; + struct reftable_log_record log = { 0 }; int n; - struct reftable_iterator it = { NULL }; - struct reftable_reader rd = { NULL }; - struct reftable_block_source source = { NULL }; + struct reftable_iterator it = { 0 }; + struct reftable_reader rd = { 0 }; + struct reftable_block_source source = { 0 }; struct strbuf buf = STRBUF_INIT; struct reftable_writer *w = reftable_new_writer(&strbuf_add_void, &noop_flush, &buf, &opts); @@ -204,17 +213,17 @@ static void test_log_write_read(void) reftable_writer_set_limits(w, 0, N); for (i = 0; i < N; i++) { char name[256]; - struct reftable_ref_record ref = { NULL }; + struct reftable_ref_record ref = { 0 }; snprintf(name, sizeof(name), "b%02d%0*d", i, 130, 7); names[i] = xstrdup(name); ref.refname = name; ref.update_index = i; err = reftable_writer_add_ref(w, &ref); - EXPECT_ERR(err); + check(!err); } for (i = 0; i < N; i++) { - struct reftable_log_record log = { NULL }; + struct reftable_log_record log = { 0 }; log.refname = names[i]; log.update_index = i; @@ -223,33 +232,33 @@ static void test_log_write_read(void) set_test_hash(log.value.update.new_hash, i + 1); err = reftable_writer_add_log(w, &log); - EXPECT_ERR(err); + check(!err); } n = reftable_writer_close(w); - EXPECT(n == 0); + check_int(n, ==, 0); stats = reftable_writer_stats(w); - EXPECT(stats->log_stats.blocks > 0); + check_int(stats->log_stats.blocks, >, 0); reftable_writer_free(w); w = NULL; block_source_from_strbuf(&source, &buf); err = init_reader(&rd, &source, "file.log"); - EXPECT_ERR(err); + check(!err); reftable_reader_init_ref_iterator(&rd, &it); err = reftable_iterator_seek_ref(&it, names[N - 1]); - EXPECT_ERR(err); + check(!err); err = reftable_iterator_next_ref(&it, &ref); - EXPECT_ERR(err); + check(!err); /* end of iteration. */ err = reftable_iterator_next_ref(&it, &ref); - EXPECT(0 < err); + check_int(err, >, 0); reftable_iterator_destroy(&it); reftable_ref_record_release(&ref); @@ -257,23 +266,21 @@ static void test_log_write_read(void) reftable_reader_init_log_iterator(&rd, &it); err = reftable_iterator_seek_log(&it, ""); - EXPECT_ERR(err); + check(!err); i = 0; while (1) { int err = reftable_iterator_next_log(&it, &log); - if (err > 0) { + if (err > 0) break; - } - - EXPECT_ERR(err); - EXPECT_STREQ(names[i], log.refname); - EXPECT(i == log.update_index); + check(!err); + check_str(names[i], log.refname); + check_int(i, ==, log.update_index); i++; reftable_log_record_release(&log); } - EXPECT(i == N); + check_int(i, ==, N); reftable_iterator_destroy(&it); /* cleanup. */ @@ -282,7 +289,7 @@ static void test_log_write_read(void) reader_close(&rd); } -static void test_log_zlib_corruption(void) +static void t_log_zlib_corruption(void) { struct reftable_write_options opts = { .block_size = 256, @@ -316,13 +323,13 @@ static void test_log_zlib_corruption(void) reftable_writer_set_limits(w, 1, 1); err = reftable_writer_add_log(w, &log); - EXPECT_ERR(err); + check(!err); n = reftable_writer_close(w); - EXPECT(n == 0); + check_int(n, ==, 0); stats = reftable_writer_stats(w); - EXPECT(stats->log_stats.blocks > 0); + check_int(stats->log_stats.blocks, >, 0); reftable_writer_free(w); w = NULL; @@ -332,11 +339,11 @@ static void test_log_zlib_corruption(void) block_source_from_strbuf(&source, &buf); err = init_reader(&rd, &source, "file.log"); - EXPECT_ERR(err); + check(!err); reftable_reader_init_log_iterator(&rd, &it); err = reftable_iterator_seek_log(&it, "refname"); - EXPECT(err == REFTABLE_ZLIB_ERROR); + check_int(err, ==, REFTABLE_ZLIB_ERROR); reftable_iterator_destroy(&it); @@ -345,14 +352,14 @@ static void test_log_zlib_corruption(void) reader_close(&rd); } -static void test_table_read_write_sequential(void) +static void t_table_read_write_sequential(void) { char **names; struct strbuf buf = STRBUF_INIT; int N = 50; - struct reftable_iterator it = { NULL }; - struct reftable_block_source source = { NULL }; - struct reftable_reader rd = { NULL }; + struct reftable_iterator it = { 0 }; + struct reftable_block_source source = { 0 }; + struct reftable_reader rd = { 0 }; int err = 0; int j = 0; @@ -361,26 +368,25 @@ static void test_table_read_write_sequential(void) block_source_from_strbuf(&source, &buf); err = init_reader(&rd, &source, "file.ref"); - EXPECT_ERR(err); + check(!err); reftable_reader_init_ref_iterator(&rd, &it); err = reftable_iterator_seek_ref(&it, ""); - EXPECT_ERR(err); + check(!err); while (1) { - struct reftable_ref_record ref = { NULL }; + struct reftable_ref_record ref = { 0 }; int r = reftable_iterator_next_ref(&it, &ref); - EXPECT(r >= 0); - if (r > 0) { + check_int(r, >=, 0); + if (r > 0) break; - } - EXPECT(0 == strcmp(names[j], ref.refname)); - EXPECT(update_index == ref.update_index); + check_str(names[j], ref.refname); + check_int(update_index, ==, ref.update_index); j++; reftable_ref_record_release(&ref); } - EXPECT(j == N); + check_int(j, ==, N); reftable_iterator_destroy(&it); strbuf_release(&buf); free_names(names); @@ -388,90 +394,88 @@ static void test_table_read_write_sequential(void) reader_close(&rd); } -static void test_table_write_small_table(void) +static void t_table_write_small_table(void) { char **names; struct strbuf buf = STRBUF_INIT; int N = 1; write_table(&names, &buf, N, 4096, GIT_SHA1_FORMAT_ID); - EXPECT(buf.len < 200); + check_int(buf.len, <, 200); strbuf_release(&buf); free_names(names); } -static void test_table_read_api(void) +static void t_table_read_api(void) { char **names; struct strbuf buf = STRBUF_INIT; int N = 50; - struct reftable_reader rd = { NULL }; - struct reftable_block_source source = { NULL }; + struct reftable_reader rd = { 0 }; + struct reftable_block_source source = { 0 }; int err; int i; - struct reftable_log_record log = { NULL }; - struct reftable_iterator it = { NULL }; + struct reftable_log_record log = { 0 }; + struct reftable_iterator it = { 0 }; write_table(&names, &buf, N, 256, GIT_SHA1_FORMAT_ID); block_source_from_strbuf(&source, &buf); err = init_reader(&rd, &source, "file.ref"); - EXPECT_ERR(err); + check(!err); reftable_reader_init_ref_iterator(&rd, &it); err = reftable_iterator_seek_ref(&it, names[0]); - EXPECT_ERR(err); + check(!err); err = reftable_iterator_next_log(&it, &log); - EXPECT(err == REFTABLE_API_ERROR); + check_int(err, ==, REFTABLE_API_ERROR); strbuf_release(&buf); - for (i = 0; i < N; i++) { + for (i = 0; i < N; i++) reftable_free(names[i]); - } reftable_iterator_destroy(&it); reftable_free(names); reader_close(&rd); strbuf_release(&buf); } -static void test_table_read_write_seek(int index, int hash_id) +static void t_table_read_write_seek(int index, int hash_id) { char **names; struct strbuf buf = STRBUF_INIT; int N = 50; - struct reftable_reader rd = { NULL }; - struct reftable_block_source source = { NULL }; + struct reftable_reader rd = { 0 }; + struct reftable_block_source source = { 0 }; int err; int i = 0; - struct reftable_iterator it = { NULL }; + struct reftable_iterator it = { 0 }; struct strbuf pastLast = STRBUF_INIT; - struct reftable_ref_record ref = { NULL }; + struct reftable_ref_record ref = { 0 }; write_table(&names, &buf, N, 256, hash_id); block_source_from_strbuf(&source, &buf); err = init_reader(&rd, &source, "file.ref"); - EXPECT_ERR(err); - EXPECT(hash_id == reftable_reader_hash_id(&rd)); + check(!err); + check_int(hash_id, ==, reftable_reader_hash_id(&rd)); - if (!index) { + if (!index) rd.ref_offsets.index_offset = 0; - } else { - EXPECT(rd.ref_offsets.index_offset > 0); - } + else + check_int(rd.ref_offsets.index_offset, >, 0); for (i = 1; i < N; i++) { reftable_reader_init_ref_iterator(&rd, &it); err = reftable_iterator_seek_ref(&it, names[i]); - EXPECT_ERR(err); + check(!err); err = reftable_iterator_next_ref(&it, &ref); - EXPECT_ERR(err); - EXPECT(0 == strcmp(names[i], ref.refname)); - EXPECT(REFTABLE_REF_VAL1 == ref.value_type); - EXPECT(i == ref.value.val1[0]); + check(!err); + check_str(names[i], ref.refname); + check_int(REFTABLE_REF_VAL1, ==, ref.value_type); + check_int(i, ==, ref.value.val1[0]); reftable_ref_record_release(&ref); reftable_iterator_destroy(&it); @@ -483,40 +487,39 @@ static void test_table_read_write_seek(int index, int hash_id) reftable_reader_init_ref_iterator(&rd, &it); err = reftable_iterator_seek_ref(&it, pastLast.buf); if (err == 0) { - struct reftable_ref_record ref = { NULL }; + struct reftable_ref_record ref = { 0 }; int err = reftable_iterator_next_ref(&it, &ref); - EXPECT(err > 0); + check_int(err, >, 0); } else { - EXPECT(err > 0); + check_int(err, >, 0); } strbuf_release(&pastLast); reftable_iterator_destroy(&it); strbuf_release(&buf); - for (i = 0; i < N; i++) { + for (i = 0; i < N; i++) reftable_free(names[i]); - } reftable_free(names); reader_close(&rd); } -static void test_table_read_write_seek_linear(void) +static void t_table_read_write_seek_linear(void) { - test_table_read_write_seek(0, GIT_SHA1_FORMAT_ID); + t_table_read_write_seek(0, GIT_SHA1_FORMAT_ID); } -static void test_table_read_write_seek_linear_sha256(void) +static void t_table_read_write_seek_linear_sha256(void) { - test_table_read_write_seek(0, GIT_SHA256_FORMAT_ID); + t_table_read_write_seek(0, GIT_SHA256_FORMAT_ID); } -static void test_table_read_write_seek_index(void) +static void t_table_read_write_seek_index(void) { - test_table_read_write_seek(1, GIT_SHA1_FORMAT_ID); + t_table_read_write_seek(1, GIT_SHA1_FORMAT_ID); } -static void test_table_refs_for(int indexed) +static void t_table_refs_for(int indexed) { int N = 50; char **want_names = reftable_calloc(N + 1, sizeof(*want_names)); @@ -526,18 +529,18 @@ static void test_table_refs_for(int indexed) struct reftable_write_options opts = { .block_size = 256, }; - struct reftable_ref_record ref = { NULL }; + struct reftable_ref_record ref = { 0 }; int i = 0; int n; int err; struct reftable_reader rd; - struct reftable_block_source source = { NULL }; + struct reftable_block_source source = { 0 }; struct strbuf buf = STRBUF_INIT; struct reftable_writer *w = reftable_new_writer(&strbuf_add_void, &noop_flush, &buf, &opts); - struct reftable_iterator it = { NULL }; + struct reftable_iterator it = { 0 }; int j; set_test_hash(want_hash, 4); @@ -546,7 +549,7 @@ static void test_table_refs_for(int indexed) uint8_t hash[GIT_SHA1_RAWSZ]; char fill[51] = { 0 }; char name[100]; - struct reftable_ref_record ref = { NULL }; + struct reftable_ref_record ref = { 0 }; memset(hash, i, sizeof(hash)); memset(fill, 'x', 50); @@ -563,16 +566,15 @@ static void test_table_refs_for(int indexed) */ /* blocks. */ n = reftable_writer_add_ref(w, &ref); - EXPECT(n == 0); + check_int(n, ==, 0); if (!memcmp(ref.value.val2.value, want_hash, GIT_SHA1_RAWSZ) || - !memcmp(ref.value.val2.target_value, want_hash, GIT_SHA1_RAWSZ)) { + !memcmp(ref.value.val2.target_value, want_hash, GIT_SHA1_RAWSZ)) want_names[want_names_len++] = xstrdup(name); - } } n = reftable_writer_close(w); - EXPECT(n == 0); + check_int(n, ==, 0); reftable_writer_free(w); w = NULL; @@ -580,33 +582,30 @@ static void test_table_refs_for(int indexed) block_source_from_strbuf(&source, &buf); err = init_reader(&rd, &source, "file.ref"); - EXPECT_ERR(err); - if (!indexed) { + check(!err); + if (!indexed) rd.obj_offsets.is_present = 0; - } reftable_reader_init_ref_iterator(&rd, &it); err = reftable_iterator_seek_ref(&it, ""); - EXPECT_ERR(err); + check(!err); reftable_iterator_destroy(&it); err = reftable_reader_refs_for(&rd, &it, want_hash); - EXPECT_ERR(err); + check(!err); j = 0; while (1) { int err = reftable_iterator_next_ref(&it, &ref); - EXPECT(err >= 0); - if (err > 0) { + check_int(err, >=, 0); + if (err > 0) break; - } - - EXPECT(j < want_names_len); - EXPECT(0 == strcmp(ref.refname, want_names[j])); + check_int(j, <, want_names_len); + check_str(ref.refname, want_names[j]); j++; reftable_ref_record_release(&ref); } - EXPECT(j == want_names_len); + check_int(j, ==, want_names_len); strbuf_release(&buf); free_names(want_names); @@ -614,54 +613,54 @@ static void test_table_refs_for(int indexed) reader_close(&rd); } -static void test_table_refs_for_no_index(void) +static void t_table_refs_for_no_index(void) { - test_table_refs_for(0); + t_table_refs_for(0); } -static void test_table_refs_for_obj_index(void) +static void t_table_refs_for_obj_index(void) { - test_table_refs_for(1); + t_table_refs_for(1); } -static void test_write_empty_table(void) +static void t_write_empty_table(void) { struct reftable_write_options opts = { 0 }; struct strbuf buf = STRBUF_INIT; struct reftable_writer *w = reftable_new_writer(&strbuf_add_void, &noop_flush, &buf, &opts); - struct reftable_block_source source = { NULL }; + struct reftable_block_source source = { 0 }; struct reftable_reader *rd = NULL; - struct reftable_ref_record rec = { NULL }; - struct reftable_iterator it = { NULL }; + struct reftable_ref_record rec = { 0 }; + struct reftable_iterator it = { 0 }; int err; reftable_writer_set_limits(w, 1, 1); err = reftable_writer_close(w); - EXPECT(err == REFTABLE_EMPTY_TABLE_ERROR); + check_int(err, ==, REFTABLE_EMPTY_TABLE_ERROR); reftable_writer_free(w); - EXPECT(buf.len == header_size(1) + footer_size(1)); + check_int(buf.len, ==, header_size(1) + footer_size(1)); block_source_from_strbuf(&source, &buf); err = reftable_new_reader(&rd, &source, "filename"); - EXPECT_ERR(err); + check(!err); reftable_reader_init_ref_iterator(rd, &it); err = reftable_iterator_seek_ref(&it, ""); - EXPECT_ERR(err); + check(!err); err = reftable_iterator_next_ref(&it, &rec); - EXPECT(err > 0); + check_int(err, >, 0); reftable_iterator_destroy(&it); reftable_reader_free(rd); strbuf_release(&buf); } -static void test_write_object_id_min_length(void) +static void t_write_object_id_min_length(void) { struct reftable_write_options opts = { .block_size = 75, @@ -686,17 +685,17 @@ static void test_write_object_id_min_length(void) snprintf(name, sizeof(name), "ref%05d", i); ref.refname = name; err = reftable_writer_add_ref(w, &ref); - EXPECT_ERR(err); + check(!err); } err = reftable_writer_close(w); - EXPECT_ERR(err); - EXPECT(reftable_writer_stats(w)->object_id_len == 2); + check(!err); + check_int(reftable_writer_stats(w)->object_id_len, ==, 2); reftable_writer_free(w); strbuf_release(&buf); } -static void test_write_object_id_length(void) +static void t_write_object_id_length(void) { struct reftable_write_options opts = { .block_size = 75, @@ -722,17 +721,17 @@ static void test_write_object_id_length(void) ref.refname = name; ref.value.val1[15] = i; err = reftable_writer_add_ref(w, &ref); - EXPECT_ERR(err); + check(!err); } err = reftable_writer_close(w); - EXPECT_ERR(err); - EXPECT(reftable_writer_stats(w)->object_id_len == 16); + check(!err); + check_int(reftable_writer_stats(w)->object_id_len, ==, 16); reftable_writer_free(w); strbuf_release(&buf); } -static void test_write_empty_key(void) +static void t_write_empty_key(void) { struct reftable_write_options opts = { 0 }; struct strbuf buf = STRBUF_INIT; @@ -747,15 +746,15 @@ static void test_write_empty_key(void) reftable_writer_set_limits(w, 1, 1); err = reftable_writer_add_ref(w, &ref); - EXPECT(err == REFTABLE_API_ERROR); + check_int(err, ==, REFTABLE_API_ERROR); err = reftable_writer_close(w); - EXPECT(err == REFTABLE_EMPTY_TABLE_ERROR); + check_int(err, ==, REFTABLE_EMPTY_TABLE_ERROR); reftable_writer_free(w); strbuf_release(&buf); } -static void test_write_key_order(void) +static void t_write_key_order(void) { struct reftable_write_options opts = { 0 }; struct strbuf buf = STRBUF_INIT; @@ -782,15 +781,15 @@ static void test_write_key_order(void) reftable_writer_set_limits(w, 1, 1); err = reftable_writer_add_ref(w, &refs[0]); - EXPECT_ERR(err); + check(!err); err = reftable_writer_add_ref(w, &refs[1]); - EXPECT(err == REFTABLE_API_ERROR); + check_int(err, ==, REFTABLE_API_ERROR); reftable_writer_close(w); reftable_writer_free(w); strbuf_release(&buf); } -static void test_write_multiple_indices(void) +static void t_write_multiple_indices(void) { struct reftable_write_options opts = { .block_size = 100, @@ -817,7 +816,7 @@ static void test_write_multiple_indices(void) ref.refname = buf.buf, err = reftable_writer_add_ref(writer, &ref); - EXPECT_ERR(err); + check(!err); } for (i = 0; i < 100; i++) { @@ -835,7 +834,7 @@ static void test_write_multiple_indices(void) log.refname = buf.buf, err = reftable_writer_add_log(writer, &log); - EXPECT_ERR(err); + check(!err); } reftable_writer_close(writer); @@ -845,13 +844,13 @@ static void test_write_multiple_indices(void) * for each of the block types. */ stats = reftable_writer_stats(writer); - EXPECT(stats->ref_stats.index_offset > 0); - EXPECT(stats->obj_stats.index_offset > 0); - EXPECT(stats->log_stats.index_offset > 0); + check_int(stats->ref_stats.index_offset, >, 0); + check_int(stats->obj_stats.index_offset, >, 0); + check_int(stats->log_stats.index_offset, >, 0); block_source_from_strbuf(&source, &writer_buf); err = reftable_new_reader(&reader, &source, "filename"); - EXPECT_ERR(err); + check(!err); /* * Seeking the log uses the log index now. In case there is any @@ -859,7 +858,7 @@ static void test_write_multiple_indices(void) */ reftable_reader_init_log_iterator(reader, &it); err = reftable_iterator_seek_log(&it, ""); - EXPECT_ERR(err); + check(!err); reftable_iterator_destroy(&it); reftable_writer_free(writer); @@ -868,7 +867,7 @@ static void test_write_multiple_indices(void) strbuf_release(&buf); } -static void test_write_multi_level_index(void) +static void t_write_multi_level_index(void) { struct reftable_write_options opts = { .block_size = 100, @@ -895,7 +894,7 @@ static void test_write_multi_level_index(void) ref.refname = buf.buf, err = reftable_writer_add_ref(writer, &ref); - EXPECT_ERR(err); + check(!err); } reftable_writer_close(writer); @@ -904,18 +903,18 @@ static void test_write_multi_level_index(void) * multi-level index. */ stats = reftable_writer_stats(writer); - EXPECT(stats->ref_stats.max_index_level == 2); + check_int(stats->ref_stats.max_index_level, ==, 2); block_source_from_strbuf(&source, &writer_buf); err = reftable_new_reader(&reader, &source, "filename"); - EXPECT_ERR(err); + check(!err); /* * Seeking the last ref should work as expected. */ reftable_reader_init_ref_iterator(reader, &it); err = reftable_iterator_seek_ref(&it, "refs/heads/199"); - EXPECT_ERR(err); + check(!err); reftable_iterator_destroy(&it); reftable_writer_free(writer); @@ -924,56 +923,57 @@ static void test_write_multi_level_index(void) strbuf_release(&buf); } -static void test_corrupt_table_empty(void) +static void t_corrupt_table_empty(void) { struct strbuf buf = STRBUF_INIT; - struct reftable_block_source source = { NULL }; - struct reftable_reader rd = { NULL }; + struct reftable_block_source source = { 0 }; + struct reftable_reader rd = { 0 }; int err; block_source_from_strbuf(&source, &buf); err = init_reader(&rd, &source, "file.log"); - EXPECT(err == REFTABLE_FORMAT_ERROR); + check_int(err, ==, REFTABLE_FORMAT_ERROR); } -static void test_corrupt_table(void) +static void t_corrupt_table(void) { uint8_t zeros[1024] = { 0 }; struct strbuf buf = STRBUF_INIT; - struct reftable_block_source source = { NULL }; - struct reftable_reader rd = { NULL }; + struct reftable_block_source source = { 0 }; + struct reftable_reader rd = { 0 }; int err; strbuf_add(&buf, zeros, sizeof(zeros)); block_source_from_strbuf(&source, &buf); err = init_reader(&rd, &source, "file.log"); - EXPECT(err == REFTABLE_FORMAT_ERROR); + check_int(err, ==, REFTABLE_FORMAT_ERROR); strbuf_release(&buf); } -int readwrite_test_main(int argc, const char *argv[]) +int cmd_main(int argc, const char *argv[]) { - RUN_TEST(test_log_zlib_corruption); - RUN_TEST(test_corrupt_table); - RUN_TEST(test_corrupt_table_empty); - RUN_TEST(test_log_write_read); - RUN_TEST(test_write_key_order); - RUN_TEST(test_table_read_write_seek_linear_sha256); - RUN_TEST(test_log_buffer_size); - RUN_TEST(test_table_write_small_table); - RUN_TEST(test_buffer); - RUN_TEST(test_table_read_api); - RUN_TEST(test_table_read_write_sequential); - RUN_TEST(test_table_read_write_seek_linear); - RUN_TEST(test_table_read_write_seek_index); - RUN_TEST(test_table_refs_for_no_index); - RUN_TEST(test_table_refs_for_obj_index); - RUN_TEST(test_write_empty_key); - RUN_TEST(test_write_empty_table); - RUN_TEST(test_log_overflow); - RUN_TEST(test_write_object_id_length); - RUN_TEST(test_write_object_id_min_length); - RUN_TEST(test_write_multiple_indices); - RUN_TEST(test_write_multi_level_index); - return 0; + TEST(t_buffer(), "strbuf works as blocksource"); + TEST(t_corrupt_table(), "read-write on corrupted table"); + TEST(t_corrupt_table_empty(), "read-write on an empty table"); + TEST(t_log_buffer_size(), "buffer extension for log compression"); + TEST(t_log_overflow(), "log overflow returns expected error"); + TEST(t_log_write_read(), "read-write on log records"); + TEST(t_log_zlib_corruption(), "reading corrupted log record returns expected error"); + TEST(t_table_read_api(), "read on a table"); + TEST(t_table_read_write_seek_index(), "read-write on a table with index"); + TEST(t_table_read_write_seek_linear(), "read-write on a table without index (SHA1)"); + TEST(t_table_read_write_seek_linear_sha256(), "read-write on a table without index (SHA256)"); + TEST(t_table_read_write_sequential(), "sequential read-write on a table"); + TEST(t_table_refs_for_no_index(), "refs-only table with no index"); + TEST(t_table_refs_for_obj_index(), "refs-only table with index"); + TEST(t_table_write_small_table(), "write_table works"); + TEST(t_write_empty_key(), "write on refs with empty keys"); + TEST(t_write_empty_table(), "read-write on empty tables"); + TEST(t_write_key_order(), "refs must be written in increasing order"); + TEST(t_write_multi_level_index(), "table with multi-level index"); + TEST(t_write_multiple_indices(), "table with indices for multiple block types"); + TEST(t_write_object_id_length(), "prefix compression on writing refs"); + TEST(t_write_object_id_min_length(), "prefix compression on writing refs"); + + return test_done(); } From patchwork Fri Aug 9 11:05:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chandra Pratap X-Patchwork-Id: 13758703 Received: from mail-oi1-f176.google.com (mail-oi1-f176.google.com [209.85.167.176]) (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 9FFDD193082 for ; Fri, 9 Aug 2024 11:13:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723202028; cv=none; b=FqA3C7EYd9glEs4D4RWnAyp52Yu55nJsUdrqJEnKakNJ6h1DHCH6z4Df1EdhsQ2e7nukLhwIn3TgNCDcrY+gpcAWTd3IMkygo49+7VUbycb8q7zAKJyIdPSntaTeBx/GgmatRKEPJMqgnFXaLaT+naFvyrxNxkMNGYSTG1QBYkY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723202028; c=relaxed/simple; bh=W7USWDooWkcEvUhlGe44fC9J7Hisd3kdMpQXKM6JAOM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gNtyoCtwvAMiKPYisooLRlt0J6DcD6XULd0/VJg1wUChJGfDaSrMXbS5HHM92jVKt+MLRhCa70ZTumMSQX9S2rYN8oF6MQLJNbT4KsB7trTAXY0KxMlqUWN6EGDmJZ3WFFV7GDGGRpRC5sNgoI94HFTRb8JXF9adZOH58OqH3Cg= 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=BMNdxC44; arc=none smtp.client-ip=209.85.167.176 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="BMNdxC44" Received: by mail-oi1-f176.google.com with SMTP id 5614622812f47-3db157d3bb9so1298825b6e.2 for ; Fri, 09 Aug 2024 04:13:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723202025; x=1723806825; 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=j7OgYFBtkBdCtX7syiausLF4DmD8poKJ5EHK6kAOs/8=; b=BMNdxC44FfoVQ/BNiFxZSTwYDKYBbpQASfsJUGMkJlS6230l7Pcc0oDMakl6En9Uq7 TKcERazvDTlIdKGiaNW+841IdN96krCiqYADhc7d9aWpzYkiVQnIaJVFrL0KESOFJ89/ vqm6vaKBSNyLyOF4C9iWnIIc24xzWwoJkPUOaTiwZXxJcZ200krx3FdNI2Pj7CjW+cSr Ar0bIgG5T74LneRwz970tmhe+xO5gxzFoQ+FU+vDt0ZTporZtPG9pImf7IFL9+DT2zZx X8lNLeju0hyublCky2yLHX9RUxSIUK6k5GjPaE9+CmfmS64cS1IR0vgD+7FDq2nZh8Xk s54A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723202025; x=1723806825; 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=j7OgYFBtkBdCtX7syiausLF4DmD8poKJ5EHK6kAOs/8=; b=BazBoaVDhnBPrlZuFmfTvM35c0wCvfhtTewqizrUP0/9YeczrCDK2nQ3QJqoSaDLVR tczHpmiDDg2Ynlm71VsXSpg0+T3ZmkZvegHPPNcGn5jgJWYAvTG+KJzzzgdPYsxd7dBI hjB5a7L86DINxSQLgA6c92zkm6DmheCxdgVigLUULklAwdptjNWfzv87TsZZQBr1DSdX 8WJ4z0ei+Pjw/9k5h0McayqvWZNmYab796KgHS/rqiP6NGTAemEA4k1w5p91mdzZrQqN BmY9g2kYENuPo+YvUkCGf9rmaSHgCY/F7vvO9v1x9J1UYrgCOfeLCYNxgsy1aAidomO9 oM+w== X-Gm-Message-State: AOJu0YxF/PtruWYOGzfjxsqWsRg3jZrpeI93o0HIlhTSiZlEHfTd0LV6 ExiSPLgwGqPDnqwPEjSYSnlvBvGyud4kfc3eNibODa1YUWtRaIc2us8oIe9x X-Google-Smtp-Source: AGHT+IES7AD4MyhBlpSeReJ0JXNLNqntmeSyFzIyKQ2th2IoUmoa4aeX+QDNB052WScyyqWMii5PMQ== X-Received: by 2002:a05:6808:3c91:b0:3dc:4223:898a with SMTP id 5614622812f47-3dc42238d0fmr1003440b6e.14.1723202025244; Fri, 09 Aug 2024 04:13:45 -0700 (PDT) Received: from Ubuntu.. ([117.99.60.199]) by smtp.googlemail.com with ESMTPSA id d2e1a72fcca58-710cb2e74d1sm2490377b3a.171.2024.08.09.04.13.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Aug 2024 04:13:44 -0700 (PDT) From: Chandra Pratap To: git@vger.kernel.org Cc: Chandra Pratap , Patrick Steinhardt , Christian Couder Subject: [PATCH v2 2/4] t-reftable-readwrite: use free_names() instead of a for loop Date: Fri, 9 Aug 2024 16:35:42 +0530 Message-ID: <20240809111312.4401-3-chandrapratap3519@gmail.com> X-Mailer: git-send-email 2.45.GIT In-Reply-To: <20240809111312.4401-1-chandrapratap3519@gmail.com> References: <20240807141608.4524-1-chandrapratap3519@gmail.com> <20240809111312.4401-1-chandrapratap3519@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 free_names() as defined by reftable/basics.{c,h} frees a NULL terminated array of malloced strings along with the array itself. Use this function instead of a for loop to free such an array. Mentored-by: Patrick Steinhardt Mentored-by: Christian Couder Signed-off-by: Chandra Pratap --- t/unit-tests/t-reftable-readwrite.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/t/unit-tests/t-reftable-readwrite.c b/t/unit-tests/t-reftable-readwrite.c index 235e3d94c7..e90f2bf9de 100644 --- a/t/unit-tests/t-reftable-readwrite.c +++ b/t/unit-tests/t-reftable-readwrite.c @@ -413,7 +413,6 @@ static void t_table_read_api(void) struct reftable_reader rd = { 0 }; struct reftable_block_source source = { 0 }; int err; - int i; struct reftable_log_record log = { 0 }; struct reftable_iterator it = { 0 }; @@ -432,10 +431,8 @@ static void t_table_read_api(void) check_int(err, ==, REFTABLE_API_ERROR); strbuf_release(&buf); - for (i = 0; i < N; i++) - reftable_free(names[i]); + free_names(names); reftable_iterator_destroy(&it); - reftable_free(names); reader_close(&rd); strbuf_release(&buf); } @@ -498,9 +495,7 @@ static void t_table_read_write_seek(int index, int hash_id) reftable_iterator_destroy(&it); strbuf_release(&buf); - for (i = 0; i < N; i++) - reftable_free(names[i]); - reftable_free(names); + free_names(names); reader_close(&rd); } From patchwork Fri Aug 9 11:05:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chandra Pratap X-Patchwork-Id: 13758704 Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.177]) (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 F0349192B85 for ; Fri, 9 Aug 2024 11:13:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723202031; cv=none; b=ru2L+dBQ+u6DAHAnMia4OOhogow7ubwmGCGaphHnpWLI9FvkFhYFXfkPNppsY7E8JDseXoUH2zAsBI0BxUxppUx5jCZe4D2/p5ifIRCozdySE116UJMclwDkTPFU+phRBIY/zDR1tXOTP7Q2Y6nInaauUeBLCfBnahABRT2UsHQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723202031; c=relaxed/simple; bh=rNjYv8v/nNvu5fkAterIaONp+7KgWiJuJ3zrzWZNg3E=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KvPDK5YqDVnUxhwyU8j29j1PD6G2VcIH54Tq03JvZ54UDWVsbiqaAch9tWGnYySD4Ojn2mbNPo5mkARepPQCS/CFbA2iWcJraE2N0jmeKUli24XaCwdFk6WDNTk2yH+FhyxL6gutrpgfxVFfbcbjNrGV2e3dh+VCFnX0uzLmh4k= 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=FBcU2CVp; arc=none smtp.client-ip=209.85.210.177 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="FBcU2CVp" Received: by mail-pf1-f177.google.com with SMTP id d2e1a72fcca58-7107b16be12so1614454b3a.3 for ; Fri, 09 Aug 2024 04:13:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723202028; x=1723806828; 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=dxcNKp4a5xaumj3da3OqlAmj+xm5gsA3osHbMT4TyUs=; b=FBcU2CVpBdGAHqDQT9MBVcuQgAWILGAUc2Geyp1h8iP2xbVFkBgYEX7cW5LtYD2Jwm n8MChD6HlUZ4zIfWbetK3mND4Ot7bWO6lUvyLUTwnNvtIPnkZXVXyEbUjAyOHt5bUrHJ vdmUe9ieemBypDFpPREKK5hSBoTUHSBIS7tWtbvHL30OccW5amXHQiX2yPSFg+buXsYl e5qYqXyZzAz8N+TeQw9Szl2FTRdykVc9sXm7Yd//FtKbq3XQUzAbZrABsAFm4umgw8+Z SjN7HiXM8mSKJZASkxrun0pmvJdaBYBU51DxHmuU84Ks4OFcYqM2Ba0Yor34LWhGlumC oVQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723202028; x=1723806828; 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=dxcNKp4a5xaumj3da3OqlAmj+xm5gsA3osHbMT4TyUs=; b=QY25yIcZvqLjCeb9pITfwpoVAihIi9UtRkLrjK9dXcmCKluQbZsap4mztspegk8A7I 0QZdvp8L23iIPaPiv4IbksziB4O0FCu9VpsZlSRP6jugvuvMC7UJdv40e0gFDBwrZ7WC QjfsKvuSVEK+eSA41f6e/0SeHLsYmpbWEtRBDWSnzgOiXBvc6LCsSMwbdxlvViuxHRZg UWek6dJQC7zU5IRsPpZOX46gfbnKEtjQ4BbM8F7nFspzKDyyhTxcARaoHSwbLyaaIFB7 tVHfQcQQiD+NoalCH6/ZXI6rcNMWLC+1pm3hJL9mYAs35XJbJn97u7+gqloC5b6jKXHK ou4g== X-Gm-Message-State: AOJu0YzsakuNT3E9sutpdqEtq30HvqlrNUuh93OOPE68nar58+TMtQZS zYGb4QxTI7Zq9g9y1mMeDEfGSDXfrw6bDjai/jRB4bvKakJzyqy5QkkO2Mpd X-Google-Smtp-Source: AGHT+IEjn5WNo9xSXPXZtVbuwOW+cY42FIaO4r4GiXwgiH8ZdqT2KDk3YD6isfFR30F+gplGazQCVA== X-Received: by 2002:a05:6a00:4603:b0:70b:a46:7db7 with SMTP id d2e1a72fcca58-710dc760456mr1097875b3a.16.1723202028216; Fri, 09 Aug 2024 04:13:48 -0700 (PDT) Received: from Ubuntu.. ([117.99.60.199]) by smtp.googlemail.com with ESMTPSA id d2e1a72fcca58-710cb2e74d1sm2490377b3a.171.2024.08.09.04.13.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Aug 2024 04:13:47 -0700 (PDT) From: Chandra Pratap To: git@vger.kernel.org Cc: Chandra Pratap , Patrick Steinhardt , Christian Couder Subject: [PATCH v2 3/4] t-reftable-readwrite: use 'for' in place of infinite 'while' loops Date: Fri, 9 Aug 2024 16:35:43 +0530 Message-ID: <20240809111312.4401-4-chandrapratap3519@gmail.com> X-Mailer: git-send-email 2.45.GIT In-Reply-To: <20240809111312.4401-1-chandrapratap3519@gmail.com> References: <20240807141608.4524-1-chandrapratap3519@gmail.com> <20240809111312.4401-1-chandrapratap3519@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Using a for loop with an empty conditional statement is more concise and easier to read than an infinite 'while' loop in instances where we need a loop variable. Hence, replace such instances of a 'while' loop with the equivalent 'for' loop. Mentored-by: Patrick Steinhardt Mentored-by: Christian Couder Signed-off-by: Chandra Pratap --- t/unit-tests/t-reftable-readwrite.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/t/unit-tests/t-reftable-readwrite.c b/t/unit-tests/t-reftable-readwrite.c index e90f2bf9de..7daf28ec6d 100644 --- a/t/unit-tests/t-reftable-readwrite.c +++ b/t/unit-tests/t-reftable-readwrite.c @@ -268,15 +268,13 @@ static void t_log_write_read(void) err = reftable_iterator_seek_log(&it, ""); check(!err); - i = 0; - while (1) { + for (i = 0; ; i++) { int err = reftable_iterator_next_log(&it, &log); if (err > 0) break; check(!err); check_str(names[i], log.refname); check_int(i, ==, log.update_index); - i++; reftable_log_record_release(&log); } @@ -374,7 +372,7 @@ static void t_table_read_write_sequential(void) err = reftable_iterator_seek_ref(&it, ""); check(!err); - while (1) { + for (j = 0; ; j++) { struct reftable_ref_record ref = { 0 }; int r = reftable_iterator_next_ref(&it, &ref); check_int(r, >=, 0); @@ -382,8 +380,6 @@ static void t_table_read_write_sequential(void) break; check_str(names[j], ref.refname); check_int(update_index, ==, ref.update_index); - - j++; reftable_ref_record_release(&ref); } check_int(j, ==, N); @@ -589,15 +585,13 @@ static void t_table_refs_for(int indexed) err = reftable_reader_refs_for(&rd, &it, want_hash); check(!err); - j = 0; - while (1) { + for (j = 0; ; j++) { int err = reftable_iterator_next_ref(&it, &ref); check_int(err, >=, 0); if (err > 0) break; check_int(j, <, want_names_len); check_str(ref.refname, want_names[j]); - j++; reftable_ref_record_release(&ref); } check_int(j, ==, want_names_len); From patchwork Fri Aug 9 11:05:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chandra Pratap X-Patchwork-Id: 13758705 Received: from mail-oi1-f176.google.com (mail-oi1-f176.google.com [209.85.167.176]) (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 9CA7E19309D for ; Fri, 9 Aug 2024 11:13:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723202034; cv=none; b=iodxT4T8BUgsKCeqCVtEpM67BRkjzE20BDB1rp9sNhNC7lkiQWCE4KqKNO5goUOMPusDJW3tC8m/nnLam+E9RKnVK7Wso0kX/Vq7qjoULv55imEslosRtQ/G/x1hv2Sq5sX3oMBkd2nNkMcXi7p3s8homWYnFPB3REe6aySi60k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723202034; c=relaxed/simple; bh=CS77GZaKmbqjoRwfTMFlhcsQ9bukzaXFxtqoDthO32k=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rcWv0bDxtXzIoiHVwv0mb0Lh/uYPivcLVLCZTo4baZHS2LGnVk3BwRzuY2jIBuKOuE4KmK9347tIoWWW/ybsUYEv5evhOTuk/RP+wePyz25hlIBuTSGOgMiU6HABW23mT+rDs3TaUCT5oiRKryt8MQKA5+3lB2jpQKBs9XAKQ9A= 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=nWtySYV3; arc=none smtp.client-ip=209.85.167.176 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="nWtySYV3" Received: by mail-oi1-f176.google.com with SMTP id 5614622812f47-3db1d0fca58so1286161b6e.3 for ; Fri, 09 Aug 2024 04:13:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723202031; x=1723806831; 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=ay7cmSxiwFmgG+lInLAEoiXWb4RJh7Hitfijj9jFTcw=; b=nWtySYV3kwHoTKzGN6c9r+PjTpbt8qsVtO47xe8w7cjYnBL7aEerzV+437OIZRIACk o1kSkQYY5XQXxwSdKbHTvdA6rG//ICUQPlwzcYzpeWiWsNLVIt/FKQSGeb9ZxTGTJz8B GplP/tdys+e320B9E0bk8vfND/61tGLPSwZJI/ulV7uAX2ZioJ1U0LwzHwMWCNSqA2Lj 8pvU1l5y6eHiNR9qwaoUofcQrOSuvFIGYAHQ6udvHXHsur0nzY91SMlKxSWcXcWCFQTp ILM0f1YPmY/1rWU5Pwz3yU9xC6PSnRO8mLtnCz9v9amgGb85xFOrv+CS/0PTMudzgJ81 DukQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723202031; x=1723806831; 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=ay7cmSxiwFmgG+lInLAEoiXWb4RJh7Hitfijj9jFTcw=; b=KjL32raJ2E3c1d1VLcoEN3cRmdzB4Uv9VvED047J3oRzp3cj6Ho4Uk5jgIZIrqZnsX qaRyLiT9I9M8QTftyTpqsk0j0/WApmlwvPvyF+ZMPFROIpMtyboxzGULeFkCPiKzWldb 5rU2of8gqhcl0ONbfffzThhJHbrCvcA7PIXvgtNIy3JCokuPT7pvZFYAPjkdy6w7YBXK 2VU47vFMbsdogXcoFmwVDhFf/cGWhVYs4hKCJXuBqW8McTCAoQavNWbV6pgsnUVreFo1 76RhXTFGwDdcad+11A2ut/0eZHZzbLESsnY/zSmKWQiT4mRgZDZMRNd5YRVjAV4sbfSF aPUA== X-Gm-Message-State: AOJu0YxlecUs1ufDvaqJAsTE8bBIxVRLUklm75IusyndGkKgdoc2Dq6v HHv7CbuOJyBEaiVvgtMLen7HKJzKEd3I6CbVXcZHzXozokNawcL1h/1Hff8t X-Google-Smtp-Source: AGHT+IGdt9g9PvdfNekkVgzK3TZ+ACzhZ8EzUt4APiAzr4iQLTQs30GE3vm1Y3bMdYEVzZ/yKmrTig== X-Received: by 2002:a05:6808:d46:b0:3d9:2e63:8330 with SMTP id 5614622812f47-3dc416ec079mr1173063b6e.43.1723202031185; Fri, 09 Aug 2024 04:13:51 -0700 (PDT) Received: from Ubuntu.. ([117.99.60.199]) by smtp.googlemail.com with ESMTPSA id d2e1a72fcca58-710cb2e74d1sm2490377b3a.171.2024.08.09.04.13.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Aug 2024 04:13:50 -0700 (PDT) From: Chandra Pratap To: git@vger.kernel.org Cc: Chandra Pratap , Patrick Steinhardt , Christian Couder Subject: [PATCH v2 4/4] t-reftable-readwrite: add test for known error Date: Fri, 9 Aug 2024 16:35:44 +0530 Message-ID: <20240809111312.4401-5-chandrapratap3519@gmail.com> X-Mailer: git-send-email 2.45.GIT In-Reply-To: <20240809111312.4401-1-chandrapratap3519@gmail.com> References: <20240807141608.4524-1-chandrapratap3519@gmail.com> <20240809111312.4401-1-chandrapratap3519@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 When using reftable_writer_add_ref() to add a ref record to a reftable writer, The update_index of the ref record must be within the limits set by reftable_writer_set_limits(), or REFTABLE_API_ERROR is returned. This scenario is currently left untested. Add a test case for the same. Mentored-by: Patrick Steinhardt Mentored-by: Christian Couder Signed-off-by: Chandra Pratap --- t/unit-tests/t-reftable-readwrite.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/t/unit-tests/t-reftable-readwrite.c b/t/unit-tests/t-reftable-readwrite.c index 7daf28ec6d..a5462441d3 100644 --- a/t/unit-tests/t-reftable-readwrite.c +++ b/t/unit-tests/t-reftable-readwrite.c @@ -773,6 +773,11 @@ static void t_write_key_order(void) check(!err); err = reftable_writer_add_ref(w, &refs[1]); check_int(err, ==, REFTABLE_API_ERROR); + + refs[0].update_index = 2; + err = reftable_writer_add_ref(w, &refs[0]); + check_int(err, ==, REFTABLE_API_ERROR); + reftable_writer_close(w); reftable_writer_free(w); strbuf_release(&buf);