t-reftable-basics: use if_test
The macro TEST takes a single expression. If a test requires multiple statements then they need to be placed in a function that's called in the TEST expression. Remove the overhead of defining and calling single-use functions by using if_test instead. Run the tests in the order of definition. We can reorder them like that because they are independent. Technically this changes the output, but retains the meaning of a full run and allows for easier review e.g. with diff option --ignore-all-space. Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Jul 30, 2024 at 16:10 UTC
2c4a6a8d9c550ca863511e9787982558d3698182
1 file changed
+106
-122
t/unit-tests/t-reftable-basics.c
+106
-122
@@ -20,141 +20,125 @@ static int integer_needle_lesseq(size_t i, void *_args)
20
return args->needle <= args->haystack[i];
21
}
22
23
-static void test_binsearch(void)
23
+int cmd_main(int argc, const char *argv[])
24
{
25
- int haystack[] = { 2, 4, 6, 8, 10 };
26
- struct {
27
- int needle;
28
- size_t expected_idx;
29
- } testcases[] = {
30
- {-9000, 0},
31
- {-1, 0},
32
- {0, 0},
33
- {2, 0},
34
- {3, 1},
35
- {4, 1},
36
- {7, 3},
37
- {9, 4},
38
- {10, 4},
39
- {11, 5},
40
- {9000, 5},
41
- };
42
-
43
- for (size_t i = 0; i < ARRAY_SIZE(testcases); i++) {
44
- struct integer_needle_lesseq_args args = {
45
- .haystack = haystack,
46
- .needle = testcases[i].needle,
25
+ if_test ("binary search with binsearch works") {
26
+ int haystack[] = { 2, 4, 6, 8, 10 };
27
+ struct {
28
+ int needle;
29
+ size_t expected_idx;
30
+ } testcases[] = {
31
+ {-9000, 0},
32
+ {-1, 0},
33
+ {0, 0},
34
+ {2, 0},
35
+ {3, 1},
36
+ {4, 1},
37
+ {7, 3},
38
+ {9, 4},
39
+ {10, 4},
40
+ {11, 5},
41
+ {9000, 5},
42
};
48
- size_t idx;
43
50
- idx = binsearch(ARRAY_SIZE(haystack), &integer_needle_lesseq, &args);
51
- check_int(idx, ==, testcases[i].expected_idx);
44
+ for (size_t i = 0; i < ARRAY_SIZE(testcases); i++) {
45
+ struct integer_needle_lesseq_args args = {
46
+ .haystack = haystack,
47
+ .needle = testcases[i].needle,
48
+ };
49
+ size_t idx;
50
+
51
+ idx = binsearch(ARRAY_SIZE(haystack),
52
+ &integer_needle_lesseq, &args);
53
+ check_int(idx, ==, testcases[i].expected_idx);
54
+ }
55
}
53
-}
56
55
-static void test_names_length(void)
56
-{
57
- const char *a[] = { "a", "b", NULL };
58
- check_int(names_length(a), ==, 2);
59
-}
60
-
61
-static void test_names_equal(void)
62
-{
63
- const char *a[] = { "a", "b", "c", NULL };
64
- const char *b[] = { "a", "b", "d", NULL };
65
- const char *c[] = { "a", "b", NULL };
57
+ if_test ("names_length retuns size of a NULL-terminated string array") {
58
+ const char *a[] = { "a", "b", NULL };
59
+ check_int(names_length(a), ==, 2);
60
+ }
61
67
- check(names_equal(a, a));
68
- check(!names_equal(a, b));
69
- check(!names_equal(a, c));
70
-}
62
+ if_test ("names_equal compares NULL-terminated string arrays") {
63
+ const char *a[] = { "a", "b", "c", NULL };
64
+ const char *b[] = { "a", "b", "d", NULL };
65
+ const char *c[] = { "a", "b", NULL };
66
72
-static void test_parse_names_normal(void)
73
-{
74
- char in1[] = "line\n";
75
- char in2[] = "a\nb\nc";
76
- char **out = NULL;
77
- parse_names(in1, strlen(in1), &out);
78
- check_str(out[0], "line");
79
- check(!out[1]);
80
- free_names(out);
81
-
82
- parse_names(in2, strlen(in2), &out);
83
- check_str(out[0], "a");
84
- check_str(out[1], "b");
85
- check_str(out[2], "c");
86
- check(!out[3]);
87
- free_names(out);
88
-}
67
+ check(names_equal(a, a));
68
+ check(!names_equal(a, b));
69
+ check(!names_equal(a, c));
70
+ }
71
90
-static void test_parse_names_drop_empty(void)
91
-{
92
- char in[] = "a\n\nb\n";
93
- char **out = NULL;
94
- parse_names(in, strlen(in), &out);
95
- check_str(out[0], "a");
96
- /* simply '\n' should be dropped as empty string */
97
- check_str(out[1], "b");
98
- check(!out[2]);
99
- free_names(out);
100
-}
72
+ if_test ("parse_names works for basic input") {
73
+ char in1[] = "line\n";
74
+ char in2[] = "a\nb\nc";
75
+ char **out = NULL;
76
+ parse_names(in1, strlen(in1), &out);
77
+ check_str(out[0], "line");
78
+ check(!out[1]);
79
+ free_names(out);
80
+
81
+ parse_names(in2, strlen(in2), &out);
82
+ check_str(out[0], "a");
83
+ check_str(out[1], "b");
84
+ check_str(out[2], "c");
85
+ check(!out[3]);
86
+ free_names(out);
87
+ }
88
102
-static void test_common_prefix(void)
103
-{
104
- struct strbuf a = STRBUF_INIT;
105
- struct strbuf b = STRBUF_INIT;
106
- struct {
107
- const char *a, *b;
108
- int want;
109
- } cases[] = {
110
- {"abcdef", "abc", 3},
111
- { "abc", "ab", 2 },
112
- { "", "abc", 0 },
113
- { "abc", "abd", 2 },
114
- { "abc", "pqr", 0 },
115
- };
116
-
117
- for (size_t i = 0; i < ARRAY_SIZE(cases); i++) {
118
- strbuf_addstr(&a, cases[i].a);
119
- strbuf_addstr(&b, cases[i].b);
120
- check_int(common_prefix_size(&a, &b), ==, cases[i].want);
121
- strbuf_reset(&a);
122
- strbuf_reset(&b);
89
+ if_test ("parse_names drops empty string") {
90
+ char in[] = "a\n\nb\n";
91
+ char **out = NULL;
92
+ parse_names(in, strlen(in), &out);
93
+ check_str(out[0], "a");
94
+ /* simply '\n' should be dropped as empty string */
95
+ check_str(out[1], "b");
96
+ check(!out[2]);
97
+ free_names(out);
98
}
124
- strbuf_release(&a);
125
- strbuf_release(&b);
126
-}
99
128
-static void test_u24_roundtrip(void)
129
-{
130
- uint32_t in = 0x112233;
131
- uint8_t dest[3];
132
- uint32_t out;
133
- put_be24(dest, in);
134
- out = get_be24(dest);
135
- check_int(in, ==, out);
136
-}
100
+ if_test ("common_prefix_size works") {
101
+ struct strbuf a = STRBUF_INIT;
102
+ struct strbuf b = STRBUF_INIT;
103
+ struct {
104
+ const char *a, *b;
105
+ int want;
106
+ } cases[] = {
107
+ {"abcdef", "abc", 3},
108
+ { "abc", "ab", 2 },
109
+ { "", "abc", 0 },
110
+ { "abc", "abd", 2 },
111
+ { "abc", "pqr", 0 },
112
+ };
113
138
-static void test_u16_roundtrip(void)
139
-{
140
- uint32_t in = 0xfef1;
141
- uint8_t dest[3];
142
- uint32_t out;
143
- put_be16(dest, in);
144
- out = get_be16(dest);
145
- check_int(in, ==, out);
146
-}
114
+ for (size_t i = 0; i < ARRAY_SIZE(cases); i++) {
115
+ strbuf_addstr(&a, cases[i].a);
116
+ strbuf_addstr(&b, cases[i].b);
117
+ check_int(common_prefix_size(&a, &b), ==, cases[i].want);
118
+ strbuf_reset(&a);
119
+ strbuf_reset(&b);
120
+ }
121
+ strbuf_release(&a);
122
+ strbuf_release(&b);
123
+ }
124
148
-int cmd_main(int argc, const char *argv[])
149
-{
150
- TEST(test_common_prefix(), "common_prefix_size works");
151
- TEST(test_parse_names_normal(), "parse_names works for basic input");
152
- TEST(test_parse_names_drop_empty(), "parse_names drops empty string");
153
- TEST(test_binsearch(), "binary search with binsearch works");
154
- TEST(test_names_length(), "names_length retuns size of a NULL-terminated string array");
155
- TEST(test_names_equal(), "names_equal compares NULL-terminated string arrays");
156
- TEST(test_u24_roundtrip(), "put_be24 and get_be24 work");
157
- TEST(test_u16_roundtrip(), "put_be16 and get_be16 work");
125
+ if_test ("put_be24 and get_be24 work") {
126
+ uint32_t in = 0x112233;
127
+ uint8_t dest[3];
128
+ uint32_t out;
129
+ put_be24(dest, in);
130
+ out = get_be24(dest);
131
+ check_int(in, ==, out);
132
+ }
133
+
134
+ if_test ("put_be16 and get_be16 work") {
135
+ uint32_t in = 0xfef1;
136
+ uint8_t dest[3];
137
+ uint32_t out;
138
+ put_be16(dest, in);
139
+ out = get_be16(dest);
140
+ check_int(in, ==, out);
141
+ }
142
143
return test_done();
144
}