summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2021-06-05 17:31:57 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2021-06-05 17:31:57 +0100
commitc5974cfe2726d8088b08dc52edd4a825dc86e147 (patch)
tree633b6cbe9682da243505b530e13f96761c8a7cb4 /test
parentWrap DbValue in a class so we can add helpers to it (diff)
downloadmygrate-c5974cfe2726d8088b08dc52edd4a825dc86e147.tar.bz2
mygrate-c5974cfe2726d8088b08dc52edd4a825dc86e147.tar.xz
mygrate-c5974cfe2726d8088b08dc52edd4a825dc86e147.zip
Add conversion operators to get common types from DbValues
Diffstat (limited to 'test')
-rw-r--r--test/test-misc.cpp78
1 files changed, 78 insertions, 0 deletions
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 <boost/test/unit_test.hpp>
+#include <cstddef>
+#include <cstdint>
+#include <dbTypes.h>
#include <helpers.h>
#include <stdexcept>
+#include <string>
+#include <string_view>
+#include <tuple>
+#include <variant>
+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<std::runtime_error>(false, "throw re"), std::runtime_error);
BOOST_CHECK_THROW(MyGrate::verify<std::logic_error>(false, "throw le"), std::logic_error);
}
+
+using Ints = std::tuple<int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, uint64_t>;
+using Floats = std::tuple<float, double>;
+using Times = std::tuple<timespec, MyGrate::Date, MyGrate::Time, MyGrate::DateTime>;
+using Str = std::tuple<std::string_view>;
+using Others = std::tuple<std::nullptr_t, MyGrate::BitSet, MyGrate::Blob>;
+using TinyInts = std::tuple<int8_t, uint8_t>;
+using SmallInts = std::tuple<int8_t, uint8_t, int16_t, uint16_t>;
+
+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<int>);
+static_assert(!MyGrate::detail::HasToString<MyGrate::Date>);
+
+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);
+}