fix: time offset may change while conversion

This commit is contained in:
ToruNiina 2019-06-21 14:59:28 +09:00
parent 7b37d876ae
commit 4032b438c0
2 changed files with 88 additions and 83 deletions

View File

@ -308,7 +308,12 @@ BOOST_AUTO_TEST_CASE(test_construct_value_with_comments)
BOOST_TEST(v.comments().at(0) == "comment1");
BOOST_TEST(v.comments().at(1) == "comment2");
BOOST_TEST(v.is_offset_datetime());
BOOST_TEST(v.as_offset_datetime() == odt);
// While the conversion, the information about time offset may change.
const auto systp2 = static_cast<std::chrono::system_clock::time_point>(
v.as_offset_datetime());
const bool result = systp == systp2; // because there is no operator<<
BOOST_TEST(result);
}
{
const typename value_type::array_type a{1,2,3,4,5};

View File

@ -46,7 +46,7 @@ BOOST_AUTO_TEST_CASE(test_find_throws)
{
// the positive control.
toml::value v{{"key", 42}};
BOOST_CHECK_EQUAL(42, toml::find<int>(v, "key"));
BOOST_TEST(42 == toml::find<int>(v, "key"));
}
}
@ -63,17 +63,17 @@ BOOST_AUTO_TEST_CASE(test_find_recursive)
}}
}}
};
BOOST_CHECK_EQUAL(42, toml::find<int>(v, "a", "b", "c", "d"));
BOOST_TEST(42 == toml::find<int>(v, "a", "b", "c", "d"));
// reference that can be used to modify the content
auto& num = toml::find<toml::integer>(v, "a", "b", "c", "d");
num = 54;
BOOST_CHECK_EQUAL(54, toml::find<int>(v, "a", "b", "c", "d"));
BOOST_TEST(54 == toml::find<int>(v, "a", "b", "c", "d"));
const std::string a("a"), b("b"), c("c"), d("d");
auto& num2 = toml::find<toml::integer>(v, a, b, c, d);
num2 = 42;
BOOST_CHECK_EQUAL(42, toml::find<int>(v, a, b, c, d));
BOOST_TEST(42 == toml::find<int>(v, a, b, c, d));
}
}
@ -81,24 +81,24 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_find_exact, value_type, test_value_types)
{
{
value_type v{{"key", true}};
BOOST_CHECK_EQUAL(true, toml::find<toml::boolean>(v, "key"));
BOOST_TEST(true == toml::find<toml::boolean>(v, "key"));
toml::find<toml::boolean>(v, "key") = false;
BOOST_CHECK_EQUAL(false, toml::find<toml::boolean>(v, "key"));
BOOST_TEST(false == toml::find<toml::boolean>(v, "key"));
}
{
value_type v{{"key", 42}};
BOOST_CHECK_EQUAL(toml::integer(42), toml::find<toml::integer>(v, "key"));
BOOST_TEST(toml::integer(42) == toml::find<toml::integer>(v, "key"));
toml::find<toml::integer>(v, "key") = 54;
BOOST_CHECK_EQUAL(toml::integer(54), toml::find<toml::integer>(v, "key"));
BOOST_TEST(toml::integer(54) == toml::find<toml::integer>(v, "key"));
}
{
value_type v{{"key", 3.14}};
BOOST_CHECK_EQUAL(toml::floating(3.14), toml::find<toml::floating>(v, "key"));
BOOST_TEST(toml::floating(3.14) == toml::find<toml::floating>(v, "key"));
toml::find<toml::floating>(v, "key") = 2.71;
BOOST_CHECK_EQUAL(toml::floating(2.71), toml::find<toml::floating>(v, "key"));
BOOST_TEST(toml::floating(2.71) == toml::find<toml::floating>(v, "key"));
}
{
value_type v{{"key", "foo"}};
@ -200,15 +200,15 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_find_integer_type, value_type, test_value_typ
{
{
value_type v{{"key", 42}};
BOOST_CHECK_EQUAL(int(42), toml::find<int >(v, "key"));
BOOST_CHECK_EQUAL(short(42), toml::find<short >(v, "key"));
BOOST_CHECK_EQUAL(char(42), toml::find<char >(v, "key"));
BOOST_CHECK_EQUAL(unsigned(42), toml::find<unsigned >(v, "key"));
BOOST_CHECK_EQUAL(long(42), toml::find<long >(v, "key"));
BOOST_CHECK_EQUAL(std::int64_t(42), toml::find<std::int64_t >(v, "key"));
BOOST_CHECK_EQUAL(std::uint64_t(42), toml::find<std::uint64_t>(v, "key"));
BOOST_CHECK_EQUAL(std::int16_t(42), toml::find<std::int16_t >(v, "key"));
BOOST_CHECK_EQUAL(std::uint16_t(42), toml::find<std::uint16_t>(v, "key"));
BOOST_TEST(int(42) == toml::find<int >(v, "key"));
BOOST_TEST(short(42) == toml::find<short >(v, "key"));
BOOST_TEST(char(42) == toml::find<char >(v, "key"));
BOOST_TEST(unsigned(42) == toml::find<unsigned >(v, "key"));
BOOST_TEST(long(42) == toml::find<long >(v, "key"));
BOOST_TEST(std::int64_t(42) == toml::find<std::int64_t >(v, "key"));
BOOST_TEST(std::uint64_t(42) == toml::find<std::uint64_t>(v, "key"));
BOOST_TEST(std::int16_t(42) == toml::find<std::int16_t >(v, "key"));
BOOST_TEST(std::uint16_t(42) == toml::find<std::uint16_t>(v, "key"));
}
}
@ -216,9 +216,9 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_find_floating_type, value_type, test_value_ty
{
{
value_type v{{"key", 3.14}};
BOOST_CHECK_EQUAL(static_cast<float >(3.14), toml::find<float >(v, "key"));
BOOST_CHECK_EQUAL(static_cast<double >(3.14), toml::find<double >(v, "key"));
BOOST_CHECK_EQUAL(static_cast<long double>(3.14), toml::find<long double>(v, "key"));
BOOST_TEST(static_cast<float >(3.14) == toml::find<float >(v, "key"));
BOOST_TEST(static_cast<double >(3.14) == toml::find<double >(v, "key"));
BOOST_TEST(static_cast<long double>(3.14) == toml::find<long double>(v, "key"));
}
}
@ -226,25 +226,25 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_find_string_type, value_type, test_value_type
{
{
value_type v{{"key", toml::string("foo", toml::string_t::basic)}};
BOOST_CHECK_EQUAL("foo", toml::find<std::string>(v, "key"));
BOOST_TEST("foo" == toml::find<std::string>(v, "key"));
toml::find<std::string>(v, "key") += "bar";
BOOST_CHECK_EQUAL("foobar", toml::find<std::string>(v, "key"));
BOOST_TEST("foobar" == toml::find<std::string>(v, "key"));
}
{
value_type v{{"key", toml::string("foo", toml::string_t::literal)}};
BOOST_CHECK_EQUAL("foo", toml::find<std::string>(v, "key"));
BOOST_TEST("foo" == toml::find<std::string>(v, "key"));
toml::find<std::string>(v, "key") += "bar";
BOOST_CHECK_EQUAL("foobar", toml::find<std::string>(v, "key"));
BOOST_TEST("foobar" == toml::find<std::string>(v, "key"));
}
#if __cplusplus >= 201703L
{
value_type v{{"key", toml::string("foo", toml::string_t::basic)}};
BOOST_CHECK_EQUAL("foo", toml::find<std::string_view>(v, "key"));
BOOST_TEST("foo" == toml::find<std::string_view>(v, "key"));
}
{
value_type v{{"key", toml::string("foo", toml::string_t::literal)}};
BOOST_CHECK_EQUAL("foo", toml::find<std::string_view>(v, "key"));
BOOST_TEST("foo" == toml::find<std::string_view>(v, "key"));
}
#endif
}
@ -257,39 +257,39 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_find_toml_array, value_type, test_value_types
const std::list<short> lst = toml::find<std::list<short>>(v, "key");
const std::deque<std::int64_t> deq = toml::find<std::deque<std::int64_t>>(v, "key");
BOOST_CHECK_EQUAL(42, vec.at(0));
BOOST_CHECK_EQUAL(54, vec.at(1));
BOOST_CHECK_EQUAL(69, vec.at(2));
BOOST_CHECK_EQUAL(72, vec.at(3));
BOOST_TEST(42 == vec.at(0));
BOOST_TEST(54 == vec.at(1));
BOOST_TEST(69 == vec.at(2));
BOOST_TEST(72 == vec.at(3));
std::list<short>::const_iterator iter = lst.begin();
BOOST_CHECK_EQUAL(static_cast<short>(42), *(iter++));
BOOST_CHECK_EQUAL(static_cast<short>(54), *(iter++));
BOOST_CHECK_EQUAL(static_cast<short>(69), *(iter++));
BOOST_CHECK_EQUAL(static_cast<short>(72), *(iter++));
BOOST_TEST(static_cast<short>(42) == *(iter++));
BOOST_TEST(static_cast<short>(54) == *(iter++));
BOOST_TEST(static_cast<short>(69) == *(iter++));
BOOST_TEST(static_cast<short>(72) == *(iter++));
BOOST_CHECK_EQUAL(static_cast<std::int64_t>(42), deq.at(0));
BOOST_CHECK_EQUAL(static_cast<std::int64_t>(54), deq.at(1));
BOOST_CHECK_EQUAL(static_cast<std::int64_t>(69), deq.at(2));
BOOST_CHECK_EQUAL(static_cast<std::int64_t>(72), deq.at(3));
BOOST_TEST(static_cast<std::int64_t>(42) == deq.at(0));
BOOST_TEST(static_cast<std::int64_t>(54) == deq.at(1));
BOOST_TEST(static_cast<std::int64_t>(69) == deq.at(2));
BOOST_TEST(static_cast<std::int64_t>(72) == deq.at(3));
std::array<int, 4> ary = toml::find<std::array<int, 4>>(v, "key");
BOOST_CHECK_EQUAL(static_cast<int>(42), ary.at(0));
BOOST_CHECK_EQUAL(static_cast<int>(54), ary.at(1));
BOOST_CHECK_EQUAL(static_cast<int>(69), ary.at(2));
BOOST_CHECK_EQUAL(static_cast<int>(72), ary.at(3));
BOOST_TEST(static_cast<int>(42) == ary.at(0));
BOOST_TEST(static_cast<int>(54) == ary.at(1));
BOOST_TEST(static_cast<int>(69) == ary.at(2));
BOOST_TEST(static_cast<int>(72) == ary.at(3));
std::tuple<int, short, unsigned, long> tpl =
toml::find<std::tuple<int, short, unsigned, long>>(v, "key");
BOOST_CHECK_EQUAL(static_cast<int >(42), std::get<0>(tpl));
BOOST_CHECK_EQUAL(static_cast<short >(54), std::get<1>(tpl));
BOOST_CHECK_EQUAL(static_cast<unsigned>(69), std::get<2>(tpl));
BOOST_CHECK_EQUAL(static_cast<long >(72), std::get<3>(tpl));
BOOST_TEST(static_cast<int >(42) == std::get<0>(tpl));
BOOST_TEST(static_cast<short >(54) == std::get<1>(tpl));
BOOST_TEST(static_cast<unsigned>(69) == std::get<2>(tpl));
BOOST_TEST(static_cast<long >(72) == std::get<3>(tpl));
value_type p{{"key", {3.14, 2.71}}};
std::pair<double, double> pr = toml::find<std::pair<double, double> >(p, "key");
BOOST_CHECK_EQUAL(3.14, pr.first);
BOOST_CHECK_EQUAL(2.71, pr.second);
BOOST_TEST(3.14 == pr.first);
BOOST_TEST(2.71 == pr.second);
}
BOOST_AUTO_TEST_CASE_TEMPLATE(test_find_toml_array_of_array, value_type, test_value_types)
@ -301,26 +301,26 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_find_toml_array_of_array, value_type, test_va
std::pair<std::vector<int>, std::vector<std::string>> p =
toml::find<std::pair<std::vector<int>, std::vector<std::string>>>(v, "key");
BOOST_CHECK_EQUAL(p.first.at(0), 42);
BOOST_CHECK_EQUAL(p.first.at(1), 54);
BOOST_CHECK_EQUAL(p.first.at(2), 69);
BOOST_CHECK_EQUAL(p.first.at(3), 72);
BOOST_TEST(p.first.at(0) == 42);
BOOST_TEST(p.first.at(1) == 54);
BOOST_TEST(p.first.at(2) == 69);
BOOST_TEST(p.first.at(3) == 72);
BOOST_CHECK_EQUAL(p.second.at(0), "foo");
BOOST_CHECK_EQUAL(p.second.at(1), "bar");
BOOST_CHECK_EQUAL(p.second.at(2), "baz");
BOOST_TEST(p.second.at(0) == "foo");
BOOST_TEST(p.second.at(1) == "bar");
BOOST_TEST(p.second.at(2) == "baz");
std::tuple<std::vector<int>, std::vector<std::string>> t =
toml::find<std::tuple<std::vector<int>, std::vector<std::string>>>(v, "key");
BOOST_CHECK_EQUAL(std::get<0>(t).at(0), 42);
BOOST_CHECK_EQUAL(std::get<0>(t).at(1), 54);
BOOST_CHECK_EQUAL(std::get<0>(t).at(2), 69);
BOOST_CHECK_EQUAL(std::get<0>(t).at(3), 72);
BOOST_TEST(std::get<0>(t).at(0) == 42);
BOOST_TEST(std::get<0>(t).at(1) == 54);
BOOST_TEST(std::get<0>(t).at(2) == 69);
BOOST_TEST(std::get<0>(t).at(3) == 72);
BOOST_CHECK_EQUAL(std::get<1>(t).at(0), "foo");
BOOST_CHECK_EQUAL(std::get<1>(t).at(1), "bar");
BOOST_CHECK_EQUAL(std::get<1>(t).at(2), "baz");
BOOST_TEST(std::get<1>(t).at(0) == "foo");
BOOST_TEST(std::get<1>(t).at(1) == "bar");
BOOST_TEST(std::get<1>(t).at(2) == "baz");
}
BOOST_AUTO_TEST_CASE_TEMPLATE(test_find_toml_table, value_type, test_value_types)
@ -329,10 +329,10 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_find_toml_table, value_type, test_value_types
{"key1", 1}, {"key2", 2}, {"key3", 3}, {"key4", 4}
}}};
const auto v = toml::find<std::map<std::string, int>>(v1, "key");
BOOST_CHECK_EQUAL(v.at("key1"), 1);
BOOST_CHECK_EQUAL(v.at("key2"), 2);
BOOST_CHECK_EQUAL(v.at("key3"), 3);
BOOST_CHECK_EQUAL(v.at("key4"), 4);
BOOST_TEST(v.at("key1") == 1);
BOOST_TEST(v.at("key2") == 2);
BOOST_TEST(v.at("key3") == 3);
BOOST_TEST(v.at("key4") == 4);
}
BOOST_AUTO_TEST_CASE_TEMPLATE(test_find_toml_local_date, value_type, test_value_types)
@ -350,7 +350,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_find_toml_local_date, value_type, test_value_
t.tm_sec = 0;
t.tm_isdst = -1;
const auto c = std::mktime(&t);
BOOST_CHECK_EQUAL(c, date);
BOOST_TEST(c == date);
}
BOOST_AUTO_TEST_CASE_TEMPLATE(test_find_toml_local_time, value_type, test_value_types)
@ -378,7 +378,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_find_toml_local_datetime, value_type, test_va
t.tm_sec = 45;
t.tm_isdst = -1;
const auto c = std::mktime(&t);
BOOST_CHECK_EQUAL(c, date);
BOOST_TEST(c == date);
}
BOOST_AUTO_TEST_CASE_TEMPLATE(test_get_toml_offset_datetime, value_type, test_value_types)
@ -398,12 +398,12 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_get_toml_offset_datetime, value_type, test_va
const auto tmp = std::gmtime(std::addressof(timet)); // XXX not threadsafe!
BOOST_CHECK(tmp);
const auto tm = *tmp;
BOOST_CHECK_EQUAL(tm.tm_year + 1900, 2018);
BOOST_CHECK_EQUAL(tm.tm_mon + 1, 4);
BOOST_CHECK_EQUAL(tm.tm_mday, 1);
BOOST_CHECK_EQUAL(tm.tm_hour, 3);
BOOST_CHECK_EQUAL(tm.tm_min, 30);
BOOST_CHECK_EQUAL(tm.tm_sec, 0);
BOOST_TEST(tm.tm_year + 1900 == 2018);
BOOST_TEST(tm.tm_mon + 1 == 4);
BOOST_TEST(tm.tm_mday == 1);
BOOST_TEST(tm.tm_hour == 3);
BOOST_TEST(tm.tm_min == 30);
BOOST_TEST(tm.tm_sec == 0);
}
{
@ -421,12 +421,12 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_get_toml_offset_datetime, value_type, test_va
const auto tmp = std::gmtime(std::addressof(timet)); // XXX not threadsafe!
BOOST_CHECK(tmp);
const auto tm = *tmp;
BOOST_CHECK_EQUAL(tm.tm_year + 1900, 2018);
BOOST_CHECK_EQUAL(tm.tm_mon + 1, 4);
BOOST_CHECK_EQUAL(tm.tm_mday, 1);
BOOST_CHECK_EQUAL(tm.tm_hour, 20);
BOOST_CHECK_EQUAL(tm.tm_min, 30);
BOOST_CHECK_EQUAL(tm.tm_sec, 0);
BOOST_TEST(tm.tm_year + 1900 == 2018);
BOOST_TEST(tm.tm_mon + 1 == 4);
BOOST_TEST(tm.tm_mday == 1);
BOOST_TEST(tm.tm_hour == 20);
BOOST_TEST(tm.tm_min == 30);
BOOST_TEST(tm.tm_sec == 0);
}
}