The eighth batch
[alt-git.git] / reftable / basics_test.c
blob997c4d9e0113ba52fcaaaa423aabe08c511322af
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 "system.h"
11 #include "basics.h"
12 #include "test_framework.h"
13 #include "reftable-tests.h"
15 struct integer_needle_lesseq_args {
16 int needle;
17 int *haystack;
20 static int integer_needle_lesseq(size_t i, void *_args)
22 struct integer_needle_lesseq_args *args = _args;
23 return args->needle <= args->haystack[i];
26 static void test_binsearch(void)
28 int haystack[] = { 2, 4, 6, 8, 10 };
29 struct {
30 int needle;
31 size_t expected_idx;
32 } testcases[] = {
33 {-9000, 0},
34 {-1, 0},
35 {0, 0},
36 {2, 0},
37 {3, 1},
38 {4, 1},
39 {7, 3},
40 {9, 4},
41 {10, 4},
42 {11, 5},
43 {9000, 5},
45 size_t i = 0;
47 for (i = 0; i < ARRAY_SIZE(testcases); i++) {
48 struct integer_needle_lesseq_args args = {
49 .haystack = haystack,
50 .needle = testcases[i].needle,
52 size_t idx;
54 idx = binsearch(ARRAY_SIZE(haystack), &integer_needle_lesseq, &args);
55 EXPECT(idx == testcases[i].expected_idx);
59 static void test_names_length(void)
61 char *a[] = { "a", "b", NULL };
62 EXPECT(names_length(a) == 2);
65 static void test_parse_names_normal(void)
67 char in[] = "a\nb\n";
68 char **out = NULL;
69 parse_names(in, strlen(in), &out);
70 EXPECT(!strcmp(out[0], "a"));
71 EXPECT(!strcmp(out[1], "b"));
72 EXPECT(!out[2]);
73 free_names(out);
76 static void test_parse_names_drop_empty(void)
78 char in[] = "a\n\n";
79 char **out = NULL;
80 parse_names(in, strlen(in), &out);
81 EXPECT(!strcmp(out[0], "a"));
82 EXPECT(!out[1]);
83 free_names(out);
86 static void test_common_prefix(void)
88 struct strbuf s1 = STRBUF_INIT;
89 struct strbuf s2 = STRBUF_INIT;
90 strbuf_addstr(&s1, "abcdef");
91 strbuf_addstr(&s2, "abc");
92 EXPECT(common_prefix_size(&s1, &s2) == 3);
93 strbuf_release(&s1);
94 strbuf_release(&s2);
97 int basics_test_main(int argc, const char *argv[])
99 RUN_TEST(test_common_prefix);
100 RUN_TEST(test_parse_names_normal);
101 RUN_TEST(test_parse_names_drop_empty);
102 RUN_TEST(test_binsearch);
103 RUN_TEST(test_names_length);
104 return 0;