tst-width.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* Test integer width macros.
  2. Copyright (C) 2016-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <limits.h>
  16. #include <stdio.h>
  17. #define CHECK_WIDTH(TYPE, MAX, WIDTH) \
  18. do \
  19. { \
  20. if ((MAX >> ((TYPE) -1 < 0 ? (WIDTH - 2) : (WIDTH - 1))) != 1) \
  21. { \
  22. puts ("bad width of " #TYPE); \
  23. result = 1; \
  24. } \
  25. else \
  26. puts ("width of " #TYPE " OK"); \
  27. } \
  28. while (0)
  29. static int
  30. do_test (void)
  31. {
  32. int result = 0;
  33. #ifndef CHAR_WIDTH
  34. # error "missing CHAR_WIDTH"
  35. #endif
  36. CHECK_WIDTH (char, CHAR_MAX, CHAR_WIDTH);
  37. #ifndef SCHAR_WIDTH
  38. # error "missing SCHAR_WIDTH"
  39. #endif
  40. CHECK_WIDTH (signed char, SCHAR_MAX, SCHAR_WIDTH);
  41. #ifndef UCHAR_WIDTH
  42. # error "missing UCHAR_WIDTH"
  43. #endif
  44. CHECK_WIDTH (unsigned char, UCHAR_MAX, UCHAR_WIDTH);
  45. #ifndef SHRT_WIDTH
  46. # error "missing SHRT_WIDTH"
  47. #endif
  48. CHECK_WIDTH (signed short, SHRT_MAX, SHRT_WIDTH);
  49. #ifndef USHRT_WIDTH
  50. # error "missing USHRT_WIDTH"
  51. #endif
  52. CHECK_WIDTH (unsigned short, USHRT_MAX, USHRT_WIDTH);
  53. #ifndef INT_WIDTH
  54. # error "missing INT_WIDTH"
  55. #endif
  56. CHECK_WIDTH (signed int, INT_MAX, INT_WIDTH);
  57. #ifndef UINT_WIDTH
  58. # error "missing UINT_WIDTH"
  59. #endif
  60. CHECK_WIDTH (unsigned int, UINT_MAX, UINT_WIDTH);
  61. #ifndef LONG_WIDTH
  62. # error "missing LONG_WIDTH"
  63. #endif
  64. CHECK_WIDTH (signed long, LONG_MAX, LONG_WIDTH);
  65. #ifndef ULONG_WIDTH
  66. # error "missing ULONG_WIDTH"
  67. #endif
  68. CHECK_WIDTH (unsigned long, ULONG_MAX, ULONG_WIDTH);
  69. #ifndef LLONG_WIDTH
  70. # error "missing LLONG_WIDTH"
  71. #endif
  72. CHECK_WIDTH (signed long long, LLONG_MAX, LLONG_WIDTH);
  73. #ifndef ULLONG_WIDTH
  74. # error "missing ULLONG_WIDTH"
  75. #endif
  76. CHECK_WIDTH (unsigned long long, ULLONG_MAX, ULLONG_WIDTH);
  77. return result;
  78. }
  79. #define TEST_FUNCTION do_test ()
  80. #include "../test-skeleton.c"