-
Notifications
You must be signed in to change notification settings - Fork 1
/
BiNumsTest.cpp
396 lines (346 loc) · 12.5 KB
/
BiNumsTest.cpp
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// BiNums, see binary numbers
#include "precomp.h"
#if _WIN32
#define NOMINMAX
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#endif
extern int MainImplementation(std::string_view commandLine, std::string& stringOutput);
////////////////////////////////////////////////////////////////////////////////
// Generic functions/classes.
bool StringsMatch(char const* a, char const* b)
{
return strcmp(a, b) == 0;
}
// TODO: Add VTT codes for Linux.
struct SetAndSaveConsoleAttribute
{
#ifdef _WIN32
CONSOLE_SCREEN_BUFFER_INFO consoleInfo_;
HANDLE hConsole_;
#endif
SetAndSaveConsoleAttribute()
{
#ifdef _WIN32
hConsole_ = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsole_, &consoleInfo_);
#endif
}
SetAndSaveConsoleAttribute(WORD newAttributes) : SetAndSaveConsoleAttribute()
{
UpdateAttributes(newAttributes);
}
SetAndSaveConsoleAttribute(WORD setAttributes, WORD clearAttributes) : SetAndSaveConsoleAttribute()
{
UpdateAttributes(setAttributes, clearAttributes);
}
void UpdateForegroundColor(WORD foregroundColor)
{
UpdateAttributes(foregroundColor & 0x000F, 0x000F);
}
void UpdateAttributes(WORD newAttributes)
{
#ifdef _WIN32
SetConsoleTextAttribute(hConsole_, newAttributes);
#endif
}
void UpdateAttributes(WORD setAttributes, WORD clearAttributes)
{
#ifdef _WIN32
SetConsoleTextAttribute(hConsole_, (consoleInfo_.wAttributes & ~clearAttributes) | setAttributes);
#endif
}
void Reset()
{
#ifdef _WIN32
SetConsoleTextAttribute(hConsole_, consoleInfo_.wAttributes);
#endif
}
~SetAndSaveConsoleAttribute()
{
#ifdef _WIN32
SetConsoleTextAttribute(hConsole_, consoleInfo_.wAttributes);
#endif
}
};
struct BiNumsTest
{
bool shouldRegenerateExpectedBaseline = false;
};
BiNumsTest g_test;
int ParseCommandLineParameters(int argc, char* argv[])
{
for (int i = 1; i < argc; ++i)
{
char const* argument = argv[i];
if (StringsMatch(argument, "/?") || StringsMatch(argument, "-h") || StringsMatch(argument, "-help"))
{
printf(
"Run without any arguments to execute the tests.\n"
"Failed test results go into 'TestResults\\'.\n"
"\n"
"regenerate : regenerate the test cases (use git diff afterward to verify differences)\n"
);
return EXIT_FAILURE;
}
else if (StringsMatch(argument, "regenerate"))
{
g_test.shouldRegenerateExpectedBaseline = true;
}
else
{
printf(
"Unrecognized parameter: %s\n"
"Type -help for help.",
argument
);
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
void PrintIndentedText(
std::string_view text,
std::string_view indentPrefix
)
{
if (indentPrefix.empty())
{
fwrite(text.data(), 1, text.size(), stdout);
}
std::string indentedText;
for (auto p = text.begin(), end = text.end(); p != end; )
{
indentedText.append(indentPrefix);
auto endOfLine = std::find(p, end, '\n');
if (endOfLine != end)
{
++endOfLine;
}
indentedText.append(&*p, endOfLine - p);
p = endOfLine;
}
if (indentedText.empty() || indentedText.back() != '\n')
{
indentedText.push_back('\n');
}
fwrite(indentedText.data(), 1, indentedText.size(), stdout);
}
bool CompareExpectedVsActual(
std::string_view testTitle,
std::string_view actual,
std::string_view expected
)
{
bool stringsMatch = (expected == actual);
// Display colored OK/FAILED.
SetAndSaveConsoleAttribute consoleAttributes;
consoleAttributes.UpdateForegroundColor(stringsMatch ? FOREGROUND_GREEN : FOREGROUND_RED);
printf(stringsMatch ? "OK " : "FAILED");
consoleAttributes.Reset();
printf(" %.*s\n", static_cast<int>(testTitle.size()), testTitle.data());
if (!stringsMatch)
{
printf(" ");
consoleAttributes.UpdateAttributes(COMMON_LVB_UNDERSCORE);
printf("Expected output:\n");
consoleAttributes.Reset();
PrintIndentedText(expected, " |");
printf(" ");
consoleAttributes.UpdateAttributes(COMMON_LVB_UNDERSCORE);
printf("Actual output:\n");
consoleAttributes.Reset();
PrintIndentedText(actual, " |");
}
return stringsMatch;
}
bool VerifyFloatingTypes()
{
constexpr double testNumbersFloat8f3e4s1[] = {
0.0f,
1.0f,
-1.0f,
0.5f,
-0.5f,
224.0f,
-224.0f,
std::numeric_limits<float>::quiet_NaN(),
-std::numeric_limits<float>::quiet_NaN(),
// Exclude infinity because this type doesn't have it.
// So it would not round-trip successfully.
// std::numeric_limits<float>::infinity(),
//-std::numeric_limits<float>::infinity()
};
constexpr double testNumbersFloat8f2e5s1[] = {
0.0f,
1.0f,
-1.0f,
0.5f,
-0.5f,
224.0f, // Maximum value
-224.0f, // Maximum value
std::numeric_limits<float>::quiet_NaN(),
-std::numeric_limits<float>::quiet_NaN(),
std::numeric_limits<float>::infinity(),
-std::numeric_limits<float>::infinity()
};
constexpr double testNumbersFloat16[] = {
0.0f,
1.0f,
-1.0f,
0.5f,
-0.5f,
65504.0f, // Maximum precise value
-65504.0f, // Maximum precise value
std::numeric_limits<float>::quiet_NaN(),
-std::numeric_limits<float>::quiet_NaN(),
std::numeric_limits<float>::infinity(),
-std::numeric_limits<float>::infinity()
};
constexpr double testNumbersBFloat16[] = {
0.0f,
1.0f,
-1.0f,
0.5f,
-0.5f,
65280.0f, // Maximum value
-65280.0f, // Maximum value
std::numeric_limits<float>::quiet_NaN(),
-std::numeric_limits<float>::quiet_NaN(),
std::numeric_limits<float>::infinity(),
-std::numeric_limits<float>::infinity()
};
constexpr double testNumbersFloat32[] = {
0.0,
1.0,
-1.0,
0.5,
-0.5,
65504.0,
-65504.0,
16777216.0, // Maximum precise value
-16777216.0, // Maximum precise value
std::numeric_limits<float>::min(),
-std::numeric_limits<float>::min(),
std::numeric_limits<float>::max(),
-std::numeric_limits<float>::max(),
std::numeric_limits<float>::quiet_NaN(),
-std::numeric_limits<float>::quiet_NaN(),
std::numeric_limits<float>::infinity(),
-std::numeric_limits<float>::infinity()
};
constexpr auto& testNumbersFloat64 = testNumbersFloat32;
bool success = true;
SetAndSaveConsoleAttribute consoleAttributes;
auto PrintResult = [&](char const* title, double a, double b)
{
bool valuesMatch = (a == b) || (std::isnan(a) && std::isnan(b));
success &= valuesMatch;
consoleAttributes.UpdateForegroundColor(valuesMatch ? FOREGROUND_GREEN : FOREGROUND_RED);
printf(
valuesMatch ? "OK - %s - %g == %g\n"
: "FAILED - %s - %g != %g\n",
title,
a,
b
);
consoleAttributes.Reset();
};
// TODO: Fix FloatNumber to handle atypical NaN case with a single NaN representation.
for (double originalValue : testNumbersFloat8f3e4s1)
{
FloatNumber<uint8_t, 3, 4, true, true, false, true> convertedValue = float(originalValue);
float reconvertedValue = convertedValue;
PrintResult("float32 to float8m3e4s1", originalValue, reconvertedValue);
}
for (double originalValue : testNumbersFloat8f2e5s1)
{
FloatNumber<uint8_t, 2, 5, true, true, true, true> convertedValue = float(originalValue);
float reconvertedValue = convertedValue;
PrintResult("float32 to float8m2e5s1", originalValue, reconvertedValue);
}
for (double originalValue : testNumbersFloat16)
{
FloatNumber<uint16_t, 10, 5, true, true, true, true> convertedValue = float(originalValue);
float reconvertedValue = convertedValue;
PrintResult("float32 to float16", originalValue, reconvertedValue);
}
for (double originalValue : testNumbersFloat16)
{
FloatNumber<uint16_t, 10, 5, true, true, true, true> convertedValue(originalValue);
double reconvertedValue = convertedValue;
PrintResult("float64 to float16", originalValue, reconvertedValue);
}
for (double originalValue : testNumbersBFloat16)
{
FloatNumber<uint16_t, 7, 8, true, true, true, true> convertedValue = float(originalValue);
float reconvertedValue = convertedValue;
PrintResult("float32 to bfloat16", originalValue, reconvertedValue);
}
for (double originalValue : testNumbersBFloat16)
{
FloatNumber<uint16_t, 10, 5, true, true, true, true> convertedValue = float(originalValue);
FloatNumber<uint16_t, 7, 8, true, true, true, true> convertedValue2 = float(convertedValue);
FloatNumber<uint16_t, 10, 5, true, true, true, true> convertedValue3 = float(convertedValue2);
float reconvertedValue = convertedValue3;
PrintResult("float16 to bfloat16", originalValue, reconvertedValue);
}
for (double originalValue : testNumbersFloat32)
{
FloatNumber<uint32_t, 23, 8, true, true, true, true> convertedValue = float(originalValue);
float reconvertedValue = convertedValue;
PrintResult("float32 to float32", originalValue, reconvertedValue);
}
for (double originalValue : testNumbersFloat32)
{
FloatNumber<uint32_t, 23, 8, true, true, true, true> convertedValue(originalValue);
double reconvertedValue = convertedValue;
PrintResult("float64 to float32", originalValue, reconvertedValue);
}
// The custom float64 should behave identical to double (or std::float64_t).
for (double originalValue : testNumbersFloat64)
{
FloatNumber<uint64_t, 52, 11, true, true, true, true> convertedValue(originalValue);
double reconvertedValue = convertedValue;
PrintResult("float64 to float64", originalValue, reconvertedValue);
}
return success;
}
int main(int argc, char* argv[])
{
printf("*** This test suite is just a skeleton for now. ***\n\n");
auto exitCode = ParseCommandLineParameters(argc, argv);
if (exitCode != EXIT_SUCCESS)
{
return exitCode;
}
// Placeholder for real tests, which should be data driven in a test file,
// preferably JSON or XML (XML might simpler given all the new lines).
std::string stringOutput;
exitCode = MainImplementation("uint8 42 int8 -42 uint16 42 int16 -42 uint32 42 int32 -42 uint64 42 int64 -42 fixed12_12 -42.25 fixed16_16 -42.25 fixed8_24 -42.25 float16 -42.25 float32 -42.25 float64 -42.25", stringOutput);
uint32_t errorCount = 0;
auto CheckFailure = [&](bool success)
{
errorCount += success ? 0 : 1;
};
char const* expectedOutput =
" uint8 42 (0x2A)\n"
" int8 -42 (0xD6)\n"
" uint16 42 (0x002A)\n"
" int16 -42 (0xFFD6)\n"
" uint32 42 (0x0000002A)\n"
" int32 -42 (0xFFFFFFD6)\n"
" uint64 42 (0x000000000000002A)\n"
" int64 -42 (0xFFFFFFFFFFFFFFD6)\n"
" fixed12_12 -42.25 (0xFD5C00)\n"
" fixed16_16 -42.25 (0xFFD5C000)\n"
" fixed8_24 -42.25 (0xD5C00000)\n"
" float16 -42.25 (0xD148)\n"
" float32 -42.25 (0xC2290000)\n"
" float64 -42.25 (0xC045200000000000)\n"
;
CheckFailure(CompareExpectedVsActual("All data types", stringOutput, expectedOutput));
CheckFailure(CompareExpectedVsActual("Expected failure case to verify output comparison", stringOutput, "Gibberish just to verify failure"));
CheckFailure(VerifyFloatingTypes());
return EXIT_SUCCESS;
}