aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src/test_itoa.c
diff options
context:
space:
mode:
authorThomas Vanbesien <tvanbesi@proton.me>2026-02-21 16:29:51 +0100
committerThomas Vanbesien <tvanbesi@proton.me>2026-02-21 16:29:51 +0100
commita8f7e6e83bc4846c94c0489e0f6e0f807b35073e (patch)
tree0d245c73490427a39d4338da483e335a267c6917 /tests/src/test_itoa.c
parentfd62678a9cf38e3f70efbdd093c1012d448548e1 (diff)
downloadLibft-a8f7e6e83bc4846c94c0489e0f6e0f807b35073e.tar.gz
Libft-a8f7e6e83bc4846c94c0489e0f6e0f807b35073e.zip
Implement libft Part 2 with tests
Add ft_substr, ft_strjoin, ft_strtrim, ft_split, ft_itoa, ft_strmapi, ft_striteri, ft_putchar_fd, ft_putstr_fd, ft_putendl_fd, ft_putnbr_fd.
Diffstat (limited to 'tests/src/test_itoa.c')
-rw-r--r--tests/src/test_itoa.c99
1 files changed, 99 insertions, 0 deletions
diff --git a/tests/src/test_itoa.c b/tests/src/test_itoa.c
new file mode 100644
index 0000000..17cafe3
--- /dev/null
+++ b/tests/src/test_itoa.c
@@ -0,0 +1,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);
+}