2016-02-10 00:58:03 +01:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <gmock/gmock.h>
|
2015-06-26 01:07:46 +02:00
|
|
|
#include <boost/optional/optional_io.hpp>
|
2016-02-11 12:53:42 +01:00
|
|
|
#include "cpp-utils/either.h"
|
|
|
|
#include "cpp-utils/macros.h"
|
2015-06-26 02:17:08 +02:00
|
|
|
#include <sstream>
|
2015-06-25 15:46:53 +02:00
|
|
|
|
2015-06-26 01:07:46 +02:00
|
|
|
//TODO Go through all test cases and think about whether it makes sense to add the same test case but with primitive types.
|
|
|
|
|
2015-06-26 02:17:08 +02:00
|
|
|
using std::ostringstream;
|
2015-06-26 01:07:46 +02:00
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
2015-06-26 12:41:25 +02:00
|
|
|
using std::pair;
|
|
|
|
using std::make_pair;
|
2015-06-26 01:07:46 +02:00
|
|
|
using namespace cpputils;
|
|
|
|
using ::testing::Test;
|
|
|
|
|
2015-06-25 15:46:53 +02:00
|
|
|
class OnlyMoveable {
|
|
|
|
public:
|
|
|
|
OnlyMoveable(int value_): value(value_) {}
|
|
|
|
OnlyMoveable(OnlyMoveable &&source): value(source.value) {source.value = -1;}
|
2015-06-26 02:06:33 +02:00
|
|
|
bool operator==(const OnlyMoveable &rhs) const {
|
|
|
|
return value == rhs.value;
|
|
|
|
}
|
2015-06-25 15:46:53 +02:00
|
|
|
int value;
|
|
|
|
private:
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(OnlyMoveable);
|
|
|
|
};
|
|
|
|
|
2015-06-26 01:07:46 +02:00
|
|
|
template<typename T>
|
|
|
|
struct StoreWith1ByteFlag {
|
|
|
|
T val;
|
|
|
|
char flag;
|
|
|
|
};
|
2015-06-25 15:46:53 +02:00
|
|
|
|
|
|
|
class EitherTest: public Test {
|
|
|
|
public:
|
|
|
|
template<class Left, class Right>
|
2015-07-24 20:08:03 +02:00
|
|
|
void EXPECT_IS_LEFT(const either<Left,Right> &val) {
|
2015-06-25 15:46:53 +02:00
|
|
|
EXPECT_TRUE(val.is_left());
|
|
|
|
EXPECT_FALSE(val.is_right());
|
|
|
|
}
|
|
|
|
template<class Left, class Right>
|
2015-07-24 20:08:03 +02:00
|
|
|
void EXPECT_IS_RIGHT(const either<Left,Right> &val) {
|
2015-06-25 15:46:53 +02:00
|
|
|
EXPECT_FALSE(val.is_left());
|
|
|
|
EXPECT_TRUE(val.is_right());
|
|
|
|
}
|
|
|
|
template<class Left, class Right, class Expected>
|
2015-07-24 20:08:03 +02:00
|
|
|
void EXPECT_LEFT_IS(const Expected &expected, either<Left, Right> &value) {
|
2015-06-25 15:46:53 +02:00
|
|
|
EXPECT_IS_LEFT(value);
|
|
|
|
EXPECT_EQ(expected, value.left());
|
2017-11-04 20:03:04 +01:00
|
|
|
EXPECT_EQ(expected, value.left_opt().get());
|
2015-06-26 01:07:46 +02:00
|
|
|
EXPECT_EQ(boost::none, value.right_opt());
|
2015-07-24 20:08:03 +02:00
|
|
|
const either<Left, Right> &const_value = value;
|
2015-06-25 15:46:53 +02:00
|
|
|
EXPECT_EQ(expected, const_value.left());
|
2017-11-04 20:03:04 +01:00
|
|
|
EXPECT_EQ(expected, const_value.left_opt().get());
|
2015-06-26 01:07:46 +02:00
|
|
|
EXPECT_EQ(boost::none, const_value.right_opt());
|
2015-06-25 15:46:53 +02:00
|
|
|
}
|
|
|
|
template<class Left, class Right, class Expected>
|
2015-07-24 20:08:03 +02:00
|
|
|
void EXPECT_RIGHT_IS(const Expected &expected, either<Left, Right> &value) {
|
2015-06-25 15:46:53 +02:00
|
|
|
EXPECT_IS_RIGHT(value);
|
|
|
|
EXPECT_EQ(expected, value.right());
|
2017-11-04 20:03:04 +01:00
|
|
|
EXPECT_EQ(expected, value.right_opt().get());
|
2015-06-26 01:07:46 +02:00
|
|
|
EXPECT_EQ(boost::none, value.left_opt());
|
2015-07-24 20:08:03 +02:00
|
|
|
const either<Left, Right> &const_value = value;
|
2015-06-25 15:46:53 +02:00
|
|
|
EXPECT_EQ(expected, const_value.right());
|
2017-11-04 20:03:04 +01:00
|
|
|
EXPECT_EQ(expected, const_value.right_opt().get());
|
2015-06-26 01:07:46 +02:00
|
|
|
EXPECT_EQ(boost::none, const_value.left_opt());
|
2015-06-25 15:46:53 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-06-26 01:07:46 +02:00
|
|
|
template<typename Left, typename Right>
|
|
|
|
void TestSpaceUsage() {
|
2015-07-24 20:08:03 +02:00
|
|
|
EXPECT_EQ(std::max(sizeof(StoreWith1ByteFlag<Left>), sizeof(StoreWith1ByteFlag<Right>)), sizeof(either<Left, Right>));
|
2015-06-26 01:07:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest, SpaceUsage) {
|
|
|
|
TestSpaceUsage<char, int>();
|
|
|
|
TestSpaceUsage<int, short>();
|
|
|
|
TestSpaceUsage<char, short>();
|
|
|
|
TestSpaceUsage<int, string>();
|
|
|
|
TestSpaceUsage<string, vector<string>>();
|
|
|
|
}
|
|
|
|
|
2015-06-25 15:46:53 +02:00
|
|
|
TEST_F(EitherTest, LeftCanBeConstructed) {
|
2015-07-24 20:08:03 +02:00
|
|
|
either<int, string> val = 3;
|
2015-06-25 15:46:53 +02:00
|
|
|
UNUSED(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest, RightCanBeConstructed) {
|
2015-07-24 20:08:03 +02:00
|
|
|
either<int, string> val = string("string");
|
2015-06-25 15:46:53 +02:00
|
|
|
UNUSED(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest, IsLeft) {
|
2015-07-24 20:08:03 +02:00
|
|
|
either<int, string> val = 3;
|
2015-06-25 15:46:53 +02:00
|
|
|
EXPECT_IS_LEFT(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest, IsRight) {
|
2015-07-24 20:08:03 +02:00
|
|
|
either<int, string> val = string("string");
|
2015-06-25 15:46:53 +02:00
|
|
|
EXPECT_IS_RIGHT(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest, LeftIsStored) {
|
2015-07-24 20:08:03 +02:00
|
|
|
either<int, string> val = 3;
|
2015-06-25 15:46:53 +02:00
|
|
|
EXPECT_LEFT_IS(3, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest, RightIsStored) {
|
2015-07-24 20:08:03 +02:00
|
|
|
either<int, string> val = string("string");
|
2015-06-25 15:46:53 +02:00
|
|
|
EXPECT_RIGHT_IS("string", val);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest, LeftCanBeMoveContructed) {
|
2015-07-24 20:08:03 +02:00
|
|
|
either<OnlyMoveable, string> val = OnlyMoveable(1);
|
2015-06-25 15:46:53 +02:00
|
|
|
UNUSED(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest, RightCanBeMoveContructed) {
|
2015-07-24 20:08:03 +02:00
|
|
|
either<string, OnlyMoveable> val = OnlyMoveable(1);
|
2015-06-25 15:46:53 +02:00
|
|
|
UNUSED(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest, IsLeftWhenMoveContructed) {
|
2015-07-24 20:08:03 +02:00
|
|
|
either<OnlyMoveable, string> val = OnlyMoveable(1);
|
2015-06-25 15:46:53 +02:00
|
|
|
EXPECT_IS_LEFT(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest, IsRightWhenMoveContructed) {
|
2015-07-24 20:08:03 +02:00
|
|
|
either<string, OnlyMoveable> val = OnlyMoveable(1);
|
2015-06-25 15:46:53 +02:00
|
|
|
EXPECT_IS_RIGHT(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest, LeftIsStoredWhenMoveContructed) {
|
2015-07-24 20:08:03 +02:00
|
|
|
either<OnlyMoveable, string> val = OnlyMoveable(2);
|
2015-06-26 02:06:33 +02:00
|
|
|
EXPECT_LEFT_IS(OnlyMoveable(2), val);
|
2015-06-25 15:46:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest, RightIsStoredWhenMoveContructed) {
|
2015-07-24 20:08:03 +02:00
|
|
|
either<string, OnlyMoveable> val = OnlyMoveable(3);
|
2015-06-26 02:06:33 +02:00
|
|
|
EXPECT_RIGHT_IS(OnlyMoveable(3), val);
|
2015-06-25 15:46:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest, LeftCanBeCopied) {
|
2015-07-24 20:08:03 +02:00
|
|
|
either<string, int> val = string("string");
|
|
|
|
either<string, int> val2 = val;
|
2015-06-25 15:46:53 +02:00
|
|
|
EXPECT_LEFT_IS("string", val2);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest, CopyingLeftDoesntChangeSource) {
|
2015-07-24 20:08:03 +02:00
|
|
|
either<string, int> val = string("string");
|
|
|
|
either<string, int> val2 = val;
|
2015-06-25 15:46:53 +02:00
|
|
|
EXPECT_LEFT_IS("string", val);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest, RightCanBeCopied) {
|
2015-07-24 20:08:03 +02:00
|
|
|
either<int, string> val = string("string");
|
|
|
|
either<int, string> val2 = val;
|
2015-06-25 15:46:53 +02:00
|
|
|
EXPECT_RIGHT_IS("string", val2);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest, CopyingRightDoesntChangeSource) {
|
2015-07-24 20:08:03 +02:00
|
|
|
either<int, string> val = string("string");
|
|
|
|
either<int, string> val2 = val;
|
2015-06-25 15:46:53 +02:00
|
|
|
EXPECT_RIGHT_IS("string", val);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest, LeftCanBeMoved) {
|
2015-07-24 20:08:03 +02:00
|
|
|
either<OnlyMoveable, int> val = OnlyMoveable(5);
|
|
|
|
either<OnlyMoveable, int> val2 = std::move(val);
|
2015-06-26 02:06:33 +02:00
|
|
|
EXPECT_LEFT_IS(OnlyMoveable(5), val2);
|
2015-06-25 15:46:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest, RightCanBeMoved) {
|
2015-07-24 20:08:03 +02:00
|
|
|
either<int, OnlyMoveable> val = OnlyMoveable(5);
|
|
|
|
either<int, OnlyMoveable> val2 = std::move(val);
|
2015-06-26 02:06:33 +02:00
|
|
|
EXPECT_RIGHT_IS(OnlyMoveable(5), val2);
|
2015-06-25 15:46:53 +02:00
|
|
|
}
|
|
|
|
|
2015-06-26 01:59:29 +02:00
|
|
|
TEST_F(EitherTest, LeftCanBeAssigned) {
|
2015-07-24 20:08:03 +02:00
|
|
|
either<string, int> val = string("string");
|
|
|
|
either<string, int> val2 = string("otherstring");
|
2015-06-26 01:59:29 +02:00
|
|
|
val2 = val;
|
|
|
|
EXPECT_LEFT_IS("string", val2);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest, RightCanBeAssigned) {
|
2015-07-24 20:08:03 +02:00
|
|
|
either<int, string> val = string("string");
|
|
|
|
either<int, string> val2 = string("otherstring");
|
2015-06-26 01:59:29 +02:00
|
|
|
val2 = val;
|
|
|
|
EXPECT_RIGHT_IS("string", val2);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest, LeftCanBeMoveAssigned) {
|
2015-07-24 20:08:03 +02:00
|
|
|
either<OnlyMoveable, int> val = OnlyMoveable(3);
|
|
|
|
either<OnlyMoveable, int> val2 = OnlyMoveable(4);
|
2015-06-26 01:59:29 +02:00
|
|
|
val2 = std::move(val);
|
2015-06-26 02:06:33 +02:00
|
|
|
EXPECT_LEFT_IS(OnlyMoveable(3), val2);
|
2015-06-26 01:59:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest, RightCanBeMoveAssigned) {
|
2015-07-24 20:08:03 +02:00
|
|
|
either<int, OnlyMoveable> val = OnlyMoveable(3);
|
|
|
|
either<int, OnlyMoveable> val2 = OnlyMoveable(4);
|
2015-06-26 01:59:29 +02:00
|
|
|
val2 = std::move(val);
|
2015-06-26 02:06:33 +02:00
|
|
|
EXPECT_RIGHT_IS(OnlyMoveable(3), val2);
|
2015-06-26 01:59:29 +02:00
|
|
|
}
|
|
|
|
|
2015-06-26 02:02:29 +02:00
|
|
|
TEST_F(EitherTest, LeftCanBeDirectlyAssigned) {
|
2015-07-24 20:08:03 +02:00
|
|
|
either<string, int> val = string("string");
|
2015-06-26 02:02:29 +02:00
|
|
|
val = string("otherstring");
|
|
|
|
EXPECT_LEFT_IS("otherstring", val);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest, RightCanBeDirectlyAssigned) {
|
2015-07-24 20:08:03 +02:00
|
|
|
either<int, string> val = string("string");
|
2015-06-26 02:02:29 +02:00
|
|
|
val = string("otherstring");
|
|
|
|
EXPECT_RIGHT_IS("otherstring", val);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest, LeftCanBeDirectlyMoveAssigned) {
|
2015-07-24 20:08:03 +02:00
|
|
|
either<OnlyMoveable, int> val = OnlyMoveable(3);
|
2015-06-26 02:02:29 +02:00
|
|
|
val = OnlyMoveable(5);
|
2015-06-26 02:06:33 +02:00
|
|
|
EXPECT_LEFT_IS(OnlyMoveable(5), val);
|
2015-06-26 02:02:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest, RightCanBeDirectlyMoveAssigned) {
|
2015-07-24 20:08:03 +02:00
|
|
|
either<int, OnlyMoveable> val = OnlyMoveable(3);
|
2015-06-26 02:02:29 +02:00
|
|
|
val = OnlyMoveable(5);
|
2015-06-26 02:06:33 +02:00
|
|
|
EXPECT_RIGHT_IS(OnlyMoveable(5), val);
|
2015-06-26 02:02:29 +02:00
|
|
|
}
|
|
|
|
|
2015-06-26 01:07:46 +02:00
|
|
|
TEST_F(EitherTest, ModifyLeft) {
|
2015-07-24 20:08:03 +02:00
|
|
|
either<string, int> val = string("mystring1");
|
2015-06-26 01:07:46 +02:00
|
|
|
val.left() = "mystring2";
|
|
|
|
EXPECT_LEFT_IS("mystring2", val);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest, ModifyRight) {
|
2015-07-24 20:08:03 +02:00
|
|
|
either<int, string> val = string("mystring1");
|
2015-06-26 01:07:46 +02:00
|
|
|
val.right() = "mystring2";
|
|
|
|
EXPECT_RIGHT_IS("mystring2", val);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest, ModifyLeftOpt) {
|
2015-07-24 20:08:03 +02:00
|
|
|
either<string, int> val = string("mystring1");
|
2017-11-04 20:03:04 +01:00
|
|
|
val.left_opt().get() = "mystring2";
|
2015-06-26 01:07:46 +02:00
|
|
|
EXPECT_LEFT_IS("mystring2", val);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest, ModifyRightOpt) {
|
2015-07-24 20:08:03 +02:00
|
|
|
either<int, string> val = string("mystring1");
|
2017-10-27 05:02:51 +02:00
|
|
|
val.right_opt().value() = "mystring2";
|
2015-06-26 01:07:46 +02:00
|
|
|
EXPECT_RIGHT_IS("mystring2", val);
|
|
|
|
}
|
|
|
|
|
2015-06-25 16:27:26 +02:00
|
|
|
TEST_F(EitherTest, LeftEquals) {
|
2015-07-24 20:08:03 +02:00
|
|
|
either<string, int> val1 = string("mystring");
|
|
|
|
either<string, int> val2 = string("mystring");
|
2015-06-25 16:27:26 +02:00
|
|
|
EXPECT_TRUE(val1 == val2);
|
|
|
|
EXPECT_TRUE(val2 == val1);
|
|
|
|
EXPECT_FALSE(val1 != val2);
|
|
|
|
EXPECT_FALSE(val2 != val1);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest, LeftNotEquals) {
|
2015-07-24 20:08:03 +02:00
|
|
|
either<string, int> val1 = string("mystring");
|
|
|
|
either<string, int> val2 = string("mystring2");
|
2015-06-25 16:27:26 +02:00
|
|
|
EXPECT_TRUE(val1 != val2);
|
|
|
|
EXPECT_TRUE(val2 != val1);
|
|
|
|
EXPECT_FALSE(val1 == val2);
|
|
|
|
EXPECT_FALSE(val2 == val1);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest, RightEquals) {
|
2015-07-24 20:08:03 +02:00
|
|
|
either<int, string> val1 = string("mystring");
|
|
|
|
either<int, string> val2 = string("mystring");
|
2015-06-25 16:27:26 +02:00
|
|
|
EXPECT_TRUE(val1 == val2);
|
|
|
|
EXPECT_TRUE(val2 == val1);
|
|
|
|
EXPECT_FALSE(val1 != val2);
|
|
|
|
EXPECT_FALSE(val2 != val1);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest, RightNotEquals) {
|
2015-07-24 20:08:03 +02:00
|
|
|
either<int, string> val1 = string("mystring");
|
|
|
|
either<int, string> val2 = string("mystring2");
|
2015-06-25 16:27:26 +02:00
|
|
|
EXPECT_TRUE(val1 != val2);
|
|
|
|
EXPECT_TRUE(val2 != val1);
|
|
|
|
EXPECT_FALSE(val1 == val2);
|
|
|
|
EXPECT_FALSE(val2 == val1);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest, LeftNotEqualsRight) {
|
2015-07-24 20:08:03 +02:00
|
|
|
either<string, int> val1 = string("mystring");
|
|
|
|
either<string, int> val2 = 3;
|
2015-06-25 16:27:26 +02:00
|
|
|
EXPECT_TRUE(val1 != val2);
|
|
|
|
EXPECT_TRUE(val2 != val1);
|
|
|
|
EXPECT_FALSE(val1 == val2);
|
|
|
|
EXPECT_FALSE(val2 == val1);
|
|
|
|
}
|
|
|
|
|
2015-06-26 02:17:08 +02:00
|
|
|
TEST_F(EitherTest, OutputLeft) {
|
|
|
|
ostringstream str;
|
2015-07-25 15:17:27 +02:00
|
|
|
str << either<string, int>("mystring");
|
2015-06-26 02:17:08 +02:00
|
|
|
EXPECT_EQ("Left(mystring)", str.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest, OutputRight) {
|
|
|
|
ostringstream str;
|
2015-07-25 15:17:27 +02:00
|
|
|
str << either<int, string>("mystring");
|
2015-06-26 02:17:08 +02:00
|
|
|
EXPECT_EQ("Right(mystring)", str.str());
|
|
|
|
}
|
|
|
|
|
2015-06-26 12:41:25 +02:00
|
|
|
TEST_F(EitherTest, MakeLeft) {
|
2015-07-24 20:08:03 +02:00
|
|
|
either<string, int> var = make_left<string, int>("mystring");
|
2015-06-26 12:41:25 +02:00
|
|
|
EXPECT_LEFT_IS("mystring", var);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest, MakeLeft_OnlyMoveable) {
|
2015-07-24 20:08:03 +02:00
|
|
|
either<OnlyMoveable, int> var = make_left<OnlyMoveable, int>(4);
|
2015-06-26 12:41:25 +02:00
|
|
|
EXPECT_LEFT_IS(OnlyMoveable(4), var);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest, MakeLeft_MultiParam) {
|
2015-07-24 20:08:03 +02:00
|
|
|
either<pair<int, int>, int> var = make_left<pair<int, int>, int>(4, 5);
|
2015-06-26 12:41:25 +02:00
|
|
|
EXPECT_LEFT_IS(make_pair(4,5), var);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest, MakeRight) {
|
2015-07-24 20:08:03 +02:00
|
|
|
either<int, string> var = make_right<int, string>("mystring");
|
2015-06-26 12:41:25 +02:00
|
|
|
EXPECT_RIGHT_IS("mystring", var);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest, MakeRight_OnlyMoveable) {
|
2015-07-24 20:08:03 +02:00
|
|
|
either<int, OnlyMoveable> var = make_right<int, OnlyMoveable>(4);
|
2015-06-26 12:41:25 +02:00
|
|
|
EXPECT_RIGHT_IS(OnlyMoveable(4), var);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest, MakeRight_MultiParam) {
|
2015-07-24 20:08:03 +02:00
|
|
|
either<int, pair<int, int>> var = make_right<int, pair<int, int>>(4, 5);
|
2015-06-26 12:41:25 +02:00
|
|
|
EXPECT_RIGHT_IS(make_pair(4,5), var);
|
|
|
|
}
|
|
|
|
|
2015-06-28 17:25:13 +02:00
|
|
|
TEST_F(EitherTest, LeftCanBeQueriedAsRvalue) {
|
|
|
|
OnlyMoveable val = make_left<OnlyMoveable, int>(3).left();
|
|
|
|
EXPECT_EQ(OnlyMoveable(3), val);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest, RightCanBeQueriedAsRvalue) {
|
|
|
|
OnlyMoveable val = make_right<int, OnlyMoveable>(3).right();
|
|
|
|
EXPECT_EQ(OnlyMoveable(3), val);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest, LeftOptCanBeQueriedAsRvalue) {
|
|
|
|
OnlyMoveable val = make_left<OnlyMoveable, int>(3).left_opt().value();
|
|
|
|
EXPECT_EQ(OnlyMoveable(3), val);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest, RightOptCanBeQueriedAsRvalue) {
|
|
|
|
OnlyMoveable val = make_right<int, OnlyMoveable>(3).right_opt().value();
|
|
|
|
EXPECT_EQ(OnlyMoveable(3), val);
|
|
|
|
}
|
|
|
|
|
2015-06-25 15:46:53 +02:00
|
|
|
class DestructorCallback {
|
|
|
|
public:
|
|
|
|
MOCK_CONST_METHOD0(call, void());
|
2015-06-26 01:59:29 +02:00
|
|
|
|
|
|
|
void EXPECT_CALLED(int times = 1) {
|
|
|
|
EXPECT_CALL(*this, call()).Times(times);
|
|
|
|
}
|
2015-06-25 15:46:53 +02:00
|
|
|
};
|
|
|
|
class ClassWithDestructorCallback {
|
|
|
|
public:
|
2015-06-26 01:59:29 +02:00
|
|
|
ClassWithDestructorCallback(const DestructorCallback *destructorCallback) : _destructorCallback(destructorCallback) {}
|
|
|
|
ClassWithDestructorCallback(const ClassWithDestructorCallback &rhs): _destructorCallback(rhs._destructorCallback) {}
|
2015-06-25 15:46:53 +02:00
|
|
|
|
|
|
|
~ClassWithDestructorCallback() {
|
|
|
|
_destructorCallback->call();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const DestructorCallback *_destructorCallback;
|
2015-10-17 20:15:20 +02:00
|
|
|
|
|
|
|
ClassWithDestructorCallback &operator=(const ClassWithDestructorCallback &rhs) = delete;
|
2015-06-25 15:46:53 +02:00
|
|
|
};
|
|
|
|
class OnlyMoveableClassWithDestructorCallback {
|
|
|
|
public:
|
|
|
|
OnlyMoveableClassWithDestructorCallback(const DestructorCallback *destructorCallback) : _destructorCallback(destructorCallback) { }
|
|
|
|
OnlyMoveableClassWithDestructorCallback(OnlyMoveableClassWithDestructorCallback &&source): _destructorCallback(source._destructorCallback) {}
|
|
|
|
|
|
|
|
~OnlyMoveableClassWithDestructorCallback() {
|
|
|
|
_destructorCallback->call();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(OnlyMoveableClassWithDestructorCallback);
|
|
|
|
const DestructorCallback *_destructorCallback;
|
|
|
|
};
|
|
|
|
|
|
|
|
class EitherTest_Destructor: public EitherTest {
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(EitherTest_Destructor, LeftDestructorIsCalled) {
|
2015-06-26 01:59:29 +02:00
|
|
|
DestructorCallback destructorCallback;
|
|
|
|
destructorCallback.EXPECT_CALLED(2); //Once for the temp object, once when the either class destructs
|
|
|
|
|
2015-06-25 15:46:53 +02:00
|
|
|
ClassWithDestructorCallback temp(&destructorCallback);
|
2015-07-24 20:08:03 +02:00
|
|
|
either<ClassWithDestructorCallback, string> var = temp;
|
2015-06-25 15:46:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest_Destructor, RightDestructorIsCalled) {
|
2015-06-26 01:59:29 +02:00
|
|
|
DestructorCallback destructorCallback;
|
|
|
|
destructorCallback.EXPECT_CALLED(2); //Once for the temp object, once when the either class destructs
|
|
|
|
|
2015-06-25 15:46:53 +02:00
|
|
|
ClassWithDestructorCallback temp(&destructorCallback);
|
2015-07-24 20:08:03 +02:00
|
|
|
either<string, ClassWithDestructorCallback> var = temp;
|
2015-06-25 15:46:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest_Destructor, LeftDestructorIsCalledAfterCopying) {
|
2015-06-26 01:59:29 +02:00
|
|
|
DestructorCallback destructorCallback;
|
|
|
|
destructorCallback.EXPECT_CALLED(3); //Once for the temp object, once for var1 and once for var2
|
|
|
|
|
2015-06-25 15:46:53 +02:00
|
|
|
ClassWithDestructorCallback temp(&destructorCallback);
|
2015-07-24 20:08:03 +02:00
|
|
|
either<ClassWithDestructorCallback, string> var1 = temp;
|
|
|
|
either<ClassWithDestructorCallback, string> var2 = var1;
|
2015-06-25 15:46:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest_Destructor, RightDestructorIsCalledAfterCopying) {
|
2015-06-26 01:59:29 +02:00
|
|
|
DestructorCallback destructorCallback;
|
|
|
|
destructorCallback.EXPECT_CALLED(3); //Once for the temp object, once for var1 and once for var2
|
|
|
|
|
2015-06-25 15:46:53 +02:00
|
|
|
ClassWithDestructorCallback temp(&destructorCallback);
|
2015-07-24 20:08:03 +02:00
|
|
|
either<string, ClassWithDestructorCallback> var1 = temp;
|
|
|
|
either<string, ClassWithDestructorCallback> var2 = var1;
|
2015-06-25 15:46:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest_Destructor, LeftDestructorIsCalledAfterMoving) {
|
2015-06-26 01:59:29 +02:00
|
|
|
DestructorCallback destructorCallback;
|
|
|
|
destructorCallback.EXPECT_CALLED(3); //Once for the temp object, once for var1 and once for var2
|
|
|
|
|
2015-06-25 15:46:53 +02:00
|
|
|
OnlyMoveableClassWithDestructorCallback temp(&destructorCallback);
|
2015-07-24 20:08:03 +02:00
|
|
|
either<OnlyMoveableClassWithDestructorCallback, string> var1 = std::move(temp);
|
|
|
|
either<OnlyMoveableClassWithDestructorCallback, string> var2 = std::move(var1);
|
2015-06-25 15:46:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest_Destructor, RightDestructorIsCalledAfterMoving) {
|
2015-06-26 01:59:29 +02:00
|
|
|
DestructorCallback destructorCallback;
|
|
|
|
destructorCallback.EXPECT_CALLED(3); //Once for the temp object, once for var1 and once for var2
|
|
|
|
|
2015-06-25 15:46:53 +02:00
|
|
|
OnlyMoveableClassWithDestructorCallback temp(&destructorCallback);
|
2015-07-24 20:08:03 +02:00
|
|
|
either<string, OnlyMoveableClassWithDestructorCallback> var1 = std::move(temp);
|
|
|
|
either<string, OnlyMoveableClassWithDestructorCallback> var2 = std::move(var1);
|
2015-06-26 01:59:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest_Destructor, LeftDestructorIsCalledAfterAssignment) {
|
|
|
|
DestructorCallback destructorCallback1;
|
|
|
|
DestructorCallback destructorCallback2;
|
|
|
|
destructorCallback1.EXPECT_CALLED(2); //Once for the temp1 object, once at the assignment
|
|
|
|
destructorCallback2.EXPECT_CALLED(3); //Once for the temp2 object, once in destructor of var2, once in destructor of var1
|
|
|
|
|
|
|
|
ClassWithDestructorCallback temp1(&destructorCallback1);
|
2015-07-24 20:08:03 +02:00
|
|
|
either<ClassWithDestructorCallback, string> var1 = temp1;
|
2015-06-26 01:59:29 +02:00
|
|
|
ClassWithDestructorCallback temp2(&destructorCallback2);
|
2015-07-24 20:08:03 +02:00
|
|
|
either<ClassWithDestructorCallback, string> var2 = temp2;
|
2015-06-26 01:59:29 +02:00
|
|
|
var1 = var2;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest_Destructor, RightDestructorIsCalledAfterAssignment) {
|
|
|
|
DestructorCallback destructorCallback1;
|
|
|
|
DestructorCallback destructorCallback2;
|
|
|
|
destructorCallback1.EXPECT_CALLED(2); //Once for the temp1 object, once at the assignment
|
|
|
|
destructorCallback2.EXPECT_CALLED(3); //Once for the temp2 object, once in destructor of var2, once in destructor of var1
|
|
|
|
|
|
|
|
ClassWithDestructorCallback temp1(&destructorCallback1);
|
2015-07-24 20:08:03 +02:00
|
|
|
either<string, ClassWithDestructorCallback> var1 = temp1;
|
2015-06-26 01:59:29 +02:00
|
|
|
ClassWithDestructorCallback temp2(&destructorCallback2);
|
2015-07-24 20:08:03 +02:00
|
|
|
either<string, ClassWithDestructorCallback> var2 = temp2;
|
2015-06-26 01:59:29 +02:00
|
|
|
var1 = var2;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest_Destructor, LeftDestructorIsCalledAfterMoveAssignment) {
|
|
|
|
DestructorCallback destructorCallback1;
|
|
|
|
DestructorCallback destructorCallback2;
|
|
|
|
destructorCallback1.EXPECT_CALLED(2); //Once for the temp1 object, once at the assignment
|
|
|
|
destructorCallback2.EXPECT_CALLED(3); //Once for the temp2 object, once in destructor of var2, once in destructor of var1
|
|
|
|
|
|
|
|
OnlyMoveableClassWithDestructorCallback temp1(&destructorCallback1);
|
2015-07-24 20:08:03 +02:00
|
|
|
either<OnlyMoveableClassWithDestructorCallback, string> var1 = std::move(temp1);
|
2015-06-26 01:59:29 +02:00
|
|
|
OnlyMoveableClassWithDestructorCallback temp2(&destructorCallback2);
|
2015-07-24 20:08:03 +02:00
|
|
|
either<OnlyMoveableClassWithDestructorCallback, string> var2 = std::move(temp2);
|
2015-06-26 01:59:29 +02:00
|
|
|
var1 = std::move(var2);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EitherTest_Destructor, RightDestructorIsCalledAfterMoveAssignment) {
|
|
|
|
DestructorCallback destructorCallback1;
|
|
|
|
DestructorCallback destructorCallback2;
|
|
|
|
destructorCallback1.EXPECT_CALLED(2); //Once for the temp1 object, once at the assignment
|
|
|
|
destructorCallback2.EXPECT_CALLED(3); //Once for the temp2 object, once in destructor of var2, once in destructor of var1
|
|
|
|
|
|
|
|
OnlyMoveableClassWithDestructorCallback temp1(&destructorCallback1);
|
2015-07-24 20:08:03 +02:00
|
|
|
either<string, OnlyMoveableClassWithDestructorCallback> var1 = std::move(temp1);
|
2015-06-26 01:59:29 +02:00
|
|
|
OnlyMoveableClassWithDestructorCallback temp2(&destructorCallback2);
|
2015-07-24 20:08:03 +02:00
|
|
|
either<string, OnlyMoveableClassWithDestructorCallback> var2 = std::move(temp2);
|
2015-06-26 01:59:29 +02:00
|
|
|
var1 = std::move(var2);
|
|
|
|
}
|