aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src/test_strjoin.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_strjoin.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_strjoin.c')
-rw-r--r--tests/src/test_strjoin.c111
1 files changed, 111 insertions, 0 deletions
diff --git a/tests/src/test_strjoin.c b/tests/src/test_strjoin.c
new file mode 100644
index 0000000..023f84d
--- /dev/null
+++ b/tests/src/test_strjoin.c
@@ -0,0 +1,111 @@
+#include "libft.h"
+#include "test_utils.h"
+
+_S_CRASH (ft_strjoin_null_s1, ft_strjoin (NULL, "hello"))
+_S_CRASH (ft_strjoin_null_s2, ft_strjoin ("hello", NULL))
+_S_CRASH (ft_strjoin_null_both, ft_strjoin (NULL, NULL))
+
+static void
+_s_test_strjoin (void)
+{
+ int i;
+ char label[128];
+
+ _s_section ("ft_strjoin");
+
+ /* NULL crashes */
+ _s_check ("NULL s1 crashes", _s_crashes (_s_crash_ft_strjoin_null_s1));
+ _s_check ("NULL s2 crashes", _s_crashes (_s_crash_ft_strjoin_null_s2));
+ _s_check ("NULL both crashes", _s_crashes (_s_crash_ft_strjoin_null_both));
+
+ /* two normal strings */
+ {
+ char *p = ft_strjoin ("hello ", "world");
+ _s_check ("basic", p && strcmp (p, "hello world") == 0);
+ free (p);
+ }
+
+ /* first string empty */
+ {
+ char *p = ft_strjoin ("", "world");
+ _s_check ("s1 empty", p && strcmp (p, "world") == 0);
+ free (p);
+ }
+
+ /* second string empty */
+ {
+ char *p = ft_strjoin ("hello", "");
+ _s_check ("s2 empty", p && strcmp (p, "hello") == 0);
+ free (p);
+ }
+
+ /* both empty */
+ {
+ char *p = ft_strjoin ("", "");
+ _s_check ("both empty", p && strcmp (p, "") == 0);
+ free (p);
+ }
+
+ /* returns independent copy */
+ {
+ char s1[] = "abc";
+ char s2[] = "def";
+ char *p = ft_strjoin (s1, s2);
+ _s_check ("independent ptr", p && p != s1 && p != s2);
+ s1[0] = 'X';
+ _s_check ("mutation safe", p[0] == 'a');
+ free (p);
+ }
+
+ /* randomized */
+ for (i = 0; i < _S_RAND_ITERS; i++)
+ {
+ int len1 = rand () % 200 + 1;
+ int len2 = rand () % 200 + 1;
+ char *s1 = malloc (len1 + 1);
+ char *s2 = malloc (len2 + 1);
+ char *expected;
+ char *p;
+ int j;
+
+ if (!s1 || !s2)
+ {
+ free (s1);
+ free (s2);
+ continue;
+ }
+ for (j = 0; j < len1; j++)
+ s1[j] = 'A' + rand () % 26;
+ s1[len1] = '\0';
+ for (j = 0; j < len2; j++)
+ s2[j] = 'a' + rand () % 26;
+ s2[len2] = '\0';
+
+ expected = malloc (len1 + len2 + 1);
+ if (!expected)
+ {
+ free (s1);
+ free (s2);
+ continue;
+ }
+ memcpy (expected, s1, len1);
+ memcpy (expected + len1, s2, len2 + 1);
+
+ p = ft_strjoin (s1, s2);
+ snprintf (label, sizeof (label), "random len1=%d len2=%d", len1, len2);
+ _s_check (label, p && strcmp (p, expected) == 0);
+ free (s1);
+ free (s2);
+ free (expected);
+ free (p);
+ }
+}
+
+int
+main (void)
+{
+ srand (time (NULL));
+ _s_test_strjoin ();
+ _s_print_results ();
+ return (_s_fail != 0);
+}