Skip to content
This repository was archived by the owner on Feb 28, 2025. It is now read-only.

Commit 56a39c2

Browse files
committed
tests: detect_linebreaks.c: Revamp for latest codebase.
Also, test only line break detection algorithm, not anything else (no file operations, etc.). Signed-off-by: Paul Sokolovsky <pfalcon@users.sourceforge.net>
1 parent f69b91b commit 56a39c2

File tree

2 files changed

+48
-132
lines changed

2 files changed

+48
-132
lines changed

tests/src/editor/common_editor_includes.c

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
along with this program. If not, see <http://www.gnu.org/licenses/>.
2424
*/
2525

26-
#include "edit-widget.h"
26+
#include "editwidget.h"
2727
#include "lib/global.h"
2828
#include "lib/keybind.h"
2929

@@ -50,13 +50,6 @@ user_menu_cmd (struct WEdit *edit_widget, const char *menu_file, int selected_en
5050
(void) selected_entry;
5151
return TRUE;
5252
}
53-
static int
54-
check_for_default (const char *default_file, const char *file)
55-
{
56-
(void) default_file;
57-
(void) file;
58-
return 0;
59-
}
6053
static void
6154
save_setup_cmd (void)
6255
{
@@ -77,11 +70,9 @@ view_other_cmd (void)
7770
#include "editcmd.c"
7871
#include "editwidget.c"
7972
#include "editdraw.c"
80-
#include "editkeys.c"
8173
#include "editmenu.c"
8274
#include "editoptions.c"
8375
#include "syntax.c"
84-
#include "wordproc.c"
8576
#include "choosesyntax.c"
8677
#include "etags.c"
8778
#include "editcmd_dialogs.c"

tests/src/editor/detect_linebreaks.c

Lines changed: 47 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525

2626
#define TEST_SUITE_NAME "src/editor/detect_linebreaks"
2727

28+
#include <sys/types.h>
29+
#include <sys/stat.h>
30+
#include <fcntl.h>
31+
2832
#include <check.h>
2933

3034
#include <config.h>
@@ -57,22 +61,9 @@ teardown (void)
5761
START_TEST (test_detect_lb_type)
5862
{
5963
LineBreaks result;
60-
/* prepare for test */
61-
int fd = open (filename, O_WRONLY|O_CREAT, 0644);
62-
int i;
63-
if (fd == -1)
64-
{
65-
fail("unable to create test input file %s",filename);
66-
return;
67-
}
68-
for (i = 0;i<200;i++)
69-
{
70-
write (fd, "Test for detect line break\r\n", 29);
71-
}
72-
write (fd, "\r\n", 2);
73-
close(fd);
64+
char buf[] = "Test for detect line break\r\n";
7465

75-
result = detect_lb_type ((char *) filename);
66+
result = detect_lb_type_buf ((unsigned char *) buf, strlen(buf));
7667
fail_unless(result == LB_WIN, "Incorrect lineBreak: result(%d) != LB_WIN(%d)",result, LB_WIN);
7768

7869
unlink(filename);
@@ -84,25 +75,16 @@ END_TEST
8475
START_TEST (test_detect_lb_type_very_long_string)
8576
{
8677
LineBreaks result;
87-
/* prepare for test */
88-
int fd = open (filename, O_WRONLY|O_CREAT, 0644);
78+
char buf[1024] = "";
8979
int i;
90-
if (fd == -1)
91-
{
92-
fail("unable to create test input file %s",filename);
93-
return;
94-
}
9580
for (i = 0; i<20 ; i++)
9681
{
97-
write (fd, "Very long string. ", 18);
82+
strcat (buf, "Very long string. ");
9883
}
99-
write (fd, "\r\n", 2);
100-
close(fd);
84+
strcat (buf, "\r\n");
10185

102-
result = detect_lb_type ((char *) filename);
86+
result = detect_lb_type_buf ((unsigned char *) buf, strlen(buf));
10387
fail_unless(result == LB_WIN, "Incorrect lineBreak: result(%d) != LB_WIN(%d)",result, LB_WIN);
104-
105-
unlink(filename);
10688
}
10789
END_TEST
10890

@@ -111,20 +93,11 @@ END_TEST
11193
START_TEST (test_detect_lb_type_rrrrrn)
11294
{
11395
LineBreaks result;
114-
/* prepare for test */
115-
int fd = open (filename, O_WRONLY|O_CREAT, 0644);
116-
if (fd == -1)
117-
{
118-
fail("unable to create test input file %s",filename);
119-
return;
120-
}
121-
write (fd, "test\r\r\r\r\r\n", 10);
122-
close(fd);
96+
char buf[1024] = "";
97+
strcat (buf, "test\r\r\r\r\r\n");
12398

124-
result = detect_lb_type ((char *) filename);
125-
fail_unless(result == LB_MAC, "Incorrect lineBreak: result(%d) != LB_MAC(%d)",result, LB_MAC);
126-
127-
unlink(filename);
99+
result = detect_lb_type_buf ((unsigned char *) buf, strlen(buf));
100+
fail_unless(result == LB_ASIS, "Incorrect lineBreak: result(%d) != LB_ASIS(%d)", result, LB_ASIS);
128101
}
129102
END_TEST
130103

@@ -133,20 +106,22 @@ END_TEST
133106
START_TEST (test_detect_lb_type_nnnnnr)
134107
{
135108
LineBreaks result;
136-
/* prepare for test */
137-
int fd = open (filename, O_WRONLY|O_CREAT, 0644);
138-
if (fd == -1)
139-
{
140-
fail("unable to create test input file %s",filename);
141-
return;
142-
}
143-
write (fd, "test\n\n\n\n\n\r", 10);
144-
close(fd);
109+
char buf[] = "test\n\n\n\n\n\r ";
145110

146-
result = detect_lb_type ((char *) filename);
111+
result = detect_lb_type_buf ((unsigned char *) buf, strlen(buf));
147112
fail_unless(result == LB_ASIS, "Incorrect lineBreak: result(%d) != LB_ASIS(%d)",result, LB_ASIS);
113+
}
114+
END_TEST
148115

149-
unlink(filename);
116+
/* --------------------------------------------------------------------------------------------- */
117+
118+
START_TEST (test_detect_lb_type_nnnrnrnnnn)
119+
{
120+
LineBreaks result;
121+
char buf[] = "test\n\n\n\r\n\r\n\r\n\n\n\n";
122+
123+
result = detect_lb_type_buf ((unsigned char *) buf, strlen(buf));
124+
fail_unless(result == LB_ASIS, "Incorrect lineBreak: result(%d) != LB_ASIS(%d)",result, LB_ASIS);
150125
}
151126
END_TEST
152127

@@ -155,20 +130,10 @@ END_TEST
155130
START_TEST (test_detect_lb_type_rrrrrr)
156131
{
157132
LineBreaks result;
158-
/* prepare for test */
159-
int fd = open (filename, O_WRONLY|O_CREAT, 0644);
160-
if (fd == -1)
161-
{
162-
fail("unable to create test input file %s",filename);
163-
return;
164-
}
165-
write (fd, "test\r\r\r\r\r\r", 10);
166-
close(fd);
133+
char buf[1024] = "test\r\r\r\r\r\r";
167134

168-
result = detect_lb_type ((char *) filename);
169-
fail_unless(result == LB_MAC, "Incorrect lineBreak: result(%d) != LB_MAC(%d)",result, LB_MAC);
170-
171-
unlink(filename);
135+
result = detect_lb_type_buf ((unsigned char *) buf, strlen(buf));
136+
fail_unless(result == LB_MAC, "Incorrect lineBreak: result(%d) != LB_MAC(%d)", result, LB_MAC);
172137
}
173138
END_TEST
174139

@@ -177,20 +142,10 @@ END_TEST
177142
START_TEST (test_detect_lb_type_nnnnnn)
178143
{
179144
LineBreaks result;
180-
/* prepare for test */
181-
int fd = open (filename, O_WRONLY|O_CREAT, 0644);
182-
if (fd == -1)
183-
{
184-
fail("unable to create test input file %s",filename);
185-
return;
186-
}
187-
write (fd, "test\n\n\n\n\n\n", 10);
188-
close(fd);
189-
190-
result = detect_lb_type ((char *) filename);
191-
fail_unless(result == LB_ASIS, "Incorrect lineBreak: result(%d) != LB_ASIS(%d)",result, LB_ASIS);
145+
char buf[1024] = "test\n\n\n\n\n\n";
192146

193-
unlink(filename);
147+
result = detect_lb_type_buf ((unsigned char *) buf, strlen(buf));
148+
fail_unless(result == LB_ASIS, "Incorrect lineBreak: result(%d) != LB_ASIS(%d)", result, LB_ASIS);
194149
}
195150
END_TEST
196151

@@ -199,25 +154,15 @@ END_TEST
199154
START_TEST (test_detect_lb_type_buffer_border)
200155
{
201156
LineBreaks result;
202-
char buf[DETECT_LB_TYPE_BUFLEN];
203-
/* prepare for test */
204-
int fd = open (filename, O_WRONLY|O_CREAT, 0644);
205-
if (fd == -1)
206-
{
207-
fail("unable to create test input file %s",filename);
208-
return;
209-
}
157+
char buf[DETECT_LB_TYPE_BUFLEN + 100];
210158
memset(buf, ' ', DETECT_LB_TYPE_BUFLEN);
159+
buf[DETECT_LB_TYPE_BUFLEN - 101] = '\r';
160+
buf[DETECT_LB_TYPE_BUFLEN - 100] = '\n';
211161
buf[DETECT_LB_TYPE_BUFLEN - 1] = '\r';
162+
buf[DETECT_LB_TYPE_BUFLEN] = '\n';
212163

213-
write (fd, buf, DETECT_LB_TYPE_BUFLEN);
214-
write (fd, "\n", 1);
215-
close(fd);
216-
217-
result = detect_lb_type ((char *) filename);
218-
fail_unless(result == LB_WIN, "Incorrect lineBreak: result(%d) != LB_WIN(%d)",result, LB_WIN);
219-
220-
unlink(filename);
164+
result = detect_lb_type_buf ((unsigned char *) buf, DETECT_LB_TYPE_BUFLEN);
165+
fail_unless(result == LB_WIN, "Incorrect lineBreak: result(%d) != LB_WIN(%d)", result, LB_WIN);
221166
}
222167
END_TEST
223168

@@ -226,25 +171,15 @@ END_TEST
226171
START_TEST (test_detect_lb_type_buffer_border_overflow)
227172
{
228173
LineBreaks result;
229-
char buf[DETECT_LB_TYPE_BUFLEN];
230-
/* prepare for test */
231-
int fd = open (filename, O_WRONLY|O_CREAT, 0644);
232-
if (fd == -1)
233-
{
234-
fail("unable to create test input file %s",filename);
235-
return;
236-
}
174+
char buf[DETECT_LB_TYPE_BUFLEN + 100];
237175
memset(buf, ' ', DETECT_LB_TYPE_BUFLEN);
176+
buf[DETECT_LB_TYPE_BUFLEN - 100] = '\r';
238177
buf[DETECT_LB_TYPE_BUFLEN - 1] = '\r';
239178

240-
write (fd, buf, DETECT_LB_TYPE_BUFLEN);
241-
write (fd, "bla-bla\r\n", 9);
242-
close(fd);
179+
strcat (buf, "bla-bla\r\n");
243180

244-
result = detect_lb_type ((char *) filename);
181+
result = detect_lb_type_buf ((unsigned char *) buf, DETECT_LB_TYPE_BUFLEN);
245182
fail_unless(result == LB_MAC, "Incorrect lineBreak: result(%d) != LB_MAC(%d)",result, LB_MAC);
246-
247-
unlink(filename);
248183
}
249184
END_TEST
250185

@@ -253,24 +188,13 @@ END_TEST
253188
START_TEST (test_detect_lb_type_buffer_border_more)
254189
{
255190
LineBreaks result;
256-
char buf[DETECT_LB_TYPE_BUFLEN];
257-
/* prepare for test */
258-
int fd = open (filename, O_WRONLY|O_CREAT, 0644);
259-
if (fd == -1)
260-
{
261-
fail("unable to create test input file %s",filename);
262-
return;
263-
}
191+
char buf[DETECT_LB_TYPE_BUFLEN + 100];
264192
memset(buf, ' ', DETECT_LB_TYPE_BUFLEN);
265193

266-
write (fd, buf, DETECT_LB_TYPE_BUFLEN);
267-
write (fd, "bla-bla\n", 8);
268-
close(fd);
194+
strcat (buf, "bla-bla\n");
269195

270-
result = detect_lb_type ((char *) filename);
196+
result = detect_lb_type_buf ((unsigned char *) buf, strlen(buf));
271197
fail_unless(result == LB_ASIS, "Incorrect lineBreak: result(%d) != LB_ASIS(%d)",result, LB_ASIS);
272-
273-
unlink(filename);
274198
}
275199
END_TEST
276200

@@ -292,6 +216,7 @@ main (void)
292216
tcase_add_test (tc_core, test_detect_lb_type_very_long_string);
293217
tcase_add_test (tc_core, test_detect_lb_type_rrrrrn);
294218
tcase_add_test (tc_core, test_detect_lb_type_nnnnnr);
219+
tcase_add_test (tc_core, test_detect_lb_type_nnnrnrnnnn);
295220
tcase_add_test (tc_core, test_detect_lb_type_rrrrrr);
296221
tcase_add_test (tc_core, test_detect_lb_type_nnnnnn);
297222
tcase_add_test (tc_core, test_detect_lb_type_buffer_border);

0 commit comments

Comments
 (0)