aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_atoi.c
diff options
context:
space:
mode:
authorThomas Vanbesien <tvanbesi@proton.me>2026-02-21 12:39:20 +0100
committerThomas Vanbesien <tvanbesi@proton.me>2026-02-21 12:39:34 +0100
commitd699849b2360f90c61f645c5d4d4232cd3e1c962 (patch)
treec929ec20ca3b16fe3d34bd75e412ca49f7420125 /tests/test_atoi.c
downloadLibft-d699849b2360f90c61f645c5d4d4232cd3e1c962.tar.gz
Libft-d699849b2360f90c61f645c5d4d4232cd3e1c962.zip
Initial commit: libft Part 1 with tests and documentation
Reimplements 24 libc functions with Doxygen-documented header and test suite comparing against libc.
Diffstat (limited to 'tests/test_atoi.c')
-rw-r--r--tests/test_atoi.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/tests/test_atoi.c b/tests/test_atoi.c
new file mode 100644
index 0000000..196a081
--- /dev/null
+++ b/tests/test_atoi.c
@@ -0,0 +1,58 @@
+#include "../libft.h"
+#include "test_utils.h"
+#include <limits.h>
+
+static void
+_s_ft_atoi_null (void)
+{
+ _s_sink_i = ft_atoi (NULL);
+}
+static void
+_s_libc_atoi_null (void)
+{
+ _s_sink_i = atoi (NULL);
+}
+
+int
+main (void)
+{
+ int i;
+ char buf[64];
+ char label[64];
+
+ printf ("=== ft_atoi ===\n");
+
+ /* NULL */
+ _s_check_both_crash ("atoi NULL", _s_ft_atoi_null, _s_libc_atoi_null);
+
+ /* edge cases */
+ _s_check_eq_int ("zero", ft_atoi ("0"), atoi ("0"));
+ _s_check_eq_int ("INT_MAX", ft_atoi ("2147483647"), atoi ("2147483647"));
+ _s_check_eq_int ("INT_MIN", ft_atoi ("-2147483648"), atoi ("-2147483648"));
+ _s_check_eq_int ("negative", ft_atoi ("-42"), atoi ("-42"));
+ _s_check_eq_int ("explicit +", ft_atoi ("+42"), atoi ("+42"));
+ _s_check_eq_int ("leading spaces", ft_atoi (" 42"), atoi (" 42"));
+ _s_check_eq_int ("leading tabs", ft_atoi ("\t\n\v\f\r 42"),
+ atoi ("\t\n\v\f\r 42"));
+ _s_check_eq_int ("trailing chars", ft_atoi ("42abc"), atoi ("42abc"));
+ _s_check_eq_int ("empty string", ft_atoi (""), atoi (""));
+ _s_check_eq_int ("only spaces", ft_atoi (" "), atoi (" "));
+ _s_check_eq_int ("only sign", ft_atoi ("-"), atoi ("-"));
+ _s_check_eq_int ("sign then space", ft_atoi ("- 42"), atoi ("- 42"));
+ _s_check_eq_int ("double sign", ft_atoi ("--42"), atoi ("--42"));
+ _s_check_eq_int ("space between", ft_atoi ("4 2"), atoi ("4 2"));
+ _s_check_eq_int ("leading zeros", ft_atoi ("00042"), atoi ("00042"));
+
+ /* randomized */
+ srand (time (NULL));
+ for (i = 0; i < 50; i++)
+ {
+ int v = rand () - RAND_MAX / 2;
+ snprintf (buf, sizeof (buf), "%d", v);
+ snprintf (label, sizeof (label), "random %s", buf);
+ _s_check_eq_int (label, ft_atoi (buf), atoi (buf));
+ }
+
+ _s_print_results ();
+ return (_s_fail != 0);
+}