2
0
mirror of https://github.com/boostorg/mysql.git synced 2026-02-14 12:52:17 +00:00
Files
mysql/test/unit/value_constexpr.cpp
Ruben Perez 50cd20bb7d Field view
2022-08-08 17:04:50 +02:00

208 lines
7.2 KiB
C++

//
// Copyright (c) 2019-2022 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include <boost/mysql/field_view.hpp>
#include "test_common.hpp"
#include <boost/test/unit_test.hpp>
#include <boost/test/unit_test_suite.hpp>
#include <cstdint>
using boost::mysql::field_view;
using boost::mysql::null_t;
using boost::mysql::datetime;
using boost::mysql::date;
using boost::mysql::days;
using vt = boost::mysql::field_view::variant_type;
#ifndef BOOST_NO_CXX14_CONSTEXPR
BOOST_AUTO_TEST_SUITE(test_value_constexpr)
#ifndef _MSC_VER // for some reason, MSVC doesn't like these tests
BOOST_AUTO_TEST_CASE(null_type)
{
constexpr field_view v {};
constexpr field_view v2 (nullptr);
static_assert(v.is_null(), "");
static_assert(v.is<null_t>(), "");
static_assert(v.is_convertible_to<null_t>(), "");
static_assert(v.to_variant() == vt(null_t()), "");
static_assert(v == v2, "");
static_assert(v != field_view(10), "");
static_assert(v < field_view(10), "");
static_assert(v <= field_view(10), "");
static_assert(v >= v2, "");
#ifndef BOOST_NO_CXX17_HDR_OPTIONAL
static_assert(v.get_std_optional<null_t>(), "");
#endif
}
#endif
BOOST_AUTO_TEST_CASE(int64_t_type)
{
constexpr field_view v (std::int64_t(60));
constexpr field_view v2 (-4); // from other int type
static_assert(!v.is_null(), "");
static_assert(v.is<std::int64_t>(), "");
static_assert(v.is_convertible_to<std::int64_t>(), "");
static_assert(v.is_convertible_to<std::uint64_t>(), "");
static_assert(v.to_variant() == vt(std::int64_t(60)), "");
static_assert(v == v, "");
static_assert(v != v2, "");
static_assert(v < field_view(boost::mysql::time(9999)), "");
static_assert(v <= field_view(boost::mysql::time(9999)), "");
static_assert(v > field_view(std::int64_t(0)), "");
static_assert(v >= field_view(std::int64_t(0)), "");
#ifndef BOOST_NO_CXX17_HDR_OPTIONAL
static_assert(v.get_std_optional<std::int64_t>(), "");
#endif
}
BOOST_AUTO_TEST_CASE(uint64_t_type)
{
constexpr field_view v (std::uint64_t(60));
static_assert(!v.is_null(), "");
static_assert(v.is<std::uint64_t>(), "");
static_assert(v.is_convertible_to<std::int64_t>(), "");
static_assert(v.is_convertible_to<std::uint64_t>(), "");
static_assert(v.to_variant() == vt(std::uint64_t(60)), "");
static_assert(v == v, "");
static_assert(v != field_view(std::int64_t(0)), "");
static_assert(v < field_view(boost::mysql::time(9999)), "");
static_assert(v <= field_view(boost::mysql::time(9999)), "");
static_assert(v > field_view(std::int64_t(0)), "");
static_assert(v >= field_view(std::int64_t(0)), "");
#ifndef BOOST_NO_CXX17_HDR_OPTIONAL
static_assert(v.get_std_optional<std::uint64_t>(), "");
#endif
}
BOOST_AUTO_TEST_CASE(string_view_type)
{
constexpr field_view v (boost::string_view("test", 4));
static_assert(!v.is_null(), "");
static_assert(v.is<boost::string_view>(), "");
static_assert(v.is_convertible_to<boost::string_view>(), "");
// Equality is not constexpr for strings
static_assert(v != field_view(std::int64_t(0)), "");
static_assert(v < field_view(boost::mysql::time(9999)), "");
static_assert(v <= field_view(boost::mysql::time(9999)), "");
static_assert(v > field_view(std::int64_t(0)), "");
static_assert(v >= field_view(std::int64_t(0)), "");
#ifndef BOOST_NO_CXX17_HDR_OPTIONAL
static_assert(v.get_std_optional<boost::string_view>(), "");
#endif
}
BOOST_AUTO_TEST_CASE(float_type)
{
constexpr field_view v (3.14f);
static_assert(!v.is_null(), "");
static_assert(v.is<float>(), "");
static_assert(v.is_convertible_to<float>(), "");
static_assert(v.is_convertible_to<double>(), "");
static_assert(v.to_variant() == vt(3.14f), "");
static_assert(v == v, "");
static_assert(v != field_view(std::int64_t(0)), "");
static_assert(v < field_view(boost::mysql::time(9999)), "");
static_assert(v <= field_view(boost::mysql::time(9999)), "");
static_assert(v > field_view(std::int64_t(0)), "");
static_assert(v >= field_view(std::int64_t(0)), "");
#ifndef BOOST_NO_CXX17_HDR_OPTIONAL
static_assert(v.get_std_optional<float>(), "");
#endif
}
BOOST_AUTO_TEST_CASE(double_type)
{
constexpr field_view v (3.14);
static_assert(!v.is_null(), "");
static_assert(v.is<double>(), "");
static_assert(v.is_convertible_to<double>(), "");
static_assert(v.to_variant() == vt(3.14), "");
static_assert(v == v, "");
static_assert(v != field_view(std::int64_t(0)), "");
static_assert(v < field_view(boost::mysql::time(9999)), "");
static_assert(v <= field_view(boost::mysql::time(9999)), "");
static_assert(v > field_view(std::int64_t(0)), "");
static_assert(v >= field_view(std::int64_t(0)), "");
#ifndef BOOST_NO_CXX17_HDR_OPTIONAL
static_assert(v.get_std_optional<double>(), "");
#endif
}
BOOST_AUTO_TEST_CASE(date_type)
{
constexpr date d (days(1));
constexpr field_view v (d);
static_assert(!v.is_null(), "");
static_assert(v.is<date>(), "");
static_assert(v.is_convertible_to<date>(), "");
static_assert(v.to_variant() == vt(d), "");
static_assert(v == v, "");
static_assert(v != field_view(std::int64_t(0)), "");
static_assert(v < field_view(boost::mysql::time(9999)), "");
static_assert(v <= field_view(boost::mysql::time(9999)), "");
static_assert(v > field_view(std::int64_t(0)), "");
static_assert(v >= field_view(std::int64_t(0)), "");
#ifndef BOOST_NO_CXX17_HDR_OPTIONAL
static_assert(v.get_std_optional<date>(), "");
#endif
}
BOOST_AUTO_TEST_CASE(datetime_type)
{
constexpr datetime d (days(1));
constexpr field_view v (d);
static_assert(!v.is_null(), "");
static_assert(v.is<datetime>(), "");
static_assert(v.is_convertible_to<datetime>(), "");
static_assert(v.to_variant() == vt(d), "");
static_assert(v == v, "");
static_assert(v != field_view(std::int64_t(0)), "");
static_assert(v < field_view(boost::mysql::time(9999)), "");
static_assert(v <= field_view(boost::mysql::time(9999)), "");
static_assert(v > field_view(std::int64_t(0)), "");
static_assert(v >= field_view(std::int64_t(0)), "");
#ifndef BOOST_NO_CXX17_HDR_OPTIONAL
static_assert(v.get_std_optional<datetime>(), "");
#endif
}
BOOST_AUTO_TEST_CASE(time_type)
{
constexpr boost::mysql::time t (1);
constexpr field_view v (t);
static_assert(!v.is_null(), "");
static_assert(v.is<boost::mysql::time>(), "");
static_assert(v.is_convertible_to<boost::mysql::time>(), "");
static_assert(v.to_variant() == vt(t), "");
static_assert(v == v, "");
static_assert(v != field_view(std::int64_t(0)), "");
static_assert(v < field_view(boost::mysql::time(9999)), "");
static_assert(v <= field_view(boost::mysql::time(9999)), "");
static_assert(v > field_view(std::int64_t(0)), "");
static_assert(v >= field_view(std::int64_t(0)), "");
#ifndef BOOST_NO_CXX17_HDR_OPTIONAL
static_assert(v.get_std_optional<boost::mysql::time>(), "");
#endif
}
BOOST_AUTO_TEST_SUITE_END()
#endif