Merge branch 'cp/reftable-unit-test'

Basic unit tests for reftable have been reimplemented under the
unit test framework.

* cp/reftable-unit-test:
  t: improve the test-case for parse_names()
  t: add test for put_be16()
  t: move tests from reftable/record_test.c to the new unit test
  t: move tests from reftable/stack_test.c to the new unit test
  t: move reftable/basics_test.c to the unit testing framework
This commit is contained in:
Junio C Hamano 2024-06-12 13:37:14 -07:00
commit 56346ba24e
6 changed files with 161 additions and 169 deletions

View file

@ -1336,6 +1336,7 @@ THIRD_PARTY_SOURCES += sha1dc/%
UNIT_TEST_PROGRAMS += t-ctype
UNIT_TEST_PROGRAMS += t-mem-pool
UNIT_TEST_PROGRAMS += t-prio-queue
UNIT_TEST_PROGRAMS += t-reftable-basics
UNIT_TEST_PROGRAMS += t-strbuf
UNIT_TEST_PROGRAMS += t-strcmp-offset
UNIT_TEST_PROGRAMS += t-strvec
@ -2672,7 +2673,6 @@ REFTABLE_OBJS += reftable/stack.o
REFTABLE_OBJS += reftable/tree.o
REFTABLE_OBJS += reftable/writer.o
REFTABLE_TEST_OBJS += reftable/basics_test.o
REFTABLE_TEST_OBJS += reftable/block_test.o
REFTABLE_TEST_OBJS += reftable/dump.o
REFTABLE_TEST_OBJS += reftable/merged_test.o

View file

