]> git.ipfire.org Git - thirdparty/git.git/blame - reftable/basics_test.c
Merge branch 'ds/fetch-config-parse-microfix'
[thirdparty/git.git] / reftable / basics_test.c
CommitLineData
ef8a6c62
HWN
1/*
2Copyright 2020 Google LLC
3
4Use of this source code is governed by a BSD-style
5license that can be found in the LICENSE file or at
6https://developers.google.com/open-source/licenses/bsd
7*/
8
9#include "system.h"
10
11#include "basics.h"
12#include "test_framework.h"
13#include "reftable-tests.h"
14
e8b80825
PS
15struct integer_needle_lesseq_args {
16 int needle;
17 int *haystack;
ef8a6c62
HWN
18};
19
e8b80825 20static int integer_needle_lesseq(size_t i, void *_args)
ef8a6c62 21{
e8b80825
PS
22 struct integer_needle_lesseq_args *args = _args;
23 return args->needle <= args->haystack[i];
ef8a6c62
HWN
24}
25
26static void test_binsearch(void)
27{
e8b80825
PS
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},
ef8a6c62 44 };
e8b80825 45 size_t i = 0;
ef8a6c62 46
e8b80825
PS
47 for (i = 0; i < ARRAY_SIZE(testcases); i++) {
48 struct integer_needle_lesseq_args args = {
49 .haystack = haystack,
50 .needle = testcases[i].needle,
51 };
52 size_t idx;
ef8a6c62 53
e8b80825
PS
54 idx = binsearch(ARRAY_SIZE(haystack), &integer_needle_lesseq, &args);
55 EXPECT(idx == testcases[i].expected_idx);
ef8a6c62
HWN
56 }
57}
58
59static void test_names_length(void)
60{
61 char *a[] = { "a", "b", NULL };
62 EXPECT(names_length(a) == 2);
63}
64
65static void test_parse_names_normal(void)
66{
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);
74}
75
76static void test_parse_names_drop_empty(void)
77{
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);
84}
85
86static void test_common_prefix(void)
87{
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);
95}
96
97int basics_test_main(int argc, const char *argv[])
98{
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;
105}