type::Primitive: Make boolean a separate ObjectWrapper. Tests: Primitive test.

This commit is contained in:
lganzzzo 2020-04-30 03:04:15 +03:00
parent 76da4714c2
commit 11c7be7a50
4 changed files with 355 additions and 12 deletions

View File

@ -205,6 +205,64 @@ public:
};
/**
* ObjectWrapper for Boolean.
*/
class Boolean : public type::ObjectWrapper<bool, __class::Boolean> {
public:
OATPP_DEFINE_OBJECT_WRAPPER_DEFAULTS(Boolean, bool, __class::Boolean)
Boolean(bool value)
: type::ObjectWrapper<bool, __class::Boolean>(std::make_shared<bool>(value))
{}
Boolean& operator = (bool value) {
this->m_ptr = std::make_shared<bool>(value);
return *this;
}
bool operator*() const {
return this->m_ptr.operator*();
}
inline bool operator == (std::nullptr_t){
return m_ptr.get() == nullptr;
}
inline bool operator != (std::nullptr_t){
return m_ptr.get() != nullptr;
}
bool operator == (bool value) const {
if(!this->m_ptr) return false;
return *this->m_ptr == value;
}
bool operator != (bool value) const {
if(!this->m_ptr) return true;
return *this->m_ptr != value;
}
bool operator == (const Boolean &other) const {
if(this->m_ptr.get() == other.m_ptr.get()) return true;
if(!this->m_ptr || !other.m_ptr) return false;
return *this->m_ptr == *other.m_ptr;
}
bool operator != (const Boolean &other) const {
return !operator == (other);
}
explicit operator bool() const {
if(this->m_ptr) {
return *(this->m_ptr);
}
return false;
}
};
/**
* Int8 is an ObjectWrapper over `v_int8` and __class::Int8.
*/
@ -254,11 +312,6 @@ typedef Primitive<v_float32, __class::Float32> Float32;
* Float64 is an ObjectWrapper over `v_float64` and __class::Float64.
*/
typedef Primitive<v_float64, __class::Float64> Float64;
/**
* Boolean is an ObjectWrapper over `bool` and __class::Boolean.
*/
typedef Primitive<bool, __class::Boolean> Boolean;
namespace __class {

View File

@ -90,15 +90,15 @@ void runTests() {
OATPP_RUN_TEST(oatpp::test::core::data::stream::ChunkedBufferTest);
OATPP_RUN_TEST(oatpp::test::core::data::stream::BufferStreamTest);
*/
OATPP_RUN_TEST(oatpp::test::core::data::mapping::type::ObjectWrapperTest);
OATPP_RUN_TEST(oatpp::test::core::data::mapping::type::TypeTest);
OATPP_RUN_TEST(oatpp::test::core::data::mapping::type::AnyTest);
// OATPP_RUN_TEST(oatpp::test::core::data::mapping::type::ObjectWrapperTest);
// OATPP_RUN_TEST(oatpp::test::core::data::mapping::type::TypeTest);
// OATPP_RUN_TEST(oatpp::test::core::data::mapping::type::AnyTest);
OATPP_RUN_TEST(oatpp::test::core::data::mapping::type::StringTest);
OATPP_RUN_TEST(oatpp::test::core::data::mapping::type::PrimitiveTest);
OATPP_RUN_TEST(oatpp::test::core::data::mapping::type::ListTest);
OATPP_RUN_TEST(oatpp::test::core::data::mapping::type::VectorTest);
OATPP_RUN_TEST(oatpp::test::core::data::mapping::type::PairListTest);
OATPP_RUN_TEST(oatpp::test::core::data::mapping::type::UnorderedMapTest);
// OATPP_RUN_TEST(oatpp::test::core::data::mapping::type::ListTest);
// OATPP_RUN_TEST(oatpp::test::core::data::mapping::type::VectorTest);
// OATPP_RUN_TEST(oatpp::test::core::data::mapping::type::PairListTest);
// OATPP_RUN_TEST(oatpp::test::core::data::mapping::type::UnorderedMapTest);
/*
OATPP_RUN_TEST(oatpp::test::async::LockTest);

View File

@ -28,8 +28,194 @@
namespace oatpp { namespace test { namespace core { namespace data { namespace mapping { namespace type {
namespace {
template<class T>
void checkHash(const T& val) {
std::hash<T>{}(val);
}
}
void PrimitiveTest::onRun() {
{
//checkHash(oatpp::Int8(8));
//checkHash(oatpp::UInt8(8));
}
{
OATPP_LOGI(TAG, "test default constructor");
oatpp::Int32 i;
OATPP_ASSERT(!i);
OATPP_ASSERT(i == nullptr);
OATPP_ASSERT(i.valueType == oatpp::Int32::Class::getType());
OATPP_LOGI(TAG, "OK");
}
{
OATPP_LOGI(TAG, "test value constructor");
oatpp::Int32 i(0);
OATPP_ASSERT(i);
OATPP_ASSERT(i != nullptr);
OATPP_ASSERT(i == 0);
OATPP_ASSERT(i.valueType == oatpp::Int32::Class::getType());
OATPP_LOGI(TAG, "OK");
}
{
OATPP_LOGI(TAG, "test implicit value constructor");
oatpp::Int32 i = 0;
OATPP_ASSERT(i);
OATPP_ASSERT(i != nullptr);
OATPP_ASSERT(i == 0);
OATPP_ASSERT(i.valueType == oatpp::Int32::Class::getType());
OATPP_LOGI(TAG, "OK");
}
{
OATPP_LOGI(TAG, "test '==' and '!=' operators");
oatpp::Int32 i1 = 0;
oatpp::Int32 i2;
OATPP_ASSERT(i1);
OATPP_ASSERT(i1 != nullptr);
OATPP_ASSERT(i1 == 0);
OATPP_ASSERT(i1 != 1);
OATPP_ASSERT(!i2);
OATPP_ASSERT(i2 == nullptr)
OATPP_ASSERT(i1 != i2);
OATPP_ASSERT(i2 != i1);
i2 = 0;
OATPP_ASSERT(i1 == i2);
OATPP_ASSERT(i2 == i1);
i1 = nullptr;
i2 = nullptr;
OATPP_ASSERT(i1 == i2);
OATPP_LOGI(TAG, "OK");
}
{
OATPP_LOGI(TAG, "test copy-assign operator");
oatpp::Int32 i1 = 0;
oatpp::Int32 i2;
OATPP_ASSERT(i1 != i2);
i2 = i1;
OATPP_ASSERT(i1 == i2);
OATPP_ASSERT(i1.get() == i2.get());
i2 = 1;
OATPP_ASSERT(i1 != i2);
OATPP_ASSERT(i1.get() != i2.get());
OATPP_ASSERT(i1 == 0);
OATPP_ASSERT(i2 == 1);
}
{
OATPP_LOGI(TAG, "test move-assign operator");
oatpp::Int32 i1 = 0;
oatpp::Int32 i2;
OATPP_ASSERT(i1 != i2);
i2 = std::move(i1);
OATPP_ASSERT(i1 == nullptr);
OATPP_ASSERT(i2 != nullptr);
OATPP_ASSERT(i2 == 0);
}
{
OATPP_LOGI(TAG, "Test Boolean [nullptr]");
oatpp::Boolean b;
OATPP_ASSERT(!b);
OATPP_ASSERT(b == nullptr);
OATPP_ASSERT(b != false);
OATPP_ASSERT(b != true);
OATPP_LOGI(TAG, "OK");
}
{
OATPP_LOGI(TAG, "Test Boolean [false]");
oatpp::Boolean b = false;
OATPP_ASSERT(!b); // <--- still !b
OATPP_ASSERT(b != nullptr);
OATPP_ASSERT(b == false);
OATPP_ASSERT(b != true);
OATPP_LOGI(TAG, "OK");
}
{
OATPP_LOGI(TAG, "Test Boolean [true]");
oatpp::Boolean b = true;
OATPP_ASSERT(b);
OATPP_ASSERT(b != nullptr);
OATPP_ASSERT(b != false);
OATPP_ASSERT(b == true);
OATPP_LOGI(TAG, "OK");
}
{
OATPP_LOGI(TAG, "Test Boolean copy-assign operator");
oatpp::Boolean b1 = true;
oatpp::Boolean b2;
b2 = b1;
OATPP_ASSERT(b2);
OATPP_ASSERT(b1);
OATPP_ASSERT(b1 == b2);
OATPP_ASSERT(b1.get() == b2.get());
b2 = false;
OATPP_ASSERT(b1.get() != b2.get());
OATPP_ASSERT(b1 != b2);
OATPP_ASSERT(b2 != b1);
b1 = false;
b2 = nullptr;
OATPP_ASSERT(b1 != b2);
OATPP_ASSERT(b2 != b1);
b1 = nullptr;
b2 = nullptr;
OATPP_ASSERT(b1 == b2);
OATPP_LOGI(TAG, "OK");
}
{
OATPP_LOGI(TAG, "Test Boolean move-assign operator");
oatpp::Boolean b1 = true;
oatpp::Boolean b2;
b2 = std::move(b1);
OATPP_ASSERT(b2 != nullptr);
OATPP_ASSERT(b1 == nullptr);
OATPP_LOGI(TAG, "OK");
}
}
}}}}}}

View File

@ -26,10 +26,114 @@
#include "oatpp/core/Types.hpp"
#include <functional>
namespace oatpp { namespace test { namespace core { namespace data { namespace mapping { namespace type {
void StringTest::onRun() {
{
oatpp::String s = "hello"; // check hash function exists
std::hash<oatpp::String>{}(s);
}
{
OATPP_LOGI(TAG, "test default constructor");
oatpp::String s;
OATPP_ASSERT(!s);
OATPP_ASSERT(s == nullptr);
OATPP_ASSERT(s == (const char*) nullptr);
OATPP_ASSERT(s.valueType == oatpp::String::Class::getType());
OATPP_LOGI(TAG, "OK");
}
{
OATPP_LOGI(TAG, "test const char* constructor");
oatpp::String s("");
OATPP_ASSERT(s);
OATPP_ASSERT(s != nullptr);
OATPP_ASSERT(s != (const char*) nullptr)
OATPP_ASSERT(s->getSize() == 0);
OATPP_LOGI(TAG, "OK");
}
{
OATPP_LOGI(TAG, "test const char* implicit constructor");
oatpp::String s = "";
OATPP_ASSERT(s);
OATPP_ASSERT(s != nullptr);
OATPP_ASSERT(s != (const char*) nullptr)
OATPP_ASSERT(s->getSize() == 0);
OATPP_LOGI(TAG, "OK");
}
{
OATPP_LOGI(TAG, "test '==', '!=' operators");
oatpp::String s1 = "a";
oatpp::String s2;
OATPP_ASSERT(s1 != s2);
OATPP_ASSERT(s2 != s1);
OATPP_ASSERT(s1 == "a");
OATPP_ASSERT(s1 != "aa");
OATPP_ASSERT(s1 != "");
s2 = "aa";
OATPP_ASSERT(s1 != s2);
OATPP_ASSERT(s2 != s1);
OATPP_LOGI(TAG, "OK");
}
{
OATPP_LOGI(TAG, "test copy-asssign operator");
oatpp::String s1 = "s1";
oatpp::String s2;
s2 = s1;
OATPP_ASSERT(s1 == s2);
OATPP_ASSERT(s1.get() == s2.get());
s1 = "s2";
OATPP_ASSERT(s1 != s2);
OATPP_ASSERT(s2 != s1);
OATPP_LOGI(TAG, "OK");
}
{
OATPP_LOGI(TAG, "test const char* assign operator");
oatpp::String s1 = "s1";
oatpp::String s2(s1);
OATPP_ASSERT(s1 == s2);
OATPP_ASSERT(s1.get() == s2.get());
s1 = "s2";
OATPP_ASSERT(s1 != s2);
OATPP_ASSERT(s2 != s1);
OATPP_LOGI(TAG, "OK");
}
{
OATPP_LOGI(TAG, "test move assign operator");
oatpp::String s1 = "s1";
oatpp::String s2;
s2 = std::move(s1);
OATPP_ASSERT(s1 == nullptr);
OATPP_ASSERT(s2 != nullptr);
OATPP_ASSERT(s2 == "s1");
OATPP_ASSERT(s1 != s2);
OATPP_ASSERT(s1.get() != s2.get());
OATPP_LOGI(TAG, "OK");
}
}
}}}}}}