ctype.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 i;
  28. int retval = 0;
  29. for(i = 0; i < num_test_cases; ++i) {
  30. struct test_case tc = test_cases[i];
  31. CHECK_TEST(tc, isalnum, retval);
  32. CHECK_TEST(tc, isalpha, retval);
  33. CHECK_TEST(tc, isascii, retval);
  34. CHECK_TEST(tc, isdigit, retval);
  35. CHECK_TEST(tc, islower, retval);
  36. CHECK_TEST(tc, isspace, retval);
  37. CHECK_TEST(tc, isupper, retval);
  38. }
  39. if (!retval) {
  40. printf("Success: %d\n", retval);
  41. } else {
  42. printf("Failure: %d\n", retval);
  43. }
  44. return retval;
  45. }