summaryrefslogtreecommitdiffstats
path: root/Utilities/KWIML/test/test_INT_format.h.in
blob: 72a62f259ddf2e7bb20a87d315bc2b62d48d6ae0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*============================================================================
  Kitware Information Macro Library
  Copyright 2010-2011 Kitware, Inc.

  Distributed under the OSI-approved BSD License (the "License");
  see accompanying file Copyright.txt for details.

  This software is distributed WITHOUT ANY WARRANTY; without even the
  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  See the License for more information.
============================================================================*/
#include <stdio.h>
#include <string.h>

#ifdef __cplusplus
# define LANG "C++ "
#else
# define LANG "C "
#endif

#define VALUE(T, U)                                                     \
    (@KWIML@_INT_##T)((@KWIML@_INT_##U)0xab <<                          \
                    ((sizeof(@KWIML@_INT_##T)-1)<<3))                   \

#define TEST_C(C, V, PRI, T, U)                                         \
  {                                                                     \
  @KWIML@_INT_##T const x = VALUE(T, U);                                \
  @KWIML@_INT_##T y = @KWIML@_INT_##C(V);                               \
  printf(LANG "@KWIML@_INT_" #C ":"                                     \
         " expression [%"@KWIML@_INT_PRI##PRI"],"                       \
         " literal [%"@KWIML@_INT_PRI##PRI"]", x, y);                   \
  if(x == y)                                                            \
    {                                                                   \
    printf(", PASSED\n");                                               \
    }                                                                   \
  else                                                                  \
    {                                                                   \
    printf(", FAILED\n");                                               \
    result = 0;                                                         \
    }                                                                   \
  }

#define TEST_PRI(PRI, T, U, STR)                                        \
  {                                                                     \
  @KWIML@_INT_##T const x = VALUE(T, U);                                \
  char const* str = STR;                                                \
  sprintf(buf, "%"@KWIML@_INT_PRI##PRI, x);                             \
  printf(LANG "@KWIML@_INT_PRI" #PRI ":"                                \
         " expected [%s], got [%s]", str, buf);                         \
  if(strcmp(str, buf) == 0)                                             \
    {                                                                   \
    printf(", PASSED\n");                                               \
    }                                                                   \
  else                                                                  \
    {                                                                   \
    printf(", FAILED\n");                                               \
    result = 0;                                                         \
    }                                                                   \
  }

#define TEST_SCN(SCN, T, U, STR) TEST_SCN2(SCN, SCN, T, U, STR)
#define TEST_SCN2(PRI, SCN, T, U, STR)                                  \
  {                                                                     \
  @KWIML@_INT_##T const x = VALUE(T, U);                                \
  @KWIML@_INT_##T y;                                                    \
  char const* str = STR;                                                \
  if(sscanf(str, "%"@KWIML@_INT_SCN##SCN, &y) != 1)                     \
    {                                                                   \
    y = 0;                                                              \
    }                                                                   \
  printf(LANG "@KWIML@_INT_SCN" #SCN ":"                                \
         " expected [%"@KWIML@_INT_PRI##PRI"],"                         \
         " got [%"@KWIML@_INT_PRI##PRI"]", x, y);                       \
  if(x == y)                                                            \
    {                                                                   \
    printf(", PASSED\n");                                               \
    }                                                                   \
  else                                                                  \
    {                                                                   \
    printf(", FAILED\n");                                               \
    result = 0;                                                         \
    }                                                                   \
  }

#define TEST(FMT, T, U, STR) TEST2(FMT, FMT, T, U, STR)
#define TEST2(PRI, SCN, T, U, STR)                                       \
  TEST_PRI(PRI, T, U, STR)                                               \
  TEST_SCN2(PRI, SCN, T, U, STR)

static int test_INT_format(void)
{
  int result = 1;
  char buf[256];
  TEST_PRI(i8, int8_t, uint8_t, "-85")
#if defined(@KWIML@_INT_SCNi8)
  TEST_SCN(i8, int8_t, uint8_t, "-85")
#endif
  TEST_PRI(d8, int8_t, uint8_t, "-85")
#if defined(@KWIML@_INT_SCNd8)
  TEST_SCN(d8, int8_t, uint8_t, "-85")
#endif
  TEST_PRI(o8, uint8_t, uint8_t, "253")
#if defined(@KWIML@_INT_SCNo8)
  TEST_SCN(o8, uint8_t, uint8_t, "253")
#endif
  TEST_PRI(u8, uint8_t, uint8_t, "171")
#if defined(@KWIML@_INT_SCNu8)
  TEST_SCN(u8, uint8_t, uint8_t, "171")
#endif
  TEST_PRI(x8, uint8_t, uint8_t, "ab")
  TEST_PRI(X8, uint8_t, uint8_t, "AB")
#if defined(@KWIML@_INT_SCNx8)
  TEST_SCN(x8, uint8_t, uint8_t, "ab")
  TEST_SCN2(X8, x8, uint8_t, uint8_t, "AB")
#endif

  TEST(i16, int16_t, uint16_t, "-21760")
  TEST(d16, int16_t, uint16_t, "-21760")
  TEST(o16, uint16_t, uint16_t, "125400")
  TEST(u16, uint16_t, uint16_t, "43776")
  TEST(x16, uint16_t, uint16_t, "ab00")
  TEST2(X16, x16, uint16_t, uint16_t, "AB00")

  TEST(i32, int32_t, uint32_t, "-1426063360")
  TEST(d32, int32_t, uint32_t, "-1426063360")
  TEST(o32, uint32_t, uint32_t, "25300000000")
  TEST(u32, uint32_t, uint32_t, "2868903936")
  TEST(x32, uint32_t, uint32_t, "ab000000")
  TEST2(X32, x32, uint32_t, uint32_t, "AB000000")

  TEST_PRI(i64, int64_t, uint64_t, "-6124895493223874560")
#if defined(@KWIML@_INT_SCNi64)
  TEST_SCN(i64, int64_t, uint64_t, "-6124895493223874560")
#endif
  TEST_PRI(d64, int64_t, uint64_t, "-6124895493223874560")
#if defined(@KWIML@_INT_SCNd64)
  TEST_SCN(d64, int64_t, uint64_t, "-6124895493223874560")
#endif
  TEST_PRI(o64, uint64_t, uint64_t, "1254000000000000000000")
#if defined(@KWIML@_INT_SCNo64)
  TEST_SCN(o64, uint64_t, uint64_t, "1254000000000000000000")
#endif
  TEST_PRI(u64, uint64_t, uint64_t, "12321848580485677056")
#if defined(@KWIML@_INT_SCNu64)
  TEST_SCN(u64, uint64_t, uint64_t, "12321848580485677056")
#endif
  TEST_PRI(x64, uint64_t, uint64_t, "ab00000000000000")
  TEST_PRI(X64, uint64_t, uint64_t, "AB00000000000000")
#if defined(@KWIML@_INT_SCNx64)
  TEST_SCN(x64, uint64_t, uint64_t, "ab00000000000000")
  TEST_SCN2(X64, x64, uint64_t, uint64_t, "AB00000000000000")
#endif

#if !defined(@KWIML@_INT_NO_INTPTR_T)
# if @KWIML@_ABI_SIZEOF_DATA_PTR == 4
  TEST(iPTR, intptr_t, uint32_t, "-1426063360")
  TEST(dPTR, intptr_t, uint32_t, "-1426063360")
# else
  TEST(iPTR, intptr_t, uint64_t, "-6124895493223874560")
  TEST(dPTR, intptr_t, uint64_t, "-6124895493223874560")
# endif
#endif

#if !defined(@KWIML@_INT_NO_UINTPTR_T)
# if @KWIML@_ABI_SIZEOF_DATA_PTR == 4
  TEST(oPTR, uintptr_t, uintptr_t, "25300000000")
  TEST(uPTR, uintptr_t, uintptr_t, "2868903936")
  TEST(xPTR, uintptr_t, uintptr_t, "ab000000")
  TEST2(XPTR, xPTR, uintptr_t, uintptr_t, "AB000000")
# else
  TEST(oPTR, uintptr_t, uintptr_t, "1254000000000000000000")
  TEST(uPTR, uintptr_t, uintptr_t, "12321848580485677056")
  TEST(xPTR, uintptr_t, uintptr_t, "ab00000000000000")
  TEST2(XPTR, xPTR, uintptr_t, uintptr_t, "AB00000000000000")
# endif
#endif

  TEST_C(INT8_C,  -0x55, i8, int8_t, uint8_t)
  TEST_C(UINT8_C,  0xAB, u8, uint8_t, uint8_t)
  TEST_C(INT16_C, -0x5500, i16, int16_t, uint16_t)
  TEST_C(UINT16_C, 0xAB00, u16, uint16_t, uint16_t)
  TEST_C(INT32_C, -0x55000000, i32, int32_t, uint32_t)
  TEST_C(UINT32_C, 0xAB000000, u32, uint32_t, uint32_t)
  TEST_C(INT64_C, -0x5500000000000000, i64, int64_t, uint64_t)
  TEST_C(UINT64_C, 0xAB00000000000000, u64, uint64_t, uint64_t)

  return result;
}