test_INT_format.h.in 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*============================================================================
  2. Kitware Information Macro Library
  3. Copyright 2010-2011 Kitware, Inc.
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include <stdio.h>
  11. #include <string.h>
  12. #ifdef __cplusplus
  13. # define LANG "C++ "
  14. #else
  15. # define LANG "C "
  16. #endif
  17. #define VALUE(T, U) (T)((U)0xab << ((sizeof(T)-1)<<3))
  18. #define TEST_C_(C, V, PRI, T, U) \
  19. { \
  20. T const x = VALUE(T, U); \
  21. T y = C(V); \
  22. printf(LANG #C ":" \
  23. " expression [%" @KWIML@_INT_PRI##PRI "]," \
  24. " literal [%" @KWIML@_INT_PRI##PRI "]", x, y); \
  25. if(x == y) \
  26. { \
  27. printf(", PASSED\n"); \
  28. } \
  29. else \
  30. { \
  31. printf(", FAILED\n"); \
  32. result = 0; \
  33. } \
  34. }
  35. #define TEST_PRI_(PRI, T, U, STR) \
  36. { \
  37. T const x = VALUE(T, U); \
  38. char const* str = STR; \
  39. sprintf(buf, "%" @KWIML@_INT_PRI##PRI, x); \
  40. printf(LANG "@KWIML@_INT_PRI" #PRI ":" \
  41. " expected [%s], got [%s]", str, buf); \
  42. if(strcmp(str, buf) == 0) \
  43. { \
  44. printf(", PASSED\n"); \
  45. } \
  46. else \
  47. { \
  48. printf(", FAILED\n"); \
  49. result = 0; \
  50. } \
  51. }
  52. #define TEST_SCN_(SCN, T, U, STR) TEST_SCN2_(SCN, SCN, T, U, STR)
  53. #define TEST_SCN2_(PRI, SCN, T, U, STR) \
  54. { \
  55. T const x = VALUE(T, U); \
  56. T y; \
  57. char const* str = STR; \
  58. if(sscanf(str, "%" @KWIML@_INT_SCN##SCN, &y) != 1) \
  59. { \
  60. y = 0; \
  61. } \
  62. printf(LANG "@KWIML@_INT_SCN" #SCN ":" \
  63. " expected [%" @KWIML@_INT_PRI##PRI "]," \
  64. " got [%" @KWIML@_INT_PRI##PRI "]", x, y); \
  65. if(x == y) \
  66. { \
  67. printf(", PASSED\n"); \
  68. } \
  69. else \
  70. { \
  71. printf(", FAILED\n"); \
  72. result = 0; \
  73. } \
  74. }
  75. #define TEST_(FMT, T, U, STR) TEST2_(FMT, FMT, T, U, STR)
  76. #define TEST2_(PRI, SCN, T, U, STR) \
  77. TEST_PRI_(PRI, T, U, STR) \
  78. TEST_SCN2_(PRI, SCN, T, U, STR)
  79. /* Concatenate T and U now to avoid expanding them. */
  80. #define TEST(FMT, T, U, STR) \
  81. TEST_(FMT, @KWIML@_INT_##T, @KWIML@_INT_##U, STR)
  82. #define TEST2(PRI, SCN, T, U, STR) \
  83. TEST2_(PRI, SCN, @KWIML@_INT_##T, @KWIML@_INT_##U, STR)
  84. #define TEST_C(C, V, PRI, T, U) \
  85. TEST_C_(@KWIML@_INT_##C, V, PRI, @KWIML@_INT_##T, @KWIML@_INT_##U)
  86. #define TEST_PRI(PRI, T, U, STR) \
  87. TEST_PRI_(PRI, @KWIML@_INT_##T, @KWIML@_INT_##U, STR)
  88. #define TEST_SCN(SCN, T, U, STR) \
  89. TEST_SCN_(SCN, @KWIML@_INT_##T, @KWIML@_INT_##U, STR)
  90. #define TEST_SCN2(PRI, SCN, T, U, STR) \
  91. TEST_SCN2_(PRI, SCN, @KWIML@_INT_##T, @KWIML@_INT_##U, STR)
  92. static int test_INT_format(void)
  93. {
  94. int result = 1;
  95. char buf[256];
  96. TEST_PRI(i8, int8_t, uint8_t, "-85")
  97. #if defined(@KWIML@_INT_SCNi8)
  98. TEST_SCN(i8, int8_t, uint8_t, "-85")
  99. #endif
  100. TEST_PRI(d8, int8_t, uint8_t, "-85")
  101. #if defined(@KWIML@_INT_SCNd8)
  102. TEST_SCN(d8, int8_t, uint8_t, "-85")
  103. #endif
  104. TEST_PRI(o8, uint8_t, uint8_t, "253")
  105. #if defined(@KWIML@_INT_SCNo8)
  106. TEST_SCN(o8, uint8_t, uint8_t, "253")
  107. #endif
  108. TEST_PRI(u8, uint8_t, uint8_t, "171")
  109. #if defined(@KWIML@_INT_SCNu8)
  110. TEST_SCN(u8, uint8_t, uint8_t, "171")
  111. #endif
  112. TEST_PRI(x8, uint8_t, uint8_t, "ab")
  113. TEST_PRI(X8, uint8_t, uint8_t, "AB")
  114. #if defined(@KWIML@_INT_SCNx8)
  115. TEST_SCN(x8, uint8_t, uint8_t, "ab")
  116. TEST_SCN2(X8, x8, uint8_t, uint8_t, "AB")
  117. #endif
  118. TEST(i16, int16_t, uint16_t, "-21760")
  119. TEST(d16, int16_t, uint16_t, "-21760")
  120. TEST(o16, uint16_t, uint16_t, "125400")
  121. TEST(u16, uint16_t, uint16_t, "43776")
  122. TEST(x16, uint16_t, uint16_t, "ab00")
  123. TEST2(X16, x16, uint16_t, uint16_t, "AB00")
  124. TEST(i32, int32_t, uint32_t, "-1426063360")
  125. TEST(d32, int32_t, uint32_t, "-1426063360")
  126. TEST(o32, uint32_t, uint32_t, "25300000000")
  127. TEST(u32, uint32_t, uint32_t, "2868903936")
  128. TEST(x32, uint32_t, uint32_t, "ab000000")
  129. TEST2(X32, x32, uint32_t, uint32_t, "AB000000")
  130. TEST_PRI(i64, int64_t, uint64_t, "-6124895493223874560")
  131. #if defined(@KWIML@_INT_SCNi64)
  132. TEST_SCN(i64, int64_t, uint64_t, "-6124895493223874560")
  133. #endif
  134. TEST_PRI(d64, int64_t, uint64_t, "-6124895493223874560")
  135. #if defined(@KWIML@_INT_SCNd64)
  136. TEST_SCN(d64, int64_t, uint64_t, "-6124895493223874560")
  137. #endif
  138. TEST_PRI(o64, uint64_t, uint64_t, "1254000000000000000000")
  139. #if defined(@KWIML@_INT_SCNo64)
  140. TEST_SCN(o64, uint64_t, uint64_t, "1254000000000000000000")
  141. #endif
  142. TEST_PRI(u64, uint64_t, uint64_t, "12321848580485677056")
  143. #if defined(@KWIML@_INT_SCNu64)
  144. TEST_SCN(u64, uint64_t, uint64_t, "12321848580485677056")
  145. #endif
  146. TEST_PRI(x64, uint64_t, uint64_t, "ab00000000000000")
  147. TEST_PRI(X64, uint64_t, uint64_t, "AB00000000000000")
  148. #if defined(@KWIML@_INT_SCNx64)
  149. TEST_SCN(x64, uint64_t, uint64_t, "ab00000000000000")
  150. TEST_SCN2(X64, x64, uint64_t, uint64_t, "AB00000000000000")
  151. #endif
  152. #if !defined(@KWIML@_INT_NO_INTPTR_T)
  153. # if @KWIML@_ABI_SIZEOF_DATA_PTR == 4
  154. TEST(iPTR, intptr_t, uint32_t, "-1426063360")
  155. TEST(dPTR, intptr_t, uint32_t, "-1426063360")
  156. # else
  157. TEST(iPTR, intptr_t, uint64_t, "-6124895493223874560")
  158. TEST(dPTR, intptr_t, uint64_t, "-6124895493223874560")
  159. # endif
  160. #endif
  161. #if !defined(@KWIML@_INT_NO_UINTPTR_T)
  162. # if @KWIML@_ABI_SIZEOF_DATA_PTR == 4
  163. TEST(oPTR, uintptr_t, uintptr_t, "25300000000")
  164. TEST(uPTR, uintptr_t, uintptr_t, "2868903936")
  165. TEST(xPTR, uintptr_t, uintptr_t, "ab000000")
  166. TEST2(XPTR, xPTR, uintptr_t, uintptr_t, "AB000000")
  167. # else
  168. TEST(oPTR, uintptr_t, uintptr_t, "1254000000000000000000")
  169. TEST(uPTR, uintptr_t, uintptr_t, "12321848580485677056")
  170. TEST(xPTR, uintptr_t, uintptr_t, "ab00000000000000")
  171. TEST2(XPTR, xPTR, uintptr_t, uintptr_t, "AB00000000000000")
  172. # endif
  173. #endif
  174. TEST_C(INT8_C, -0x55, i8, int8_t, uint8_t)
  175. TEST_C(UINT8_C, 0xAB, u8, uint8_t, uint8_t)
  176. TEST_C(INT16_C, -0x5500, i16, int16_t, uint16_t)
  177. TEST_C(UINT16_C, 0xAB00, u16, uint16_t, uint16_t)
  178. TEST_C(INT32_C, -0x55000000, i32, int32_t, uint32_t)
  179. TEST_C(UINT32_C, 0xAB000000, u32, uint32_t, uint32_t)
  180. TEST_C(INT64_C, -0x5500000000000000, i64, int64_t, uint64_t)
  181. TEST_C(UINT64_C, 0xAB00000000000000, u64, uint64_t, uint64_t)
  182. return result;
  183. }