| 1 |
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ |
| 2 |
|
| 3 |
#ifndef MPT_PARSE_TESTS_PARSE_HPP |
| 4 |
#define MPT_PARSE_TESTS_PARSE_HPP |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "mpt/base/detect.hpp" |
| 9 |
#include "mpt/base/integer.hpp" |
| 10 |
#include "mpt/base/namespace.hpp" |
| 11 |
#include "mpt/format/simple.hpp" |
| 12 |
#include "mpt/parse/parse.hpp" |
| 13 |
#include "mpt/string/types.hpp" |
| 14 |
#include "mpt/test/test.hpp" |
| 15 |
#include "mpt/test/test_macros.hpp" |
| 16 |
|
| 17 |
#include <limits> |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace mpt { |
| 22 |
inline namespace MPT_INLINE_NS { |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
namespace tests { |
| 27 |
namespace parse { |
| 28 |
|
| 29 |
#if MPT_COMPILER_CLANG |
| 30 |
#pragma clang diagnostic push |
| 31 |
#pragma clang diagnostic ignored "-Wglobal-constructors" |
| 32 |
#endif |
| 33 |
MPT_TEST_GROUP_INLINE("mpt/parse") |
| 34 |
#if MPT_COMPILER_CLANG |
| 35 |
#pragma clang diagnostic pop |
| 36 |
#endif |
| 37 |
{ |
| 38 |
|
| 39 |
MPT_TEST_EXPECT_EQUAL(mpt::parse<bool>("1"), true); |
| 40 |
MPT_TEST_EXPECT_EQUAL(mpt::parse<bool>("0"), false); |
| 41 |
MPT_TEST_EXPECT_EQUAL(mpt::parse<bool>("2"), true); |
| 42 |
MPT_TEST_EXPECT_EQUAL(mpt::parse<bool>("-0"), false); |
| 43 |
MPT_TEST_EXPECT_EQUAL(mpt::parse<bool>("-1"), true); |
| 44 |
|
| 45 |
MPT_TEST_EXPECT_EQUAL(mpt::parse<uint32>("586"), 586u); |
| 46 |
MPT_TEST_EXPECT_EQUAL(mpt::parse<uint32>("2147483647"), (uint32)std::numeric_limits<int32>::max()); |
| 47 |
MPT_TEST_EXPECT_EQUAL(mpt::parse<uint32>("4294967295"), std::numeric_limits<uint32>::max()); |
| 48 |
|
| 49 |
MPT_TEST_EXPECT_EQUAL(mpt::parse<int64>("-9223372036854775808"), std::numeric_limits<int64>::min()); |
| 50 |
MPT_TEST_EXPECT_EQUAL(mpt::parse<int64>("-159"), -159); |
| 51 |
MPT_TEST_EXPECT_EQUAL(mpt::parse<int64>("9223372036854775807"), std::numeric_limits<int64>::max()); |
| 52 |
|
| 53 |
MPT_TEST_EXPECT_EQUAL(mpt::parse<uint64>("85059"), 85059u); |
| 54 |
MPT_TEST_EXPECT_EQUAL(mpt::parse<uint64>("9223372036854775807"), (uint64)std::numeric_limits<int64>::max()); |
| 55 |
MPT_TEST_EXPECT_EQUAL(mpt::parse<uint64>("18446744073709551615"), std::numeric_limits<uint64>::max()); |
| 56 |
|
| 57 |
MPT_TEST_EXPECT_EQUAL(mpt::parse<float>("-87.0"), -87.0f); |
| 58 |
#if !MPT_OS_DJGPP |
| 59 |
MPT_TEST_EXPECT_EQUAL(mpt::parse<double>("-0.5e-6"), -0.5e-6); |
| 60 |
#endif |
| 61 |
#if !MPT_OS_DJGPP |
| 62 |
MPT_TEST_EXPECT_EQUAL(mpt::parse<double>("58.65403492763"), 58.65403492763); |
| 63 |
#else |
| 64 |
MPT_TEST_EXPECT_EQUAL(std::abs(mpt::parse<double>("58.65403492763") - 58.65403492763) <= 0.0001, true); |
| 65 |
#endif |
| 66 |
|
| 67 |
MPT_TEST_EXPECT_EQUAL(mpt::parse<float>(mpt::format<std::string>::val(-87.0)), -87.0f); |
| 68 |
// VS2022 17.7.2 parses "-5e-07" as -5.0000000000000004e-06 instead of -4.9999999999999998e-07 which is closer |
| 69 |
// <https://developercommunity.visualstudio.com/t/Parsing-double-from-stringstream-returns/10450694> |
| 70 |
// <https://developercommunity.visualstudio.com/t/v143-1437-istringstream-incorrectly-con/10450662> |
| 71 |
#if !MPT_OS_DJGPP && !(MPT_MSVC_AT_LEAST(2022, 7) && MPT_MSVC_BEFORE(2022, 9)) |
| 72 |
MPT_TEST_EXPECT_EQUAL(mpt::parse<double>(mpt::format<std::string>::val(-0.5e-6)), -0.5e-6); |
| 73 |
#endif |
| 74 |
|
| 75 |
MPT_TEST_EXPECT_EQUAL(mpt::parse_hex<unsigned char>("fe"), 254); |
| 76 |
#if !defined(MPT_COMPILER_QUIRK_NO_WCHAR) |
| 77 |
MPT_TEST_EXPECT_EQUAL(mpt::parse_hex<unsigned char>(L"fe"), 254); |
| 78 |
#endif // !MPT_COMPILER_QUIRK_NO_WCHAR |
| 79 |
MPT_TEST_EXPECT_EQUAL(mpt::parse_hex<unsigned int>(MPT_USTRING("ffff")), 65535u); |
| 80 |
} |
| 81 |
|
| 82 |
} // namespace parse |
| 83 |
} // namespace tests |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
} // namespace MPT_INLINE_NS |
| 88 |
} // namespace mpt |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
#endif // MPT_PARSE_TESTS_PARSE_HPP |