Skip to content

Commit

Permalink
Added unit test less or equal variant comparision
Browse files Browse the repository at this point in the history
fixes #61
  • Loading branch information
acki-m committed Aug 6, 2017
1 parent 55855f6 commit ce4b322
Show file tree
Hide file tree
Showing 2 changed files with 269 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/unit_tests/unit_tests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ set(SOURCE_FILES main.cpp
variant/variant_cmp_equal_test.cpp
variant/variant_cmp_less_test.cpp
variant/variant_cmp_greater_test.cpp
variant/variant_cmp_less_or_equal.cpp
variant/variant_cmp_greater_or_equal.cpp
variant/variant_misc_test.cpp
variant/variant_conv_to_bool.cpp
Expand Down
268 changes: 268 additions & 0 deletions src/unit_tests/variant/variant_cmp_less_or_equal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
/************************************************************************************
* *
* Copyright (c) 2014, 2015 - 2017 Axel Menzel <[email protected]> *
* *
* This file is part of RTTR (Run Time Type Reflection) *
* License: MIT License *
* *
* Permission is hereby granted, free of charge, to any person obtaining *
* a copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
* SOFTWARE. *
* *
*************************************************************************************/

#include <catch/catch.hpp>

#include <rttr/type>
#include <tuple>

using namespace rttr;
using namespace std;

template<typename T>
struct custom_compare_type_2
{
T i;

bool operator==(const custom_compare_type_2<T>& other) const { return (i == other.i); }

bool operator<(const custom_compare_type_2<T>& other) const { return (i < other.i); }
};

/////////////////////////////////////////////////////////////////////////////////////////

TEST_CASE("variant::operator<=() - empty", "[variant]")
{
SECTION("empty type")
{
variant a;
variant b;

CHECK((a <= b) == true);
}

SECTION("full type lhs")
{
variant a = 23;
variant b;

CHECK((a <= b) == false);
}

SECTION("full type rhs")
{
variant a;
variant b = 23;

CHECK((a <= b) == false);
}

}

/////////////////////////////////////////////////////////////////////////////////////////

TEST_CASE("variant::operator<=() - integer", "[variant]")
{
SECTION("equal")
{
variant a = static_cast<int8_t>(100);
variant b = static_cast<int32_t>(100);

CHECK((a <= b) == true);
}

SECTION("lower <= bigger")
{
variant a = static_cast<int8_t>(100);
variant b = static_cast<int32_t>(10000);

CHECK((a <= b) == true);
}

SECTION("bigger <= lower")
{
variant a = static_cast<int32_t>(10000);
variant b = static_cast<int8_t>(100);

CHECK((a <= b) == false);
}

SECTION("lower <= bigger[unsigned]")
{
variant a = static_cast<int32_t>(-10000);
variant b = static_cast<int8_t>(100);

CHECK((a <= b) == true);
}
}

/////////////////////////////////////////////////////////////////////////////////////////

TEST_CASE("variant::operator<=() - float", "[variant]")
{
SECTION("equal")
{
variant a = 123.2f;
variant b = 123.2f;

CHECK((a <= b) == true);
}

SECTION("lower <= bigger")
{
variant a = static_cast<int8_t>(123);
variant b = 123.2f;

CHECK((a <= b) == true);
}

SECTION("bigger <= lower")
{
variant a = 123.2f;
variant b = static_cast<int8_t>(123);

CHECK((a <= b) == false);
}

SECTION("lower <= bigger")
{
variant a = -1230.3f;
variant b = static_cast<int8_t>(100);

CHECK((a <= b) == true);
}

SECTION("lower <= bigger")
{
variant a = -1230.37f;
variant b = -1230.31;

CHECK((a <= b) == true);
}
}

/////////////////////////////////////////////////////////////////////////////////////////

TEST_CASE("variant::operator<=() - double", "[variant]")
{
SECTION("equal")
{
variant a = 123.2;
variant b = 123.2;

CHECK((a <= b) == true);
}

SECTION("lower <= bigger")
{
variant a = static_cast<int8_t>(123);
variant b = 123.2;

CHECK((a <= b) == true);
}

SECTION("int - bigger <= lower")
{
variant a = 123.2;
variant b = static_cast<int8_t>(123);

CHECK((a <= b) == false);
}

SECTION("int UnSigned - lower <= bigger")
{
variant a = -1230.3;
variant b = static_cast<int8_t>(100);

CHECK((a <= b) == true);
}
}

/////////////////////////////////////////////////////////////////////////////////////////

TEST_CASE("variant::operator<=() - with nullptr", "[variant]")
{
SECTION("nullptr > nullptr")
{
variant a = nullptr;
variant b = nullptr;

CHECK((a <= b) == true);
}

SECTION("nullptr > valid")
{
int obj = 12;
variant a = nullptr;
variant b = &obj;

CHECK((a <= b) == true);
}

SECTION("valid > nullptr")
{
int obj = 12;
variant a = &obj;
variant b = nullptr;

CHECK((a <= b) == false);
}
}

/////////////////////////////////////////////////////////////////////////////////////////

TEST_CASE("variant::operator<=() - raw arrays", "[variant]")
{
SECTION("int - pos. - equal")
{
int array[2][5] = {{1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}};
int arrays[2][5] = {{1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}};
variant a = array;
variant b = arrays;

CHECK((a <= b) == true);
}

SECTION("int - neg.")
{
int array[2][5] = {{1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}};
int arrays[2][5] = {{1, 2, 3, 4, 5}, {1, 2, 3, 12, 5}};

variant a = array;
variant b = arrays;

CHECK((a <= b) == true);
}
}

/////////////////////////////////////////////////////////////////////////////////////////

TEST_CASE("variant::operator<=() - custom type", "[variant]")
{
variant a = custom_compare_type_2<int> { 2 };
variant b = custom_compare_type_2<int> { 2 };

type::register_less_than_comparator<custom_compare_type_2<int>>();

CHECK((a <= b) == false); // no valid comparision, because the equal operator was not registered

type::register_equal_comparator<custom_compare_type_2<int>>();

CHECK((a <= b) == true); // now we can compare
}

/////////////////////////////////////////////////////////////////////////////////////////

0 comments on commit ce4b322

Please sign in to comment.