@ -1,105 +0,0 @@
/*
Copyright 2020 Google LLC
Use of this source code is governed by a BSD-style
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 "test_framework.h"
#include "reftable-tests.h"
struct integer_needle_lesseq_args {
int needle;
int *haystack;
};
static int integer_needle_lesseq(size_t i, void *_args)
{
struct integer_needle_lesseq_args *args = _args;
return args->needle <= args->haystack[i];
}
static void test_binsearch(void)
{
int haystack[] = { 2, 4, 6, 8, 10 };
struct {
int needle;
size_t expected_idx;
} testcases[] = {
{-9000, 0},
{-1, 0},
{0, 0},
{2, 0},
{3, 1},
{4, 1},
{7, 3},
{9, 4},
{10, 4},
{11, 5},
{9000, 5},
};
size_t i = 0;
for (i = 0; i < ARRAY_SIZE(testcases); i++) {
struct integer_needle_lesseq_args args = {
.haystack = haystack,
.needle = testcases[i].needle,
};
size_t idx;
idx = binsearch(ARRAY_SIZE(haystack), &integer_needle_lesseq, &args);
EXPECT(idx == testcases[i].expected_idx);
}
}
static void test_names_length(void)
{
char *a[] = { "a", "b", NULL };
EXPECT(names_length(a) == 2);
}
static void test_parse_names_normal(void)
{
char in[] = "a\nb\n";
char **out = NULL;
parse_names(in, strlen(in), &out);
EXPECT(!strcmp(out[0], "a"));
EXPECT(!strcmp(out[1], "b"));
EXPECT(!out[2]);
free_names(out);
}
static void test_parse_names_drop_empty(void)
{
char in[] = "a\n\n";
char **out = NULL;
parse_names(in, strlen(in), &out);
EXPECT(!strcmp(out[0], "a"));
EXPECT(!out[1]);
free_names(out);
}
static void test_common_prefix(void)
{
struct strbuf s1 = STRBUF_INIT;
struct strbuf s2 = STRBUF_INIT;
strbuf_addstr(&s1, "abcdef");
strbuf_addstr(&s2, "abc");
EXPECT(common_prefix_size(&s1, &s2) == 3);
strbuf_release(&s1);
strbuf_release(&s2);
}
int basics_test_main(int argc, const char *argv[])
{
RUN_TEST(test_common_prefix);
RUN_TEST(test_parse_names_normal);
RUN_TEST(test_parse_names_drop_empty);
RUN_TEST(test_binsearch);
RUN_TEST(test_names_length);
return 0;
}

View file

@ -64,31 +64,6 @@ static void test_varint_roundtrip(void)
}
}
static void test_common_prefix(void)
{
struct {
const char *a, *b;
int want;
} cases[] = {
{ "abc", "ab", 2 },
{ "", "abc", 0 },
{ "abc", "abd", 2 },
{ "abc", "pqr", 0 },
};
int i = 0;
for (i = 0; i < ARRAY_SIZE(cases); i++) {
struct strbuf a = STRBUF_INIT;
struct strbuf b = STRBUF_INIT;
strbuf_addstr(&a, cases[i].a);
strbuf_addstr(&b, cases[i].b);
EXPECT(common_prefix_size(&a, &b) == cases[i].want);
strbuf_release(&a);
strbuf_release(&b);
}
}
static void set_hash(uint8_t *h, int j)
{
int i = 0;
@ -258,16 +233,6 @@ static void test_reftable_log_record_roundtrip(void)
strbuf_release(&scratch);
}
static void test_u24_roundtrip(void)
{
uint32_t in = 0x112233;
uint8_t dest[3];
uint32_t out;
put_be24(dest, in);
out = get_be24(dest);
EXPECT(in == out);
}
static void test_key_roundtrip(void)
{
uint8_t buffer[1024] = { 0 };
@ -411,9 +376,7 @@ int record_test_main(int argc, const char *argv[])
RUN_TEST(test_reftable_ref_record_roundtrip);
RUN_TEST(test_varint_roundtrip);
RUN_TEST(test_key_roundtrip);
RUN_TEST(test_common_prefix);
RUN_TEST(test_reftable_obj_record_roundtrip);
RUN_TEST(test_reftable_index_record_roundtrip);
RUN_TEST(test_u24_roundtrip);
return 0;
}

View file

@ -102,29 +102,6 @@ static void test_read_file(void)
(void) remove(fn);
}
static void test_parse_names(void)
{
char buf[] = "line\n";
char **names = NULL;
parse_names(buf, strlen(buf), &names);
EXPECT(NULL != names[0]);
EXPECT(0 == strcmp(names[0], "line"));
EXPECT(NULL == names[1]);
free_names(names);
}
static void test_names_equal(void)
{
char *a[] = { "a", "b", "c", NULL };
char *b[] = { "a", "b", "d", NULL };
char *c[] = { "a", "b", NULL };
EXPECT(names_equal(a, a));
EXPECT(!names_equal(a, b));
EXPECT(!names_equal(a, c));
}
static int write_test_ref(struct reftable_writer *wr, void *arg)
{
struct reftable_ref_record *ref = arg;
@ -1034,8 +1011,6 @@ static void test_reftable_stack_compaction_concurrent_clean(void)
int stack_test_main(int argc, const char *argv[])
{
RUN_TEST(test_empty_add);
RUN_TEST(test_names_equal);
RUN_TEST(test_parse_names);
RUN_TEST(test_read_file);
RUN_TEST(test_reflog_expire);
RUN_TEST(test_reftable_stack_add);

View file

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

View file

@ -0,0 +1,160 @@
/*
Copyright 2020 Google LLC
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file or at
https://developers.google.com/open-source/licenses/bsd
*/
#include "test-lib.h"
#include "reftable/basics.h"
struct integer_needle_lesseq_args {
int needle;
int *haystack;
};
static int integer_needle_lesseq(size_t i, void *_args)
{
struct integer_needle_lesseq_args *args = _args;
return args->needle <= args->haystack[i];
}
static void test_binsearch(void)
{
int haystack[] = { 2, 4, 6, 8, 10 };
struct {
int needle;
size_t expected_idx;
} testcases[] = {
{-9000, 0},
{-1, 0},
{0, 0},
{2, 0},
{3, 1},
{4, 1},
{7, 3},
{9, 4},
{10, 4},
{11, 5},
{9000, 5},
};
for (size_t i = 0; i < ARRAY_SIZE(testcases); i++) {
struct integer_needle_lesseq_args args = {
.haystack = haystack,
.needle = testcases[i].needle,
};
size_t idx;
idx = binsearch(ARRAY_SIZE(haystack), &integer_needle_lesseq, &args);
check_int(idx, ==, testcases[i].expected_idx);
}
}
static void test_names_length(void)
{
char *a[] = { "a", "b", NULL };
check_int(names_length(a), ==, 2);
}
static void test_names_equal(void)
{
char *a[] = { "a", "b", "c", NULL };
char *b[] = { "a", "b", "d", NULL };
char *c[] = { "a", "b", NULL };
check(names_equal(a, a));
check(!names_equal(a, b));
check(!names_equal(a, c));
}
static void test_parse_names_normal(void)
{
char in1[] = "line\n";
char in2[] = "a\nb\nc";
char **out = NULL;
parse_names(in1, strlen(in1), &out);
check_str(out[0], "line");
check(!out[1]);
free_names(out);
parse_names(in2, strlen(in2), &out);
check_str(out[0], "a");
check_str(out[1], "b");
check_str(out[2], "c");
check(!out[3]);
free_names(out);
}
static void test_parse_names_drop_empty(void)
{
char in[] = "a\n\nb\n";
char **out = NULL;
parse_names(in, strlen(in), &out);
check_str(out[0], "a");
/* simply '\n' should be dropped as empty string */
check_str(out[1], "b");
check(!out[2]);
free_names(out);
}
static void test_common_prefix(void)
{
struct strbuf a = STRBUF_INIT;
struct strbuf b = STRBUF_INIT;
struct {
const char *a, *b;
int want;
} cases[] = {
{"abcdef", "abc", 3},
{ "abc", "ab", 2 },
{ "", "abc", 0 },
{ "abc", "abd", 2 },
{ "abc", "pqr", 0 },
};
for (size_t i = 0; i < ARRAY_SIZE(cases); i++) {
strbuf_addstr(&a, cases[i].a);
strbuf_addstr(&b, cases[i].b);
check_int(common_prefix_size(&a, &b), ==, cases[i].want);
strbuf_reset(&a);
strbuf_reset(&b);
}
strbuf_release(&a);
strbuf_release(&b);
}
static void test_u24_roundtrip(void)
{
uint32_t in = 0x112233;
uint8_t dest[3];
uint32_t out;
put_be24(dest, in);
out = get_be24(dest);
check_int(in, ==, out);
}
static void test_u16_roundtrip(void)
{
uint32_t in = 0xfef1;
uint8_t dest[3];
uint32_t out;
put_be16(dest, in);
out = get_be16(dest);
check_int(in, ==, out);
}
int cmd_main(int argc, const char *argv[])
{
TEST(test_common_prefix(), "common_prefix_size works");
TEST(test_parse_names_normal(), "parse_names works for basic input");
TEST(test_parse_names_drop_empty(), "parse_names drops empty string");
TEST(test_binsearch(), "binary search with binsearch works");
TEST(test_names_length(), "names_length retuns size of a NULL-terminated string array");
TEST(test_names_equal(), "names_equal compares NULL-terminated string arrays");
TEST(test_u24_roundtrip(), "put_be24 and get_be24 work");
TEST(test_u16_roundtrip(), "put_be16 and get_be16 work");
return test_done();
}