]>
Commit | Line | Data |
---|---|---|
ef8a6c62 HWN |
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 "system.h" | |
10 | ||
11 | #include "basics.h" | |
12 | #include "test_framework.h" | |
13 | #include "reftable-tests.h" | |
14 | ||
15 | struct binsearch_args { | |
16 | int key; | |
17 | int *arr; | |
18 | }; | |
19 | ||
20 | static int binsearch_func(size_t i, void *void_args) | |
21 | { | |
22 | struct binsearch_args *args = void_args; | |
23 | ||
24 | return args->key < args->arr[i]; | |
25 | } | |
26 | ||
27 | static void test_binsearch(void) | |
28 | { | |
29 | int arr[] = { 2, 4, 6, 8, 10 }; | |
30 | size_t sz = ARRAY_SIZE(arr); | |
31 | struct binsearch_args args = { | |
32 | .arr = arr, | |
33 | }; | |
34 | ||
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); | |
40 | ||
41 | if (res < sz) { | |
42 | EXPECT(args.key < arr[res]); | |
43 | if (res > 0) { | |
44 | EXPECT(args.key >= arr[res - 1]); | |
45 | } | |
46 | } else { | |
47 | EXPECT(args.key == 10 || args.key == 11); | |
48 | } | |
49 | } | |
50 | } | |
51 | ||
52 | static void test_names_length(void) | |
53 | { | |
54 | char *a[] = { "a", "b", NULL }; | |
55 | EXPECT(names_length(a) == 2); | |
56 | } | |
57 | ||
58 | static void test_parse_names_normal(void) | |
59 | { | |
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); | |
67 | } | |
68 | ||
69 | static void test_parse_names_drop_empty(void) | |
70 | { | |
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); | |
77 | } | |
78 | ||
79 | static void test_common_prefix(void) | |
80 | { | |
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); | |
88 | } | |
89 | ||
90 | int basics_test_main(int argc, const char *argv[]) | |
91 | { | |
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; | |
98 | } |