-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-gnl.c
181 lines (148 loc) · 4.66 KB
/
test-gnl.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* ************************************************************************** */
/* LE - / */
/* / */
/* test-gnl.c .:: .:/ . .:: */
/* +:+:+ +: +: +:+:+ */
/* By: aalleman <[email protected]> +:+ +: +: +:+ */
/* #+# #+ #+ #+# */
/* Created: 2019/10/17 15:10:26 by aalleman #+# ## ## #+# */
/* Updated: 2019/11/09 17:22:58 by aalleman ### #+. /#+ ###.fr */
/* / */
/* / */
/* ************************************************************************** */
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include "get_next_line.h"
void test(int fd, char **line, int testnb)
{
int ret;
int i;
i = 0;
while (i < testnb)
{
ret = get_next_line(fd, line);
if (ret < 0)
printf("Return %i\n", ret);
else
printf("%i - [%s]\n", ret, *line);
if (*line)
free(*line);
else
printf("Error : line = 0\n");
i++;
}
}
int main(int ac, char **av)
{
int fd;
int ret;
char *line;
if (ac == 2 && av[1][0] == 'g')
{
fd = open("get_next_line.c", O_RDONLY);
while ((ret = get_next_line(fd, &line)) == 1)
{
printf("l : %s\n", line);
free(line);
}
free(line);
return (0);
}
else if (ac == 2 && av[1][0] == '0')
{
while ((ret = get_next_line(0, &line)) == 1)
{
printf("l : %s\n", line);
free(line);
}
free(line);
return (0);
}
else if (ac == 2 && av[1][0] == 'l')
{
printf("\nVery long lines :\n");
fd = open("testfilesgnl/longlines", O_RDONLY);
test(fd, &line, 4);
return (0);
}
printf("BUFFER_SIZE : %i\n", BUFFER_SIZE);
line = 0;
printf("\nNegative fd :\n");
test(-2, &line, 1);
printf("\nNo file :\n");
test(42, &line, 1);
printf("\nEmpty file :\n");
fd = open("testfilesgnl/empty", O_RDONLY);
test(fd, &line, 1);
printf("\nOnly newline :\n");
fd = open("testfilesgnl/onlynewline", O_RDONLY);
test(fd, &line, 2);
printf("\nOnly a :\n");
fd = open("testfilesgnl/onlya", O_RDONLY);
test(fd, &line, 2);
printf("\nOnly z + nl :\n");
fd = open("testfilesgnl/onlyznl", O_RDONLY);
test(fd, &line, 2);
printf("\nLorem ipsum (+1 call) :\n");
fd = open("testfilesgnl/loremipsum", O_RDONLY);
test(fd, &line, 3);
printf("\nLorem ipsum + nl :\n");
fd = open("testfilesgnl/loremipsumnl", O_RDONLY);
test(fd, &line, 2);
printf("\nMultiple lines :\n");
fd = open("testfilesgnl/multiplelines", O_RDONLY);
test(fd, &line, 6);
printf("\nMultiple short lines :\n");
fd = open("testfilesgnl/multipleshortlines", O_RDONLY);
test(fd, &line, 6);
printf("\nMultiple lines, last line is long :\n");
fd = open("testfilesgnl/multiplelineslonglast", O_RDONLY);
test(fd, &line, 6);
printf("\nOnly empty lines (4) :\n");
fd = open("testfilesgnl/emptylines", O_RDONLY);
test(fd, &line, 5);
printf("\nSome empty lines (begin with full, finish with full) :\n");
fd = open("testfilesgnl/someemptylines", O_RDONLY);
test(fd, &line, 6);
printf("\nSome empty lines (begin with empty, finish with empty) :\n");
fd = open("testfilesgnl/someemptylines2", O_RDONLY);
test(fd, &line, 8);
#if BUFFER_SIZE == 32
printf("\n31 chars :\n");
fd = open("testfilesgnl/31chars", O_RDONLY);
test(fd, &line, 2);
printf("\n30 chars + nl :\n");
fd = open("testfilesgnl/30charsplusnl", O_RDONLY);
test(fd, &line, 2);
printf("\n32 chars :\n");
fd = open("testfilesgnl/32chars", O_RDONLY);
test(fd, &line, 2);
printf("\n31 chars + nl :\n");
fd = open("testfilesgnl/31charsplusnl", O_RDONLY);
test(fd, &line, 2);
printf("\n33 chars :\n");
fd = open("testfilesgnl/33chars", O_RDONLY);
test(fd, &line, 2);
printf("\n32 chars + nl :\n");
fd = open("testfilesgnl/32charsplusnl", O_RDONLY);
test(fd, &line, 2);
printf("\n33 chars + nl :\n");
fd = open("testfilesgnl/33charsplusnl", O_RDONLY);
test(fd, &line, 2);
printf("\n30 chars + nl and small line :\n");
fd = open("testfilesgnl/30charsplusnlmore", O_RDONLY);
test(fd, &line, 3);
printf("\n31 chars + nl and small line :\n");
fd = open("testfilesgnl/31charsplusnlmore", O_RDONLY);
test(fd, &line, 3);
printf("\n32 chars + nl and small line :\n");
fd = open("testfilesgnl/32charsplusnlmore", O_RDONLY);
test(fd, &line, 3);
printf("\n33 chars + nl and small line :\n");
fd = open("testfilesgnl/33charsplusnlmore", O_RDONLY);
test(fd, &line, 3);
#endif
return (0);
}