t: move reftable/record_test.c to the unit testing framework

reftable/record_test.c exercises the functions defined in
reftable/record.{c, h}. Migrate reftable/record_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 fit unit-tests' naming scheme.

While at it, change the type of index variable 'i' to 'size_t'
from 'int'. This is because 'i' is used in comparison against
'ARRAY_SIZE(x)' which is of type 'size_t'.

Also, use set_hash() which is defined locally in the test file
instead of set_test_hash() which is defined by
reftable/test_framework.{c, h}. This is fine to do as both these
functions are similarly implemented, and
reftable/test_framework.{c, h} is not #included in the ported test.

Get rid of reftable_record_print() from the tests as well, because
it clutters the test framework's output and we have no way of
verifying the output.

Mentored-by: Patrick Steinhardt <ps@pks.im>
Mentored-by: Christian Couder <chriscool@tuxfamily.org>
Signed-off-by: Chandra Pratap <chandrapratap3519@gmail.com>
Acked-by: Karthik Nayak <karthik.188@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
maint
Chandra Pratap 2024-07-02 12:52:14 +05:30 committed by Junio C Hamano
parent d63586cb31
commit ba9661b457
3 changed files with 61 additions and 73 deletions

View File

@ -1338,6 +1338,7 @@ UNIT_TEST_PROGRAMS += t-hash
UNIT_TEST_PROGRAMS += t-mem-pool UNIT_TEST_PROGRAMS += t-mem-pool
UNIT_TEST_PROGRAMS += t-prio-queue UNIT_TEST_PROGRAMS += t-prio-queue
UNIT_TEST_PROGRAMS += t-reftable-basics UNIT_TEST_PROGRAMS += t-reftable-basics
UNIT_TEST_PROGRAMS += t-reftable-record
UNIT_TEST_PROGRAMS += t-strbuf UNIT_TEST_PROGRAMS += t-strbuf
UNIT_TEST_PROGRAMS += t-strcmp-offset UNIT_TEST_PROGRAMS += t-strcmp-offset
UNIT_TEST_PROGRAMS += t-strvec UNIT_TEST_PROGRAMS += t-strvec
@ -2678,7 +2679,6 @@ REFTABLE_TEST_OBJS += reftable/block_test.o
REFTABLE_TEST_OBJS += reftable/dump.o REFTABLE_TEST_OBJS += reftable/dump.o
REFTABLE_TEST_OBJS += reftable/merged_test.o REFTABLE_TEST_OBJS += reftable/merged_test.o
REFTABLE_TEST_OBJS += reftable/pq_test.o REFTABLE_TEST_OBJS += reftable/pq_test.o
REFTABLE_TEST_OBJS += reftable/record_test.o
REFTABLE_TEST_OBJS += reftable/readwrite_test.o REFTABLE_TEST_OBJS += reftable/readwrite_test.o
REFTABLE_TEST_OBJS += reftable/stack_test.o REFTABLE_TEST_OBJS += reftable/stack_test.o
REFTABLE_TEST_OBJS += reftable/test_framework.o REFTABLE_TEST_OBJS += reftable/test_framework.o

View File

