forked from Sherchryst/gnlkiller
-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.c
101 lines (91 loc) · 2.37 KB
/
test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aseo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/14 17:39:00 by ncolomer #+# #+# */
/* Updated: 2019/10/25 13:53:11 by aseo ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include "get_next_line.h"
void sigcatch()
{
write(2, "Your GNL got killed because it got stuck \n", 42);
exit(-1);
}
void
test_fd(char **buffer, char const *filename, int fd)
{
int r;
alarm(5);
signal(SIGALRM, sigcatch);
if (fd == STDIN_FILENO)
printf("Reading stdin...\n---\n");
else
printf("Reading %s...\n---\n", filename);
while ((r = get_next_line(fd, buffer)) > 0)
{
printf("%2d-%s\n", r, *buffer);
if (*buffer)
{
free(*buffer);
*buffer = NULL;
}
}
if (r >= 0)
printf("%2d-%s\n---\n", r, *buffer);
else
printf("%2d#error\n---\n", r);
if (*buffer)
{
free(*buffer);
*buffer = NULL;
}
alarm(0);
}
int
main(void)
{
char *buffer = NULL;
int i, fd;
int test_count;
char const *tests[100] = {
"tests/simple",
"tests/empty",
"tests/empty_one",
"tests/multi_in_one",
"tests/many_lines",
"tests/over_buffer",
"tests/63_line",
"tests/63_line_nl",
"tests/64_line",
"tests/64_line_nl",
"tests/65_line",
"tests/65_line_nl",
"tests/baudelaire.txt",
"tests/large_file.txt",
"tests/lorem_ipsum",
//"tests/one_big_fat_line.txt",
//"tests/Mr. Justice Maxell by Edgar Wallace.txt",
NULL
};
test_count = 0;
while (tests[test_count])
test_count++;
i = 0;
while (i < test_count)
{
fd = open(tests[i], O_RDONLY);
test_fd(&buffer, tests[i++], fd);
close(fd);
}
test_fd(&buffer, "Invalid file descriptor", 42);
test_fd(&buffer, NULL, STDIN_FILENO);
return (0);
}