ctype.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include <ctype.h>
  2. #include <stdio.h>
  3. struct test_case {
  4. char c;
  5. int isalnum;
  6. int isalpha;
  7. int isascii;
  8. int isdigit;
  9. int islower;
  10. int isspace;
  11. int isupper;
  12. } test_cases[] = {
  13. { 'A', 1, 1, 1, 0, 0, 0, 1},
  14. { 'z', 1, 1, 1, 0, 1, 0, 0},
  15. { ' ', 0, 0, 1, 0, 0, 1, 0},
  16. { '1', 1, 0, 1, 1, 0, 0, 0},
  17. { '9', 1, 0, 1, 1, 0, 0, 0},
  18. {0x80, 0, 0, 0, 0, 0, 0, 0}
  19. };
  20. size_t num_test_cases = sizeof(test_cases)/sizeof(struct test_case);
  21. #define CHECK_TEST(tc, fn, retval) \
  22. if (fn(tc.c) != tc.fn) { \
  23. retval = -1; \
  24. printf("Unexpected result: " #fn "('%c') != %d\n", tc.c, tc.fn); \
  25. }
  26. int main(int argc, char* argv[]) {
  27. int retval = 0;
  28. for(int i = 0; i < num_test_cases; ++i) {
  29. struct test_case tc = test_cases[i];
  30. CHECK_TEST(tc, isalnum, retval);
  31. CHECK_TEST(tc, isalpha, retval);
  32. CHECK_TEST(tc, isascii, retval);
  33. CHECK_TEST(tc, isdigit, retval);
  34. CHECK_TEST(tc, islower, retval);
  35. CHECK_TEST(tc, isspace, retval);
  36. CHECK_TEST(tc, isupper, retval);
  37. }
  38. if (!retval) {
  39. printf("Success: %d\n", retval);
  40. } else {
  41. printf("Failure: %d\n", retval);
  42. }
  43. return retval;
  44. }