aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src/test_atoi.c
diff options
context:
space:
mode:
authorThomas Vanbesien <tvanbesi@proton.me>2026-02-21 15:49:06 +0100
committerThomas Vanbesien <tvanbesi@proton.me>2026-02-21 15:51:44 +0100
commit671a58519ef6207b54947ff70eea497ff7eb58ae (patch)
tree783a971119bfed965113b84bc306ba941e884663 /tests/src/test_atoi.c
parentd699849b2360f90c61f645c5d4d4232cd3e1c962 (diff)
downloadLibft-671a58519ef6207b54947ff70eea497ff7eb58ae.tar.gz
Libft-671a58519ef6207b54947ff70eea497ff7eb58ae.zip
Restructure project layout and clean up test suite
Move sources to src/, header to inc/, and tests to tests/src/. Update Makefiles and .gitignore for the new layout. Refactor test harness: add crash-wrapper macros (_S_CRASH, _S_CRASH_I, _S_CRASH_V, _S_CRASH_BUF) replacing 58 hand-written wrappers, add shared _s_test_int_range helper eliminating duplicate _s_test_func, add _S_RAND_ITERS constant, move srand() to main() in all test binaries, and add Doxygen comments to test_utils.h.
Diffstat (limited to 'tests/src/test_atoi.c')
-rw-r--r--tests/src/test_atoi.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/tests/src/test_atoi.c b/tests/src/test_atoi.c
new file mode 100644
index 0000000..91e4dc4
--- /dev/null
+++ b/tests/src/test_atoi.c
@@ -0,0 +1,49 @@
+#include "libft.h"
+#include "test_utils.h"
+#include <limits.h>
+
+_S_CRASH_I (ft_atoi_null, ft_atoi (NULL))
+_S_CRASH_I (libc_atoi_null, atoi (NULL))
+
+int
+main (void)
+{
+ int i;
+ char buf[64];
+ char label[64];
+
+ srand (time (NULL));
+ _s_section ("ft_atoi");
+ _s_check_both_crash ("atoi NULL", _s_crash_ft_atoi_null,
+ _s_crash_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 */
+ for (i = 0; i < _S_RAND_ITERS; 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);
+}