The twelfth batch
[git/gitster.git] / t / unit-tests / t-reftable-basics.c
blob65d50df0916f046696116a3d29bc97cc8bb44e8d
1 /*
2 Copyright 2020 Google LLC
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 */
9 #include "test-lib.h"
10 #include "reftable/basics.h"
12 struct integer_needle_lesseq_args {
13 int needle;
14 int *haystack;
17 static int integer_needle_lesseq(size_t i, void *_args)
19 struct integer_needle_lesseq_args *args = _args;
20 return args->needle <= args->haystack[i];
23 int cmd_main(int argc UNUSED, const char *argv[] UNUSED)
25 if_test ("binary search with binsearch works") {
26 int haystack[] = { 2, 4, 6, 8, 10 };
27 struct {
28 int needle;
29 size_t expected_idx;
30 } testcases[] = {
31 {-9000, 0},
32 {-1, 0},
33 {0, 0},
34 {2, 0},
35 {3, 1},
36 {4, 1},
37 {7, 3},
38 {9, 4},
39 {10, 4},
40 {11, 5},
41 {9000, 5},
44 for (size_t i = 0; i < ARRAY_SIZE(testcases); i++) {
45 struct integer_needle_lesseq_args args = {
46 .haystack = haystack,
47 .needle = testcases[i].needle,
49 size_t idx;
51 idx = binsearch(ARRAY_SIZE(haystack),
52 &integer_needle_lesseq, &args);
53 check_int(idx, ==, testcases[i].expected_idx);
57 if_test ("names_length returns size of a NULL-terminated string array") {
58 const char *a[] = { "a", "b", NULL };
59 check_int(names_length(a), ==, 2);
62 if_test ("names_equal compares NULL-terminated string arrays") {
63 const char *a[] = { "a", "b", "c", NULL };
64 const char *b[] = { "a", "b", "d", NULL };
65 const char *c[] = { "a", "b", NULL };
67 check(names_equal(a, a));
68 check(!names_equal(a, b));
69 check(!names_equal(a, c));
72 if_test ("parse_names works for basic input") {
73 char in1[] = "line\n";
74 char in2[] = "a\nb\nc";
75 char **out = parse_names(in1, strlen(in1));
76 check(out != NULL);
77 check_str(out[0], "line");
78 check(!out[1]);
79 free_names(out);
81 out = parse_names(in2, strlen(in2));
82 check(out != NULL);
83 check_str(out[0], "a");
84 check_str(out[1], "b");
85 check_str(out[2], "c");
86 check(!out[3]);
87 free_names(out);
90 if_test ("parse_names drops empty string") {
91 char in[] = "a\n\nb\n";
92 char **out = parse_names(in, strlen(in));
93 check(out != NULL);
94 check_str(out[0], "a");
95 /* simply '\n' should be dropped as empty string */
96 check_str(out[1], "b");
97 check(!out[2]);
98 free_names(out);
101 if_test ("common_prefix_size works") {
102 struct reftable_buf a = REFTABLE_BUF_INIT;
103 struct reftable_buf b = REFTABLE_BUF_INIT;
104 struct {
105 const char *a, *b;
106 int want;
107 } cases[] = {
108 {"abcdef", "abc", 3},
109 { "abc", "ab", 2 },
110 { "", "abc", 0 },
111 { "abc", "abd", 2 },
112 { "abc", "pqr", 0 },
115 for (size_t i = 0; i < ARRAY_SIZE(cases); i++) {
116 check(!reftable_buf_addstr(&a, cases[i].a));
117 check(!reftable_buf_addstr(&b, cases[i].b));
118 check_int(common_prefix_size(&a, &b), ==, cases[i].want);
119 reftable_buf_reset(&a);
120 reftable_buf_reset(&b);
122 reftable_buf_release(&a);
123 reftable_buf_release(&b);
126 if_test ("put_be24 and get_be24 work") {
127 uint32_t in = 0x112233;
128 uint8_t dest[3];
129 uint32_t out;
130 put_be24(dest, in);
131 out = get_be24(dest);
132 check_int(in, ==, out);
135 if_test ("put_be16 and get_be16 work") {
136 uint32_t in = 0xfef1;
137 uint8_t dest[3];
138 uint32_t out;
139 put_be16(dest, in);
140 out = get_be16(dest);
141 check_int(in, ==, out);
144 return test_done();