forked from CloverHackyColor/CloverBootloader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoolprintfloat-test.cpp
More file actions
135 lines (115 loc) · 3.71 KB
/
Copy pathpoolprintfloat-test.cpp
File metadata and controls
135 lines (115 loc) · 3.71 KB
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
//
// main.cpp
// Printf-UnitTests
//
// Created by Jief on 29/08/17.
// Copyright © 2017 Jief. All rights reserved.
//
#include <Platform.h>
#include <limits.h>
#include "unicode_conversions.h"
#include <poolprint-test-cpp_conf.h>
#include "poolprint-test.h"
static int nbTestFailed = 0;
#ifdef DISPLAY_ONLY_FAILED
static bool displayOnlyFailed = true;
#else
static bool displayOnlyFailed = false;
#endif
/*
* Print wchar string as a utf8 string.
* This eliminate all problems about wprintf and compilation with short-wchar or long-wchar I had on macOs (2020-03)
*/
static void print_wchar_string(const wchar_t* s)
{
// char utf8[wchar_len(s)*4+1];
// some compiler doesn't like variable length array.
// use a fixed length instead.
char utf8[200];
utf8_string_from_wchar_string(utf8, sizeof(utf8), s);
if ( strlen(utf8) > sizeof(utf8)-2 ) {
loggf("fixed size buf not big enough");
abort();
}
loggf("%s", utf8);
}
VOID
EFIAPI
_PoolCatPrint (
IN CONST CHAR16 *fmt,
IN VA_LIST args,
IN OUT POOL_PRINT *spc,
IN EFI_STATUS
(EFIAPI
*Output)
(
POOL_PRINT *context,
CHAR16 *str
)
);
#include "../libeg/FloatLib.h"
static int testPoolPrintFloat(const char* label, const wchar_t* expectResult, float param)
{
wchar_t* wbuf = PoolPrintFloat(param);
if ( memcmp(wbuf, expectResult, wchar_len(expectResult)*sizeof(expectResult[0])) != 0 ) {
// loggf(F(" -> ERROR. Expect " PRILF " and get %ls\n"), expectResult, buf);
// not using wprintf, it crashes sometimes, it doesn't work for short-wchar
loggf(F("%s -> ERROR. Expect "), label);
print_wchar_string(expectResult);
loggf(F(" and get "));
print_wchar_string(wbuf);
loggf("\n");
nbTestFailed += 1;
wchar_t* wbuf2 = PoolPrintFloat(param); // for stepping with a debugger.
FreePool(wbuf2);
}else if ( !displayOnlyFailed ) {
loggf(F("%s : "), label);
print_wchar_string(wbuf);
loggf(F(" -> OK\n"));
}
FreePool(wbuf);
return 1;
}
#define Test1arg(expectResult,param) \
{ \
char label[1024]; \
snprintf(label, sizeof(label), F("testPoolPrintFloat(%s)"), F(#param)); \
testPoolPrintFloat(label,L##expectResult,param); \
}
#define Test2arg(expectResult,format,c,d) \
{ \
char label[1024]; \
snprintf(label, sizeof(label), F("Test swprintf(%s, %s, %s)"), F(#format), F(#c), F(#d)); \
testWPrintf(label,L##expectResult,(int)wcslen(L##expectResult),L##format,c,d); \
}
#define Test5arg(expectResult,format,c,d,e,f,g) \
{ \
char label[1024]; \
snprintf(label, sizeof(label), F("Test swprintf(%s, %s, %s, %s, %s, %s)"), F(#format), F(#c), F(#d), F(#e), F(#f), F(#g)); \
testWPrintf(label,L##expectResult,(int)wcslen(L##expectResult),L##format,c,d,e,f,g); \
}
int poolprintfloat_tests(void)
{
#ifdef DISPLAY_START_INFO
loggf(F("\n"));
loggf(F("PoolPrintFloat unit test\n"));
loggf(F("\n"));
loggf(F("\n"));
// These depends on the plateform. They are not printf unit test, but it's nice to check size of builtin type.
loggf(F("sizeof(float)=%lu\n"), sizeof(float));
loggf(F("sizeof(double)=%zu\n"), sizeof(double));
loggf(F("\n"));
#endif
Test1arg(F(" 0.000000"), 0.0);
Test1arg(F(" 0.123456"), 0.1234567890);
Test1arg(F("-0.123456"), -0.1234567890);
Test1arg(F(" 1.100000"), 1.1);
Test1arg(F(" -1.100000"), -1.1);
Test1arg(F(" 123.456787"), 123.456789);
Test1arg(F(" -123.456787"), -123.456789);
Test1arg(F(" 1234567936.000000"), 1234567890.456789);
Test1arg(F(" -1234567936.000000"), -1234567890.456789);
Test1arg(F(" 0.000000"), 12345678901234567890.456789);
Test1arg(F(" 0.000000"), -12345678901234567890.456789);
return nbTestFailed;
}