-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_GNL_bonus.c
186 lines (165 loc) Β· 4.78 KB
/
main_GNL_bonus.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
182
183
184
185
186
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main_GNL_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: anpayot <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/30 16:10:50 by anpayot #+# #+# */
/* Updated: 2024/12/30 16:15:47 by anpayot ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include "get_next_line_bonus.h"
#define MAX_TEST_FILES 4
typedef struct s_test_stats
{
int line_count;
int total_chars;
double time_taken;
} t_test_stats;
void print_results(t_test_stats *stats, const char *filename)
{
printf("\nπ Results for %s:\n", filename);
printf("β Lines read: %d\n", stats->line_count);
printf("β Total characters: %d\n", stats->total_chars);
printf("β± Time taken: %.4f seconds\n", stats->time_taken);
}
void test_single_file(const char *filename)
{
int fd;
char *line;
t_test_stats stats;
struct timeval start, end;
printf("\nπ Testing single file: %s\n", filename);
gettimeofday(&start, NULL);
fd = open(filename, O_RDONLY);
if (fd == -1)
{
perror("β Error opening file");
return ;
}
stats.line_count = 0;
stats.total_chars = 0;
printf("\nπ File contents:\n");
printf("------------------------\n");
while ((line = get_next_line(fd)) != NULL)
{
stats.line_count++;
stats.total_chars += ft_strlen(line);
printf("π Line %d: %s", stats.line_count, line);
free(line);
}
printf("------------------------\n");
close(fd);
gettimeofday(&end, NULL);
stats.time_taken = (end.tv_sec - start.tv_sec) +
((end.tv_usec - start.tv_usec) / 1000000.0);
print_results(&stats, filename);
}
void test_multiple_files(char **filenames, int file_count)
{
int fds[MAX_TEST_FILES];
char *lines[MAX_TEST_FILES];
t_test_stats stats[MAX_TEST_FILES];
int active_files;
struct timeval start, end;
int i;
printf("\nπ Testing multiple files simultaneously\n");
gettimeofday(&start, NULL);
active_files = 0;
for (i = 0; i < file_count && i < MAX_TEST_FILES; i++)
{
fds[i] = open(filenames[i], O_RDONLY);
if (fds[i] == -1)
{
perror("β Error opening file");
continue;
}
stats[i].line_count = 0;
stats[i].total_chars = 0;
active_files++;
}
printf("\nπ Interleaved reading:\n");
printf("------------------------\n");
while (active_files > 0)
{
active_files = 0;
for (i = 0; i < file_count && i < MAX_TEST_FILES; i++)
{
if (fds[i] != -1)
{
lines[i] = get_next_line(fds[i]);
if (lines[i])
{
stats[i].line_count++;
stats[i].total_chars += ft_strlen(lines[i]);
printf("π File %d, Line %d: %s",
i + 1, stats[i].line_count, lines[i]);
free(lines[i]);
active_files++;
}
else
{
close(fds[i]);
fds[i] = -1;
}
}
}
}
printf("------------------------\n");
gettimeofday(&end, NULL);
double total_time = (end.tv_sec - start.tv_sec) +
((end.tv_usec - start.tv_usec) / 1000000.0);
for (i = 0; i < file_count && i < MAX_TEST_FILES; i++)
{
stats[i].time_taken = total_time;
print_results(&stats[i], filenames[i]);
}
}
void test_invalid_fd(void)
{
char *line;
printf("\nπ Testing invalid file descriptors\n");
printf("------------------------\n");
printf("Testing fd = -1: ");
line = get_next_line(-1);
printf("%s\n", line ? "β Failed" : "β
Success");
free(line);
printf("Testing fd = 1023: ");
line = get_next_line(1023);
printf("%s\n", line ? "β Failed" : "β
Success");
free(line);
printf("Testing closed fd: ");
int fd = open("test.txt", O_RDONLY);
close(fd);
line = get_next_line(fd);
printf("%s\n", line ? "β Failed" : "β
Success");
free(line);
printf("------------------------\n");
}
int main(int argc, char **argv)
{
printf("\n=== GET_NEXT_LINE BONUS TESTER ===\n");
printf("π Buffer size: %d\n", BUFFER_SIZE);
if (argc < 2)
{
fprintf(stderr, "β Usage: %s <file1> [file2] [file3] [file4]\n", argv[0]);
return (EXIT_FAILURE);
}
// Test invalid file descriptors
test_invalid_fd();
// Test single file reading
test_single_file(argv[1]);
// Test multiple files if more than one file is provided
if (argc > 2)
{
test_multiple_files(argv + 1, argc - 1);
}
printf("\nβ
All tests completed!\n\n");
return (EXIT_SUCCESS);
}