#include "libft.h" #include "test_utils.h" _S_CRASH (ft_split_null, ft_split (NULL, ' ')) static void _s_free_split (char **arr) { size_t i; if (!arr) return; i = 0; while (arr[i]) free (arr[i++]); free (arr); } static size_t _s_arr_len (char **arr) { size_t n; n = 0; while (arr[n]) n++; return (n); } static void _s_test_split (void) { int i; char label[128]; _s_section ("ft_split"); /* NULL crashes */ _s_check ("NULL crashes", _s_crashes (_s_crash_ft_split_null)); /* basic split */ { char **arr = ft_split ("hello world foo", ' '); _s_check ("basic count", arr && _s_arr_len (arr) == 3); _s_check ("basic [0]", arr && strcmp (arr[0], "hello") == 0); _s_check ("basic [1]", arr && strcmp (arr[1], "world") == 0); _s_check ("basic [2]", arr && strcmp (arr[2], "foo") == 0); _s_check ("basic NULL-term", arr && arr[3] == NULL); _s_free_split (arr); } /* leading delimiters */ { char **arr = ft_split (" hello", ' '); _s_check ("leading delim count", arr && _s_arr_len (arr) == 1); _s_check ("leading delim [0]", arr && strcmp (arr[0], "hello") == 0); _s_free_split (arr); } /* trailing delimiters */ { char **arr = ft_split ("hello ", ' '); _s_check ("trailing delim count", arr && _s_arr_len (arr) == 1); _s_check ("trailing delim [0]", arr && strcmp (arr[0], "hello") == 0); _s_free_split (arr); } /* consecutive delimiters */ { char **arr = ft_split ("a,,b,,c", ','); _s_check ("consec count", arr && _s_arr_len (arr) == 3); _s_check ("consec [0]", arr && strcmp (arr[0], "a") == 0); _s_check ("consec [1]", arr && strcmp (arr[1], "b") == 0); _s_check ("consec [2]", arr && strcmp (arr[2], "c") == 0); _s_free_split (arr); } /* no delimiter in string */ { char **arr = ft_split ("hello", ' '); _s_check ("no delim count", arr && _s_arr_len (arr) == 1); _s_check ("no delim [0]", arr && strcmp (arr[0], "hello") == 0); _s_free_split (arr); } /* empty string */ { char **arr = ft_split ("", ' '); _s_check ("empty count", arr && _s_arr_len (arr) == 0); _s_check ("empty NULL-term", arr && arr[0] == NULL); _s_free_split (arr); } /* string is only delimiters */ { char **arr = ft_split (" ", ' '); _s_check ("only delims count", arr && _s_arr_len (arr) == 0); _s_check ("only delims NULL-term", arr && arr[0] == NULL); _s_free_split (arr); } /* single character string, is delimiter */ { char **arr = ft_split (",", ','); _s_check ("single delim count", arr && _s_arr_len (arr) == 0); _s_free_split (arr); } /* single character string, not delimiter */ { char **arr = ft_split ("a", ','); _s_check ("single non-delim count", arr && _s_arr_len (arr) == 1); _s_check ("single non-delim [0]", arr && strcmp (arr[0], "a") == 0); _s_free_split (arr); } /* delimiter '\0' — whole string is one word */ { char **arr = ft_split ("hello", '\0'); _s_check ("nul delim count", arr && _s_arr_len (arr) == 1); _s_check ("nul delim [0]", arr && strcmp (arr[0], "hello") == 0); _s_free_split (arr); } /* each word is independently allocated */ { char **arr = ft_split ("a b c", ' '); _s_check ("independent ptrs", arr && arr[0] != arr[1] && arr[1] != arr[2]); _s_free_split (arr); } /* randomized: build string from words, split, verify */ for (i = 0; i < _S_RAND_ITERS; i++) { int nwords = rand () % 10 + 1; int pads = rand () % 5; char **words = malloc (nwords * sizeof (char *)); char *src; char **arr; size_t total; int j, k; int ok; if (!words) continue; /* generate random words (letters only) */ total = 0; ok = 1; for (j = 0; j < nwords; j++) { int wlen = rand () % 20 + 1; words[j] = malloc (wlen + 1); if (!words[j]) { ok = 0; break; } for (k = 0; k < wlen; k++) words[j][k] = 'A' + rand () % 26; words[j][wlen] = '\0'; total += wlen; } if (!ok) { for (k = 0; k < j; k++) free (words[k]); free (words); continue; } /* build: pads + word + delimiters + ... + pads */ total += (nwords - 1) + 2 * pads; src = malloc (total + 1); if (!src) { for (j = 0; j < nwords; j++) free (words[j]); free (words); continue; } k = 0; for (j = 0; j < pads; j++) src[k++] = ','; for (j = 0; j < nwords; j++) { size_t wlen = strlen (words[j]); memcpy (src + k, words[j], wlen); k += wlen; if (j < nwords - 1) src[k++] = ','; } for (j = 0; j < pads; j++) src[k++] = ','; src[k] = '\0'; arr = ft_split (src, ','); snprintf (label, sizeof (label), "random nwords=%d pads=%d", nwords, pads); if (!arr || (int)_s_arr_len (arr) != nwords) { _s_check (label, 0); } else { int match = 1; for (j = 0; j < nwords; j++) { if (strcmp (arr[j], words[j]) != 0) { match = 0; break; } } _s_check (label, match && arr[nwords] == NULL); } for (j = 0; j < nwords; j++) free (words[j]); free (words); free (src); _s_free_split (arr); } } int main (void) { srand (time (NULL)); _s_test_split (); _s_print_results (); return (_s_fail != 0); }