-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_ft_memcpy.c
182 lines (170 loc) · 4.85 KB
/
test_ft_memcpy.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_ft_memcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jliew <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/07/01 21:44:17 by jliew #+# #+# */
/* Updated: 2020/07/10 23:03:03 by jliew ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "libft.h"
void gen_rand_2string(char *s1, char *s2, unsigned long n)
{
while (n--)
{
int c = rand() % 95 + 32;
*s1++ = c;
*s2++ = c;
}
*s1 = '\0';
*s2 = '\0';
}
void gen_rand_string(char* str, unsigned long n)
{
while (n--)
*str++ = rand() % 95 + 32;
*str = '\0';
}
void print_i(char *dst)
{
int i = 0;
while (i < 100)
printf("%c", dst[i++]);
printf("\n");
}
int main(int argc, char **argv)
{
srand(time(0));
if (argc == 1)
{
printf("-------------------------------------------------------\n");
printf(" void *ft_memcpy(void *dst. const void *src, size_t n)\n");
printf("-------------------------------------------------------\n");
printf("usage [auto]:\n");
printf("1. a --run\n");
printf("2. a --run <test_cases>\n");
printf("3. a --run <test_cases> --print\n");
printf("4. a --runs\n");
printf("usage [manual]:\n");
printf("1. a <string src> <int n>\n");
printf("2. a --overlap\n");
return (42);
}
if (!strcmp(argv[1], "--run"))
{
int print = 0;
char src[101];
/*char src2[101];*/
/*char cpy_src[101];*/
char dst1[101];
char dst2[101];
unsigned long failed = 0;
unsigned long test_cases = 1000000;
if (argc >= 3)
test_cases = atoi(argv[2]);
if (argc >= 4 && !strcmp(argv[3], "--print"))
print = 1;
printf("Running test(s): ft_memcpy\n");
for (unsigned long i = 0; i < test_cases; i++)
{
gen_rand_string(src, rand() % 101);
memset(dst1, 48, sizeof(dst1));
memset(dst2, 48, sizeof(dst2));
/*gen_rand_2string(src1, src2, rand() % 101);*/
/*strcpy(cpy_src, src1);*/
int n = rand() % 51;
char *st = memcpy(dst1, src, n);
char *ft = ft_memcpy(dst2, src, n);
if (memcmp(st, ft, 100))
{
failed++;
printf("FAILED case:\nsrc: %s\nn: %d\n", src, n);
printf("st: ");
print_i(st);
printf("ft: ");
print_i(ft);
}
if (print)
{
printf("[%lu] test case:\nsrc: %s\nn: %d\n", i + 1, src, n);
printf("st: ");
print_i(st);
printf("ft: ");
print_i(ft);
}
}
double rate = ((test_cases - failed) / (double)test_cases) * 100;
printf("%.2f%%: Checks: %lu, Failures: %lu\n", rate, test_cases, failed);
}
else if (!strcmp(argv[1], "--runs"))
{
char dst1[100];
char dst2[100];
memset(dst1, 0, sizeof(dst1));
memset(dst2, 0, sizeof(dst2));
printf("-------------------------------------------------------\n");
printf(" void *ft_memcpy(void *dst. const void *src, size_t n)\n");
printf("-------------------------------------------------------\n");
printf("1. ft_memcpy(NULL, NULL, 5)\n");
char *st = memcpy(NULL, NULL, 5);
char *ft = ft_memcpy(NULL, NULL, 5);
printf("st: %s\n", st);
printf("ft: %s\n", ft);
}
else
{
if (!strcmp(argv[1], "--overlap"))
{
char dst1[] = "ABCDEF";
char dst2[] = "ABCDEF";
char dst3[] = "ABCDEF";
int n = 6;
printf("-------------------------------------------------------\n");
printf(" void *ft_memcpy(void *dst. const void *src, size_t n)\n");
printf("-------------------------------------------------------\n");
printf("str: %s\n", dst1);
memcpy(dst1 + 1, dst1, n);
memmove(dst3 + 1, dst3, n);
ft_memcpy(dst2 + 1, dst2, n);
printf("ft_memcpy(str + 1, str, 6)\n");
printf("st: ");
for (int i = 0; i < n; i ++)
printf("%c", dst1[i]);
printf("\n");
printf("ft: ");
for (int i = 0; i < n; i++)
printf("%c", dst2[i]);
printf("\n");
printf("memmove: ");
for (int i = 0; i < n; i ++)
printf("%c", dst3[i]);
printf("\n");
}
else
{
char dst1[100];
char dst2[100];
char *src = argv[1];
/*char *src = strdup(argv[1]);*/
int n = atoi(argv[2]);
memset(dst1, 48, sizeof(dst1));
memset(dst2, 48, sizeof(dst2));
memcpy(dst1, src, n);
ft_memcpy(dst2, src, n);
printf("st: ");
for (int i = 0; i < 100; i ++)
printf("%c", dst1[i]);
printf("\n");
printf("ft: ");
for (int i = 0; i < 100; i++)
printf("%c", dst2[i]);
printf("\n");
/*free(src);*/
}
}
}