Merge branch 'sk/reftable-clarify-tests'
The reftable unit tests are now ported to the "clar" unit testing framework. * sk/reftable-clarify-tests: t/unit-tests: finalize migration of reftable-related tests t/unit-tests: convert reftable stack test to use clar t/unit-tests: convert reftable record test to use clar t/unit-tests: convert reftable readwrite test to use clar t/unit-tests: convert reftable table test to use clar t/unit-tests: convert reftable pq test to use clar t/unit-tests: convert reftable merged test to use clar t/unit-tests: convert reftable block test to use clar t/unit-tests: convert reftable basics test to use clar test framework t/unit-tests: implement clar specific reftable test helper functionsmaint
commit
80b80162fd
20
Makefile
20
Makefile
|
|
@ -1362,6 +1362,13 @@ CLAR_TEST_SUITES += u-oid-array
|
|||
CLAR_TEST_SUITES += u-oidmap
|
||||
CLAR_TEST_SUITES += u-oidtree
|
||||
CLAR_TEST_SUITES += u-prio-queue
|
||||
CLAR_TEST_SUITES += u-reftable-basics
|
||||
CLAR_TEST_SUITES += u-reftable-block
|
||||
CLAR_TEST_SUITES += u-reftable-merged
|
||||
CLAR_TEST_SUITES += u-reftable-pq
|
||||
CLAR_TEST_SUITES += u-reftable-readwrite
|
||||
CLAR_TEST_SUITES += u-reftable-stack
|
||||
CLAR_TEST_SUITES += u-reftable-table
|
||||
CLAR_TEST_SUITES += u-reftable-tree
|
||||
CLAR_TEST_SUITES += u-strbuf
|
||||
CLAR_TEST_SUITES += u-strcmp-offset
|
||||
|
|
@ -1372,20 +1379,11 @@ CLAR_TEST_SUITES += u-urlmatch-normalization
|
|||
CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X)
|
||||
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
|
||||
CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/lib-oid.o
|
||||
CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/lib-reftable.o
|
||||
CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/unit-test.o
|
||||
|
||||
UNIT_TEST_PROGRAMS += t-reftable-basics
|
||||
UNIT_TEST_PROGRAMS += t-reftable-block
|
||||
UNIT_TEST_PROGRAMS += t-reftable-merged
|
||||
UNIT_TEST_PROGRAMS += t-reftable-pq
|
||||
UNIT_TEST_PROGRAMS += t-reftable-readwrite
|
||||
UNIT_TEST_PROGRAMS += t-reftable-record
|
||||
UNIT_TEST_PROGRAMS += t-reftable-stack
|
||||
UNIT_TEST_PROGRAMS += t-reftable-table
|
||||
UNIT_TEST_PROGS = $(patsubst %,$(UNIT_TEST_BIN)/%$X,$(UNIT_TEST_PROGRAMS))
|
||||
UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/test-lib.o
|
||||
UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/lib-reftable.o
|
||||
|
||||
# xdiff and reftable libs may in turn depend on what is in libgit.a
|
||||
GITLIBS = common-main.o $(LIB_FILE) $(XDIFF_LIB) $(REFTABLE_LIB) $(LIB_FILE)
|
||||
|
|
|
|||
|
|
@ -125,7 +125,6 @@ check-meson:
|
|||
@mkdir -p mesontmp && \
|
||||
printf "%s\n" \
|
||||
"integration_tests t[0-9][0-9][0-9][0-9]-*.sh" \
|
||||
"unit_test_programs unit-tests/t-*.c" \
|
||||
"clar_test_suites unit-tests/u-*.c" | \
|
||||
while read -r variable pattern; do \
|
||||
awk "/^$$variable = \[\$$/ {flag=1 ; next } /^]$$/ { flag=0 } flag { gsub(/^ \047/, \"\"); gsub(/\047,\$$/, \"\"); print }" meson.build >mesontmp/meson.txt && \
|
||||
|
|
|
|||
|
|
@ -8,6 +8,14 @@ clar_test_suites = [
|
|||
'unit-tests/u-oidmap.c',
|
||||
'unit-tests/u-oidtree.c',
|
||||
'unit-tests/u-prio-queue.c',
|
||||
'unit-tests/u-reftable-basics.c',
|
||||
'unit-tests/u-reftable-block.c',
|
||||
'unit-tests/u-reftable-merged.c',
|
||||
'unit-tests/u-reftable-pq.c',
|
||||
'unit-tests/u-reftable-readwrite.c',
|
||||
'unit-tests/u-reftable-record.c',
|
||||
'unit-tests/u-reftable-stack.c',
|
||||
'unit-tests/u-reftable-table.c',
|
||||
'unit-tests/u-reftable-tree.c',
|
||||
'unit-tests/u-strbuf.c',
|
||||
'unit-tests/u-strcmp-offset.c',
|
||||
|
|
@ -20,7 +28,8 @@ clar_test_suites = [
|
|||
clar_sources = [
|
||||
'unit-tests/clar/clar.c',
|
||||
'unit-tests/unit-test.c',
|
||||
'unit-tests/lib-oid.c'
|
||||
'unit-tests/lib-oid.c',
|
||||
'unit-tests/lib-reftable.c'
|
||||
]
|
||||
|
||||
clar_decls_h = custom_target(
|
||||
|
|
@ -54,33 +63,6 @@ clar_unit_tests = executable('unit-tests',
|
|||
)
|
||||
test('unit-tests', clar_unit_tests, kwargs: test_kwargs)
|
||||
|
||||
unit_test_programs = [
|
||||
'unit-tests/t-reftable-basics.c',
|
||||
'unit-tests/t-reftable-block.c',
|
||||
'unit-tests/t-reftable-merged.c',
|
||||
'unit-tests/t-reftable-pq.c',
|
||||
'unit-tests/t-reftable-readwrite.c',
|
||||
'unit-tests/t-reftable-record.c',
|
||||
'unit-tests/t-reftable-stack.c',
|
||||
'unit-tests/t-reftable-table.c',
|
||||
]
|
||||
|
||||
foreach unit_test_program : unit_test_programs
|
||||
unit_test_name = fs.stem(unit_test_program)
|
||||
unit_test = executable(unit_test_name,
|
||||
sources: [
|
||||
'unit-tests/test-lib.c',
|
||||
'unit-tests/lib-reftable.c',
|
||||
unit_test_program,
|
||||
],
|
||||
dependencies: [libgit_commonmain],
|
||||
)
|
||||
test(unit_test_name, unit_test,
|
||||
workdir: meson.current_source_dir(),
|
||||
kwargs: test_kwargs,
|
||||
)
|
||||
endforeach
|
||||
|
||||
subdir('helper')
|
||||
|
||||
integration_tests = [
|
||||
|
|
@ -1163,8 +1145,6 @@ benchmarks = [
|
|||
# sufficient to catch missing test suites in our CI though.
|
||||
foreach glob, tests : {
|
||||
't[0-9][0-9][0-9][0-9]-*.sh': integration_tests,
|
||||
'perf/p[0-9][0-9][0-9][0-9]-*.sh': benchmarks,
|
||||
'unit-tests/t-*.c': unit_test_programs,
|
||||
'unit-tests/u-*.c': clar_test_suites,
|
||||
}
|
||||
actual_tests = run_command(shell, '-c', 'ls ' + glob,
|
||||
|
|
@ -1231,4 +1211,4 @@ if perl.found() and time.found()
|
|||
timeout: 0,
|
||||
)
|
||||
endforeach
|
||||
endif
|
||||
endif
|
||||
|
|
@ -1,12 +1,14 @@
|
|||
#define DISABLE_SIGN_COMPARE_WARNINGS
|
||||
|
||||
#include "unit-test.h"
|
||||
#include "lib-reftable.h"
|
||||
#include "test-lib.h"
|
||||
#include "hex.h"
|
||||
#include "parse-options.h"
|
||||
#include "reftable/constants.h"
|
||||
#include "reftable/writer.h"
|
||||
#include "strbuf.h"
|
||||
#include "string-list.h"
|
||||
#include "strvec.h"
|
||||
|
||||
void t_reftable_set_hash(uint8_t *p, int i, enum reftable_hash id)
|
||||
void cl_reftable_set_hash(uint8_t *p, int i, enum reftable_hash id)
|
||||
{
|
||||
memset(p, (uint8_t)i, hash_size(id));
|
||||
}
|
||||
|
|
@ -22,17 +24,17 @@ static int strbuf_writer_flush(void *arg UNUSED)
|
|||
return 0;
|
||||
}
|
||||
|
||||
struct reftable_writer *t_reftable_strbuf_writer(struct reftable_buf *buf,
|
||||
struct reftable_writer *cl_reftable_strbuf_writer(struct reftable_buf *buf,
|
||||
struct reftable_write_options *opts)
|
||||
{
|
||||
struct reftable_writer *writer;
|
||||
int ret = reftable_writer_new(&writer, &strbuf_writer_write, &strbuf_writer_flush,
|
||||
buf, opts);
|
||||
check(!ret);
|
||||
cl_assert(!ret);
|
||||
return writer;
|
||||
}
|
||||
|
||||
void t_reftable_write_to_buf(struct reftable_buf *buf,
|
||||
void cl_reftable_write_to_buf(struct reftable_buf *buf,
|
||||
struct reftable_ref_record *refs,
|
||||
size_t nrefs,
|
||||
struct reftable_log_record *logs,
|
||||
|
|
@ -64,35 +66,36 @@ void t_reftable_write_to_buf(struct reftable_buf *buf,
|
|||
min = ui;
|
||||
}
|
||||
|
||||
writer = t_reftable_strbuf_writer(buf, &opts);
|
||||
reftable_writer_set_limits(writer, min, max);
|
||||
writer = cl_reftable_strbuf_writer(buf, &opts);
|
||||
ret = reftable_writer_set_limits(writer, min, max);
|
||||
cl_assert(!ret);
|
||||
|
||||
if (nrefs) {
|
||||
ret = reftable_writer_add_refs(writer, refs, nrefs);
|
||||
check_int(ret, ==, 0);
|
||||
cl_assert_equal_i(ret, 0);
|
||||
}
|
||||
|
||||
if (nlogs) {
|
||||
ret = reftable_writer_add_logs(writer, logs, nlogs);
|
||||
check_int(ret, ==, 0);
|
||||
cl_assert_equal_i(ret, 0);
|
||||
}
|
||||
|
||||
ret = reftable_writer_close(writer);
|
||||
check_int(ret, ==, 0);
|
||||
cl_assert_equal_i(ret, 0);
|
||||
|
||||
stats = reftable_writer_stats(writer);
|
||||
for (size_t i = 0; i < stats->ref_stats.blocks; i++) {
|
||||
for (size_t i = 0; i < (size_t)stats->ref_stats.blocks; i++) {
|
||||
size_t off = i * (opts.block_size ? opts.block_size
|
||||
: DEFAULT_BLOCK_SIZE);
|
||||
if (!off)
|
||||
off = header_size(opts.hash_id == REFTABLE_HASH_SHA256 ? 2 : 1);
|
||||
check_char(buf->buf[off], ==, 'r');
|
||||
cl_assert(buf->buf[off] == 'r');
|
||||
}
|
||||
|
||||
if (nrefs)
|
||||
check_int(stats->ref_stats.blocks, >, 0);
|
||||
cl_assert(stats->ref_stats.blocks > 0);
|
||||
if (nlogs)
|
||||
check_int(stats->log_stats.blocks, >, 0);
|
||||
cl_assert(stats->log_stats.blocks > 0);
|
||||
|
||||
reftable_writer_free(writer);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,21 +1,20 @@
|
|||
#ifndef LIB_REFTABLE_H
|
||||
#define LIB_REFTABLE_H
|
||||
|
||||
#include "git-compat-util.h"
|
||||
#include "clar/clar.h"
|
||||
#include "clar-decls.h"
|
||||
#include "git-compat-util.h"
|
||||
#include "reftable/reftable-writer.h"
|
||||
#include "strbuf.h"
|
||||
|
||||
struct reftable_buf;
|
||||
|
||||
void t_reftable_set_hash(uint8_t *p, int i, enum reftable_hash id);
|
||||
void cl_reftable_set_hash(uint8_t *p, int i, enum reftable_hash id);
|
||||
|
||||
struct reftable_writer *t_reftable_strbuf_writer(struct reftable_buf *buf,
|
||||
struct reftable_writer *cl_reftable_strbuf_writer(struct reftable_buf *buf,
|
||||
struct reftable_write_options *opts);
|
||||
|
||||
void t_reftable_write_to_buf(struct reftable_buf *buf,
|
||||
void cl_reftable_write_to_buf(struct reftable_buf *buf,
|
||||
struct reftable_ref_record *refs,
|
||||
size_t nrecords,
|
||||
struct reftable_log_record *logs,
|
||||
size_t nlogs,
|
||||
struct reftable_write_options *opts);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -1,219 +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 "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 *realloc_stub(void *p UNUSED, size_t size UNUSED)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int cmd_main(int argc UNUSED, const char *argv[] UNUSED)
|
||||
{
|
||||
if_test ("binary search with binsearch works") {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
if_test ("names_length returns size of a NULL-terminated string array") {
|
||||
const char *a[] = { "a", "b", NULL };
|
||||
check_int(names_length(a), ==, 2);
|
||||
}
|
||||
|
||||
if_test ("names_equal compares NULL-terminated string arrays") {
|
||||
const char *a[] = { "a", "b", "c", NULL };
|
||||
const char *b[] = { "a", "b", "d", NULL };
|
||||
const char *c[] = { "a", "b", NULL };
|
||||
|
||||
check(names_equal(a, a));
|
||||
check(!names_equal(a, b));
|
||||
check(!names_equal(a, c));
|
||||
}
|
||||
|
||||
if_test ("parse_names works for basic input") {
|
||||
char in1[] = "line\n";
|
||||
char in2[] = "a\nb\nc";
|
||||
char **out = parse_names(in1, strlen(in1));
|
||||
check(out != NULL);
|
||||
check_str(out[0], "line");
|
||||
check(!out[1]);
|
||||
free_names(out);
|
||||
|
||||
out = parse_names(in2, strlen(in2));
|
||||
check(out != NULL);
|
||||
check_str(out[0], "a");
|
||||
check_str(out[1], "b");
|
||||
check_str(out[2], "c");
|
||||
check(!out[3]);
|
||||
free_names(out);
|
||||
}
|
||||
|
||||
if_test ("parse_names drops empty string") {
|
||||
char in[] = "a\n\nb\n";
|
||||
char **out = parse_names(in, strlen(in));
|
||||
check(out != NULL);
|
||||
check_str(out[0], "a");
|
||||
/* simply '\n' should be dropped as empty string */
|
||||
check_str(out[1], "b");
|
||||
check(!out[2]);
|
||||
free_names(out);
|
||||
}
|
||||
|
||||
if_test ("common_prefix_size works") {
|
||||
struct reftable_buf a = REFTABLE_BUF_INIT;
|
||||
struct reftable_buf b = REFTABLE_BUF_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++) {
|
||||
check(!reftable_buf_addstr(&a, cases[i].a));
|
||||
check(!reftable_buf_addstr(&b, cases[i].b));
|
||||
check_uint(common_prefix_size(&a, &b), ==, cases[i].want);
|
||||
reftable_buf_reset(&a);
|
||||
reftable_buf_reset(&b);
|
||||
}
|
||||
reftable_buf_release(&a);
|
||||
reftable_buf_release(&b);
|
||||
}
|
||||
|
||||
if_test ("reftable_put_be64 and reftable_get_be64 work") {
|
||||
uint64_t in = 0x1122334455667788;
|
||||
uint8_t dest[8];
|
||||
uint64_t out;
|
||||
reftable_put_be64(dest, in);
|
||||
out = reftable_get_be64(dest);
|
||||
check_int(in, ==, out);
|
||||
}
|
||||
|
||||
if_test ("reftable_put_be32 and reftable_get_be32 work") {
|
||||
uint32_t in = 0x11223344;
|
||||
uint8_t dest[4];
|
||||
uint32_t out;
|
||||
reftable_put_be32(dest, in);
|
||||
out = reftable_get_be32(dest);
|
||||
check_int(in, ==, out);
|
||||
}
|
||||
|
||||
if_test ("reftable_put_be24 and reftable_get_be24 work") {
|
||||
uint32_t in = 0x112233;
|
||||
uint8_t dest[3];
|
||||
uint32_t out;
|
||||
reftable_put_be24(dest, in);
|
||||
out = reftable_get_be24(dest);
|
||||
check_int(in, ==, out);
|
||||
}
|
||||
|
||||
if_test ("put_be16 and get_be16 work") {
|
||||
uint32_t in = 0xfef1;
|
||||
uint8_t dest[3];
|
||||
uint32_t out;
|
||||
reftable_put_be16(dest, in);
|
||||
out = reftable_get_be16(dest);
|
||||
check_int(in, ==, out);
|
||||
}
|
||||
|
||||
if_test ("REFTABLE_ALLOC_GROW works") {
|
||||
int *arr = NULL, *old_arr;
|
||||
size_t alloc = 0, old_alloc;
|
||||
|
||||
check(!REFTABLE_ALLOC_GROW(arr, 1, alloc));
|
||||
check(arr != NULL);
|
||||
check_uint(alloc, >=, 1);
|
||||
arr[0] = 42;
|
||||
|
||||
old_alloc = alloc;
|
||||
old_arr = arr;
|
||||
reftable_set_alloc(NULL, realloc_stub, NULL);
|
||||
check(REFTABLE_ALLOC_GROW(arr, old_alloc + 1, alloc));
|
||||
check(arr == old_arr);
|
||||
check_uint(alloc, ==, old_alloc);
|
||||
|
||||
old_alloc = alloc;
|
||||
reftable_set_alloc(NULL, NULL, NULL);
|
||||
check(!REFTABLE_ALLOC_GROW(arr, old_alloc + 1, alloc));
|
||||
check(arr != NULL);
|
||||
check_uint(alloc, >, old_alloc);
|
||||
arr[alloc - 1] = 42;
|
||||
|
||||
reftable_free(arr);
|
||||
}
|
||||
|
||||
if_test ("REFTABLE_ALLOC_GROW_OR_NULL works") {
|
||||
int *arr = NULL;
|
||||
size_t alloc = 0, old_alloc;
|
||||
|
||||
REFTABLE_ALLOC_GROW_OR_NULL(arr, 1, alloc);
|
||||
check(arr != NULL);
|
||||
check_uint(alloc, >=, 1);
|
||||
arr[0] = 42;
|
||||
|
||||
old_alloc = alloc;
|
||||
REFTABLE_ALLOC_GROW_OR_NULL(arr, old_alloc + 1, alloc);
|
||||
check(arr != NULL);
|
||||
check_uint(alloc, >, old_alloc);
|
||||
arr[alloc - 1] = 42;
|
||||
|
||||
old_alloc = alloc;
|
||||
reftable_set_alloc(NULL, realloc_stub, NULL);
|
||||
REFTABLE_ALLOC_GROW_OR_NULL(arr, old_alloc + 1, alloc);
|
||||
check(arr == NULL);
|
||||
check_uint(alloc, ==, 0);
|
||||
reftable_set_alloc(NULL, NULL, NULL);
|
||||
|
||||
reftable_free(arr);
|
||||
}
|
||||
|
||||
return test_done();
|
||||
}
|
||||
|
|
@ -0,0 +1,227 @@
|
|||
/*
|
||||
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 "unit-test.h"
|
||||
#include "lib-reftable.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 *realloc_stub(void *p UNUSED, size_t size UNUSED)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void test_reftable_basics__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);
|
||||
cl_assert_equal_i(idx, testcases[i].expected_idx);
|
||||
}
|
||||
}
|
||||
|
||||
void test_reftable_basics__names_length(void)
|
||||
{
|
||||
const char *a[] = { "a", "b", NULL };
|
||||
cl_assert_equal_i(names_length(a), 2);
|
||||
}
|
||||
|
||||
void test_reftable_basics__names_equal(void)
|
||||
{
|
||||
const char *a[] = { "a", "b", "c", NULL };
|
||||
const char *b[] = { "a", "b", "d", NULL };
|
||||
const char *c[] = { "a", "b", NULL };
|
||||
|
||||
cl_assert(names_equal(a, a));
|
||||
cl_assert(!names_equal(a, b));
|
||||
cl_assert(!names_equal(a, c));
|
||||
}
|
||||
|
||||
void test_reftable_basics__parse_names(void)
|
||||
{
|
||||
char in1[] = "line\n";
|
||||
char in2[] = "a\nb\nc";
|
||||
char **out = parse_names(in1, strlen(in1));
|
||||
cl_assert(out != NULL);
|
||||
cl_assert_equal_s(out[0], "line");
|
||||
cl_assert(!out[1]);
|
||||
free_names(out);
|
||||
|
||||
out = parse_names(in2, strlen(in2));
|
||||
cl_assert(out != NULL);
|
||||
cl_assert_equal_s(out[0], "a");
|
||||
cl_assert_equal_s(out[1], "b");
|
||||
cl_assert_equal_s(out[2], "c");
|
||||
cl_assert(!out[3]);
|
||||
free_names(out);
|
||||
}
|
||||
|
||||
void test_reftable_basics__parse_names_drop_empty_string(void)
|
||||
{
|
||||
char in[] = "a\n\nb\n";
|
||||
char **out = parse_names(in, strlen(in));
|
||||
cl_assert(out != NULL);
|
||||
cl_assert_equal_s(out[0], "a");
|
||||
/* simply '\n' should be dropped as empty string */
|
||||
cl_assert_equal_s(out[1], "b");
|
||||
cl_assert(out[2] == NULL);
|
||||
free_names(out);
|
||||
}
|
||||
|
||||
void test_reftable_basics__common_prefix_size(void)
|
||||
{
|
||||
struct reftable_buf a = REFTABLE_BUF_INIT;
|
||||
struct reftable_buf b = REFTABLE_BUF_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++) {
|
||||
cl_assert_equal_i(reftable_buf_addstr(&a, cases[i].a), 0);
|
||||
cl_assert_equal_i(reftable_buf_addstr(&b, cases[i].b), 0);
|
||||
cl_assert_equal_i(common_prefix_size(&a, &b), cases[i].want);
|
||||
reftable_buf_reset(&a);
|
||||
reftable_buf_reset(&b);
|
||||
}
|
||||
reftable_buf_release(&a);
|
||||
reftable_buf_release(&b);
|
||||
}
|
||||
|
||||
void test_reftable_basics__put_get_be64(void)
|
||||
{
|
||||
uint64_t in = 0x1122334455667788;
|
||||
uint8_t dest[8];
|
||||
uint64_t out;
|
||||
reftable_put_be64(dest, in);
|
||||
out = reftable_get_be64(dest);
|
||||
cl_assert(in == out);
|
||||
}
|
||||
|
||||
void test_reftable_basics__put_get_be32(void)
|
||||
{
|
||||
uint32_t in = 0x11223344;
|
||||
uint8_t dest[4];
|
||||
uint32_t out;
|
||||
reftable_put_be32(dest, in);
|
||||
out = reftable_get_be32(dest);
|
||||
cl_assert_equal_i(in, out);
|
||||
}
|
||||
|
||||
void test_reftable_basics__put_get_be24(void)
|
||||
{
|
||||
uint32_t in = 0x112233;
|
||||
uint8_t dest[3];
|
||||
uint32_t out;
|
||||
reftable_put_be24(dest, in);
|
||||
out = reftable_get_be24(dest);
|
||||
cl_assert_equal_i(in, out);
|
||||
}
|
||||
|
||||
void test_reftable_basics__put_get_be16(void)
|
||||
{
|
||||
uint32_t in = 0xfef1;
|
||||
uint8_t dest[3];
|
||||
uint32_t out;
|
||||
reftable_put_be16(dest, in);
|
||||
out = reftable_get_be16(dest);
|
||||
cl_assert_equal_i(in, out);
|
||||
}
|
||||
|
||||
void test_reftable_basics__alloc_grow(void)
|
||||
{
|
||||
int *arr = NULL, *old_arr;
|
||||
size_t alloc = 0, old_alloc;
|
||||
|
||||
cl_assert_equal_i(REFTABLE_ALLOC_GROW(arr, 1, alloc), 0);
|
||||
cl_assert(arr != NULL);
|
||||
cl_assert(alloc >= 1);
|
||||
arr[0] = 42;
|
||||
|
||||
old_alloc = alloc;
|
||||
old_arr = arr;
|
||||
reftable_set_alloc(NULL, realloc_stub, NULL);
|
||||
cl_assert(REFTABLE_ALLOC_GROW(arr, old_alloc + 1, alloc));
|
||||
cl_assert(arr == old_arr);
|
||||
cl_assert_equal_i(alloc, old_alloc);
|
||||
|
||||
old_alloc = alloc;
|
||||
reftable_set_alloc(NULL, NULL, NULL);
|
||||
cl_assert_equal_i(REFTABLE_ALLOC_GROW(arr, old_alloc + 1, alloc), 0);
|
||||
cl_assert(arr != NULL);
|
||||
cl_assert(alloc > old_alloc);
|
||||
arr[alloc - 1] = 42;
|
||||
|
||||
reftable_free(arr);
|
||||
}
|
||||
|
||||
void test_reftable_basics__alloc_grow_or_null(void)
|
||||
{
|
||||
int *arr = NULL;
|
||||
size_t alloc = 0, old_alloc;
|
||||
|
||||
REFTABLE_ALLOC_GROW_OR_NULL(arr, 1, alloc);
|
||||
cl_assert(arr != NULL);
|
||||
cl_assert(alloc >= 1);
|
||||
arr[0] = 42;
|
||||
|
||||
old_alloc = alloc;
|
||||
REFTABLE_ALLOC_GROW_OR_NULL(arr, old_alloc + 1, alloc);
|
||||
cl_assert(arr != NULL);
|
||||
cl_assert(alloc > old_alloc);
|
||||
arr[alloc - 1] = 42;
|
||||
|
||||
old_alloc = alloc;
|
||||
reftable_set_alloc(NULL, realloc_stub, NULL);
|
||||
REFTABLE_ALLOC_GROW_OR_NULL(arr, old_alloc + 1, alloc);
|
||||
cl_assert(arr == NULL);
|
||||
cl_assert_equal_i(alloc, 0);
|
||||
reftable_set_alloc(NULL, NULL, NULL);
|
||||
|
||||
reftable_free(arr);
|
||||
}
|
||||
|
|
@ -6,14 +6,15 @@ license that can be found in the LICENSE file or at
|
|||
https://developers.google.com/open-source/licenses/bsd
|
||||
*/
|
||||
|
||||
#include "test-lib.h"
|
||||
#include "unit-test.h"
|
||||
#include "lib-reftable.h"
|
||||
#include "reftable/block.h"
|
||||
#include "reftable/blocksource.h"
|
||||
#include "reftable/constants.h"
|
||||
#include "reftable/reftable-error.h"
|
||||
#include "strbuf.h"
|
||||
|
||||
static void t_ref_block_read_write(void)
|
||||
void test_reftable_block__read_write(void)
|
||||
{
|
||||
const int header_off = 21; /* random */
|
||||
struct reftable_record recs[30];
|
||||
|
|
@ -34,17 +35,18 @@ static void t_ref_block_read_write(void)
|
|||
struct reftable_buf block_data = REFTABLE_BUF_INIT;
|
||||
|
||||
REFTABLE_CALLOC_ARRAY(block_data.buf, block_size);
|
||||
check(block_data.buf != NULL);
|
||||
cl_assert(block_data.buf != NULL);
|
||||
block_data.len = block_size;
|
||||
|
||||
ret = block_writer_init(&bw, REFTABLE_BLOCK_TYPE_REF, (uint8_t *) block_data.buf, block_size,
|
||||
ret = block_writer_init(&bw, REFTABLE_BLOCK_TYPE_REF,
|
||||
(uint8_t *) block_data.buf, block_size,
|
||||
header_off, hash_size(REFTABLE_HASH_SHA1));
|
||||
check(!ret);
|
||||
cl_assert(!ret);
|
||||
|
||||
rec.u.ref.refname = (char *) "";
|
||||
rec.u.ref.value_type = REFTABLE_REF_DELETION;
|
||||
ret = block_writer_add(&bw, &rec);
|
||||
check_int(ret, ==, REFTABLE_API_ERROR);
|
||||
cl_assert_equal_i(ret, REFTABLE_API_ERROR);
|
||||
|
||||
for (i = 0; i < N; i++) {
|
||||
rec.u.ref.refname = xstrfmt("branch%02"PRIuMAX, (uintmax_t)i);
|
||||
|
|
@ -55,11 +57,11 @@ static void t_ref_block_read_write(void)
|
|||
ret = block_writer_add(&bw, &rec);
|
||||
rec.u.ref.refname = NULL;
|
||||
rec.u.ref.value_type = REFTABLE_REF_DELETION;
|
||||
check_int(ret, ==, 0);
|
||||
cl_assert_equal_i(ret, 0);
|
||||
}
|
||||
|
||||
ret = block_writer_finish(&bw);
|
||||
check_int(ret, >, 0);
|
||||
cl_assert(ret > 0);
|
||||
|
||||
block_writer_release(&bw);
|
||||
|
||||
|
|
@ -71,32 +73,32 @@ static void t_ref_block_read_write(void)
|
|||
|
||||
for (i = 0; ; i++) {
|
||||
ret = block_iter_next(&it, &rec);
|
||||
check_int(ret, >=, 0);
|
||||
cl_assert(ret >= 0);
|
||||
if (ret > 0) {
|
||||
check_int(i, ==, N);
|
||||
cl_assert_equal_i(i, N);
|
||||
break;
|
||||
}
|
||||
check(reftable_record_equal(&recs[i], &rec, REFTABLE_HASH_SIZE_SHA1));
|
||||
cl_assert_equal_i(reftable_record_equal(&recs[i], &rec, REFTABLE_HASH_SIZE_SHA1), 1);
|
||||
}
|
||||
|
||||
for (i = 0; i < N; i++) {
|
||||
reftable_record_key(&recs[i], &want);
|
||||
|
||||
ret = block_iter_seek_key(&it, &want);
|
||||
check_int(ret, ==, 0);
|
||||
cl_assert_equal_i(ret, 0);
|
||||
|
||||
ret = block_iter_next(&it, &rec);
|
||||
check_int(ret, ==, 0);
|
||||
cl_assert_equal_i(ret, 0);
|
||||
|
||||
check(reftable_record_equal(&recs[i], &rec, REFTABLE_HASH_SIZE_SHA1));
|
||||
cl_assert_equal_i(reftable_record_equal(&recs[i], &rec, REFTABLE_HASH_SIZE_SHA1), 1);
|
||||
|
||||
want.len--;
|
||||
ret = block_iter_seek_key(&it, &want);
|
||||
check_int(ret, ==, 0);
|
||||
cl_assert_equal_i(ret, 0);
|
||||
|
||||
ret = block_iter_next(&it, &rec);
|
||||
check_int(ret, ==, 0);
|
||||
check(reftable_record_equal(&recs[10 * (i / 10)], &rec, REFTABLE_HASH_SIZE_SHA1));
|
||||
cl_assert_equal_i(ret, 0);
|
||||
cl_assert_equal_i(reftable_record_equal(&recs[10 * (i / 10)], &rec, REFTABLE_HASH_SIZE_SHA1), 1);
|
||||
}
|
||||
|
||||
reftable_block_release(&block);
|
||||
|
|
@ -108,7 +110,7 @@ static void t_ref_block_read_write(void)
|
|||
reftable_record_release(&recs[i]);
|
||||
}
|
||||
|
||||
static void t_log_block_read_write(void)
|
||||
void test_reftable_block__log_read_write(void)
|
||||
{
|
||||
const int header_off = 21;
|
||||
struct reftable_record recs[30];
|
||||
|
|
@ -129,12 +131,12 @@ static void t_log_block_read_write(void)
|
|||
struct reftable_buf block_data = REFTABLE_BUF_INIT;
|
||||
|
||||
REFTABLE_CALLOC_ARRAY(block_data.buf, block_size);
|
||||
check(block_data.buf != NULL);
|
||||
cl_assert(block_data.buf != NULL);
|
||||
block_data.len = block_size;
|
||||
|
||||
ret = block_writer_init(&bw, REFTABLE_BLOCK_TYPE_LOG, (uint8_t *) block_data.buf, block_size,
|
||||
header_off, hash_size(REFTABLE_HASH_SHA1));
|
||||
check(!ret);
|
||||
cl_assert(!ret);
|
||||
|
||||
for (i = 0; i < N; i++) {
|
||||
rec.u.log.refname = xstrfmt("branch%02"PRIuMAX , (uintmax_t)i);
|
||||
|
|
@ -145,11 +147,11 @@ static void t_log_block_read_write(void)
|
|||
ret = block_writer_add(&bw, &rec);
|
||||
rec.u.log.refname = NULL;
|
||||
rec.u.log.value_type = REFTABLE_LOG_DELETION;
|
||||
check_int(ret, ==, 0);
|
||||
cl_assert_equal_i(ret, 0);
|
||||
}
|
||||
|
||||
ret = block_writer_finish(&bw);
|
||||
check_int(ret, >, 0);
|
||||
cl_assert(ret > 0);
|
||||
|
||||
block_writer_release(&bw);
|
||||
|
||||
|
|
@ -161,33 +163,33 @@ static void t_log_block_read_write(void)
|
|||
|
||||
for (i = 0; ; i++) {
|
||||
ret = block_iter_next(&it, &rec);
|
||||
check_int(ret, >=, 0);
|
||||
cl_assert(ret >= 0);
|
||||
if (ret > 0) {
|
||||
check_int(i, ==, N);
|
||||
cl_assert_equal_i(i, N);
|
||||
break;
|
||||
}
|
||||
check(reftable_record_equal(&recs[i], &rec, REFTABLE_HASH_SIZE_SHA1));
|
||||
cl_assert_equal_i(reftable_record_equal(&recs[i], &rec, REFTABLE_HASH_SIZE_SHA1), 1);
|
||||
}
|
||||
|
||||
for (i = 0; i < N; i++) {
|
||||
reftable_buf_reset(&want);
|
||||
check(!reftable_buf_addstr(&want, recs[i].u.log.refname));
|
||||
cl_assert(reftable_buf_addstr(&want, recs[i].u.log.refname) == 0);
|
||||
|
||||
ret = block_iter_seek_key(&it, &want);
|
||||
check_int(ret, ==, 0);
|
||||
cl_assert_equal_i(ret, 0);
|
||||
|
||||
ret = block_iter_next(&it, &rec);
|
||||
check_int(ret, ==, 0);
|
||||
cl_assert_equal_i(ret, 0);
|
||||
|
||||
check(reftable_record_equal(&recs[i], &rec, REFTABLE_HASH_SIZE_SHA1));
|
||||
cl_assert_equal_i(reftable_record_equal(&recs[i], &rec, REFTABLE_HASH_SIZE_SHA1), 1);
|
||||
|
||||
want.len--;
|
||||
ret = block_iter_seek_key(&it, &want);
|
||||
check_int(ret, ==, 0);
|
||||
cl_assert_equal_i(ret, 0);
|
||||
|
||||
ret = block_iter_next(&it, &rec);
|
||||
check_int(ret, ==, 0);
|
||||
check(reftable_record_equal(&recs[10 * (i / 10)], &rec, REFTABLE_HASH_SIZE_SHA1));
|
||||
cl_assert_equal_i(ret, 0);
|
||||
cl_assert_equal_i(reftable_record_equal(&recs[10 * (i / 10)], &rec, REFTABLE_HASH_SIZE_SHA1), 1);
|
||||
}
|
||||
|
||||
reftable_block_release(&block);
|
||||
|
|
@ -199,7 +201,7 @@ static void t_log_block_read_write(void)
|
|||
reftable_record_release(&recs[i]);
|
||||
}
|
||||
|
||||
static void t_obj_block_read_write(void)
|
||||
void test_reftable_block__obj_read_write(void)
|
||||
{
|
||||
const int header_off = 21;
|
||||
struct reftable_record recs[30];
|
||||
|
|
@ -220,12 +222,12 @@ static void t_obj_block_read_write(void)
|
|||
struct reftable_buf block_data = REFTABLE_BUF_INIT;
|
||||
|
||||
REFTABLE_CALLOC_ARRAY(block_data.buf, block_size);
|
||||
check(block_data.buf != NULL);
|
||||
cl_assert(block_data.buf != NULL);
|
||||
block_data.len = block_size;
|
||||
|
||||
ret = block_writer_init(&bw, REFTABLE_BLOCK_TYPE_OBJ, (uint8_t *) block_data.buf, block_size,
|
||||
header_off, hash_size(REFTABLE_HASH_SHA1));
|
||||
check(!ret);
|
||||
cl_assert(!ret);
|
||||
|
||||
for (i = 0; i < N; i++) {
|
||||
uint8_t bytes[] = { i, i + 1, i + 2, i + 3, i + 5 }, *allocated;
|
||||
|
|
@ -238,11 +240,11 @@ static void t_obj_block_read_write(void)
|
|||
ret = block_writer_add(&bw, &rec);
|
||||
rec.u.obj.hash_prefix = NULL;
|
||||
rec.u.obj.hash_prefix_len = 0;
|
||||
check_int(ret, ==, 0);
|
||||
cl_assert_equal_i(ret, 0);
|
||||
}
|
||||
|
||||
ret = block_writer_finish(&bw);
|
||||
check_int(ret, >, 0);
|
||||
cl_assert(ret > 0);
|
||||
|
||||
block_writer_release(&bw);
|
||||
|
||||
|
|
@ -254,24 +256,24 @@ static void t_obj_block_read_write(void)
|
|||
|
||||
for (i = 0; ; i++) {
|
||||
ret = block_iter_next(&it, &rec);
|
||||
check_int(ret, >=, 0);
|
||||
cl_assert(ret >= 0);
|
||||
if (ret > 0) {
|
||||
check_int(i, ==, N);
|
||||
cl_assert_equal_i(i, N);
|
||||
break;
|
||||
}
|
||||
check(reftable_record_equal(&recs[i], &rec, REFTABLE_HASH_SIZE_SHA1));
|
||||
cl_assert_equal_i(reftable_record_equal(&recs[i], &rec, REFTABLE_HASH_SIZE_SHA1), 1);
|
||||
}
|
||||
|
||||
for (i = 0; i < N; i++) {
|
||||
reftable_record_key(&recs[i], &want);
|
||||
|
||||
ret = block_iter_seek_key(&it, &want);
|
||||
check_int(ret, ==, 0);
|
||||
cl_assert_equal_i(ret, 0);
|
||||
|
||||
ret = block_iter_next(&it, &rec);
|
||||
check_int(ret, ==, 0);
|
||||
cl_assert_equal_i(ret, 0);
|
||||
|
||||
check(reftable_record_equal(&recs[i], &rec, REFTABLE_HASH_SIZE_SHA1));
|
||||
cl_assert_equal_i(reftable_record_equal(&recs[i], &rec, REFTABLE_HASH_SIZE_SHA1), 1);
|
||||
}
|
||||
|
||||
reftable_block_release(&block);
|
||||
|
|
@ -283,7 +285,7 @@ static void t_obj_block_read_write(void)
|
|||
reftable_record_release(&recs[i]);
|
||||
}
|
||||
|
||||
static void t_index_block_read_write(void)
|
||||
void test_reftable_block__ref_read_write(void)
|
||||
{
|
||||
const int header_off = 21;
|
||||
struct reftable_record recs[30];
|
||||
|
|
@ -305,12 +307,12 @@ static void t_index_block_read_write(void)
|
|||
struct reftable_buf block_data = REFTABLE_BUF_INIT;
|
||||
|
||||
REFTABLE_CALLOC_ARRAY(block_data.buf, block_size);
|
||||
check(block_data.buf != NULL);
|
||||
cl_assert(block_data.buf != NULL);
|
||||
block_data.len = block_size;
|
||||
|
||||
ret = block_writer_init(&bw, REFTABLE_BLOCK_TYPE_INDEX, (uint8_t *) block_data.buf, block_size,
|
||||
header_off, hash_size(REFTABLE_HASH_SHA1));
|
||||
check(!ret);
|
||||
cl_assert(!ret);
|
||||
|
||||
for (i = 0; i < N; i++) {
|
||||
char buf[128];
|
||||
|
|
@ -319,15 +321,15 @@ static void t_index_block_read_write(void)
|
|||
|
||||
reftable_buf_init(&recs[i].u.idx.last_key);
|
||||
recs[i].type = REFTABLE_BLOCK_TYPE_INDEX;
|
||||
check(!reftable_buf_addstr(&recs[i].u.idx.last_key, buf));
|
||||
cl_assert(!reftable_buf_addstr(&recs[i].u.idx.last_key, buf));
|
||||
recs[i].u.idx.offset = i;
|
||||
|
||||
ret = block_writer_add(&bw, &recs[i]);
|
||||
check_int(ret, ==, 0);
|
||||
cl_assert_equal_i(ret, 0);
|
||||
}
|
||||
|
||||
ret = block_writer_finish(&bw);
|
||||
check_int(ret, >, 0);
|
||||
cl_assert(ret > 0);
|
||||
|
||||
block_writer_release(&bw);
|
||||
|
||||
|
|
@ -339,32 +341,32 @@ static void t_index_block_read_write(void)
|
|||
|
||||
for (i = 0; ; i++) {
|
||||
ret = block_iter_next(&it, &rec);
|
||||
check_int(ret, >=, 0);
|
||||
cl_assert(ret >= 0);
|
||||
if (ret > 0) {
|
||||
check_int(i, ==, N);
|
||||
cl_assert_equal_i(i, N);
|
||||
break;
|
||||
}
|
||||
check(reftable_record_equal(&recs[i], &rec, REFTABLE_HASH_SIZE_SHA1));
|
||||
cl_assert_equal_i(reftable_record_equal(&recs[i], &rec, REFTABLE_HASH_SIZE_SHA1), 1);
|
||||
}
|
||||
|
||||
for (i = 0; i < N; i++) {
|
||||
reftable_record_key(&recs[i], &want);
|
||||
|
||||
ret = block_iter_seek_key(&it, &want);
|
||||
check_int(ret, ==, 0);
|
||||
cl_assert_equal_i(ret, 0);
|
||||
|
||||
ret = block_iter_next(&it, &rec);
|
||||
check_int(ret, ==, 0);
|
||||
cl_assert_equal_i(ret, 0);
|
||||
|
||||
check(reftable_record_equal(&recs[i], &rec, REFTABLE_HASH_SIZE_SHA1));
|
||||
cl_assert_equal_i(reftable_record_equal(&recs[i], &rec, REFTABLE_HASH_SIZE_SHA1), 1);
|
||||
|
||||
want.len--;
|
||||
ret = block_iter_seek_key(&it, &want);
|
||||
check_int(ret, ==, 0);
|
||||
cl_assert_equal_i(ret, 0);
|
||||
|
||||
ret = block_iter_next(&it, &rec);
|
||||
check_int(ret, ==, 0);
|
||||
check(reftable_record_equal(&recs[10 * (i / 10)], &rec, REFTABLE_HASH_SIZE_SHA1));
|
||||
cl_assert_equal_i(ret, 0);
|
||||
cl_assert_equal_i(reftable_record_equal(&recs[10 * (i / 10)], &rec, REFTABLE_HASH_SIZE_SHA1), 1);
|
||||
}
|
||||
|
||||
reftable_block_release(&block);
|
||||
|
|
@ -376,7 +378,7 @@ static void t_index_block_read_write(void)
|
|||
reftable_record_release(&recs[i]);
|
||||
}
|
||||
|
||||
static void t_block_iterator(void)
|
||||
void test_reftable_block__iterator(void)
|
||||
{
|
||||
struct reftable_block_source source = { 0 };
|
||||
struct block_writer writer = {
|
||||
|
|
@ -391,11 +393,12 @@ static void t_block_iterator(void)
|
|||
|
||||
data.len = 1024;
|
||||
REFTABLE_CALLOC_ARRAY(data.buf, data.len);
|
||||
check(data.buf != NULL);
|
||||
cl_assert(data.buf != NULL);
|
||||
|
||||
err = block_writer_init(&writer, REFTABLE_BLOCK_TYPE_REF, (uint8_t *) data.buf, data.len,
|
||||
err = block_writer_init(&writer, REFTABLE_BLOCK_TYPE_REF,
|
||||
(uint8_t *) data.buf, data.len,
|
||||
0, hash_size(REFTABLE_HASH_SHA1));
|
||||
check(!err);
|
||||
cl_assert(!err);
|
||||
|
||||
for (size_t i = 0; i < ARRAY_SIZE(expected_refs); i++) {
|
||||
expected_refs[i] = (struct reftable_record) {
|
||||
|
|
@ -408,42 +411,42 @@ static void t_block_iterator(void)
|
|||
memset(expected_refs[i].u.ref.value.val1, i, REFTABLE_HASH_SIZE_SHA1);
|
||||
|
||||
err = block_writer_add(&writer, &expected_refs[i]);
|
||||
check_int(err, ==, 0);
|
||||
cl_assert_equal_i(err, 0);
|
||||
}
|
||||
|
||||
err = block_writer_finish(&writer);
|
||||
check_int(err, >, 0);
|
||||
cl_assert(err > 0);
|
||||
|
||||
block_source_from_buf(&source, &data);
|
||||
reftable_block_init(&block, &source, 0, 0, data.len,
|
||||
REFTABLE_HASH_SIZE_SHA1, REFTABLE_BLOCK_TYPE_REF);
|
||||
|
||||
err = reftable_block_init_iterator(&block, &it);
|
||||
check_int(err, ==, 0);
|
||||
cl_assert_equal_i(err, 0);
|
||||
|
||||
for (size_t i = 0; ; i++) {
|
||||
err = reftable_iterator_next_ref(&it, &ref);
|
||||
if (err > 0) {
|
||||
check_int(i, ==, ARRAY_SIZE(expected_refs));
|
||||
cl_assert_equal_i(i, ARRAY_SIZE(expected_refs));
|
||||
break;
|
||||
}
|
||||
check_int(err, ==, 0);
|
||||
cl_assert_equal_i(err, 0);
|
||||
|
||||
check(reftable_ref_record_equal(&ref, &expected_refs[i].u.ref,
|
||||
REFTABLE_HASH_SIZE_SHA1));
|
||||
cl_assert(reftable_ref_record_equal(&ref,
|
||||
&expected_refs[i].u.ref, REFTABLE_HASH_SIZE_SHA1));
|
||||
}
|
||||
|
||||
err = reftable_iterator_seek_ref(&it, "refs/heads/does-not-exist");
|
||||
check_int(err, ==, 0);
|
||||
cl_assert_equal_i(err, 0);
|
||||
err = reftable_iterator_next_ref(&it, &ref);
|
||||
check_int(err, ==, 1);
|
||||
cl_assert_equal_i(err, 1);
|
||||
|
||||
err = reftable_iterator_seek_ref(&it, "refs/heads/branch-13");
|
||||
check_int(err, ==, 0);
|
||||
cl_assert_equal_i(err, 0);
|
||||
err = reftable_iterator_next_ref(&it, &ref);
|
||||
check_int(err, ==, 0);
|
||||
check(reftable_ref_record_equal(&ref, &expected_refs[13].u.ref,
|
||||
REFTABLE_HASH_SIZE_SHA1));
|
||||
cl_assert_equal_i(err, 0);
|
||||
cl_assert(reftable_ref_record_equal(&ref,
|
||||
&expected_refs[13].u.ref,REFTABLE_HASH_SIZE_SHA1));
|
||||
|
||||
for (size_t i = 0; i < ARRAY_SIZE(expected_refs); i++)
|
||||
reftable_free(expected_refs[i].u.ref.refname);
|
||||
|
|
@ -453,14 +456,3 @@ static void t_block_iterator(void)
|
|||
block_writer_release(&writer);
|
||||
reftable_buf_release(&data);
|
||||
}
|
||||
|
||||
int cmd_main(int argc UNUSED, const char *argv[] UNUSED)
|
||||
{
|
||||
TEST(t_index_block_read_write(), "read-write operations on index blocks work");
|
||||
TEST(t_log_block_read_write(), "read-write operations on log blocks work");
|
||||
TEST(t_obj_block_read_write(), "read-write operations on obj blocks work");
|
||||
TEST(t_ref_block_read_write(), "read-write operations on ref blocks work");
|
||||
TEST(t_block_iterator(), "block iterator works");
|
||||
|
||||
return test_done();
|
||||
}
|
||||
|
|
@ -6,7 +6,7 @@ license that can be found in the LICENSE file or at
|
|||
https://developers.google.com/open-source/licenses/bsd
|
||||
*/
|
||||
|
||||
#include "test-lib.h"
|
||||
#include "unit-test.h"
|
||||
#include "lib-reftable.h"
|
||||
#include "reftable/blocksource.h"
|
||||
#include "reftable/constants.h"
|
||||
|
|
@ -29,21 +29,21 @@ merged_table_from_records(struct reftable_ref_record **refs,
|
|||
int err;
|
||||
|
||||
REFTABLE_CALLOC_ARRAY(*tables, n);
|
||||
check(*tables != NULL);
|
||||
cl_assert(*tables != NULL);
|
||||
REFTABLE_CALLOC_ARRAY(*source, n);
|
||||
check(*source != NULL);
|
||||
cl_assert(*source != NULL);
|
||||
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
t_reftable_write_to_buf(&buf[i], refs[i], sizes[i], NULL, 0, &opts);
|
||||
cl_reftable_write_to_buf(&buf[i], refs[i], sizes[i], NULL, 0, &opts);
|
||||
block_source_from_buf(&(*source)[i], &buf[i]);
|
||||
|
||||
err = reftable_table_new(&(*tables)[i], &(*source)[i],
|
||||
"name");
|
||||
check(!err);
|
||||
cl_assert(!err);
|
||||
}
|
||||
|
||||
err = reftable_merged_table_new(&mt, *tables, n, REFTABLE_HASH_SHA1);
|
||||
check(!err);
|
||||
cl_assert(!err);
|
||||
return mt;
|
||||
}
|
||||
|
||||
|
|
@ -54,7 +54,7 @@ static void tables_destroy(struct reftable_table **tables, const size_t n)
|
|||
reftable_free(tables);
|
||||
}
|
||||
|
||||
static void t_merged_single_record(void)
|
||||
void test_reftable_merged__single_record(void)
|
||||
{
|
||||
struct reftable_ref_record r1[] = { {
|
||||
.refname = (char *) "b",
|
||||
|
|
@ -85,13 +85,14 @@ static void t_merged_single_record(void)
|
|||
int err;
|
||||
|
||||
err = merged_table_init_iter(mt, &it, REFTABLE_BLOCK_TYPE_REF);
|
||||
check(!err);
|
||||
cl_assert(!err);
|
||||
err = reftable_iterator_seek_ref(&it, "a");
|
||||
check(!err);
|
||||
cl_assert(!err);
|
||||
|
||||
err = reftable_iterator_next_ref(&it, &ref);
|
||||
check(!err);
|
||||
check(reftable_ref_record_equal(&r2[0], &ref, REFTABLE_HASH_SIZE_SHA1));
|
||||
cl_assert(!err);
|
||||
cl_assert(reftable_ref_record_equal(&r2[0], &ref,
|
||||
REFTABLE_HASH_SIZE_SHA1) != 0);
|
||||
reftable_ref_record_release(&ref);
|
||||
reftable_iterator_destroy(&it);
|
||||
tables_destroy(tables, 3);
|
||||
|
|
@ -101,7 +102,7 @@ static void t_merged_single_record(void)
|
|||
reftable_free(bs);
|
||||
}
|
||||
|
||||
static void t_merged_refs(void)
|
||||
void test_reftable_merged__refs(void)
|
||||
{
|
||||
struct reftable_ref_record r1[] = {
|
||||
{
|
||||
|
|
@ -165,12 +166,12 @@ static void t_merged_refs(void)
|
|||
size_t i;
|
||||
|
||||
err = merged_table_init_iter(mt, &it, REFTABLE_BLOCK_TYPE_REF);
|
||||
check(!err);
|
||||
cl_assert(!err);
|
||||
err = reftable_iterator_seek_ref(&it, "a");
|
||||
check(!err);
|
||||
check_int(reftable_merged_table_hash_id(mt), ==, REFTABLE_HASH_SHA1);
|
||||
check_int(reftable_merged_table_min_update_index(mt), ==, 1);
|
||||
check_int(reftable_merged_table_max_update_index(mt), ==, 3);
|
||||
cl_assert(err == 0);
|
||||
cl_assert_equal_i(reftable_merged_table_hash_id(mt), REFTABLE_HASH_SHA1);
|
||||
cl_assert_equal_i(reftable_merged_table_min_update_index(mt), 1);
|
||||
cl_assert_equal_i(reftable_merged_table_max_update_index(mt), 3);
|
||||
|
||||
while (len < 100) { /* cap loops/recursion. */
|
||||
struct reftable_ref_record ref = { 0 };
|
||||
|
|
@ -178,15 +179,15 @@ static void t_merged_refs(void)
|
|||
if (err > 0)
|
||||
break;
|
||||
|
||||
check(!REFTABLE_ALLOC_GROW(out, len + 1, cap));
|
||||
cl_assert(REFTABLE_ALLOC_GROW(out, len + 1, cap) == 0);
|
||||
out[len++] = ref;
|
||||
}
|
||||
reftable_iterator_destroy(&it);
|
||||
|
||||
check_int(ARRAY_SIZE(want), ==, len);
|
||||
cl_assert_equal_i(ARRAY_SIZE(want), len);
|
||||
for (i = 0; i < len; i++)
|
||||
check(reftable_ref_record_equal(want[i], &out[i],
|
||||
REFTABLE_HASH_SIZE_SHA1));
|
||||
cl_assert(reftable_ref_record_equal(want[i], &out[i],
|
||||
REFTABLE_HASH_SIZE_SHA1) != 0);
|
||||
for (i = 0; i < len; i++)
|
||||
reftable_ref_record_release(&out[i]);
|
||||
reftable_free(out);
|
||||
|
|
@ -198,7 +199,7 @@ static void t_merged_refs(void)
|
|||
reftable_free(bs);
|
||||
}
|
||||
|
||||
static void t_merged_seek_multiple_times(void)
|
||||
void test_reftable_merged__seek_multiple_times(void)
|
||||
{
|
||||
struct reftable_ref_record r1[] = {
|
||||
{
|
||||
|
|
@ -248,20 +249,17 @@ static void t_merged_seek_multiple_times(void)
|
|||
|
||||
for (size_t i = 0; i < 5; i++) {
|
||||
int err = reftable_iterator_seek_ref(&it, "c");
|
||||
check(!err);
|
||||
cl_assert(!err);
|
||||
|
||||
err = reftable_iterator_next_ref(&it, &rec);
|
||||
check(!err);
|
||||
err = reftable_ref_record_equal(&rec, &r1[1], REFTABLE_HASH_SIZE_SHA1);
|
||||
check(err == 1);
|
||||
cl_assert(reftable_iterator_next_ref(&it, &rec) == 0);
|
||||
cl_assert_equal_i(reftable_ref_record_equal(&rec, &r1[1],
|
||||
REFTABLE_HASH_SIZE_SHA1), 1);
|
||||
|
||||
err = reftable_iterator_next_ref(&it, &rec);
|
||||
check(!err);
|
||||
err = reftable_ref_record_equal(&rec, &r2[1], REFTABLE_HASH_SIZE_SHA1);
|
||||
check(err == 1);
|
||||
cl_assert(reftable_iterator_next_ref(&it, &rec) == 0);
|
||||
cl_assert_equal_i(reftable_ref_record_equal(&rec, &r2[1],
|
||||
REFTABLE_HASH_SIZE_SHA1), 1);
|
||||
|
||||
err = reftable_iterator_next_ref(&it, &rec);
|
||||
check(err > 0);
|
||||
cl_assert(reftable_iterator_next_ref(&it, &rec) > 0);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < ARRAY_SIZE(bufs); i++)
|
||||
|
|
@ -273,7 +271,7 @@ static void t_merged_seek_multiple_times(void)
|
|||
reftable_free(sources);
|
||||
}
|
||||
|
||||
static void t_merged_seek_multiple_times_without_draining(void)
|
||||
void test_reftable_merged__seek_multiple_times_no_drain(void)
|
||||
{
|
||||
struct reftable_ref_record r1[] = {
|
||||
{
|
||||
|
|
@ -317,24 +315,19 @@ static void t_merged_seek_multiple_times_without_draining(void)
|
|||
struct reftable_ref_record rec = { 0 };
|
||||
struct reftable_iterator it = { 0 };
|
||||
struct reftable_merged_table *mt;
|
||||
int err;
|
||||
|
||||
mt = merged_table_from_records(refs, &sources, &tables, sizes, bufs, 2);
|
||||
merged_table_init_iter(mt, &it, REFTABLE_BLOCK_TYPE_REF);
|
||||
|
||||
err = reftable_iterator_seek_ref(&it, "b");
|
||||
check(!err);
|
||||
err = reftable_iterator_next_ref(&it, &rec);
|
||||
check(!err);
|
||||
err = reftable_ref_record_equal(&rec, &r2[0], REFTABLE_HASH_SIZE_SHA1);
|
||||
check(err == 1);
|
||||
cl_assert(reftable_iterator_seek_ref(&it, "b") == 0);
|
||||
cl_assert(reftable_iterator_next_ref(&it, &rec) == 0);
|
||||
cl_assert_equal_i(reftable_ref_record_equal(&rec, &r2[0],
|
||||
REFTABLE_HASH_SIZE_SHA1), 1);
|
||||
|
||||
err = reftable_iterator_seek_ref(&it, "a");
|
||||
check(!err);
|
||||
err = reftable_iterator_next_ref(&it, &rec);
|
||||
check(!err);
|
||||
err = reftable_ref_record_equal(&rec, &r1[0], REFTABLE_HASH_SIZE_SHA1);
|
||||
check(err == 1);
|
||||
cl_assert(reftable_iterator_seek_ref(&it, "a") == 0);
|
||||
cl_assert(reftable_iterator_next_ref(&it, &rec) == 0);
|
||||
cl_assert_equal_i(reftable_ref_record_equal(&rec, &r1[0],
|
||||
REFTABLE_HASH_SIZE_SHA1), 1);
|
||||
|
||||
for (size_t i = 0; i < ARRAY_SIZE(bufs); i++)
|
||||
reftable_buf_release(&bufs[i]);
|
||||
|
|
@ -359,25 +352,25 @@ merged_table_from_log_records(struct reftable_log_record **logs,
|
|||
int err;
|
||||
|
||||
REFTABLE_CALLOC_ARRAY(*tables, n);
|
||||
check(*tables != NULL);
|
||||
cl_assert(*tables != NULL);
|
||||
REFTABLE_CALLOC_ARRAY(*source, n);
|
||||
check(*source != NULL);
|
||||
cl_assert(*source != NULL);
|
||||
|
||||
for (size_t i = 0; i < n; i++) {
|
||||