-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.c
160 lines (145 loc) · 3.4 KB
/
tests.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
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
ssize_t ft_write(int fd, const void *buf, size_t count);
ssize_t ft_read(int fd, void *buf, size_t count);
size_t ft_strlen(const char *s);
int ft_strcmp(const char *s1, const char *s2);
char *ft_strcpy(char *dest, const char *src);
char *ft_strdup(const char *s1);
int test_read()
{
char *s1 = (char *)malloc(sizeof(char) * 100);
int fd;
char *s2;
int buff;
int l;
int x;
char *s_error;
printf("%19s", "===READ===\n");
// fd = 42; //bad fd
fd = open("./txt/1.txt", O_RDONLY);
printf("Insert bytes to read: ");
scanf("%s", s1);
buff = atoi(s1);
l = ft_read(fd, s2, buff);
x = read(fd, s2, buff);
// l = ft_read(fd, s_error, buff);
// x = read(fd, s_error, buff);
printf("FT: %d\nORIGINAL: %d\n\n", l, x);
free(s1);
return (0);
}
int test_write()
{
char *s1 = (char *)malloc(sizeof(char) * 100);
int fd;
char *s2 = (char *)malloc(sizeof(char) * 100);
char *s3 = (char *)malloc(sizeof(char) * 100);
int buff;
int l;
int x;
char *s_error;
printf("%19s", "===WRITE===\nInsert fd: ");
scanf("%s", s1);
fd = atoi(s1);
printf("Insert string: ");
scanf("%s", s2);
printf("Insert buff: ");
scanf("%s", s3);
buff = atoi(s3);
l = ft_write(fd, s2, buff);
x = write(fd, s2, buff);
//l = ft_write(fd, s_error, buff);
//x = write(fd, s_error, buff);
printf("FT: %d\nORIGINAL: %d\n\n", l, x);
free(s1);
free(s2);
free(s3);
return (0);
}
int test_cpy()
{
char *s1 = (char *)malloc(sizeof(char) * 100);
char *test_ft = (char *)malloc(sizeof(char) * 100);
char *test_ori = (char *)malloc(sizeof(char) * 100);
printf("%19s", "===STRCPY===\nInsert s1: ");
scanf("%s", s1);
//s1 = ""; //s1 empty
printf("FT: %s\nORIGINAL: %s\n\n", ft_strcpy(test_ft, s1), strcpy(test_ori, s1));
free(s1);
free(test_ft);
free(test_ori);
return (0);
}
int test_dup()
{
char *s1 = (char *)malloc(sizeof(char) * 100);
printf("%19s", "===STRDUP===\nInsert s1: ");
scanf("%s", s1);
//s1 = ""; //s1 empty
printf("FT: %s\nORIGINAL: %s\n\n", ft_strdup(s1), strdup(s1));
return (0);
}
int test_len()
{
int l;
int x;
char *s1 = (char *)malloc(sizeof(char) * 100);
printf("%19s", "===STRLEN===\nInsert s1: ");
scanf("%s", s1);
//s1 = ""; //s1 empty
l = ft_strlen(s1);
x = strlen(s1);
printf("FT: %d\nORIGINAL: %d\n\n", l, x);
free(s1);
return (0);
}
int test_cmp()
{
int l;
int x;
char *s1 = (char *)malloc(sizeof(char) * 100);
char *s2 = (char *)malloc(sizeof(char) * 100);
printf("%19s", "===STRCMP===\nInsert s1: ");
scanf("%s", s1);
//s1 = ""; //s1 empty
printf("Insert s2: ");
scanf("%s", s2);
//s2 = ""; //s2 empty
l = ft_strcmp(s1, s2);
x = strcmp(s1, s2);
x = x < 0 ? -1 : x > 0 ? 1 : 0;
printf("FT: %d\nORIGINAL: %d\n\n", l, x);
free(s1);
free(s2);
return (0);
}
int main(int argc, char **argv)
{
if (argc != 2)
{
printf("Invalid argument\n");
return (0);
}
else
{
if (strcmp(argv[1], "cmp") == 0)
test_cmp();
else if (strcmp(argv[1], "len") == 0)
test_len();
else if (strcmp(argv[1], "cpy") == 0)
test_cpy();
else if (strcmp(argv[1], "dup") == 0)
test_dup();
else if (strcmp(argv[1], "w") == 0)
test_write();
else if (strcmp(argv[1], "r") == 0)
test_read();
else
printf("Invalid test\n");
}
return (0);
}