// This file is a part of toml++ and is subject to the the terms of the MIT license. // Copyright (c) 2019-2020 Mark Gillard // See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text. // SPDX-License-Identifier: MIT #include "tests.h" #ifdef _WIN32 TOML_PUSH_WARNINGS TOML_DISABLE_ALL_WARNINGS #include TOML_POP_WARNINGS #endif // TOML_PUSH_WARNINGS // TOML_DISABLE_ARITHMETIC_WARNINGS template static constexpr T one = static_cast(1); TEST_CASE("values - construction") { #define CHECK_VALUE_INIT2(initializer, target_type, equiv) \ do { \ auto v = value{ initializer }; \ static_assert(std::is_same_v>); \ CHECK(v == equiv); \ CHECK(equiv == v); \ CHECK(*v == equiv); \ CHECK(v.get() == equiv); \ } while (false) #define CHECK_VALUE_INIT(initializer, target_type) \ CHECK_VALUE_INIT2(initializer, target_type, initializer) CHECK_VALUE_INIT(one, int64_t); CHECK_VALUE_INIT(one, int64_t); CHECK_VALUE_INIT(one, int64_t); CHECK_VALUE_INIT(one, int64_t); CHECK_VALUE_INIT(one, int64_t); CHECK_VALUE_INIT2(one, int64_t, 1u); CHECK_VALUE_INIT2(one, int64_t, 1u); CHECK_VALUE_INIT2(one, int64_t, 1u); CHECK_VALUE_INIT2(one, int64_t, 1u); CHECK_VALUE_INIT2(one, int64_t, 1u); CHECK_VALUE_INIT(true, bool); CHECK_VALUE_INIT(false, bool); CHECK_VALUE_INIT("kek", std::string); CHECK_VALUE_INIT("kek"s, std::string); CHECK_VALUE_INIT("kek"sv, std::string); CHECK_VALUE_INIT2("kek"sv.data(), std::string, "kek"sv); #ifdef __cpp_lib_char8_t CHECK_VALUE_INIT2(u8"kek", std::string, "kek"sv); CHECK_VALUE_INIT2(u8"kek"s, std::string, "kek"sv); CHECK_VALUE_INIT2(u8"kek"sv, std::string, "kek"sv); CHECK_VALUE_INIT2(u8"kek"sv.data(), std::string, "kek"sv); #endif #ifdef _WIN32 CHECK_VALUE_INIT(one, int64_t); CHECK_VALUE_INIT(one, int64_t); CHECK_VALUE_INIT(one, int64_t); CHECK_VALUE_INIT(one, int64_t); CHECK_VALUE_INIT(one, int64_t); CHECK_VALUE_INIT(one, int64_t); CHECK_VALUE_INIT2(one, int64_t, 1u); CHECK_VALUE_INIT2(one, int64_t, 1u); CHECK_VALUE_INIT2(one, int64_t, 1u); CHECK_VALUE_INIT2(one, int64_t, 1u); CHECK_VALUE_INIT2(one, int64_t, 1u); CHECK_VALUE_INIT2(one, int64_t, 1u); CHECK_VALUE_INIT2(one, int64_t, 1u); CHECK_VALUE_INIT2(one, int64_t, 1u); CHECK_VALUE_INIT2(one, int64_t, 1u); CHECK_VALUE_INIT2(one, int64_t, 1u); #if TOML_WINDOWS_COMPAT CHECK_VALUE_INIT2(L"kek", std::string, "kek"sv); CHECK_VALUE_INIT2(L"kek"s, std::string, "kek"sv); CHECK_VALUE_INIT2(L"kek"sv, std::string, "kek"sv); CHECK_VALUE_INIT2(L"kek"sv.data(), std::string, "kek"sv); #endif // TOML_WINDOWS_COMPAT #endif } // TOML_POP_WARNINGS TEST_CASE("values - printing") { static constexpr auto print_value = [](auto&& raw) { auto val = toml::value{ std::forward(raw) }; std::stringstream ss; ss << val; return ss.str(); }; CHECK(print_value(1) == "1"); CHECK(print_value(1.0f) == "1.0"); CHECK(print_value(1.0) == "1.0"); CHECK(print_value(1.5f) == "1.5"); CHECK(print_value(1.5) == "1.5"); CHECK(print_value(10) == "10"); CHECK(print_value(10.0f) == "10.0"); CHECK(print_value(10.0) == "10.0"); CHECK(print_value(100) == "100"); CHECK(print_value(100.0f) == "100.0"); CHECK(print_value(100.0) == "100.0"); CHECK(print_value(1000) == "1000"); CHECK(print_value(1000.0f) == "1000.0"); CHECK(print_value(1000.0) == "1000.0"); CHECK(print_value(10000) == "10000"); CHECK(print_value(10000.0f) == "10000.0"); CHECK(print_value(10000.0) == "10000.0"); CHECK(print_value(std::numeric_limits::infinity()) == "inf"); CHECK(print_value(-std::numeric_limits::infinity()) == "-inf"); CHECK(print_value(std::numeric_limits::quiet_NaN()) == "nan"); // only integers for large values; // large floats might get output as scientific notation and that's fine CHECK(print_value(10000000000) == "10000000000"); CHECK(print_value(100000000000000) == "100000000000000"); }