reftable: utility functions

This commit provides basic utility classes for the reftable library. Signed-off-by: Han-Wen Nienhuys <hanwen@google.com> Helped-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Han-Wen Nienhuys committed Oct 7, 2021 at 20:25 UTC ef8a6c62687984f2562463286e60ec1c66242b5c
16 files changed +570 -7
Makefile
+22 -3
@@ -743,6 +743,7 @@ TEST_BUILTINS_OBJS += test-read-cache.o
743 TEST_BUILTINS_OBJS += test-read-graph.o
744 TEST_BUILTINS_OBJS += test-read-midx.o
745 TEST_BUILTINS_OBJS += test-ref-store.o
746 +TEST_BUILTINS_OBJS += test-reftable.o
747 TEST_BUILTINS_OBJS += test-regex.o
748 TEST_BUILTINS_OBJS += test-repository.o
749 TEST_BUILTINS_OBJS += test-revision-walking.o
@@ -821,6 +822,8 @@ TEST_SHELL_PATH = $(SHELL_PATH)
822
823 LIB_FILE = libgit.a
824 XDIFF_LIB = xdiff/lib.a
825 +REFTABLE_LIB = reftable/libreftable.a
826 +REFTABLE_TEST_LIB = reftable/libreftable_test.a
827
828 GENERATED_H += command-list.h
829 GENERATED_H += config-list.h
@@ -1195,7 +1198,7 @@ THIRD_PARTY_SOURCES += compat/regex/%
1198 THIRD_PARTY_SOURCES += sha1collisiondetection/%
1199 THIRD_PARTY_SOURCES += sha1dc/%
1200
1198 -GITLIBS = common-main.o $(LIB_FILE) $(XDIFF_LIB)
1201 +GITLIBS = common-main.o $(LIB_FILE) $(XDIFF_LIB) $(REFTABLE_LIB)
1202 EXTLIBS =
1203
1204 GIT_USER_AGENT = git/$(GIT_VERSION)
@@ -2446,7 +2449,15 @@ XDIFF_OBJS += xdiff/xutils.o
2449 .PHONY: xdiff-objs
2450 xdiff-objs: $(XDIFF_OBJS)
2451
2452 +REFTABLE_OBJS += reftable/basics.o
2453 +REFTABLE_OBJS += reftable/error.o
2454 +REFTABLE_OBJS += reftable/publicbasics.o
2455 +
2456 +REFTABLE_TEST_OBJS += reftable/test_framework.o
2457 +REFTABLE_TEST_OBJS += reftable/basics_test.o
2458 +
2459 TEST_OBJS := $(patsubst %$X,%.o,$(TEST_PROGRAMS)) $(patsubst %,t/helper/%,$(TEST_BUILTINS_OBJS))
2460 +
2461 .PHONY: test-objs
2462 test-objs: $(TEST_OBJS)
2463
@@ -2462,6 +2473,8 @@ OBJECTS += $(PROGRAM_OBJS)
2473 OBJECTS += $(TEST_OBJS)
2474 OBJECTS += $(XDIFF_OBJS)
2475 OBJECTS += $(FUZZ_OBJS)
2476 +OBJECTS += $(REFTABLE_OBJS) $(REFTABLE_TEST_OBJS)
2477 +
2478 ifndef NO_CURL
2479 OBJECTS += http.o http-walker.o remote-curl.o
2480 endif
@@ -2612,6 +2625,12 @@ $(LIB_FILE): $(LIB_OBJS)
2625 $(XDIFF_LIB): $(XDIFF_OBJS)
2626 $(QUIET_AR)$(AR) $(ARFLAGS) $@ $^
2627
2628 +$(REFTABLE_LIB): $(REFTABLE_OBJS)
2629 + $(QUIET_AR)$(RM) $@ && $(AR) $(ARFLAGS) $@ $^
2630 +
2631 +$(REFTABLE_TEST_LIB): $(REFTABLE_TEST_OBJS)
2632 + $(QUIET_AR)$(RM) $@ && $(AR) $(ARFLAGS) $@ $^
2633 +
2634 export DEFAULT_EDITOR DEFAULT_PAGER
2635
2636 Documentation/GIT-EXCLUDED-PROGRAMS: FORCE
@@ -2904,7 +2923,7 @@ perf: all
2923
2924 t/helper/test-tool$X: $(patsubst %,t/helper/%,$(TEST_BUILTINS_OBJS))
2925
2907 -t/helper/test-%$X: t/helper/test-%.o GIT-LDFLAGS $(GITLIBS)
2926 +t/helper/test-%$X: t/helper/test-%.o GIT-LDFLAGS $(GITLIBS) $(REFTABLE_TEST_LIB)
2927 $(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(filter %.a,$^) $(LIBS)
2928
2929 check-sha1:: t/helper/test-tool$X
@@ -3234,7 +3253,7 @@ cocciclean:
3253 clean: profile-clean coverage-clean cocciclean
3254 $(RM) *.res
3255 $(RM) $(OBJECTS)
3237 - $(RM) $(LIB_FILE) $(XDIFF_LIB)
3256 + $(RM) $(LIB_FILE) $(XDIFF_LIB) $(REFTABLE_LIB) $(REFTABLE_TEST_LIB)
3257 $(RM) $(ALL_PROGRAMS) $(SCRIPT_LIB) $(BUILT_INS) git$X
3258 $(RM) $(TEST_PROGRAMS)
3259 $(RM) $(FUZZ_PROGRAMS)
contrib/buildsystems/CMakeLists.txt
+12 -2
@@ -640,6 +640,12 @@ parse_makefile_for_sources(libxdiff_SOURCES "XDIFF_OBJS")
640 list(TRANSFORM libxdiff_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
641 add_library(xdiff STATIC ${libxdiff_SOURCES})
642
643 +#reftable
644 +parse_makefile_for_sources(reftable_SOURCES "REFTABLE_OBJS")
645 +
646 +list(TRANSFORM reftable_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
647 +add_library(reftable STATIC ${reftable_SOURCES})
648 +
649 if(WIN32)
650 if(NOT MSVC)#use windres when compiling with gcc and clang
651 add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/git.res
@@ -662,7 +668,7 @@ endif()
668 #link all required libraries to common-main
669 add_library(common-main OBJECT ${CMAKE_SOURCE_DIR}/common-main.c)
670
665 -target_link_libraries(common-main libgit xdiff ${ZLIB_LIBRARIES})
671 +target_link_libraries(common-main libgit xdiff reftable ${ZLIB_LIBRARIES})
672 if(Intl_FOUND)
673 target_link_libraries(common-main ${Intl_LIBRARIES})
674 endif()
@@ -902,11 +908,15 @@ if(BUILD_TESTING)
908 add_executable(test-fake-ssh ${CMAKE_SOURCE_DIR}/t/helper/test-fake-ssh.c)
909 target_link_libraries(test-fake-ssh common-main)
910
911 +#reftable-tests
912 +parse_makefile_for_sources(test-reftable_SOURCES "REFTABLE_TEST_OBJS")
913 +list(TRANSFORM test-reftable_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/")
914 +
915 #test-tool
916 parse_makefile_for_sources(test-tool_SOURCES "TEST_BUILTINS_OBJS")
917
918 list(TRANSFORM test-tool_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/t/helper/")
909 -add_executable(test-tool ${CMAKE_SOURCE_DIR}/t/helper/test-tool.c ${test-tool_SOURCES})
919 +add_executable(test-tool ${CMAKE_SOURCE_DIR}/t/helper/test-tool.c ${test-tool_SOURCES} ${test-reftable_SOURCES})
920 target_link_libraries(test-tool common-main)
921
922 set_target_properties(test-fake-ssh test-tool
contrib/buildsystems/Generators/Vcxproj.pm
+10 -1
@@ -77,7 +77,7 @@ sub createProject {
77 my $libs_release = "\n ";
78 my $libs_debug = "\n ";
79 if (!$static_library) {
80 - $libs_release = join(";", sort(grep /^(?!libgit\.lib|xdiff\/lib\.lib|vcs-svn\/lib\.lib)/, @{$$build_structure{"$prefix${name}_LIBS"}}));
80 + $libs_release = join(";", sort(grep /^(?!libgit\.lib|xdiff\/lib\.lib|vcs-svn\/lib\.lib|reftable\/libreftable\.lib)/, @{$$build_structure{"$prefix${name}_LIBS"}}));
81 $libs_debug = $libs_release;
82 $libs_debug =~ s/zlib\.lib/zlibd\.lib/g;
83 $libs_debug =~ s/libexpat\.lib/libexpatd\.lib/g;
@@ -232,6 +232,7 @@ EOM
232 EOM
233 if (!$static_library || $target =~ 'vcs-svn' || $target =~ 'xdiff') {
234 my $uuid_libgit = $$build_structure{"LIBS_libgit_GUID"};
235 + my $uuid_libreftable = $$build_structure{"LIBS_reftable/libreftable_GUID"};
236 my $uuid_xdiff_lib = $$build_structure{"LIBS_xdiff/lib_GUID"};
237
238 print F << "EOM";
@@ -241,6 +242,14 @@ EOM
242 <ReferenceOutputAssembly>false</ReferenceOutputAssembly>
243 </ProjectReference>
244 EOM
245 + if (!($name =~ /xdiff|libreftable/)) {
246 + print F << "EOM";
247 + <ProjectReference Include="$cdup\\reftable\\libreftable\\libreftable.vcxproj">
248 + <Project>$uuid_libreftable</Project>
249 + <ReferenceOutputAssembly>false</ReferenceOutputAssembly>
250 + </ProjectReference>
251 +EOM
252 + }
253 if (!($name =~ 'xdiff')) {
254 print F << "EOM";
255 <ProjectReference Include="$cdup\\xdiff\\lib\\xdiff_lib.vcxproj">
reftable/basics.c new
+128
@@ -0,0 +1,128 @@
1 +/*
2 +Copyright 2020 Google LLC
3 +
4 +Use of this source code is governed by a BSD-style
5 +license that can be found in the LICENSE file or at
6 +https://developers.google.com/open-source/licenses/bsd
7 +*/
8 +
9 +#include "basics.h"
10 +
11 +void put_be24(uint8_t *out, uint32_t i)
12 +{
13 + out[0] = (uint8_t)((i >> 16) & 0xff);
14 + out[1] = (uint8_t)((i >> 8) & 0xff);
15 + out[2] = (uint8_t)(i & 0xff);
16 +}
17 +
18 +uint32_t get_be24(uint8_t *in)
19 +{
20 + return (uint32_t)(in[0]) << 16 | (uint32_t)(in[1]) << 8 |
21 + (uint32_t)(in[2]);
22 +}
23 +
24 +void put_be16(uint8_t *out, uint16_t i)
25 +{
26 + out[0] = (uint8_t)((i >> 8) & 0xff);
27 + out[1] = (uint8_t)(i & 0xff);
28 +}
29 +
30 +int binsearch(size_t sz, int (*f)(size_t k, void *args), void *args)
31 +{
32 + size_t lo = 0;
33 + size_t hi = sz;
34 +
35 + /* Invariants:
36 + *
37 + * (hi == sz) || f(hi) == true
38 + * (lo == 0 && f(0) == true) || fi(lo) == false
39 + */
40 + while (hi - lo > 1) {
41 + size_t mid = lo + (hi - lo) / 2;
42 +
43 + if (f(mid, args))
44 + hi = mid;
45 + else
46 + lo = mid;
47 + }
48 +
49 + if (lo)
50 + return hi;
51 +
52 + return f(0, args) ? 0 : 1;
53 +}
54 +
55 +void free_names(char **a)
56 +{
57 + char **p;
58 + if (!a) {
59 + return;
60 + }
61 + for (p = a; *p; p++) {
62 + reftable_free(*p);
63 + }
64 + reftable_free(a);
65 +}
66 +
67 +int names_length(char **names)
68 +{
69 + char **p = names;
70 + for (; *p; p++) {
71 + /* empty */
72 + }
73 + return p - names;
74 +}
75 +
76 +void parse_names(char *buf, int size, char ***namesp)
77 +{
78 + char **names = NULL;
79 + size_t names_cap = 0;
80 + size_t names_len = 0;
81 +
82 + char *p = buf;
83 + char *end = buf + size;
84 + while (p < end) {
85 + char *next = strchr(p, '\n');
86 + if (next && next < end) {
87 + *next = 0;
88 + } else {
89 + next = end;
90 + }
91 + if (p < next) {
92 + if (names_len == names_cap) {
93 + names_cap = 2 * names_cap + 1;
94 + names = reftable_realloc(
95 + names, names_cap * sizeof(*names));
96 + }
97 + names[names_len++] = xstrdup(p);
98 + }
99 + p = next + 1;
100 + }
101 +
102 + names = reftable_realloc(names, (names_len + 1) * sizeof(*names));
103 + names[names_len] = NULL;
104 + *namesp = names;
105 +}
106 +
107 +int names_equal(char **a, char **b)
108 +{
109 + int i = 0;
110 + for (; a[i] && b[i]; i++) {
111 + if (strcmp(a[i], b[i])) {
112 + return 0;
113 + }
114 + }
115 +
116 + return a[i] == b[i];
117 +}
118 +
119 +int common_prefix_size(struct strbuf *a, struct strbuf *b)
120 +{
121 + int p = 0;
122 + for (; p < a->len && p < b->len; p++) {
123 + if (a->buf[p] != b->buf[p])
124 + break;
125 + }
126 +
127 + return p;
128 +}
reftable/basics.h new
+60
@@ -0,0 +1,60 @@
1 +/*
2 +Copyright 2020 Google LLC
3 +
4 +Use of this source code is governed by a BSD-style
5 +license that can be found in the LICENSE file or at
6 +https://developers.google.com/open-source/licenses/bsd
7 +*/
8 +
9 +#ifndef BASICS_H
10 +#define BASICS_H
11 +
12 +/*
13 + * miscellaneous utilities that are not provided by Git.
14 + */
15 +
16 +#include "system.h"
17 +
18 +/* Bigendian en/decoding of integers */
19 +
20 +void put_be24(uint8_t *out, uint32_t i);
21 +uint32_t get_be24(uint8_t *in);
22 +void put_be16(uint8_t *out, uint16_t i);
23 +
24 +/*
25 + * find smallest index i in [0, sz) at which f(i) is true, assuming
26 + * that f is ascending. Return sz if f(i) is false for all indices.
27 + *
28 + * Contrary to bsearch(3), this returns something useful if the argument is not
29 + * found.
30 + */
31 +int binsearch(size_t sz, int (*f)(size_t k, void *args), void *args);
32 +
33 +/*
34 + * Frees a NULL terminated array of malloced strings. The array itself is also
35 + * freed.
36 + */
37 +void free_names(char **a);
38 +
39 +/* parse a newline separated list of names. `size` is the length of the buffer,
40 + * without terminating '\0'. Empty names are discarded. */
41 +void parse_names(char *buf, int size, char ***namesp);
42 +
43 +/* compares two NULL-terminated arrays of strings. */
44 +int names_equal(char **a, char **b);
45 +
46 +/* returns the array size of a NULL-terminated array of strings. */
47 +int names_length(char **names);
48 +
49 +/* Allocation routines; they invoke the functions set through
50 + * reftable_set_alloc() */
51 +void *reftable_malloc(size_t sz);
52 +void *reftable_realloc(void *p, size_t sz);
53 +void reftable_free(void *p);
54 +void *reftable_calloc(size_t sz);
55 +
56 +/* Find the longest shared prefix size of `a` and `b` */
57 +struct strbuf;
58 +int common_prefix_size(struct strbuf *a, struct strbuf *b);
59 +
60 +#endif
reftable/basics_test.c new
+98
@@ -0,0 +1,98 @@
1 +/*
2 +Copyright 2020 Google LLC
3 +
4 +Use of this source code is governed by a BSD-style
5 +license that can be found in the LICENSE file or at
6 +https://developers.google.com/open-source/licenses/bsd
7 +*/
8 +
9 +#include "system.h"
10 +
11 +#include "basics.h"
12 +#include "test_framework.h"
13 +#include "reftable-tests.h"
14 +
15 +struct binsearch_args {
16 + int key;
17 + int *arr;
18 +};
19 +
20 +static int binsearch_func(size_t i, void *void_args)
21 +{
22 + struct binsearch_args *args = void_args;
23 +
24 + return args->key < args->arr[i];
25 +}
26 +
27 +static void test_binsearch(void)
28 +{
29 + int arr[] = { 2, 4, 6, 8, 10 };
30 + size_t sz = ARRAY_SIZE(arr);
31 + struct binsearch_args args = {
32 + .arr = arr,
33 + };
34 +
35 + int i = 0;
36 + for (i = 1; i < 11; i++) {
37 + int res;
38 + args.key = i;
39 + res = binsearch(sz, &binsearch_func, &args);
40 +
41 + if (res < sz) {
42 + EXPECT(args.key < arr[res]);
43 + if (res > 0) {
44 + EXPECT(args.key >= arr[res - 1]);
45 + }
46 + } else {
47 + EXPECT(args.key == 10 || args.key == 11);
48 + }
49 + }
50 +}
51 +
52 +static void test_names_length(void)
53 +{
54 + char *a[] = { "a", "b", NULL };
55 + EXPECT(names_length(a) == 2);
56 +}
57 +
58 +static void test_parse_names_normal(void)
59 +{
60 + char in[] = "a\nb\n";
61 + char **out = NULL;
62 + parse_names(in, strlen(in), &out);
63 + EXPECT(!strcmp(out[0], "a"));
64 + EXPECT(!strcmp(out[1], "b"));
65 + EXPECT(!out[2]);
66 + free_names(out);
67 +}
68 +
69 +static void test_parse_names_drop_empty(void)
70 +{
71 + char in[] = "a\n\n";
72 + char **out = NULL;
73 + parse_names(in, strlen(in), &out);
74 + EXPECT(!strcmp(out[0], "a"));
75 + EXPECT(!out[1]);
76 + free_names(out);
77 +}
78 +
79 +static void test_common_prefix(void)
80 +{
81 + struct strbuf s1 = STRBUF_INIT;
82 + struct strbuf s2 = STRBUF_INIT;
83 + strbuf_addstr(&s1, "abcdef");
84 + strbuf_addstr(&s2, "abc");
85 + EXPECT(common_prefix_size(&s1, &s2) == 3);
86 + strbuf_release(&s1);
87 + strbuf_release(&s2);
88 +}
89 +
90 +int basics_test_main(int argc, const char *argv[])
91 +{
92 + RUN_TEST(test_common_prefix);
93 + RUN_TEST(test_parse_names_normal);
94 + RUN_TEST(test_parse_names_drop_empty);
95 + RUN_TEST(test_binsearch);
96 + RUN_TEST(test_names_length);
97 + return 0;
98 +}
reftable/publicbasics.c new
+65
@@ -0,0 +1,65 @@
1 +/*
2 +Copyright 2020 Google LLC
3 +
4 +Use of this source code is governed by a BSD-style
5 +license that can be found in the LICENSE file or at
6 +https://developers.google.com/open-source/licenses/bsd
7 +*/
8 +
9 +#include "reftable-malloc.h"
10 +
11 +#include "basics.h"
12 +#include "system.h"
13 +
14 +static void *(*reftable_malloc_ptr)(size_t sz);
15 +static void *(*reftable_realloc_ptr)(void *, size_t);
16 +static void (*reftable_free_ptr)(void *);
17 +
18 +void *reftable_malloc(size_t sz)
19 +{
20 + if (reftable_malloc_ptr)
21 + return (*reftable_malloc_ptr)(sz);
22 + return malloc(sz);
23 +}
24 +
25 +void *reftable_realloc(void *p, size_t sz)
26 +{
27 + if (reftable_realloc_ptr)
28 + return (*reftable_realloc_ptr)(p, sz);
29 + return realloc(p, sz);
30 +}
31 +
32 +void reftable_free(void *p)
33 +{
34 + if (reftable_free_ptr)
35 + reftable_free_ptr(p);
36 + else
37 + free(p);
38 +}
39 +
40 +void *reftable_calloc(size_t sz)
41 +{
42 + void *p = reftable_malloc(sz);
43 + memset(p, 0, sz);
44 + return p;
45 +}
46 +
47 +void reftable_set_alloc(void *(*malloc)(size_t),
48 + void *(*realloc)(void *, size_t), void (*free)(void *))
49 +{
50 + reftable_malloc_ptr = malloc;
51 + reftable_realloc_ptr = realloc;
52 + reftable_free_ptr = free;
53 +}
54 +
55 +int hash_size(uint32_t id)
56 +{
57 + switch (id) {
58 + case 0:
59 + case GIT_SHA1_FORMAT_ID:
60 + return GIT_SHA1_RAWSZ;
61 + case GIT_SHA256_FORMAT_ID:
62 + return GIT_SHA256_RAWSZ;
63 + }
64 + abort();
65 +}
reftable/reftable-malloc.h new
+18
@@ -0,0 +1,18 @@
1 +/*
2 +Copyright 2020 Google LLC
3 +
4 +Use of this source code is governed by a BSD-style
5 +license that can be found in the LICENSE file or at
6 +https://developers.google.com/open-source/licenses/bsd
7 +*/
8 +
9 +#ifndef REFTABLE_H
10 +#define REFTABLE_H
11 +
12 +#include <stddef.h>
13 +
14 +/* Overrides the functions to use for memory management. */
15 +void reftable_set_alloc(void *(*malloc)(size_t),
16 + void *(*realloc)(void *, size_t), void (*free)(void *));
17 +
18 +#endif
reftable/reftable-tests.h new
+22
@@ -0,0 +1,22 @@
1 +/*
2 +Copyright 2020 Google LLC
3 +
4 +Use of this source code is governed by a BSD-style
5 +license that can be found in the LICENSE file or at
6 +https://developers.google.com/open-source/licenses/bsd
7 +*/
8 +
9 +#ifndef REFTABLE_TESTS_H
10 +#define REFTABLE_TESTS_H
11 +
12 +int basics_test_main(int argc, const char **argv);
13 +int block_test_main(int argc, const char **argv);
14 +int merged_test_main(int argc, const char **argv);
15 +int record_test_main(int argc, const char **argv);
16 +int refname_test_main(int argc, const char **argv);
17 +int reftable_test_main(int argc, const char **argv);
18 +int stack_test_main(int argc, const char **argv);
19 +int tree_test_main(int argc, const char **argv);
20 +int reftable_dump_main(int argc, char *const *argv);
21 +
22 +#endif
reftable/system.h new
+32
@@ -0,0 +1,32 @@
1 +/*
2 +Copyright 2020 Google LLC
3 +
4 +Use of this source code is governed by a BSD-style
5 +license that can be found in the LICENSE file or at
6 +https://developers.google.com/open-source/licenses/bsd
7 +*/
8 +
9 +#ifndef SYSTEM_H
10 +#define SYSTEM_H
11 +
12 +/* This header glues the reftable library to the rest of Git */
13 +
14 +#include "git-compat-util.h"
15 +#include "strbuf.h"
16 +#include "hash.h" /* hash ID, sizes.*/
17 +#include "dir.h" /* remove_dir_recursively, for tests.*/
18 +
19 +#include <zlib.h>
20 +
21 +#ifdef NO_UNCOMPRESS2
22 +/*
23 + * This is uncompress2, which is only available in zlib >= 1.2.9
24 + * (released as of early 2017)
25 + */
26 +int uncompress2(Bytef *dest, uLongf *destLen, const Bytef *source,
27 + uLong *sourceLen);
28 +#endif
29 +
30 +int hash_size(uint32_t id);
31 +
32 +#endif
reftable/test_framework.c new
+23
@@ -0,0 +1,23 @@
1 +/*
2 +Copyright 2020 Google LLC
3 +
4 +Use of this source code is governed by a BSD-style
5 +license that can be found in the LICENSE file or at
6 +https://developers.google.com/open-source/licenses/bsd
7 +*/
8 +
9 +#include "system.h"
10 +#include "test_framework.h"
11 +
12 +#include "basics.h"
13 +
14 +void set_test_hash(uint8_t *p, int i)
15 +{
16 + memset(p, (uint8_t)i, hash_size(GIT_SHA1_FORMAT_ID));
17 +}
18 +
19 +ssize_t strbuf_add_void(void *b, const void *data, size_t sz)
20 +{
21 + strbuf_add(b, data, sz);
22 + return sz;
23 +}
reftable/test_framework.h new
+53
@@ -0,0 +1,53 @@
1 +/*
2 +Copyright 2020 Google LLC
3 +
4 +Use of this source code is governed by a BSD-style
5 +license that can be found in the LICENSE file or at
6 +https://developers.google.com/open-source/licenses/bsd
7 +*/
8 +
9 +#ifndef TEST_FRAMEWORK_H
10 +#define TEST_FRAMEWORK_H
11 +
12 +#include "system.h"
13 +#include "reftable-error.h"
14 +
15 +#define EXPECT_ERR(c) \
16 + if (c != 0) { \
17 + fflush(stderr); \
18 + fflush(stdout); \
19 + fprintf(stderr, "%s: %d: error == %d (%s), want 0\n", \
20 + __FILE__, __LINE__, c, reftable_error_str(c)); \
21 + abort(); \
22 + }
23 +
24 +#define EXPECT_STREQ(a, b) \
25 + if (strcmp(a, b)) { \
26 + fflush(stderr); \
27 + fflush(stdout); \
28 + fprintf(stderr, "%s:%d: %s (%s) != %s (%s)\n", __FILE__, \
29 + __LINE__, #a, a, #b, b); \
30 + abort(); \
31 + }
32 +
33 +#define EXPECT(c) \
34 + if (!(c)) { \
35 + fflush(stderr); \
36 + fflush(stdout); \
37 + fprintf(stderr, "%s: %d: failed assertion %s\n", __FILE__, \
38 + __LINE__, #c); \
39 + abort(); \
40 + }
41 +
42 +#define RUN_TEST(f) \
43 + fprintf(stderr, "running %s\n", #f); \
44 + fflush(stderr); \
45 + f();
46 +
47 +void set_test_hash(uint8_t *p, int i);
48 +
49 +/* Like strbuf_add, but suitable for passing to reftable_new_writer
50 + */
51 +ssize_t strbuf_add_void(void *b, const void *data, size_t sz);
52 +
53 +#endif
t/helper/test-reftable.c new
+9
@@ -0,0 +1,9 @@
1 +#include "reftable/reftable-tests.h"
2 +#include "test-tool.h"
3 +
4 +int cmd__reftable(int argc, const char **argv)
5 +{
6 + basics_test_main(argc, argv);
7 +
8 + return 0;
9 +}
t/helper/test-tool.c
+2 -1
@@ -53,13 +53,14 @@ static struct test_cmd cmds[] = {
53 { "pcre2-config", cmd__pcre2_config },
54 { "pkt-line", cmd__pkt_line },
55 { "prio-queue", cmd__prio_queue },
56 - { "proc-receive", cmd__proc_receive},
56 + { "proc-receive", cmd__proc_receive },
57 { "progress", cmd__progress },
58 { "reach", cmd__reach },
59 { "read-cache", cmd__read_cache },
60 { "read-graph", cmd__read_graph },
61 { "read-midx", cmd__read_midx },
62 { "ref-store", cmd__ref_store },
63 + { "reftable", cmd__reftable },
64 { "regex", cmd__regex },
65 { "repository", cmd__repository },
66 { "revision-walking", cmd__revision_walking },
t/helper/test-tool.h
+1
@@ -49,6 +49,7 @@ int cmd__read_cache(int argc, const char **argv);
49 int cmd__read_graph(int argc, const char **argv);
50 int cmd__read_midx(int argc, const char **argv);
51 int cmd__ref_store(int argc, const char **argv);
52 +int cmd__reftable(int argc, const char **argv);
53 int cmd__regex(int argc, const char **argv);
54 int cmd__repository(int argc, const char **argv);
55 int cmd__revision_walking(int argc, const char **argv);
t/t0032-reftable-unittest.sh new
+15
@@ -0,0 +1,15 @@
1 +#!/bin/sh
2 +#
3 +# Copyright (c) 2020 Google LLC
4 +#
5 +
6 +test_description='reftable unittests'
7 +
8 +. ./test-lib.sh
9 +
10 +test_expect_success 'unittests' '
11 + TMPDIR=$(pwd) && export TMPDIR &&
12 + test-tool reftable
13 +'
14 +
15 +test_done