dte test coverage


Directory: ./
File: test/buffer.c
Date: 2025-02-14 16:55:22
Exec Total Coverage
Lines: 100 100 100.0%
Functions: 5 5 100.0%
Branches: 2 2 100.0%

Line Branch Exec Source
1 #include <stdlib.h>
2 #include "test.h"
3 #include "buffer.h"
4 #include "editor.h"
5 #include "indent.h"
6 #include "regexp.h"
7
8 1 static void test_find_buffer_by_id(TestContext *ctx)
9 {
10 1 Buffer buffers[] = {
11 {.id = 4},
12 {.id = 7},
13 {.id = 9},
14 };
15
16 1 PointerArray array = PTR_ARRAY_INIT;
17 1 ptr_array_append(&array, &buffers[0]);
18 1 ptr_array_append(&array, &buffers[1]);
19 1 ptr_array_append(&array, &buffers[2]);
20
21 1 EXPECT_PTREQ(find_buffer_by_id(&array, 4), &buffers[0]);
22 1 EXPECT_PTREQ(find_buffer_by_id(&array, 7), &buffers[1]);
23 1 EXPECT_PTREQ(find_buffer_by_id(&array, 9), &buffers[2]);
24 1 EXPECT_NULL(find_buffer_by_id(&array, 1));
25 1 EXPECT_NULL(find_buffer_by_id(&array, 2));
26 1 EXPECT_NULL(find_buffer_by_id(&array, 3));
27 1 EXPECT_NULL(find_buffer_by_id(&array, 5));
28 1 EXPECT_NULL(find_buffer_by_id(&array, 6));
29 1 EXPECT_NULL(find_buffer_by_id(&array, 8));
30 1 EXPECT_NULL(find_buffer_by_id(&array, 10));
31
32 1 ptr_array_free_array(&array);
33 1 }
34
35 1 static void test_buffer_mark_lines_changed(TestContext *ctx)
36 {
37 1 Buffer b = {
38 .changed_line_min = 101,
39 .changed_line_max = 399,
40 };
41
42 1 buffer_mark_lines_changed(&b, 400, 12);
43 1 EXPECT_EQ(b.changed_line_min, 12);
44 1 EXPECT_EQ(b.changed_line_max, 400);
45
46 1 buffer_mark_lines_changed(&b, 3, 3);
47 1 EXPECT_EQ(b.changed_line_min, 3);
48 1 EXPECT_EQ(b.changed_line_max, 400);
49
50 1 buffer_mark_lines_changed(&b, 3, 990);
51 1 EXPECT_EQ(b.changed_line_min, 3);
52 1 EXPECT_EQ(b.changed_line_max, 990);
53
54 1 buffer_mark_lines_changed(&b, 1234, 1);
55 1 EXPECT_EQ(b.changed_line_min, 1);
56 1 EXPECT_EQ(b.changed_line_max, 1234);
57 1 }
58
59 1 static void test_make_indent(TestContext *ctx)
60 {
61 1 LocalOptions options = {
62 .expand_tab = false,
63 .indent_width = 8,
64 .tab_width = 8,
65 };
66
67 1 char *indent = make_indent(&options, 16);
68 1 EXPECT_STREQ(indent, "\t\t");
69 1 free(indent);
70
71 1 indent = make_indent(&options, 17);
72 1 EXPECT_STREQ(indent, "\t\t ");
73 1 free(indent);
74
75 1 indent = make_indent(&options, 20);
76 1 EXPECT_STREQ(indent, "\t\t ");
77 1 free(indent);
78
79 1 indent = make_indent(&options, 24);
80 1 EXPECT_STREQ(indent, "\t\t\t");
81 1 free(indent);
82
83 1 options.expand_tab = true;
84
85 1 indent = make_indent(&options, 8);
86 1 EXPECT_STREQ(indent, " ");
87 1 free(indent);
88
89 1 indent = make_indent(&options, 7);
90 1 EXPECT_STREQ(indent, " ");
91 1 free(indent);
92 1 }
93
94 1 static void test_get_indent_for_next_line(TestContext *ctx)
95 {
96 1 const char *pattern = "\\{$";
97 1 const InternedRegexp *ir = regexp_intern(NULL, pattern);
98 1 ASSERT_NONNULL(ir);
99
100 1 LocalOptions options = {
101 .brace_indent = false,
102 .expand_tab = true,
103 .indent_regex = ir,
104 .indent_width = 4,
105 .tab_width = 8
106 };
107
108 1 const StringView line1 = STRING_VIEW("foo {");
109 1 char *indent = get_indent_for_next_line(&options, &line1);
110 1 EXPECT_STREQ(indent, " ");
111 1 free(indent);
112
113 1 const StringView line2 = STRING_VIEW("foo");
114 1 indent = get_indent_for_next_line(&options, &line2);
115 1 EXPECT_STREQ(indent, NULL);
116 1 free(indent);
117 1 }
118
119 1 static void test_buffer_insert_bytes(TestContext *ctx)
120 {
121 1 size_t len = 600; // (> BLOCK_EDIT_SIZE)
122 1 char *text = xmalloc(len);
123 1 memset(text, 'a', len);
124 1 text[len - 1] = '\n';
125
2/2
✓ Branch 0 (5→4) taken 8 times.
✓ Branch 1 (5→6) taken 1 times.
9 for (size_t i = 72; i < len; i += 72) {
126 8 text[i] = '\n';
127 }
128
129 1 EditorState *e = ctx->userdata;
130 1 View *view = window_open_empty_buffer(e->window);
131 1 const Buffer *buffer = view->buffer;
132 1 uintmax_t counts[2];
133 1 buffer_count_blocks_and_bytes(buffer, counts);
134 1 EXPECT_EQ(counts[0], 1);
135 1 EXPECT_EQ(counts[1], 0);
136 1 EXPECT_EQ(buffer->nl, 0);
137
138 1 buffer_insert_bytes(view, text, len);
139 1 buffer_count_blocks_and_bytes(buffer, counts);
140 1 EXPECT_EQ(counts[0], 2);
141 1 EXPECT_EQ(counts[1], len);
142 1 EXPECT_EQ(buffer->nl, 9);
143
144 1 block_iter_goto_offset(&view->cursor, len / 2);
145 1 EXPECT_EQ(view->cursor.offset, 300);
146 1 buffer_insert_bytes(view, text, len);
147 1 EXPECT_EQ(view->cursor.offset, 300);
148 1 buffer_count_blocks_and_bytes(buffer, counts);
149 1 EXPECT_EQ(counts[0], 4);
150 1 EXPECT_EQ(counts[1], len * 2);
151 1 EXPECT_EQ(buffer->nl, 18);
152
153 1 free(text);
154 1 window_close_current_view(e->window);
155 1 }
156
157 static const TestEntry tests[] = {
158 TEST(test_find_buffer_by_id),
159 TEST(test_buffer_mark_lines_changed),
160 TEST(test_make_indent),
161 TEST(test_get_indent_for_next_line),
162 TEST(test_buffer_insert_bytes),
163 };
164
165 const TestGroup buffer_tests = TEST_GROUP(tests);
166