aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src/test_strmapi.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_strmapi.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_strmapi.c')
-rw-r--r--tests/src/test_strmapi.c108
1 files changed, 108 insertions, 0 deletions
diff --git a/tests/src/test_strmapi.c b/tests/src/test_strmapi.c
new file mode 100644
index 0000000..31dba65
--- /dev/null
+++ b/tests/src/test_strmapi.c
@@ -0,0 +1,108 @@
+#include "libft.h"
+#include "test_utils.h"
+#include <ctype.h>
+
+_S_CRASH (ft_strmapi_null, ft_strmapi (NULL, NULL))
+
+static char
+_s_to_upper (unsigned int i, char c)
+{
+ (void)i;
+ return (toupper (c));
+}
+
+static char
+_s_add_index (unsigned int i, char c)
+{
+ return (c + i);
+}
+
+static void
+_s_test_strmapi (void)
+{
+ int i;
+ char label[128];
+
+ _s_section ("ft_strmapi");
+
+ /* NULL crashes */
+ _s_check ("NULL crashes", _s_crashes (_s_crash_ft_strmapi_null));
+
+ /* basic: to uppercase */
+ {
+ char *p = ft_strmapi ("hello", _s_to_upper);
+ _s_check ("to_upper", p && strcmp (p, "HELLO") == 0);
+ free (p);
+ }
+
+ /* function receives correct index */
+ {
+ char *p = ft_strmapi ("aaaa", _s_add_index);
+ _s_check ("index passed",
+ p && p[0] == 'a' && p[1] == 'b' && p[2] == 'c' && p[3] == 'd');
+ free (p);
+ }
+
+ /* empty string */
+ {
+ char *p = ft_strmapi ("", _s_to_upper);
+ _s_check ("empty", p && strcmp (p, "") == 0);
+ free (p);
+ }
+
+ /* returns independent copy */
+ {
+ char s[] = "hello";
+ char *p = ft_strmapi (s, _s_to_upper);
+ _s_check ("independent ptr", p && p != s);
+ _s_check ("original unchanged", strcmp (s, "hello") == 0);
+ free (p);
+ }
+
+ /* single character */
+ {
+ char *p = ft_strmapi ("a", _s_to_upper);
+ _s_check ("single char", p && strcmp (p, "A") == 0);
+ free (p);
+ }
+
+ /* randomized: apply to_upper, compare with manual */
+ for (i = 0; i < _S_RAND_ITERS; i++)
+ {
+ int len = rand () % 200 + 1;
+ char *src = malloc (len + 1);
+ char *expected = malloc (len + 1);
+ char *p;
+ int j;
+
+ if (!src || !expected)
+ {
+ free (src);
+ free (expected);
+ continue;
+ }
+ for (j = 0; j < len; j++)
+ {
+ src[j] = 'a' + rand () % 26;
+ expected[j] = toupper (src[j]);
+ }
+ src[len] = '\0';
+ expected[len] = '\0';
+
+ p = ft_strmapi (src, _s_to_upper);
+ snprintf (label, sizeof (label), "random len=%d", len);
+ _s_check (label, p && strcmp (p, expected) == 0);
+ free (src);
+ free (expected);
+ free (p);
+ }
+}
+
+int
+main (void)
+{
+ srand (time (NULL));
+ _s_test_strmapi ();
+ _s_print_results ();
+ return (_s_fail != 0);
+}