15 #include "absl/random/internal/iostream_state_saver.h"
23 #include "gtest/gtest.h"
33 StreamRoundTrip(
T t) {
37 ss.precision(stream_precision_helper<T>::kPrecision);
47 << (ss.good() ?
"good " :
"")
48 << (ss.bad() ?
"bad " :
"")
49 << (ss.eof() ?
"eof " :
"")
50 << (ss.fail() ?
"fail " :
"");
57 StreamRoundTrip(
T t) {
61 ss.precision(stream_precision_helper<T>::kPrecision);
67 result = absl::random_internal::read_floating_point<T>(ss);
71 << (ss.good() ?
"good " :
"")
72 << (ss.bad() ?
"bad " :
"")
73 << (ss.eof() ?
"eof " :
"")
74 << (ss.fail() ?
"fail " :
"");
79 TEST(IOStreamStateSaver, BasicSaverState) {
83 ss.flags(std::ios_base::dec | std::ios_base::right);
90 EXPECT_NE(std::ios_base::dec | std::ios_base::right, ss.flags());
97 EXPECT_EQ(std::ios_base::dec | std::ios_base::right, ss.flags());
100 TEST(IOStreamStateSaver, RoundTripInts) {
126 static_cast<uint64_t>(-(1ll << 32)),
127 static_cast<uint64_t>(-(1ll << 50)),
128 static_cast<uint64_t>(-(1ll << 62)),
130 static_cast<uint64_t>(-(1 << 8) - 1),
131 static_cast<uint64_t>(-(1 << 16) - 1),
132 static_cast<uint64_t>(-(1ll << 32) - 1),
133 static_cast<uint64_t>(-(1ll << 50) - 1),
134 static_cast<uint64_t>(-(1ll << 62) - 1),
143 double d =
static_cast<double>(
x);
151 TEST(IOStreamStateSaver, RoundTripFloats) {
153 stream_precision_helper<float>::kPrecision >= 9,
154 "stream_precision_helper<float>::kPrecision should be at least 9");
156 const float kValues[] = {
158 std::nextafter(1.0
f, 0.0
f),
159 std::nextafter(1.0
f, 2.0
f),
168 0.00000051110000111311111111f,
169 -0.00000051110000111211111111f,
171 1.234678912345678912345e+6
f,
172 1.234678912345678912345e-6
f,
173 1.234678912345678912345e+30
f,
174 1.234678912345678912345e-30
f,
175 1.234678912345678912345e+38
f,
176 1.0234678912345678912345e-38
f,
180 std::numeric_limits<float>::lowest(),
181 std::numeric_limits<float>::epsilon(),
186 std::numeric_limits<float>::denorm_min(),
190 std::nextafter(std::numeric_limits<float>::denorm_min(), 1.0
f),
193 for (
const float f : kValues) {
203 f >=
static_cast<float>(std::numeric_limits<int64_t>::lowest())) {
210 TEST(IOStreamStateSaver, RoundTripDoubles) {
212 stream_precision_helper<double>::kPrecision >= 17,
213 "stream_precision_helper<double>::kPrecision should be at least 17");
215 const double kValues[] = {
217 std::nextafter(1.0, 0.0),
218 std::nextafter(1.0, 2.0),
227 0.00000051110000111311111111,
228 -0.00000051110000111211111111,
230 1.234678912345678912345e+6,
231 1.234678912345678912345e-6,
232 1.234678912345678912345e+30,
233 1.234678912345678912345e-30,
234 1.234678912345678912345e+38,
235 1.0234678912345678912345e-38,
239 1.234678912345678912345e+308,
240 1.0234678912345678912345e-308,
241 2.22507385850720138e-308,
245 std::numeric_limits<double>::lowest(),
246 std::numeric_limits<double>::epsilon(),
251 std::numeric_limits<double>::denorm_min(),
255 std::nextafter(std::numeric_limits<double>::denorm_min(), 1.0
f),
258 for (
const double d : kValues) {
264 d >= std::numeric_limits<float>::lowest()) {
265 float f =
static_cast<float>(
d);
271 d >=
static_cast<double>(std::numeric_limits<int64_t>::lowest())) {
278 TEST(IOStreamStateSaver, RoundTripLongDoubles) {
286 stream_precision_helper<long double>::kPrecision >= 36,
287 "stream_precision_helper<long double>::kPrecision should be at least 36");
292 std::nextafter(1.0, 0.0),
293 std::nextafter(1.0, 2.0),
302 0.00000051110000111311111111,
303 -0.00000051110000111211111111,
305 1.2346789123456789123456789123456789e+6,
306 1.2346789123456789123456789123456789e-6,
307 1.2346789123456789123456789123456789e+30,
308 1.2346789123456789123456789123456789e-30,
309 1.2346789123456789123456789123456789e+38,
310 1.2346789123456789123456789123456789e-38,
311 1.2346789123456789123456789123456789e+308,
312 1.2346789123456789123456789123456789e-308,
316 1.234678912345678912345e+308,
317 1.0234678912345678912345e-308,
321 std::numeric_limits<real_type>::lowest(),
322 std::numeric_limits<real_type>::epsilon(),
327 std::numeric_limits<real_type>::denorm_min(),
331 std::nextafter(std::numeric_limits<real_type>::denorm_min(), 1.0
f),
335 for (
const long double dd : kValues) {
342 dd >= std::numeric_limits<double>::lowest()) {
343 double d =
static_cast<double>(dd);
349 dd >= std::numeric_limits<int64_t>::lowest()) {
356 TEST(StrToDTest, DoubleMin) {
357 const char kV[] =
"2.22507385850720138e-308";
359 double x = std::strtod(kV, &
end);
364 TEST(StrToDTest, DoubleDenormMin) {
365 const char kV[] =
"4.94065645841246544e-324";
367 double x = std::strtod(kV, &
end);
368 EXPECT_EQ(std::numeric_limits<double>::denorm_min(),
x);