-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_ft_calloc.c
125 lines (116 loc) · 3.54 KB
/
test_ft_calloc.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_calloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jliew <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/07/03 21:57:42 by jliew #+# #+# */
/* Updated: 2020/07/10 19:10:43 by jliew ### ########.fr */
/* */
/* ************************************************************************** */
#include <time.h>
#include <stdio.h>
#include <string.h>
#include "libft.h"
int main(int argc, char **argv)
{
srand(time(0));
if (argc == 1)
{
printf("--------------------------------------------\n");
printf(" void *ft_calloc(size_t count, size_t size)\n");
printf("--------------------------------------------\n");
printf("usage [auto]:\n");
printf("1. a --run\n");
printf("1. a --run <test_cases>\n");
printf("1. a --run <test_cases> --print\n");
printf("usage [manual]:\n");
printf("1. a <char/int> <count>\n");
return (42);
}
if (!strcmp(argv[1], "--run"))
{
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_calloc\n");
for (unsigned long i = 0; i < test_cases; i++)
{
int type = rand() % 2;
int count = rand() % 1000;
if (type)
{
char *st = calloc(count, sizeof(char));
char *ft = ft_calloc(count, sizeof(char));
int res = memcmp(st, ft, count * sizeof(char));
if (res)
{
failed++;
printf("FAILED case:\nType: char, Count: %d\nmemcmp: %d\n", count, res);
}
if (print)
printf("[%lu] test case:\nType: char, Count: %d\nmemcmp: %d\n", i + 1, count, res);
free(st);
free(ft);
}
else
{
int *st = calloc(count, sizeof(int));
int *ft = ft_calloc(count, sizeof(int));
int res = memcmp(st, ft, count * sizeof(int));
if (res)
{
failed++;
printf("FAILED case:\nType: int, Count: %d\nmemcmp: %d\n", count, res);
}
if (print)
printf("[%lu] test case:\nType: int, Count: %d\nmemcmp: %d\n", i + 1, count, res);
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
{
int count = atoi(argv[2]);
if (!strcmp(argv[1], "int"))
{
int *n1 = calloc(count, sizeof(int));
int *n2 = ft_calloc(count, sizeof(int));
char *num1 = (char *)n1;
char *num2 = (char *)n2;
printf("st: ");
for (int i = 0; i < count * (int)sizeof(int); i++)
printf("%i", num1[i]);
printf("\n");
printf("ft: ");
for (int i = 0; i < count * (int)sizeof(int); i++)
printf("%i", num2[i]);
printf("\n");
free(n1);
free(n2);
}
else
{
char *str1 = calloc(count, sizeof(char));
char *str2 = ft_calloc(count, sizeof(char));
printf("st: ");
for (int i = 0; i < count; i++)
printf("%i", str1[i]);
printf("\n");
printf("ft: ");
for (int i = 0; i < count; i++)
printf("%i", str2[i]);
printf("\n");
free(str1);
free(str2);
}
}
}