Merge branch 'jk/repack-leakfix'
[git.git] / reftable / basics_test.c
blob1fcd2297256760c9c49c3844a23e73a1e91d0c75
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 binsearch_args {
16 int key;
17 int *arr;
20 static int binsearch_func(size_t i, void *void_args)
22 struct binsearch_args *args = void_args;
24 return args->key < args->arr[i];
27 static void test_binsearch(void)
29 int arr[] = { 2, 4, 6, 8, 10 };
30 size_t sz = ARRAY_SIZE(arr);
31 struct binsearch_args args = {
32 .arr = arr,
35 int i = 0;
36 for (i = 1; i < 11; i++) {
37 int res;
38 args.key = i;
39 res = binsearch(sz, &binsearch_func, &args);
41 if (res < sz) {
42 EXPECT(args.key < arr[res]);
43 if (res > 0) {
44 EXPECT(args.key >= arr[res - 1]);
46 } else {
47 EXPECT(args.key == 10 || args.key == 11);
52 static void test_names_length(void)
54 char *a[] = { "a", "b", NULL };
55 EXPECT(names_length(a) == 2);
58 static void test_parse_names_normal(void)
60 char in[] = "a\nb\n";
61 char **out = NULL;
62 parse_names(in, strlen(in), &out);
63 EXPECT(!strcmp(out[0], "a"));
64 EXPECT(!strcmp(out[1], "b"));
65 EXPECT(!out[2]);
66 free_names(out);
69 static void test_parse_names_drop_empty(void)
71 char in[] = "a\n\n";
72 char **out = NULL;
73 parse_names(in, strlen(in), &out);
74 EXPECT(!strcmp(out[0], "a"));
75 EXPECT(!out[1]);
76 free_names(out);
79 static void test_common_prefix(void)
81 struct strbuf s1 = STRBUF_INIT;
82 struct strbuf s2 = STRBUF_INIT;
83 strbuf_addstr(&s1, "abcdef");
84 strbuf_addstr(&s2, "abc");
85 EXPECT(common_prefix_size(&s1, &s2) == 3);
86 strbuf_release(&s1);
87 strbuf_release(&s2);
90 int basics_test_main(int argc, const char *argv[])
92 RUN_TEST(test_common_prefix);
93 RUN_TEST(test_parse_names_normal);
94 RUN_TEST(test_parse_names_drop_empty);
95 RUN_TEST(test_binsearch);
96 RUN_TEST(test_names_length);
97 return 0;