-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestFloatingPointNaN.cpp
174 lines (145 loc) · 8.99 KB
/
testFloatingPointNaN.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
// test NaN conversion exceptions for floating-point-types
#include <iostream>
#include <cmath>
#include <typeinfo>
#include <converter/converter.h>
#include "unittest.h"
#if USE_FLOATINGPOINT_FROM_CHARS_1 == e_ENABLE_FEATURE
template <typename T, converter::FailureS2Tprocess EP>
using _ConvS2T_CtoT = converter::ConvertFromStr<T, converter::S2T_Format_std_CtoT<T, EP>>;
#endif
template <typename T, converter::FailureS2Tprocess EP>
using _ConvS2T_StoT = converter::ConvertFromStr<T, converter::S2T_Format_std_StoT<T, EP>>;
template <typename T, converter::FailureS2Tprocess EP>
using _ConvS2T_ISS = converter::ConvertFromStr<T, converter::S2T_Format_StreamUseClassicLocale<T, EP, char>>;
#if USE_FLOATINGPOINT_TO_CHARS_1 == e_ENABLE_FEATURE
template <typename T>
using _ConvT2S_TtoC = converter::ConvertFromVal<T, converter::T2S_Format_std_TtoC>;
#endif
template <typename T>
using _ConvT2S_TtoS = converter::ConvertFromVal<T, converter::T2S_Format_StreamDecimalPrecision<T>>;
template <typename T>
using _ConvT2S_OSS = converter::ConvertFromVal<T, converter::T2S_Format_StreamUseClassicLocale<char>>;
template <typename T>
void conversion_String2FloatingPoint_FailureCheck(const std::string& vStr)
{
#if USE_FLOATINGPOINT_FROM_CHARS_1 == e_ENABLE_FEATURE
{
ExpectException(_ConvS2T_CtoT<T COMMA converter::FailureS2Tprocess::THROW_ERROR>::ToVal(vStr), std::invalid_argument);
unittest::ExpectTrue(std::isnan(_ConvS2T_CtoT<T COMMA converter::FailureS2Tprocess::QUIET_NAN>::ToVal(vStr)));
unittest::ExpectTrue(std::isnan(_ConvS2T_CtoT<T COMMA converter::FailureS2Tprocess::SIGNAL_NAN>::ToVal(vStr)));
unittest::ExpectTrue(std::isnan(converter::ConvertFromStr<T>::ToVal(vStr))); // default: same as above, as S2T_Format_std_CtoT<T, SIGNAL_NAN> is default
typename converter::OnError<T, converter::FailureS2Tprocess::VARIANT_NAN>::return_type varT =
_ConvS2T_CtoT<T , converter::FailureS2Tprocess::VARIANT_NAN>::ToVal(vStr);
unittest::ExpectTrue(varT.index() == 1);
unittest::ExpectEqual(std::string, std::get<std::string>(varT), vStr);
}
#endif
{
ExpectException(_ConvS2T_StoT<T COMMA converter::FailureS2Tprocess::THROW_ERROR>::ToVal(vStr), std::invalid_argument);
unittest::ExpectTrue(std::isnan(_ConvS2T_StoT<T COMMA converter::FailureS2Tprocess::QUIET_NAN>::ToVal(vStr)));
#if USE_FLOATINGPOINT_FROM_CHARS_1 == e_ENABLE_FEATURE
unittest::ExpectTrue(std::isnan(_ConvS2T_StoT<T COMMA converter::FailureS2Tprocess::SIGNAL_NAN>::ToVal(vStr)));
#elif USE_FLOATINGPOINT_FROM_CHARS_1 == e_DISABLE_FEATURE
unittest::ExpectTrue(std::isnan(_ConvS2T_StoT<T COMMA converter::FailureS2Tprocess::SIGNAL_NAN>::ToVal(vStr)));
unittest::ExpectTrue(std::isnan(converter::ConvertFromStr<T>::ToVal(vStr))); // default: same as above, as S2T_Format_std_StoT<T, SIGNAL_NAN> is default
#endif
typename converter::OnError<T, converter::FailureS2Tprocess::VARIANT_NAN>::return_type varT =
_ConvS2T_StoT<T, converter::FailureS2Tprocess::VARIANT_NAN>::ToVal(vStr);
unittest::ExpectTrue(varT.index() == 1);
unittest::ExpectEqual(std::string, std::get<std::string>(varT), vStr);
}
{
ExpectException(_ConvS2T_ISS<T COMMA converter::FailureS2Tprocess::THROW_ERROR>::ToVal(vStr), std::invalid_argument);
unittest::ExpectTrue(std::isnan(_ConvS2T_ISS<T COMMA converter::FailureS2Tprocess::QUIET_NAN>::ToVal(vStr)));
unittest::ExpectTrue(std::isnan(_ConvS2T_ISS<T COMMA converter::FailureS2Tprocess::SIGNAL_NAN>::ToVal(vStr)));
typename converter::OnError<T, converter::FailureS2Tprocess::VARIANT_NAN>::return_type varT =
_ConvS2T_ISS<T , converter::FailureS2Tprocess::VARIANT_NAN>::ToVal(vStr);
unittest::ExpectTrue(varT.index() == 1);
unittest::ExpectEqual(std::string, std::get<std::string>(varT), vStr);
}
}
template <typename T>
void conversion_FloatingPointNAN2String_FailureCheck(const std::string& vStr)
{
#if USE_FLOATINGPOINT_TO_CHARS_1 == e_ENABLE_FEATURE
{
unittest::ExpectEqual(std::string, _ConvT2S_TtoC<T>::ToStr(std::numeric_limits<T>::quiet_NaN()), "nan");
#if defined(WIN64) || defined(_WIN64) || defined(__WIN64) || defined(__WIN64__)
//std::cout << "1. _ConvT2S_TtoC<T=" << typeid(T).name() << ">::ToStr(std::numeric_limits<T>::signaling_NaN()) = #" <<
// _ConvT2S_TtoC<T>::ToStr(std::numeric_limits<T>::signaling_NaN()) << "# ; vStr=" << vStr << std::endl;
// arbitarially prints 'nan' or 'nan(snan)'
unittest::ExpectEqual(std::string, _ConvT2S_TtoC<T>::ToStr(std::numeric_limits<T>::signaling_NaN()).substr(0,3), std::string("nan"));
#else
unittest::ExpectEqual(std::string, _ConvT2S_TtoC<T>::ToStr(std::numeric_limits<T>::signaling_NaN()), std::string("nan"));
#endif
unittest::ExpectEqual(std::string, converter::ConvertFromVal< std::variant<T COMMA std::string> COMMA
converter::T2S_Format_std_TtoC
>::ToStr(std::variant<T COMMA std::string>{vStr}),
vStr);
unittest::ExpectEqual(std::string, converter::ConvertFromVal< std::variant<T COMMA std::string>
>::ToStr(std::variant<T COMMA std::string>{vStr}),
vStr); // same as above as T2S_Format_std_TtoC is default
}
#endif
{
unittest::ExpectEqual(std::string, _ConvT2S_TtoS<T>::ToStr(std::numeric_limits<T>::quiet_NaN()), "nan");
#if defined(WIN64) || defined(_WIN64) || defined(__WIN64) || defined(__WIN64__)
//std::cout << "2. _ConvT2S_TtoS<T" << typeid(T).name() << ">::ToStr(std::numeric_limits<T>::signaling_NaN()) = #" <<
// _ConvT2S_TtoS<T>::ToStr(std::numeric_limits<T>::signaling_NaN()) << "# ; vStr=" << vStr << std::endl;
unittest::ExpectEqual(std::string, _ConvT2S_TtoC<T>::ToStr(std::numeric_limits<T>::signaling_NaN()).substr(0,3), std::string("nan"));
#else
unittest::ExpectEqual(std::string, _ConvT2S_TtoS<T>::ToStr(std:: numeric_limits<T>::signaling_NaN()), std::string("nan"));
#endif
unittest::ExpectEqual(std::string, converter::ConvertFromVal< std::variant<T COMMA std::string> COMMA
converter::T2S_Format_StreamDecimalPrecision<T>
>::ToStr(std::variant<T COMMA std::string>{vStr}),
vStr);
#ifdef ENABLE_STD_TtoS
unittest::ExpectEqual(std::string, converter::ConvertFromVal< std::variant<T COMMA std::string> COMMA
converter::T2S_Format_std_TtoS
>::ToStr(std::variant<T COMMA std::string>{vStr}),
vStr);
#endif
}
{
unittest::ExpectEqual(std::string, _ConvT2S_OSS<T>::ToStr(std::numeric_limits<T>::quiet_NaN()), "nan");
#if defined(WIN64) || defined(_WIN64) || defined(__WIN64) || defined(__WIN64__)
//std::cout << "3. _ConvT2S_OSS<T" << typeid(T).name() << ">::ToStr(std::numeric_limits<T>::signaling_NaN()) = #" <<
// _ConvT2S_OSS<T>::ToStr(std::numeric_limits<T>::signaling_NaN()) << "# ; vStr=" << vStr << std::endl;
unittest::ExpectEqual(std::string, _ConvT2S_TtoC<T>::ToStr(std::numeric_limits<T>::signaling_NaN()).substr(0,3), std::string("nan"));
#else
unittest::ExpectEqual(std::string, _ConvT2S_OSS<T>::ToStr(std::numeric_limits<T>::signaling_NaN()), std::string("nan"));
#endif
unittest::ExpectEqual(std::string, converter::ConvertFromVal< std::variant<T COMMA std::string> COMMA
converter::T2S_Format_StreamUseClassicLocale<char>
>::ToStr(std::variant<T COMMA std::string>{vStr}),
vStr);
}
}
int main()
{
int rv = 0;
try {
// [========[ String to FloatingPoint-NaN
conversion_String2FloatingPoint_FailureCheck<float>("rc");
conversion_String2FloatingPoint_FailureCheck<double>("ab");
conversion_String2FloatingPoint_FailureCheck<long double>("xy");
conversion_String2FloatingPoint_FailureCheck<float>("1.2x1");
conversion_String2FloatingPoint_FailureCheck<double>("2.3.4");
conversion_String2FloatingPoint_FailureCheck<long double>("3r.422123445");
// ]========] String to FloatingPoint-NaN
// [========[ FloatingPoint-NaN to String
conversion_FloatingPointNAN2String_FailureCheck<float>("rc");
conversion_FloatingPointNAN2String_FailureCheck<double>("ab");
conversion_FloatingPointNAN2String_FailureCheck<long double>("xy");
conversion_FloatingPointNAN2String_FailureCheck<float>("1.2x1");
conversion_FloatingPointNAN2String_FailureCheck<double>("2.3.4");
conversion_FloatingPointNAN2String_FailureCheck<long double>("3r.422123445");
// ]========] FloatingPoint-NaN to String
} catch (const std::exception& ex) {
std::cout << "Test Failed : err-msg : " << ex.what() << std::endl;
rv = 1;
}
return rv;
}