#include "libft.h" #include "test_utils.h" #include _S_CRASH_V (ft_striteri_null, ft_striteri (NULL, NULL)) static void _s_to_upper (unsigned int i, char *c) { (void)i; *c = toupper (*c); } static void _s_add_index (unsigned int i, char *c) { *c = *c + i; } static void _s_test_striteri (void) { int i; char label[128]; _s_section ("ft_striteri"); /* NULL crashes */ _s_check ("NULL crashes", _s_crashes (_s_crash_ft_striteri_null)); /* basic: to uppercase in place */ { char s[] = "hello"; ft_striteri (s, _s_to_upper); _s_check ("to_upper", strcmp (s, "HELLO") == 0); } /* function receives correct index */ { char s[] = "aaaa"; ft_striteri (s, _s_add_index); _s_check ("index passed", s[0] == 'a' && s[1] == 'b' && s[2] == 'c' && s[3] == 'd'); } /* empty string: no crash, no change */ { char s[] = ""; ft_striteri (s, _s_to_upper); _s_check ("empty", strcmp (s, "") == 0); } /* single character */ { char s[] = "a"; ft_striteri (s, _s_to_upper); _s_check ("single char", strcmp (s, "A") == 0); } /* modifies in place (same pointer) */ { char s[] = "hello"; char *orig = s; ft_striteri (s, _s_to_upper); _s_check ("in place", s == orig); } /* 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); 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'; ft_striteri (src, _s_to_upper); snprintf (label, sizeof (label), "random len=%d", len); _s_check (label, strcmp (src, expected) == 0); free (src); free (expected); } } int main (void) { srand (time (NULL)); _s_test_striteri (); _s_print_results (); return (_s_fail != 0); }