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
10 #include "reftable/basics.h"
12 struct integer_needle_lesseq_args
{
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 };
44 for (size_t i
= 0; i
< ARRAY_SIZE(testcases
); i
++) {
45 struct integer_needle_lesseq_args args
= {
47 .needle
= testcases
[i
].needle
,
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";
76 parse_names(in1
, strlen(in1
), &out
);
77 check_str(out
[0], "line");
81 parse_names(in2
, strlen(in2
), &out
);
82 check_str(out
[0], "a");
83 check_str(out
[1], "b");
84 check_str(out
[2], "c");
89 if_test ("parse_names drops empty string") {
90 char in
[] = "a\n\nb\n";
92 parse_names(in
, strlen(in
), &out
);
93 check_str(out
[0], "a");
94 /* simply '\n' should be dropped as empty string */
95 check_str(out
[1], "b");
100 if_test ("common_prefix_size works") {
101 struct strbuf a
= STRBUF_INIT
;
102 struct strbuf b
= STRBUF_INIT
;
107 {"abcdef", "abc", 3},
114 for (size_t i
= 0; i
< ARRAY_SIZE(cases
); i
++) {
115 strbuf_addstr(&a
, cases
[i
].a
);
116 strbuf_addstr(&b
, cases
[i
].b
);
117 check_int(common_prefix_size(&a
, &b
), ==, cases
[i
].want
);
125 if_test ("put_be24 and get_be24 work") {
126 uint32_t in
= 0x112233;
130 out
= get_be24(dest
);
131 check_int(in
, ==, out
);
134 if_test ("put_be16 and get_be16 work") {
135 uint32_t in
= 0xfef1;
139 out
= get_be16(dest
);
140 check_int(in
, ==, out
);