#include "libft.h" #include "test_utils.h" #include static void _s_test_itoa (void) { int i; char label[128]; char expected[64]; _s_section ("ft_itoa"); /* zero */ { char *p = ft_itoa (0); _s_check ("zero", p && strcmp (p, "0") == 0); free (p); } /* positive */ { char *p = ft_itoa (42); _s_check ("positive", p && strcmp (p, "42") == 0); free (p); } /* negative */ { char *p = ft_itoa (-42); _s_check ("negative", p && strcmp (p, "-42") == 0); free (p); } /* INT_MAX */ { char *p = ft_itoa (INT_MAX); snprintf (expected, sizeof (expected), "%d", INT_MAX); _s_check ("INT_MAX", p && strcmp (p, expected) == 0); free (p); } /* INT_MIN */ { char *p = ft_itoa (INT_MIN); snprintf (expected, sizeof (expected), "%d", INT_MIN); _s_check ("INT_MIN", p && strcmp (p, expected) == 0); free (p); } /* single digit positive */ { char *p = ft_itoa (7); _s_check ("single digit", p && strcmp (p, "7") == 0); free (p); } /* single digit negative */ { char *p = ft_itoa (-1); _s_check ("neg single digit", p && strcmp (p, "-1") == 0); free (p); } /* powers of ten */ { char *p; p = ft_itoa (10); _s_check ("10", p && strcmp (p, "10") == 0); free (p); p = ft_itoa (100); _s_check ("100", p && strcmp (p, "100") == 0); free (p); p = ft_itoa (-1000); _s_check ("-1000", p && strcmp (p, "-1000") == 0); free (p); } /* randomized */ for (i = 0; i < _S_RAND_ITERS; i++) { int n = rand () - RAND_MAX / 2; char *p = ft_itoa (n); snprintf (expected, sizeof (expected), "%d", n); snprintf (label, sizeof (label), "random n=%d", n); _s_check (label, p && strcmp (p, expected) == 0); free (p); } } int main (void) { srand (time (NULL)); _s_test_itoa (); _s_print_results (); return (_s_fail != 0); }