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";
75 char **out
= parse_names(in1
, strlen(in1
));
77 check_str(out
[0], "line");
81 out
= parse_names(in2
, strlen(in2
));
83 check_str(out
[0], "a");
84 check_str(out
[1], "b");
85 check_str(out
[2], "c");
90 if_test ("parse_names drops empty string") {
91 char in
[] = "a\n\nb\n";
92 char **out
= parse_names(in
, strlen(in
));
94 check_str(out
[0], "a");
95 /* simply '\n' should be dropped as empty string */
96 check_str(out
[1], "b");
101 if_test ("common_prefix_size works") {
102 struct reftable_buf a
= REFTABLE_BUF_INIT
;
103 struct reftable_buf b
= REFTABLE_BUF_INIT
;
108 {"abcdef", "abc", 3},
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;
131 out
= get_be24(dest
);
132 check_int(in
, ==, out
);
135 if_test ("put_be16 and get_be16 work") {
136 uint32_t in
= 0xfef1;
140 out
= get_be16(dest
);
141 check_int(in
, ==, out
);