| 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 "unit-test.h" |
| 10 | #include "lib-reftable.h" |
| 11 | #include "reftable/basics.h" |
| 12 | #include "reftable/reftable-error.h" |
| 13 | |
| 14 | struct integer_needle_lesseq_args { |
| 15 | int needle; |
| 16 | int *haystack; |
| 17 | }; |
| 18 | |
| 19 | static int integer_needle_lesseq(size_t i, void *_args) |
| 20 | { |
| 21 | struct integer_needle_lesseq_args *args = _args; |
| 22 | return args->needle <= args->haystack[i]; |
| 23 | } |
| 24 | |
| 25 | static void *realloc_stub(void *p UNUSED, size_t size UNUSED) |
| 26 | { |
| 27 | return NULL; |
| 28 | } |
| 29 | |
| 30 | void test_reftable_basics__binsearch(void) |
| 31 | { |
| 32 | int haystack[] = { 2, 4, 6, 8, 10 }; |
| 33 | struct { |
| 34 | int needle; |
| 35 | size_t expected_idx; |
| 36 | } testcases[] = { |
| 37 | {-9000, 0}, |
| 38 | {-1, 0}, |
| 39 | {0, 0}, |
| 40 | {2, 0}, |
| 41 | {3, 1}, |
| 42 | {4, 1}, |
| 43 | {7, 3}, |
| 44 | {9, 4}, |
| 45 | {10, 4}, |
| 46 | {11, 5}, |
| 47 | {9000, 5}, |
| 48 | }; |
| 49 | |
| 50 | for (size_t i = 0; i < ARRAY_SIZE(testcases); i++) { |
| 51 | struct integer_needle_lesseq_args args = { |
| 52 | .haystack = haystack, |
| 53 | .needle = testcases[i].needle, |
| 54 | }; |
| 55 | size_t idx; |
| 56 | |
| 57 | idx = binsearch(ARRAY_SIZE(haystack), |
| 58 | &integer_needle_lesseq, &args); |
| 59 | cl_assert_equal_i(idx, testcases[i].expected_idx); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | static int unreachable_lesseq(size_t i UNUSED, void *args UNUSED) |
| 64 | { |
| 65 | cl_fail("comparison function called for empty range"); |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | void test_reftable_basics__binsearch_empty(void) |
| 70 | { |
| 71 | cl_assert_equal_i(binsearch(0, &unreachable_lesseq, NULL), 0); |
| 72 | } |
| 73 | |
| 74 | void test_reftable_basics__names_length(void) |
| 75 | { |
| 76 | const char *a[] = { "a", "b", NULL }; |
| 77 | cl_assert_equal_i(names_length(a), 2); |
| 78 | } |
| 79 | |
| 80 | void test_reftable_basics__names_equal(void) |
| 81 | { |
| 82 | const char *a[] = { "a", "b", "c", NULL }; |
| 83 | const char *b[] = { "a", "b", "d", NULL }; |
| 84 | const char *c[] = { "a", "b", NULL }; |
| 85 | |
| 86 | cl_assert(names_equal(a, a)); |
| 87 | cl_assert(!names_equal(a, b)); |
| 88 | cl_assert(!names_equal(a, c)); |
| 89 | } |
| 90 | |
| 91 | void test_reftable_basics__parse_names(void) |
| 92 | { |
| 93 | char in1[] = "line\n"; |
| 94 | char in2[] = "a\nb\nc\n"; |
| 95 | char **out = NULL; |
| 96 | int err = parse_names(in1, strlen(in1), &out); |
| 97 | cl_assert(err == 0); |
| 98 | cl_assert(out != NULL); |
| 99 | cl_assert_equal_s(out[0], "line"); |
| 100 | cl_assert(!out[1]); |
| 101 | free_names(out); |
| 102 | |
| 103 | out = NULL; |
| 104 | err = parse_names(in2, strlen(in2), &out); |
| 105 | cl_assert(err == 0); |
| 106 | cl_assert(out != NULL); |
| 107 | cl_assert_equal_s(out[0], "a"); |
| 108 | cl_assert_equal_s(out[1], "b"); |
| 109 | cl_assert_equal_s(out[2], "c"); |
| 110 | cl_assert(!out[3]); |
| 111 | free_names(out); |
| 112 | } |
| 113 | |
| 114 | void test_reftable_basics__parse_names_missing_newline(void) |
| 115 | { |
| 116 | char in1[] = "line\nline2"; |
| 117 | char **out = NULL; |
| 118 | int err = parse_names(in1, strlen(in1), &out); |
| 119 | cl_assert(err == REFTABLE_FORMAT_ERROR); |
| 120 | cl_assert(out == NULL); |
| 121 | } |
| 122 | |
| 123 | void test_reftable_basics__parse_names_drop_empty_string(void) |
| 124 | { |
| 125 | char in[] = "a\n\nb\n"; |
| 126 | char **out = NULL; |
| 127 | int err = parse_names(in, strlen(in), &out); |
| 128 | cl_assert(err == 0); |
| 129 | cl_assert(out != NULL); |
| 130 | cl_assert_equal_s(out[0], "a"); |
| 131 | /* simply '\n' should be dropped as empty string */ |
| 132 | cl_assert_equal_s(out[1], "b"); |
| 133 | cl_assert(out[2] == NULL); |
| 134 | free_names(out); |
| 135 | } |
| 136 | |
| 137 | void test_reftable_basics__common_prefix_size(void) |
| 138 | { |
| 139 | struct reftable_buf a = REFTABLE_BUF_INIT; |
| 140 | struct reftable_buf b = REFTABLE_BUF_INIT; |
| 141 | struct { |
| 142 | const char *a, *b; |
| 143 | int want; |
| 144 | } cases[] = { |
| 145 | {"abcdef", "abc", 3}, |
| 146 | { "abc", "ab", 2 }, |
| 147 | { "", "abc", 0 }, |
| 148 | { "abc", "abd", 2 }, |
| 149 | { "abc", "pqr", 0 }, |
| 150 | }; |
| 151 | |
| 152 | for (size_t i = 0; i < ARRAY_SIZE(cases); i++) { |
| 153 | cl_assert_equal_i(reftable_buf_addstr(&a, cases[i].a), 0); |
| 154 | cl_assert_equal_i(reftable_buf_addstr(&b, cases[i].b), 0); |
| 155 | cl_assert_equal_i(common_prefix_size(&a, &b), cases[i].want); |
| 156 | reftable_buf_reset(&a); |
| 157 | reftable_buf_reset(&b); |
| 158 | } |
| 159 | reftable_buf_release(&a); |
| 160 | reftable_buf_release(&b); |
| 161 | } |
| 162 | |
| 163 | void test_reftable_basics__put_get_be64(void) |
| 164 | { |
| 165 | uint64_t in = 0x1122334455667788; |
| 166 | uint8_t dest[8]; |
| 167 | uint64_t out; |
| 168 | reftable_put_be64(dest, in); |
| 169 | out = reftable_get_be64(dest); |
| 170 | cl_assert(in == out); |
| 171 | } |
| 172 | |
| 173 | void test_reftable_basics__put_get_be32(void) |
| 174 | { |
| 175 | uint32_t in = 0x11223344; |
| 176 | uint8_t dest[4]; |
| 177 | uint32_t out; |
| 178 | reftable_put_be32(dest, in); |
| 179 | out = reftable_get_be32(dest); |
| 180 | cl_assert_equal_i(in, out); |
| 181 | } |
| 182 | |
| 183 | void test_reftable_basics__put_get_be24(void) |
| 184 | { |
| 185 | uint32_t in = 0x112233; |
| 186 | uint8_t dest[3]; |
| 187 | uint32_t out; |
| 188 | reftable_put_be24(dest, in); |
| 189 | out = reftable_get_be24(dest); |
| 190 | cl_assert_equal_i(in, out); |
| 191 | } |
| 192 | |
| 193 | void test_reftable_basics__put_get_be16(void) |
| 194 | { |
| 195 | uint32_t in = 0xfef1; |
| 196 | uint8_t dest[3]; |
| 197 | uint32_t out; |
| 198 | reftable_put_be16(dest, in); |
| 199 | out = reftable_get_be16(dest); |
| 200 | cl_assert_equal_i(in, out); |
| 201 | } |
| 202 | |
| 203 | void test_reftable_basics__alloc_grow(void) |
| 204 | { |
| 205 | int *arr = NULL, *old_arr; |
| 206 | size_t alloc = 0, old_alloc; |
| 207 | |
| 208 | cl_assert_equal_i(REFTABLE_ALLOC_GROW(arr, 1, alloc), 0); |
| 209 | cl_assert(arr != NULL); |
| 210 | cl_assert(alloc >= 1); |
| 211 | arr[0] = 42; |
| 212 | |
| 213 | old_alloc = alloc; |
| 214 | old_arr = arr; |
| 215 | reftable_set_alloc(NULL, realloc_stub, NULL); |
| 216 | cl_assert(REFTABLE_ALLOC_GROW(arr, old_alloc + 1, alloc)); |
| 217 | cl_assert(arr == old_arr); |
| 218 | cl_assert_equal_i(alloc, old_alloc); |
| 219 | |
| 220 | old_alloc = alloc; |
| 221 | reftable_set_alloc(NULL, NULL, NULL); |
| 222 | cl_assert_equal_i(REFTABLE_ALLOC_GROW(arr, old_alloc + 1, alloc), 0); |
| 223 | cl_assert(arr != NULL); |
| 224 | cl_assert(alloc > old_alloc); |
| 225 | arr[alloc - 1] = 42; |
| 226 | |
| 227 | reftable_free(arr); |
| 228 | } |
| 229 | |
| 230 | void test_reftable_basics__alloc_grow_or_null(void) |
| 231 | { |
| 232 | int *arr = NULL; |
| 233 | size_t alloc = 0, old_alloc; |
| 234 | |
| 235 | REFTABLE_ALLOC_GROW_OR_NULL(arr, 1, alloc); |
| 236 | cl_assert(arr != NULL); |
| 237 | cl_assert(alloc >= 1); |
| 238 | arr[0] = 42; |
| 239 | |
| 240 | old_alloc = alloc; |
| 241 | REFTABLE_ALLOC_GROW_OR_NULL(arr, old_alloc + 1, alloc); |
| 242 | cl_assert(arr != NULL); |
| 243 | cl_assert(alloc > old_alloc); |
| 244 | arr[alloc - 1] = 42; |
| 245 | |
| 246 | old_alloc = alloc; |
| 247 | reftable_set_alloc(NULL, realloc_stub, NULL); |
| 248 | REFTABLE_ALLOC_GROW_OR_NULL(arr, old_alloc + 1, alloc); |
| 249 | cl_assert(arr == NULL); |
| 250 | cl_assert_equal_i(alloc, 0); |
| 251 | reftable_set_alloc(NULL, NULL, NULL); |
| 252 | |
| 253 | reftable_free(arr); |
| 254 | } |