-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_ft_memchr.c
112 lines (102 loc) · 3.31 KB
/
test_ft_memchr.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_ft_memchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jliew <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/07/02 16:24:55 by jliew #+# #+# */
/* Updated: 2020/07/10 20:21:20 by jliew ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <limits.h>
#include "libft.h"
void gen_rand_string(char *dst)
{
for (int i = 0; i < 100; i++)
{
*dst++ = rand() % 95 + 32;
}
*dst = '\0';
}
int check(const char *st, const char *ft)
{
if (!st && !ft)
return (0);
if (st && ft)
return (2);
return (1);
}
int main(int argc, char **argv)
{
srand(time(0));
if (argc == 1)
{
printf("-------------------------------------------------\n");
printf(" void *ft_memchr(const void *s, int c, 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 c> <int n>\n");
return (42);
}
else if (!strcmp(argv[1], "--run"))
{
char str[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_memchr\n");
for (unsigned long i = 0; i < test_cases; i++)
{
gen_rand_string(str);
int c = rand() % 95 + 32;
int n = rand() % 101;
char *st = memchr(str, c, n);
char *ft = ft_memchr(str, c, n);
if ((check(st, ft) == 2 && strcmp(st, ft)) || check(st, ft) == 1)
{
failed++;
printf("FAILED case:\nstr: %s\nc: %c, n: %d\nst: %s\nft: %s\n", str, c, n, st, ft);
}
if (print)
printf("[%lu] test case:\nstr: %s\nc: %c, n: %d\nst: %s\nft: %s\n", i + 1, str, c, n, st, 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"))
{
printf("-------------------------------------------------\n");
printf(" void *ft_memchr(const void *s, int c, size_t n)\n");
printf("-------------------------------------------------\n");
printf("1. ft_memchr(\"abc\\0def\", 'e', 10)\n");
char *st = memchr("abc\0def", 'e', 10);
char *ft = memchr("abc\0def", 'e', 10);
printf("st: %s\n", st);
printf("ft: %s\n", ft);
}
else
{
char *s = argv[1];
int c = argv[2][0];
int n = atoi(argv[3]);
/*s = strdup(s);*/
char *st_res = memchr(s, c, n);
char *ft_res = ft_memchr(s, c, n);
printf("st: %s\n", st_res);
printf("ft: %s\n", ft_res);
/*free(s);*/
}
}