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
|
#include "libft.h"
#include "test_utils.h"
#include <limits.h>
static void
_s_test_itoa (void)
{
int i;
char label[128];
char expected[64];
_s_section ("ft_itoa");
/* zero */
{
char *p = ft_itoa (0);
_s_check ("zero", p && strcmp (p, "0") == 0);
free (p);
}
/* positive */
{
char *p = ft_itoa (42);
_s_check ("positive", p && strcmp (p, "42") == 0);
free (p);
}
/* negative */
{
char *p = ft_itoa (-42);
_s_check ("negative", p && strcmp (p, "-42") == 0);
free (p);
}
/* INT_MAX */
{
char *p = ft_itoa (INT_MAX);
snprintf (expected, sizeof (expected), "%d", INT_MAX);
_s_check ("INT_MAX", p && strcmp (p, expected) == 0);
free (p);
}
/* INT_MIN */
{
char *p = ft_itoa (INT_MIN);
snprintf (expected, sizeof (expected), "%d", INT_MIN);
_s_check ("INT_MIN", p && strcmp (p, expected) == 0);
free (p);
}
/* single digit positive */
{
char *p = ft_itoa (7);
_s_check ("single digit", p && strcmp (p, "7") == 0);
free (p);
}
/* single digit negative */
{
char *p = ft_itoa (-1);
_s_check ("neg single digit", p && strcmp (p, "-1") == 0);
free (p);
}
/* powers of ten */
{
char *p;
p = ft_itoa (10);
_s_check ("10", p && strcmp (p, "10") == 0);
free (p);
p = ft_itoa (100);
_s_check ("100", p && strcmp (p, "100") == 0);
free (p);
p = ft_itoa (-1000);
_s_check ("-1000", p && strcmp (p, "-1000") == 0);
free (p);
}
/* randomized */
for (i = 0; i < _S_RAND_ITERS; i++)
{
int n = rand () - RAND_MAX / 2;
char *p = ft_itoa (n);
snprintf (expected, sizeof (expected), "%d", n);
snprintf (label, sizeof (label), "random n=%d", n);
_s_check (label, p && strcmp (p, expected) == 0);
free (p);
}
}
int
main (void)
{
srand (time (NULL));
_s_test_itoa ();
_s_print_results ();
return (_s_fail != 0);
}
|