dte test coverage


Directory: ./
File: test/indent.c
Date: 2024-12-21 16:03:22
Exec Total Coverage
Lines: 134 134 100.0%
Functions: 3 3 100.0%
Branches: 10 10 100.0%

Line Branch Exec Source
1 #include "test.h"
2 #include "indent.h"
3
4 1 static void test_get_indent_info(TestContext *ctx)
5 {
6 1 LocalOptions options = {
7 .expand_tab = true,
8 .indent_width = 4,
9 .tab_width = 4,
10 };
11
12 1 StringView line = strview_from_cstring(" ");
13 1 IndentInfo info = get_indent_info(&options, &line);
14 1 EXPECT_EQ(info.bytes, 12);
15 1 EXPECT_EQ(info.width, 12);
16 1 EXPECT_EQ(info.level, 3);
17 1 EXPECT_TRUE(info.wsonly);
18 1 EXPECT_TRUE(info.sane);
19
20 1 line = strview_from_cstring("\t\t");
21 1 info = get_indent_info(&options, &line);
22 1 EXPECT_EQ(info.bytes, 2);
23 1 EXPECT_EQ(info.width, 8);
24 1 EXPECT_EQ(info.level, 2);
25 1 EXPECT_TRUE(info.wsonly);
26 1 EXPECT_FALSE(info.sane);
27
28 1 line = strview_from_cstring(" xyz");
29 1 info = get_indent_info(&options, &line);
30 1 EXPECT_EQ(info.bytes, 4);
31 1 EXPECT_EQ(info.width, 4);
32 1 EXPECT_EQ(info.level, 1);
33 1 EXPECT_FALSE(info.wsonly);
34 1 EXPECT_TRUE(info.sane);
35
36 1 options.expand_tab = false;
37 1 line = strview_from_cstring("\t\t");
38 1 info = get_indent_info(&options, &line);
39 1 EXPECT_EQ(info.bytes, 2);
40 1 EXPECT_EQ(info.width, 8);
41 1 EXPECT_EQ(info.level, 2);
42 1 EXPECT_TRUE(info.wsonly);
43 1 EXPECT_TRUE(info.sane);
44
45 1 line = strview_from_cstring(" test");
46 1 info = get_indent_info(&options, &line);
47 1 EXPECT_EQ(info.bytes, 4);
48 1 EXPECT_EQ(info.width, 4);
49 1 EXPECT_EQ(info.level, 1);
50 1 EXPECT_FALSE(info.wsonly);
51 1 EXPECT_FALSE(info.sane);
52
53 1 options.indent_width = 8;
54 1 options.tab_width = 8;
55 1 line = strview_from_cstring("\t\t ");
56 1 info = get_indent_info(&options, &line);
57 1 EXPECT_EQ(info.bytes, 4);
58 1 EXPECT_EQ(info.width, 18);
59 1 EXPECT_EQ(info.level, 2);
60 1 EXPECT_TRUE(info.wsonly);
61 1 EXPECT_TRUE(info.sane);
62
63 1 line = strview_from_cstring("\t \t ");
64 1 info = get_indent_info(&options, &line);
65 1 EXPECT_EQ(info.bytes, 4);
66 1 EXPECT_EQ(info.width, 17);
67 1 EXPECT_EQ(info.level, 2);
68 1 EXPECT_TRUE(info.wsonly);
69 1 EXPECT_FALSE(info.sane);
70
71 1 line = strview_from_cstring(" test");
72 1 info = get_indent_info(&options, &line);
73 1 EXPECT_EQ(info.bytes, 4);
74 1 EXPECT_EQ(info.width, 4);
75 1 EXPECT_EQ(info.level, 0);
76 1 EXPECT_FALSE(info.wsonly);
77 1 EXPECT_TRUE(info.sane);
78
79 1 line = strview_from_cstring(" test");
80 1 info = get_indent_info(&options, &line);
81 1 EXPECT_EQ(info.bytes, 8);
82 1 EXPECT_EQ(info.width, 8);
83 1 EXPECT_EQ(info.level, 1);
84 1 EXPECT_FALSE(info.wsonly);
85 1 EXPECT_FALSE(info.sane);
86 1 }
87
88 1 static void test_indent_level(TestContext *ctx)
89 {
90 1 EXPECT_EQ(indent_level(0, 2), 0);
91 1 EXPECT_EQ(indent_level(1, 2), 0);
92 1 EXPECT_EQ(indent_level(2, 2), 1);
93 1 EXPECT_EQ(indent_level(3, 2), 1);
94 1 EXPECT_EQ(indent_level(4, 2), 2);
95 1 EXPECT_EQ(indent_level(7, 8), 0);
96 1 EXPECT_EQ(indent_level(8, 8), 1);
97 1 EXPECT_EQ(indent_level(9, 8), 1);
98
99 1 EXPECT_EQ(indent_remainder(0, 2), 0);
100 1 EXPECT_EQ(indent_remainder(1, 2), 1);
101 1 EXPECT_EQ(indent_remainder(2, 2), 0);
102 1 EXPECT_EQ(indent_remainder(3, 2), 1);
103 1 EXPECT_EQ(indent_remainder(4, 2), 0);
104 1 EXPECT_EQ(indent_remainder(7, 8), 7);
105 1 EXPECT_EQ(indent_remainder(8, 8), 0);
106 1 EXPECT_EQ(indent_remainder(9, 8), 1);
107
108
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 1 times.
19 for (size_t x = 0; x <= 17; x++) {
109
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 18 times.
162 for (size_t m = 1; m <= 8; m++) {
110 144 EXPECT_EQ(indent_level(x, m), x / m);
111 144 EXPECT_EQ(indent_remainder(x, m), x % m);
112 }
113 }
114 1 }
115
116 1 static void test_next_indent_width(TestContext *ctx)
117 {
118 1 EXPECT_EQ(next_indent_width(0, 4), 4);
119 1 EXPECT_EQ(next_indent_width(1, 4), 4);
120 1 EXPECT_EQ(next_indent_width(2, 4), 4);
121 1 EXPECT_EQ(next_indent_width(3, 4), 4);
122 1 EXPECT_EQ(next_indent_width(4, 4), 8);
123 1 EXPECT_EQ(next_indent_width(5, 4), 8);
124 1 EXPECT_EQ(next_indent_width(6, 4), 8);
125 1 EXPECT_EQ(next_indent_width(7, 4), 8);
126 1 EXPECT_EQ(next_indent_width(8, 4), 12);
127 1 EXPECT_EQ(next_indent_width(9, 4), 12);
128
129 1 EXPECT_EQ(next_indent_width(0, 3), 3);
130 1 EXPECT_EQ(next_indent_width(1, 3), 3);
131 1 EXPECT_EQ(next_indent_width(2, 3), 3);
132 1 EXPECT_EQ(next_indent_width(3, 3), 6);
133 1 EXPECT_EQ(next_indent_width(4, 3), 6);
134 1 EXPECT_EQ(next_indent_width(5, 3), 6);
135 1 EXPECT_EQ(next_indent_width(6, 3), 9);
136 1 EXPECT_EQ(next_indent_width(7, 3), 9);
137 1 EXPECT_EQ(next_indent_width(8, 3), 9);
138 1 EXPECT_EQ(next_indent_width(9, 3), 12);
139
140 1 EXPECT_EQ(next_indent_width(0, 1), 1);
141 1 EXPECT_EQ(next_indent_width(1, 1), 2);
142 1 EXPECT_EQ(next_indent_width(2, 1), 3);
143 1 EXPECT_EQ(next_indent_width(3, 1), 4);
144 1 EXPECT_EQ(next_indent_width(4, 1), 5);
145
146
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 1 times.
19 for (size_t x = 0; x <= 17; x++) {
147
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 18 times.
162 for (size_t m = 1; m <= 8; m++) {
148 144 size_t r = next_indent_width(x, m);
149 144 EXPECT_TRUE(r > x);
150 144 EXPECT_EQ(r % m, 0);
151 144 EXPECT_EQ(r, ((x + m) / m) * m);
152
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 93 times.
144 if (x % m == 0) {
153 51 EXPECT_EQ(r, x + m);
154 }
155 }
156 }
157
158 1 EXPECT_UINT_EQ(next_indent_width(SIZE_MAX - 0, 4), 0);
159 1 EXPECT_UINT_EQ(next_indent_width(SIZE_MAX - 1, 4), 0);
160 1 EXPECT_UINT_EQ(next_indent_width(SIZE_MAX - 2, 4), 0);
161 1 EXPECT_UINT_EQ(next_indent_width(SIZE_MAX - 3, 4), 0);
162
163 1 EXPECT_UINT_EQ(next_indent_width(SIZE_MAX - 0, 3), 0);
164 1 EXPECT_UINT_EQ(next_indent_width(SIZE_MAX - 1, 3), 0);
165 1 EXPECT_UINT_EQ(next_indent_width(SIZE_MAX - 2, 3), 0);
166
167 1 EXPECT_UINT_EQ(next_indent_width(SIZE_MAX - 0, 1), 0);
168 1 }
169
170 static const TestEntry tests[] = {
171 TEST(test_get_indent_info),
172 TEST(test_indent_level),
173 TEST(test_next_indent_width),
174 };
175
176 const TestGroup indent_tests = TEST_GROUP(tests);
177