1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
/*
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 test_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);
check_int(idx, ==, testcases[i].expected_idx);
}
}
static void test_names_length(void)
{
const char *a[] = { "a", "b", NULL };
check_int(names_length(a), ==, 2);
}
static void test_names_equal(void)
{
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));
}
static void test_parse_names_normal(void)
{
char in1[] = "line\n";
char in2[] = "a\nb\nc";
char **out = NULL;
parse_names(in1, strlen(in1), &out);
check_str(out[0], "line");
check(!out[1]);
free_names(out);
parse_names(in2, strlen(in2), &out);
check_str(out[0], "a");
check_str(out[1], "b");
check_str(out[2], "c");
check(!out[3]);
free_names(out);
}
static void test_parse_names_drop_empty(void)
{
char in[] = "a\n\nb\n";
char **out = NULL;
parse_names(in, strlen(in), &out);
check_str(out[0], "a");
/* simply '\n' should be dropped as empty string */
check_str(out[1], "b");
check(!out[2]);
free_names(out);
}
static void test_common_prefix(void)
{
struct strbuf a = STRBUF_INIT;
struct strbuf b = STRBUF_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++) {
strbuf_addstr(&a, cases[i].a);
strbuf_addstr(&b, cases[i].b);
check_int(common_prefix_size(&a, &b), ==, cases[i].want);
strbuf_reset(&a);
strbuf_reset(&b);
}
strbuf_release(&a);
strbuf_release(&b);
}
static void test_u24_roundtrip(void)
{
uint32_t in = 0x112233;
uint8_t dest[3];
uint32_t out;
put_be24(dest, in);
out = get_be24(dest);
check_int(in, ==, out);
}
static void test_u16_roundtrip(void)
{
uint32_t in = 0xfef1;
uint8_t dest[3];
uint32_t out;
put_be16(dest, in);
out = get_be16(dest);
check_int(in, ==, out);
}
int cmd_main(int argc, const char *argv[])
{
TEST(test_common_prefix(), "common_prefix_size works");
TEST(test_parse_names_normal(), "parse_names works for basic input");
TEST(test_parse_names_drop_empty(), "parse_names drops empty string");
TEST(test_binsearch(), "binary search with binsearch works");
TEST(test_names_length(), "names_length retuns size of a NULL-terminated string array");
TEST(test_names_equal(), "names_equal compares NULL-terminated string arrays");
TEST(test_u24_roundtrip(), "put_be24 and get_be24 work");
TEST(test_u16_roundtrip(), "put_be16 and get_be16 work");
return test_done();
}
|