@ -5,7 +5,6 @@
int cmd__reftable(int argc, const char **argv) int cmd__reftable(int argc, const char **argv)
{ {
/* test from simple to complex. */ /* test from simple to complex. */
record_test_main(argc, argv);
block_test_main(argc, argv); block_test_main(argc, argv);
tree_test_main(argc, argv); tree_test_main(argc, argv);
pq_test_main(argc, argv); pq_test_main(argc, argv);

View File

@ -6,15 +6,11 @@
https://developers.google.com/open-source/licenses/bsd https://developers.google.com/open-source/licenses/bsd
*/ */


#include "record.h" #include "test-lib.h"
#include "reftable/constants.h"
#include "reftable/record.h"


#include "system.h" static void t_copy(struct reftable_record *rec)
#include "basics.h"
#include "constants.h"
#include "test_framework.h"
#include "reftable-tests.h"

static void test_copy(struct reftable_record *rec)
{ {
struct reftable_record copy; struct reftable_record copy;
uint8_t typ; uint8_t typ;
@ -24,15 +20,12 @@ static void test_copy(struct reftable_record *rec)
reftable_record_copy_from(&copy, rec, GIT_SHA1_RAWSZ); reftable_record_copy_from(&copy, rec, GIT_SHA1_RAWSZ);
/* do it twice to catch memory leaks */ /* do it twice to catch memory leaks */
reftable_record_copy_from(&copy, rec, GIT_SHA1_RAWSZ); reftable_record_copy_from(&copy, rec, GIT_SHA1_RAWSZ);
EXPECT(reftable_record_equal(rec, &copy, GIT_SHA1_RAWSZ)); check(reftable_record_equal(rec, &copy, GIT_SHA1_RAWSZ));

puts("testing print coverage:\n");
reftable_record_print(&copy, GIT_SHA1_RAWSZ);


reftable_record_release(&copy); reftable_record_release(&copy);
} }


static void test_varint_roundtrip(void) static void t_varint_roundtrip(void)
{ {
uint64_t inputs[] = { 0, uint64_t inputs[] = { 0,
1, 1,
@ -43,8 +36,8 @@ static void test_varint_roundtrip(void)
4096, 4096,
((uint64_t)1 << 63), ((uint64_t)1 << 63),
((uint64_t)1 << 63) + ((uint64_t)1 << 63) - 1 }; ((uint64_t)1 << 63) + ((uint64_t)1 << 63) - 1 };
int i = 0;
for (i = 0; i < ARRAY_SIZE(inputs); i++) { for (size_t i = 0; i < ARRAY_SIZE(inputs); i++) {
uint8_t dest[10]; uint8_t dest[10];


struct string_view out = { struct string_view out = {
@ -55,29 +48,26 @@ static void test_varint_roundtrip(void)
int n = put_var_int(&out, in); int n = put_var_int(&out, in);
uint64_t got = 0; uint64_t got = 0;


EXPECT(n > 0); check_int(n, >, 0);
out.len = n; out.len = n;
n = get_var_int(&got, &out); n = get_var_int(&got, &out);
EXPECT(n > 0); check_int(n, >, 0);


EXPECT(got == in); check_int(got, ==, in);
} }
} }


static void set_hash(uint8_t *h, int j) static void set_hash(uint8_t *h, int j)
{ {
int i = 0; for (int i = 0; i < hash_size(GIT_SHA1_FORMAT_ID); i++)
for (i = 0; i < hash_size(GIT_SHA1_FORMAT_ID); i++) {
h[i] = (j >> i) & 0xff; h[i] = (j >> i) & 0xff;
}
} }


static void test_reftable_ref_record_roundtrip(void) static void t_reftable_ref_record_roundtrip(void)
{ {
struct strbuf scratch = STRBUF_INIT; struct strbuf scratch = STRBUF_INIT;
int i = 0;


for (i = REFTABLE_REF_DELETION; i < REFTABLE_NR_REF_VALUETYPES; i++) { for (int i = REFTABLE_REF_DELETION; i < REFTABLE_NR_REF_VALUETYPES; i++) {
struct reftable_record in = { struct reftable_record in = {
.type = BLOCK_TYPE_REF, .type = BLOCK_TYPE_REF,
}; };
@ -107,19 +97,19 @@ static void test_reftable_ref_record_roundtrip(void)
} }
in.u.ref.refname = xstrdup("refs/heads/master"); in.u.ref.refname = xstrdup("refs/heads/master");


test_copy(&in); t_copy(&in);


EXPECT(reftable_record_val_type(&in) == i); check_int(reftable_record_val_type(&in), ==, i);


reftable_record_key(&in, &key); reftable_record_key(&in, &key);
n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ); n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ);
EXPECT(n > 0); check_int(n, >, 0);


/* decode into a non-zero reftable_record to test for leaks. */ /* decode into a non-zero reftable_record to test for leaks. */
m = reftable_record_decode(&out, key, i, dest, GIT_SHA1_RAWSZ, &scratch); m = reftable_record_decode(&out, key, i, dest, GIT_SHA1_RAWSZ, &scratch);
EXPECT(n == m); check_int(n, ==, m);


EXPECT(reftable_ref_record_equal(&in.u.ref, &out.u.ref, check(reftable_ref_record_equal(&in.u.ref, &out.u.ref,
GIT_SHA1_RAWSZ)); GIT_SHA1_RAWSZ));
reftable_record_release(&in); reftable_record_release(&in);


@ -130,7 +120,7 @@ static void test_reftable_ref_record_roundtrip(void)
strbuf_release(&scratch); strbuf_release(&scratch);
} }


static void test_reftable_log_record_equal(void) static void t_reftable_log_record_equal(void)
{ {
struct reftable_log_record in[2] = { struct reftable_log_record in[2] = {
{ {
@ -143,16 +133,15 @@ static void test_reftable_log_record_equal(void)
} }
}; };


EXPECT(!reftable_log_record_equal(&in[0], &in[1], GIT_SHA1_RAWSZ)); check(!reftable_log_record_equal(&in[0], &in[1], GIT_SHA1_RAWSZ));
in[1].update_index = in[0].update_index; in[1].update_index = in[0].update_index;
EXPECT(reftable_log_record_equal(&in[0], &in[1], GIT_SHA1_RAWSZ)); check(reftable_log_record_equal(&in[0], &in[1], GIT_SHA1_RAWSZ));
reftable_log_record_release(&in[0]); reftable_log_record_release(&in[0]);
reftable_log_record_release(&in[1]); reftable_log_record_release(&in[1]);
} }


static void test_reftable_log_record_roundtrip(void) static void t_reftable_log_record_roundtrip(void)
{ {
int i;
struct reftable_log_record in[] = { struct reftable_log_record in[] = {
{ {
.refname = xstrdup("refs/heads/master"), .refname = xstrdup("refs/heads/master"),
@ -180,12 +169,12 @@ static void test_reftable_log_record_roundtrip(void)
} }
}; };
struct strbuf scratch = STRBUF_INIT; struct strbuf scratch = STRBUF_INIT;
set_hash(in[0].value.update.new_hash, 1);
set_hash(in[0].value.update.old_hash, 2);
set_hash(in[2].value.update.new_hash, 3);
set_hash(in[2].value.update.old_hash, 4);


set_test_hash(in[0].value.update.new_hash, 1); for (size_t i = 0; i < ARRAY_SIZE(in); i++) {
set_test_hash(in[0].value.update.old_hash, 2);
set_test_hash(in[2].value.update.new_hash, 3);
set_test_hash(in[2].value.update.old_hash, 4);
for (i = 0; i < ARRAY_SIZE(in); i++) {
struct reftable_record rec = { .type = BLOCK_TYPE_LOG }; struct reftable_record rec = { .type = BLOCK_TYPE_LOG };
struct strbuf key = STRBUF_INIT; struct strbuf key = STRBUF_INIT;
uint8_t buffer[1024] = { 0 }; uint8_t buffer[1024] = { 0 };
@ -212,18 +201,18 @@ static void test_reftable_log_record_roundtrip(void)


rec.u.log = in[i]; rec.u.log = in[i];


test_copy(&rec); t_copy(&rec);


reftable_record_key(&rec, &key); reftable_record_key(&rec, &key);


n = reftable_record_encode(&rec, dest, GIT_SHA1_RAWSZ); n = reftable_record_encode(&rec, dest, GIT_SHA1_RAWSZ);
EXPECT(n >= 0); check_int(n, >=, 0);
valtype = reftable_record_val_type(&rec); valtype = reftable_record_val_type(&rec);
m = reftable_record_decode(&out, key, valtype, dest, m = reftable_record_decode(&out, key, valtype, dest,
GIT_SHA1_RAWSZ, &scratch); GIT_SHA1_RAWSZ, &scratch);
EXPECT(n == m); check_int(n, ==, m);


EXPECT(reftable_log_record_equal(&in[i], &out.u.log, check(reftable_log_record_equal(&in[i], &out.u.log,
GIT_SHA1_RAWSZ)); GIT_SHA1_RAWSZ));
reftable_log_record_release(&in[i]); reftable_log_record_release(&in[i]);
strbuf_release(&key); strbuf_release(&key);
@ -233,7 +222,7 @@ static void test_reftable_log_record_roundtrip(void)
strbuf_release(&scratch); strbuf_release(&scratch);
} }


static void test_key_roundtrip(void) static void t_key_roundtrip(void)
{ {
uint8_t buffer[1024] = { 0 }; uint8_t buffer[1024] = { 0 };
struct string_view dest = { struct string_view dest = {
@ -252,21 +241,21 @@ static void test_key_roundtrip(void)
strbuf_addstr(&key, "refs/tags/bla"); strbuf_addstr(&key, "refs/tags/bla");
extra = 6; extra = 6;
n = reftable_encode_key(&restart, dest, last_key, key, extra); n = reftable_encode_key(&restart, dest, last_key, key, extra);
EXPECT(!restart); check(!restart);
EXPECT(n > 0); check_int(n, >, 0);


strbuf_addstr(&roundtrip, "refs/heads/master"); strbuf_addstr(&roundtrip, "refs/heads/master");
m = reftable_decode_key(&roundtrip, &rt_extra, dest); m = reftable_decode_key(&roundtrip, &rt_extra, dest);
EXPECT(n == m); check_int(n, ==, m);
EXPECT(0 == strbuf_cmp(&key, &roundtrip)); check(!strbuf_cmp(&key, &roundtrip));
EXPECT(rt_extra == extra); check_int(rt_extra, ==, extra);


strbuf_release(&last_key); strbuf_release(&last_key);
strbuf_release(&key); strbuf_release(&key);
strbuf_release(&roundtrip); strbuf_release(&roundtrip);
} }


static void test_reftable_obj_record_roundtrip(void) static void t_reftable_obj_record_roundtrip(void)
{ {
uint8_t testHash1[GIT_SHA1_RAWSZ] = { 1, 2, 3, 4, 0 }; uint8_t testHash1[GIT_SHA1_RAWSZ] = { 1, 2, 3, 4, 0 };
uint64_t till9[] = { 1, 2, 3, 4, 500, 600, 700, 800, 9000 }; uint64_t till9[] = { 1, 2, 3, 4, 500, 600, 700, 800, 9000 };
@ -289,9 +278,8 @@ static void test_reftable_obj_record_roundtrip(void)
}, },
}; };
struct strbuf scratch = STRBUF_INIT; struct strbuf scratch = STRBUF_INIT;
int i = 0;


for (i = 0; i < ARRAY_SIZE(recs); i++) { for (size_t i = 0; i < ARRAY_SIZE(recs); i++) {
uint8_t buffer[1024] = { 0 }; uint8_t buffer[1024] = { 0 };
struct string_view dest = { struct string_view dest = {
.buf = buffer, .buf = buffer,
@ -308,16 +296,16 @@ static void test_reftable_obj_record_roundtrip(void)
int n, m; int n, m;
uint8_t extra; uint8_t extra;


test_copy(&in); t_copy(&in);
reftable_record_key(&in, &key); reftable_record_key(&in, &key);
n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ); n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ);
EXPECT(n > 0); check_int(n, >, 0);
extra = reftable_record_val_type(&in); extra = reftable_record_val_type(&in);
m = reftable_record_decode(&out, key, extra, dest, m = reftable_record_decode(&out, key, extra, dest,
GIT_SHA1_RAWSZ, &scratch); GIT_SHA1_RAWSZ, &scratch);
EXPECT(n == m); check_int(n, ==, m);


EXPECT(reftable_record_equal(&in, &out, GIT_SHA1_RAWSZ)); check(reftable_record_equal(&in, &out, GIT_SHA1_RAWSZ));
strbuf_release(&key); strbuf_release(&key);
reftable_record_release(&out); reftable_record_release(&out);
} }
@ -325,7 +313,7 @@ static void test_reftable_obj_record_roundtrip(void)
strbuf_release(&scratch); strbuf_release(&scratch);
} }


static void test_reftable_index_record_roundtrip(void) static void t_reftable_index_record_roundtrip(void)
{ {
struct reftable_record in = { struct reftable_record in = {
.type = BLOCK_TYPE_INDEX, .type = BLOCK_TYPE_INDEX,
@ -350,18 +338,18 @@ static void test_reftable_index_record_roundtrip(void)


strbuf_addstr(&in.u.idx.last_key, "refs/heads/master"); strbuf_addstr(&in.u.idx.last_key, "refs/heads/master");
reftable_record_key(&in, &key); reftable_record_key(&in, &key);
test_copy(&in); t_copy(&in);


EXPECT(0 == strbuf_cmp(&key, &in.u.idx.last_key)); check(!strbuf_cmp(&key, &in.u.idx.last_key));
n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ); n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ);
EXPECT(n > 0); check_int(n, >, 0);


extra = reftable_record_val_type(&in); extra = reftable_record_val_type(&in);
m = reftable_record_decode(&out, key, extra, dest, GIT_SHA1_RAWSZ, m = reftable_record_decode(&out, key, extra, dest, GIT_SHA1_RAWSZ,
&scratch); &scratch);
EXPECT(m == n); check_int(m, ==, n);


EXPECT(reftable_record_equal(&in, &out, GIT_SHA1_RAWSZ)); check(reftable_record_equal(&in, &out, GIT_SHA1_RAWSZ));


reftable_record_release(&out); reftable_record_release(&out);
strbuf_release(&key); strbuf_release(&key);
@ -369,14 +357,15 @@ static void test_reftable_index_record_roundtrip(void)
strbuf_release(&in.u.idx.last_key); strbuf_release(&in.u.idx.last_key);
} }


int record_test_main(int argc, const char *argv[]) int cmd_main(int argc, const char *argv[])
{ {
RUN_TEST(test_reftable_log_record_equal); TEST(t_reftable_log_record_equal(), "reftable_log_record_equal works");
RUN_TEST(test_reftable_log_record_roundtrip); TEST(t_reftable_log_record_roundtrip(), "record operations work on log record");
RUN_TEST(test_reftable_ref_record_roundtrip); TEST(t_reftable_ref_record_roundtrip(), "record operations work on ref record");
RUN_TEST(test_varint_roundtrip); TEST(t_varint_roundtrip(), "put_var_int and get_var_int work");
RUN_TEST(test_key_roundtrip); TEST(t_key_roundtrip(), "reftable_encode_key and reftable_decode_key work");
RUN_TEST(test_reftable_obj_record_roundtrip); TEST(t_reftable_obj_record_roundtrip(), "record operations work on obj record");
RUN_TEST(test_reftable_index_record_roundtrip); TEST(t_reftable_index_record_roundtrip(), "record operations work on index record");
return 0;
return test_done();
} }