-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_ft_strlcpy.c
125 lines (116 loc) · 3.52 KB
/
test_ft_strlcpy.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_ft_strlcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jliew <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/07/01 16:34:22 by jliew #+# #+# */
/* Updated: 2020/07/10 23:22:44 by jliew ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "libft.h"
void gen_rand_2strings(char *dst1, char *dst2, int n)
{
for (int i = 0; i < n; i++)
{
int c = rand() % 95 + 32;
*dst1++ = c;
*dst2++ = c;
}
*dst1 = '\0';
*dst2 = '\0';
}
void gen_rand_string(char *dst, int n)
{
for (int i = 0; i < n; i++)
*dst++ = rand() % 95 + 32;
*dst = '\0';
}
void print_dst(char *dst)
{
for (int i = 0; i < 100; i++)
printf("%c", dst[i]);
printf("\n");
}
int main(int argc, char **argv)
{
srand(time(0));
if (argc == 1)
{
printf("-----------------------------------------------------------------------\n");
printf(" size_t ft_strlcpy(char *dst, const char *src, size_t n)\n");
printf("-----------------------------------------------------------------------\n");
printf("usage [auto]:\n1. a --run\n2. a --run <test_cases>\n3. a --run <test_cases> --print\n");
printf("usage [manual]:\n1. a <string dst> <string src> <int n>\n");
return (42);
}
else if (!strcmp(argv[1], "--run"))
{
char src[50];
char dst1[101];
char dst2[101];
int print = 0;
char cpy_dst[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_strlcpy\n");
for (unsigned long i = 0; i < test_cases; i++)
{
int n = rand() % 100;
int sn = rand() % 50;
gen_rand_2strings(dst1, dst2, 100);
gen_rand_string(src, sn);
strcpy(cpy_dst, dst1);
unsigned long st = strlcpy(dst1, src, n);
unsigned long ft = ft_strlcpy(dst2, src, n);
if (st != ft || strcmp(dst1, dst2))
{
failed++;
printf("FAILED case:\nsrc: %s\ndst: %s\nn: %d\nst: %lu | ", src, cpy_dst, n, st);
print_dst(dst1);
printf("ft: %lu | ", ft);
print_dst(dst2);
}
if (print)
{
printf("[%lu] test case:\nsrc: %s\ndst: %s\nn: %d\nst: %lu | ", i + 1, src, cpy_dst, n, st);
print_dst(dst1);
printf("ft: %lu | ", ft);
print_dst(dst2);
}
}
double rate = ((test_cases - failed) / (double)test_cases) * 100;
printf("%2.f%%: Checks: %lu, Failures: %lu\n", rate, test_cases, failed);
}
else
{
char *dst;
char *src;
char dst1[100];
char dst2[100];
int n;
dst = argv[1];
src = argv[2];
n = atoi(argv[3]);
memset(dst1, '0', sizeof(dst1));
memset(dst2, '0', sizeof(dst2));
strcpy(dst1, dst);
strcpy(dst2, dst);
printf("st: %lu\n", strlcpy(dst1, src, n));
for (int i = 0; i < 100; i++)
printf("%c", dst1[i]);
printf("\n");
printf("ft: %lu\n", ft_strlcpy(dst2, src, n));
for (int i = 0; i < 100; i++)
printf("%c", dst2[i]);
printf("\n");
}
}