From c5974cfe2726d8088b08dc52edd4a825dc86e147 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 5 Jun 2021 17:31:57 +0100 Subject: Add conversion operators to get common types from DbValues --- test/test-misc.cpp | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) (limited to 'test') diff --git a/test/test-misc.cpp b/test/test-misc.cpp index cc9154a..3f45927 100644 --- a/test/test-misc.cpp +++ b/test/test-misc.cpp @@ -1,8 +1,22 @@ #define BOOST_TEST_MODULE BitSet #include +#include +#include +#include #include #include +#include +#include +#include +#include +namespace MyGrate { + class BitSet; +} +namespace boost::numeric { + class bad_numeric_cast; +} +struct timespec; BOOST_AUTO_TEST_CASE(verify) { @@ -10,3 +24,67 @@ BOOST_AUTO_TEST_CASE(verify) BOOST_CHECK_THROW(MyGrate::verify(false, "throw re"), std::runtime_error); BOOST_CHECK_THROW(MyGrate::verify(false, "throw le"), std::logic_error); } + +using Ints = std::tuple; +using Floats = std::tuple; +using Times = std::tuple; +using Str = std::tuple; +using Others = std::tuple; +using TinyInts = std::tuple; +using SmallInts = std::tuple; + +BOOST_AUTO_TEST_CASE_TEMPLATE(DbValueConvIntToInts, I, Ints) +{ + MyGrate::DbValue v {123}; + I out {v}; + BOOST_CHECK_EQUAL(123, out); +} + +BOOST_AUTO_TEST_CASE_TEMPLATE(DbValueConvIntToTinyInts, I, TinyInts) +{ + MyGrate::DbValue v {1234}; + BOOST_CHECK_THROW([[maybe_unused]] I out {v}, boost::bad_numeric_cast); +} + +BOOST_AUTO_TEST_CASE_TEMPLATE(DbValueConvIntToSmallInts, I, SmallInts) +{ + MyGrate::DbValue v {123400}; + BOOST_CHECK_THROW([[maybe_unused]] I out {v}, boost::bad_numeric_cast); +} + +BOOST_AUTO_TEST_CASE_TEMPLATE(DbValueConvIntToFloats, F, Floats) +{ + MyGrate::DbValue v {123400}; + BOOST_CHECK_THROW([[maybe_unused]] F out {v}, std::logic_error); +} + +BOOST_AUTO_TEST_CASE(DbValueConvIntToStringView) +{ + MyGrate::DbValue v {123}; + BOOST_CHECK_THROW([[maybe_unused]] std::string_view out {v}, std::bad_variant_access); +} + +BOOST_AUTO_TEST_CASE(DbValueConvStrViewToStringView) +{ + using namespace std::literals; + MyGrate::DbValue v {"str"}; + BOOST_CHECK_EQUAL((std::string_view)v, "str"sv); + BOOST_CHECK_EQUAL((std::string)v, "str"s); +} + +static_assert(MyGrate::detail::HasToString); +static_assert(!MyGrate::detail::HasToString); + +BOOST_AUTO_TEST_CASE_TEMPLATE(DbValueConvIntToString, I, Ints) +{ + using namespace std::literals; + MyGrate::DbValue v {I {123}}; + BOOST_CHECK_EQUAL((std::string)v, "123"s); +} + +BOOST_AUTO_TEST_CASE_TEMPLATE(DbValueConvFloatToString, F, Floats) +{ + using namespace std::literals; + MyGrate::DbValue v {F {123}}; + BOOST_CHECK_EQUAL((std::string)v, "123.000000"s); +} -- cgit v1.2.3