-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_ft_memset.c
126 lines (117 loc) · 3.38 KB
/
test_ft_memset.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_ft_memset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jliew <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/07/01 21:44:17 by jliew #+# #+# */
/* Updated: 2020/07/10 23:20:06 by jliew ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "libft.h"
void print_dst(const char *dst)
{
for (int i = 0; i < 100; i++)
printf("%i", dst[i]);
printf("\n");
}
int main(int argc, char **argv)
{
srand(time(0));
if (argc == 1)
{
printf("-------------------------------------------\n");
printf(" void *ft_memset(void *b, 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("usage [manual]:\n1. a <char/int> <int c> <int n>\n");
return (42);
}
else if (!strcmp(argv[1], "--run"))
{
char dst1[100];
char dst2[100];
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_memset\n");
for (unsigned long i = 0; i < test_cases; i++)
{
int c = rand() % 10;
int n = rand() % 101;
memset(dst1, 0, sizeof(dst1));
memset(dst2, 0, sizeof(dst2));
char *st = memset(dst1, c, n);
char *ft = ft_memset(dst2, c, n);
if (memcmp(st, ft, 100))
{
failed++;
printf("FAILED case:\nc: %i, n: %i\nst: ", c, n);
print_dst(st);
printf("ft: ");
print_dst(ft);
}
if (print)
{
printf("[%lu] test case:\nc: %i, n: %i\nst: ", i + 1, c, n);
print_dst(st);
printf("ft: ");
print_dst(ft);
}
}
double rate = ((test_cases - failed) / (double)test_cases) * 100;
printf("%.2f%%: Checks: %lu, Failures: %lu\n", rate, test_cases, failed);
}
else
{
char c_dst1[100];
char c_dst2[100];
int i_dst1[100];
int i_dst2[100];
memset(c_dst1, 0, sizeof(c_dst1));
memset(c_dst2, 0, sizeof(c_dst2));
memset(i_dst1, 0, sizeof(i_dst1));
memset(i_dst2, 0, sizeof(i_dst2));
int c = atoi(argv[2]);
int n = atoi(argv[3]);
if (argc != 4)
return (42);
if (!strcmp(argv[1], "char"))
{
memset(c_dst1, c, n);
printf("st: ");
for (int i = 0; i < 100; i++)
printf("%i", c_dst1[i]);
printf("\n");
ft_memset(c_dst2, c, n);
printf("ft: ");
for (int i = 0; i < 100; i++)
printf("%i", c_dst2[i]);
printf("\n");
}
else
{
memset(i_dst1, c, n);
printf("st: ");
for (int i = 0; i < 100; i++)
printf("%i", i_dst1[i]);
printf("\n");
ft_memset(i_dst2, c, n);
printf("ft: ");
for (int i = 0; i < 100; i++)
printf("%i", i_dst2[i]);
printf("\n");
}
}
}