-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_ft_strdup.c
90 lines (84 loc) · 2.6 KB
/
test_ft_strdup.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jliew <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/07/03 16:23:43 by jliew #+# #+# */
/* Updated: 2020/07/10 19:04:59 by jliew ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "libft.h"
void gen_rand_string(char *str, unsigned long n)
{
while (n--)
*str++ = rand() % 95 + 32;
*str = '\0';
}
int main(int argc, char **argv)
{
srand(time(0));
if (argc == 1)
{
printf("-----------------------------------\n");
printf(" char * ft_strdup(const char *str)\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("usage [manual]:\n");
printf("1. a <string src>\n");
return (42);
}
if (!strcmp(argv[1], "--run"))
{
char src[101];
int print = 0;
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_strdup\n");
for (unsigned long i = 0; i < test_cases; i++)
{
int n = rand() % 101;
gen_rand_string(src, n);
char *st = strdup(src);
char *ft = ft_strdup(src);
if (st == NULL || ft == NULL)
{
printf("malloc failed at n: %d\n", n);
continue;
}
if (strcmp(st, ft))
{
failed++;
printf("FAILED case:\nsrc: %s\nst: %s\nft: %s\n", src, st, ft);
}
if (print)
printf("[%lu] test case:\nsrc: %s\nst: %s\nft: %s\n", i + 1, src, st, ft);
free(st);
free(ft);
}
double rate = ((test_cases - failed) / (double)test_cases) * 100;
printf("%.2f%%: Checks: %lu, Failures: %lu\n", rate, test_cases, failed);
}
else
{
char *src = argv[1];
char *st = strdup(src);
char *ft = ft_strdup(src);
printf("st: %s\n", st);
printf("ft: %s\n", ft);
free(st);
free(ft);
printf("freed\n");
}
}