ctype.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 isblank;
  9. int iscntrl;
  10. int isdigit;
  11. int isgraph;
  12. int islower;
  13. int isprint;
  14. int ispunct;
  15. int isspace;
  16. int isupper;
  17. int isxdigit;
  18. } test_cases[] = {
  19. // a a a b c d g l p p s u x
  20. // l l s l n i r o r u p p d
  21. // n p c a t g a w i n a p i
  22. // u h i n r i p e n c c e g
  23. // m a i k l t h r t t e r i
  24. { 'A', 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1},
  25. { 'z', 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0},
  26. { ' ', 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0},
  27. { '1', 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1},
  28. { '9', 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1},
  29. {0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
  30. };
  31. size_t num_test_cases = sizeof(test_cases)/sizeof(struct test_case);
  32. #define CHECK_TEST(tc, fn, retval) \
  33. if (fn(tc.c) != tc.fn) { \
  34. retval = -1; \
  35. printf("Unexpected result: " #fn "('%c') != %d\n", tc.c, tc.fn); \
  36. }
  37. int main(int argc, char* argv[]) {
  38. int i;
  39. int retval = 0;
  40. for(i = 0; i < num_test_cases; ++i) {
  41. struct test_case tc = test_cases[i];
  42. CHECK_TEST(tc, isalnum, retval);
  43. CHECK_TEST(tc, isalpha, retval);
  44. CHECK_TEST(tc, isascii, retval);
  45. CHECK_TEST(tc, isdigit, retval);
  46. CHECK_TEST(tc, islower, retval);
  47. CHECK_TEST(tc, isspace, retval);
  48. CHECK_TEST(tc, isupper, retval);
  49. }
  50. if (!retval) {
  51. printf("Success: %d\n", retval);
  52. } else {
  53. printf("Failure: %d\n", retval);
  54. }
  55. return retval;
  56. }