-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_write.c
114 lines (105 loc) · 3.6 KB
/
test_write.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_write.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apuchill <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/02/28 21:14:55 by apuchill #+# #+# */
/* Updated: 2021/03/06 20:42:58 by apuchill ### ########.fr */
/* */
/* ************************************************************************** */
#include "tests.h"
static int test_fd(char *str, int fd_1, int fd_2)
{
int ret_ft[2];
int errno_ft[2];
int ret;
ret_ft[0] = write(fd_1, str, strlen(str));
errno_ft[0] = errno;
ret_ft[1] = ft_write(fd_2, str, strlen(str));
errno_ft[1] = errno;
printf("%sINPUT fd #1 :%s %i\n", C_TITLE, C_END, fd_1);
printf("%sINPUT fd #2 :%s %i\n", C_TITLE, C_END, fd_2);
printf("%sRETURN write :%s %i\n", C_TITLE, C_END, ret_ft[0]);
printf("%sRETURN ft_write :%s %i\n", C_TITLE, C_END, ret_ft[1]);
printf("%sERRNO write :%s %i\n", C_TITLE, C_END, errno_ft[0]);
printf("%sERRNO ft_write :%s %i\n", C_TITLE, C_END, errno_ft[1]);
if (ret_ft[0] == ret_ft[1] && errno_ft[0] == errno_ft[1])
ret = print_test_passed();
else
ret = print_test_failed();
return (ret);
}
static int test_stdout(char *str)
{
int ret_ft[2];
int errno_ft[2];
int ret;
printf("%sINPUT fd :%s %i\n", C_TITLE, C_END, 1);
write(1, "\033[38;5;75mOUTPUT write :\033[0m >|", 35);
ret_ft[0] = write(1, str, strlen(str));
errno_ft[0] = errno;
write(1, "|<\n\033[38;5;75mOUTPUT ft_write :\033[0m >|", 38);
ret_ft[1] = ft_write(1, str, strlen(str));
errno_ft[1] = errno;
write(1, "|<", 2);
printf("\n%sRETURN write :%s %i\n", C_TITLE, C_END, ret_ft[0]);
printf("%sRETURN ft_write :%s %i\n", C_TITLE, C_END, ret_ft[1]);
printf("%sERRNO write :%s %i\n", C_TITLE, C_END, errno_ft[0]);
printf("%sERRNO ft_write :%s %i\n", C_TITLE, C_END, errno_ft[1]);
if (ret_ft[0] == ret_ft[1] && errno_ft[0] == errno_ft[1])
ret = print_test_passed();
else
ret = print_test_failed();
return (ret);
}
static int test(char *str, int type)
{
static int test_nbr;
int ret;
int fd[2];
test_nbr += 1;
print_test_title(test_nbr);
if (type == 0)
{
ret = test_stdout(str);
test_nbr += 1;
print_test_title(test_nbr);
fd[0] = open("tests/out_lib.txt", O_CREAT | O_WRONLY | O_APPEND, 0777);
fd[1] = open("tests/out_ft.txt", O_CREAT | O_WRONLY | O_APPEND, 0777);
if (!fd[0] || !fd[1])
printf("%sError: unable to open file.%s\n\n", C_ERROR, C_END);
else
{
ret += test_fd(str, fd[0], fd[1]);
close(fd[0]);
close(fd[1]);
}
}
else
ret = test_fd(str, -1, -1);
return (ret);
}
void test_write(char *argv)
{
int total;
int result;
char long_str[1024];
print_ft_title("ft_write");
total = 2 * 7 + 1;
result = 0;
memset(long_str, '.', 1024);
long_str[1023] = '\0';
result += test("Hello, world!", 0);
result += test("", 0);
result += test(long_str, 0);
result += test("Some special characters 你好世界!", 0);
result += test("\ttabulation", 0);
result += test("\0", 0);
result += test("42\0this should not be here", 0);
result += test("", 1);
test_suit("ft_write", total, result);
if (!strcmp(argv, "all"))
print_continue();
}