Renamed Either to either

This commit is contained in:
Sebastian Messmer 2015-07-24 20:08:03 +02:00
parent cce24dd64b
commit 50503d9ce0
2 changed files with 103 additions and 103 deletions

View File

@ -7,30 +7,30 @@
namespace cpputils { namespace cpputils {
template<class Left, class Right> template<class Left, class Right>
class Either final { class either final {
public: public:
//TODO Try allowing construction with any type that std::is_convertible to Left or Right. //TODO Try allowing construction with any type that std::is_convertible to Left or Right.
Either(const Left &left): _side(Side::left) { either(const Left &left): _side(Side::left) {
_construct_left(left); _construct_left(left);
} }
Either(Left &&left): _side(Side::left) { either(Left &&left): _side(Side::left) {
_construct_left(std::move(left)); _construct_left(std::move(left));
} }
Either(const Right &right): _side(Side::right) { either(const Right &right): _side(Side::right) {
_construct_right(right); _construct_right(right);
} }
Either(Right &&right): _side(Side::right) { either(Right &&right): _side(Side::right) {
_construct_right(std::move(right)); _construct_right(std::move(right));
} }
//TODO Try allowing copy-construction when Left/Right types are std::is_convertible //TODO Try allowing copy-construction when Left/Right types are std::is_convertible
Either(const Either<Left, Right> &rhs): _side(rhs._side) { either(const either<Left, Right> &rhs): _side(rhs._side) {
if(_side == Side::left) { if(_side == Side::left) {
_construct_left(rhs._left); _construct_left(rhs._left);
} else { } else {
_construct_right(rhs._right); _construct_right(rhs._right);
} }
} }
Either(Either<Left, Right> &&rhs): _side(rhs._side) { either(either<Left, Right> &&rhs): _side(rhs._side) {
if(_side == Side::left) { if(_side == Side::left) {
_construct_left(std::move(rhs._left)); _construct_left(std::move(rhs._left));
} else { } else {
@ -38,12 +38,12 @@ namespace cpputils {
} }
} }
~Either() { ~either() {
_destruct(); _destruct();
} }
//TODO Try allowing copy-assignment when Left/Right types are std::is_convertible //TODO Try allowing copy-assignment when Left/Right types are std::is_convertible
Either<Left, Right> &operator=(const Either<Left, Right> &rhs) { either<Left, Right> &operator=(const either<Left, Right> &rhs) {
_destruct(); _destruct();
_side = rhs._side; _side = rhs._side;
if (_side == Side::left) { if (_side == Side::left) {
@ -54,7 +54,7 @@ namespace cpputils {
return *this; return *this;
} }
Either<Left, Right> &operator=(Either<Left, Right> &&rhs) { either<Left, Right> &operator=(either<Left, Right> &&rhs) {
_destruct(); _destruct();
_side = rhs._side; _side = rhs._side;
if (_side == Side::left) { if (_side == Side::left) {
@ -79,7 +79,7 @@ namespace cpputils {
return _left; return _left;
} }
Left &left() & { Left &left() & {
return const_cast<Left&>(const_cast<const Either<Left, Right>*>(this)->left()); return const_cast<Left&>(const_cast<const either<Left, Right>*>(this)->left());
} }
Left &&left() && { Left &&left() && {
return std::move(left()); return std::move(left());
@ -89,7 +89,7 @@ namespace cpputils {
return _right; return _right;
} }
Right &right() & { Right &right() & {
return const_cast<Right&>(const_cast<const Either<Left, Right>*>(this)->right()); return const_cast<Right&>(const_cast<const either<Left, Right>*>(this)->right());
} }
Right &&right() && { Right &&right() && {
return std::move(right()); return std::move(right());
@ -146,7 +146,7 @@ namespace cpputils {
}; };
enum class Side : unsigned char {left, right} _side; enum class Side : unsigned char {left, right} _side;
Either(Side side): _side(side) {} either(Side side): _side(side) {}
template<typename... Args> template<typename... Args>
void _construct_left(Args&&... args) { void _construct_left(Args&&... args) {
@ -165,14 +165,14 @@ namespace cpputils {
} }
template<typename Left_, typename Right_, typename... Args> template<typename Left_, typename Right_, typename... Args>
friend Either<Left_, Right_> make_left(Args&&... args); friend either<Left_, Right_> make_left(Args&&... args);
template<typename Left_, typename Right_, typename... Args> template<typename Left_, typename Right_, typename... Args>
friend Either<Left_, Right_> make_right(Args&&... args); friend either<Left_, Right_> make_right(Args&&... args);
}; };
template<class Left, class Right> template<class Left, class Right>
bool operator==(const Either<Left, Right> &lhs, const Either<Left, Right> &rhs) { bool operator==(const either<Left, Right> &lhs, const either<Left, Right> &rhs) {
if (lhs.is_left() != rhs.is_left()) { if (lhs.is_left() != rhs.is_left()) {
return false; return false;
} }
@ -184,12 +184,12 @@ namespace cpputils {
} }
template<class Left, class Right> template<class Left, class Right>
bool operator!=(const Either<Left, Right> &lhs, const Either<Left, Right> &rhs) { bool operator!=(const either<Left, Right> &lhs, const either<Left, Right> &rhs) {
return !operator==(lhs, rhs); return !operator==(lhs, rhs);
} }
template<class Left, class Right> template<class Left, class Right>
std::ostream &operator<<(std::ostream &stream, const Either<Left, Right> &value) { std::ostream &operator<<(std::ostream &stream, const either<Left, Right> &value) {
if (value.is_left()) { if (value.is_left()) {
stream << "Left(" << value.left() << ")"; stream << "Left(" << value.left() << ")";
} else { } else {
@ -199,15 +199,15 @@ namespace cpputils {
} }
template<typename Left, typename Right, typename... Args> template<typename Left, typename Right, typename... Args>
Either<Left, Right> make_left(Args&&... args) { either<Left, Right> make_left(Args&&... args) {
Either<Left, Right> result(Either<Left, Right>::Side::left); either<Left, Right> result(either<Left, Right>::Side::left);
result._construct_left(std::forward<Args>(args)...); result._construct_left(std::forward<Args>(args)...);
return result; return result;
} }
template<typename Left, typename Right, typename... Args> template<typename Left, typename Right, typename... Args>
Either<Left, Right> make_right(Args&&... args) { either<Left, Right> make_right(Args&&... args) {
Either<Left, Right> result(Either<Left, Right>::Side::right); either<Left, Right> result(either<Left, Right>::Side::right);
result._construct_right(std::forward<Args>(args)...); result._construct_right(std::forward<Args>(args)...);
return result; return result;
} }

View File

@ -36,33 +36,33 @@ struct StoreWith1ByteFlag {
class EitherTest: public Test { class EitherTest: public Test {
public: public:
template<class Left, class Right> template<class Left, class Right>
void EXPECT_IS_LEFT(const Either<Left,Right> &val) { void EXPECT_IS_LEFT(const either<Left,Right> &val) {
EXPECT_TRUE(val.is_left()); EXPECT_TRUE(val.is_left());
EXPECT_FALSE(val.is_right()); EXPECT_FALSE(val.is_right());
} }
template<class Left, class Right> template<class Left, class Right>
void EXPECT_IS_RIGHT(const Either<Left,Right> &val) { void EXPECT_IS_RIGHT(const either<Left,Right> &val) {
EXPECT_FALSE(val.is_left()); EXPECT_FALSE(val.is_left());
EXPECT_TRUE(val.is_right()); EXPECT_TRUE(val.is_right());
} }
template<class Left, class Right, class Expected> template<class Left, class Right, class Expected>
void EXPECT_LEFT_IS(const Expected &expected, Either<Left, Right> &value) { void EXPECT_LEFT_IS(const Expected &expected, either<Left, Right> &value) {
EXPECT_IS_LEFT(value); EXPECT_IS_LEFT(value);
EXPECT_EQ(expected, value.left()); EXPECT_EQ(expected, value.left());
EXPECT_EQ(expected, value.left_opt().get()); EXPECT_EQ(expected, value.left_opt().get());
EXPECT_EQ(boost::none, value.right_opt()); EXPECT_EQ(boost::none, value.right_opt());
const Either<Left, Right> &const_value = value; const either<Left, Right> &const_value = value;
EXPECT_EQ(expected, const_value.left()); EXPECT_EQ(expected, const_value.left());
EXPECT_EQ(expected, const_value.left_opt().get()); EXPECT_EQ(expected, const_value.left_opt().get());
EXPECT_EQ(boost::none, const_value.right_opt()); EXPECT_EQ(boost::none, const_value.right_opt());
} }
template<class Left, class Right, class Expected> template<class Left, class Right, class Expected>
void EXPECT_RIGHT_IS(const Expected &expected, Either<Left, Right> &value) { void EXPECT_RIGHT_IS(const Expected &expected, either<Left, Right> &value) {
EXPECT_IS_RIGHT(value); EXPECT_IS_RIGHT(value);
EXPECT_EQ(expected, value.right()); EXPECT_EQ(expected, value.right());
EXPECT_EQ(expected, value.right_opt().get()); EXPECT_EQ(expected, value.right_opt().get());
EXPECT_EQ(boost::none, value.left_opt()); EXPECT_EQ(boost::none, value.left_opt());
const Either<Left, Right> &const_value = value; const either<Left, Right> &const_value = value;
EXPECT_EQ(expected, const_value.right()); EXPECT_EQ(expected, const_value.right());
EXPECT_EQ(expected, const_value.right_opt().get()); EXPECT_EQ(expected, const_value.right_opt().get());
EXPECT_EQ(boost::none, const_value.left_opt()); EXPECT_EQ(boost::none, const_value.left_opt());
@ -71,7 +71,7 @@ public:
template<typename Left, typename Right> template<typename Left, typename Right>
void TestSpaceUsage() { void TestSpaceUsage() {
EXPECT_EQ(std::max(sizeof(StoreWith1ByteFlag<Left>), sizeof(StoreWith1ByteFlag<Right>)), sizeof(Either<Left, Right>)); EXPECT_EQ(std::max(sizeof(StoreWith1ByteFlag<Left>), sizeof(StoreWith1ByteFlag<Right>)), sizeof(either<Left, Right>));
} }
TEST_F(EitherTest, SpaceUsage) { TEST_F(EitherTest, SpaceUsage) {
@ -83,180 +83,180 @@ TEST_F(EitherTest, SpaceUsage) {
} }
TEST_F(EitherTest, LeftCanBeConstructed) { TEST_F(EitherTest, LeftCanBeConstructed) {
Either<int, string> val = 3; either<int, string> val = 3;
UNUSED(val); UNUSED(val);
} }
TEST_F(EitherTest, RightCanBeConstructed) { TEST_F(EitherTest, RightCanBeConstructed) {
Either<int, string> val = string("string"); either<int, string> val = string("string");
UNUSED(val); UNUSED(val);
} }
TEST_F(EitherTest, IsLeft) { TEST_F(EitherTest, IsLeft) {
Either<int, string> val = 3; either<int, string> val = 3;
EXPECT_IS_LEFT(val); EXPECT_IS_LEFT(val);
} }
TEST_F(EitherTest, IsRight) { TEST_F(EitherTest, IsRight) {
Either<int, string> val = string("string"); either<int, string> val = string("string");
EXPECT_IS_RIGHT(val); EXPECT_IS_RIGHT(val);
} }
TEST_F(EitherTest, LeftIsStored) { TEST_F(EitherTest, LeftIsStored) {
Either<int, string> val = 3; either<int, string> val = 3;
EXPECT_LEFT_IS(3, val); EXPECT_LEFT_IS(3, val);
} }
TEST_F(EitherTest, RightIsStored) { TEST_F(EitherTest, RightIsStored) {
Either<int, string> val = string("string"); either<int, string> val = string("string");
EXPECT_RIGHT_IS("string", val); EXPECT_RIGHT_IS("string", val);
} }
TEST_F(EitherTest, LeftCanBeMoveContructed) { TEST_F(EitherTest, LeftCanBeMoveContructed) {
Either<OnlyMoveable, string> val = OnlyMoveable(1); either<OnlyMoveable, string> val = OnlyMoveable(1);
UNUSED(val); UNUSED(val);
} }
TEST_F(EitherTest, RightCanBeMoveContructed) { TEST_F(EitherTest, RightCanBeMoveContructed) {
Either<string, OnlyMoveable> val = OnlyMoveable(1); either<string, OnlyMoveable> val = OnlyMoveable(1);
UNUSED(val); UNUSED(val);
} }
TEST_F(EitherTest, IsLeftWhenMoveContructed) { TEST_F(EitherTest, IsLeftWhenMoveContructed) {
Either<OnlyMoveable, string> val = OnlyMoveable(1); either<OnlyMoveable, string> val = OnlyMoveable(1);
EXPECT_IS_LEFT(val); EXPECT_IS_LEFT(val);
} }
TEST_F(EitherTest, IsRightWhenMoveContructed) { TEST_F(EitherTest, IsRightWhenMoveContructed) {
Either<string, OnlyMoveable> val = OnlyMoveable(1); either<string, OnlyMoveable> val = OnlyMoveable(1);
EXPECT_IS_RIGHT(val); EXPECT_IS_RIGHT(val);
} }
TEST_F(EitherTest, LeftIsStoredWhenMoveContructed) { TEST_F(EitherTest, LeftIsStoredWhenMoveContructed) {
Either<OnlyMoveable, string> val = OnlyMoveable(2); either<OnlyMoveable, string> val = OnlyMoveable(2);
EXPECT_LEFT_IS(OnlyMoveable(2), val); EXPECT_LEFT_IS(OnlyMoveable(2), val);
} }
TEST_F(EitherTest, RightIsStoredWhenMoveContructed) { TEST_F(EitherTest, RightIsStoredWhenMoveContructed) {
Either<string, OnlyMoveable> val = OnlyMoveable(3); either<string, OnlyMoveable> val = OnlyMoveable(3);
EXPECT_RIGHT_IS(OnlyMoveable(3), val); EXPECT_RIGHT_IS(OnlyMoveable(3), val);
} }
TEST_F(EitherTest, LeftCanBeCopied) { TEST_F(EitherTest, LeftCanBeCopied) {
Either<string, int> val = string("string"); either<string, int> val = string("string");
Either<string, int> val2 = val; either<string, int> val2 = val;
EXPECT_LEFT_IS("string", val2); EXPECT_LEFT_IS("string", val2);
} }
TEST_F(EitherTest, CopyingLeftDoesntChangeSource) { TEST_F(EitherTest, CopyingLeftDoesntChangeSource) {
Either<string, int> val = string("string"); either<string, int> val = string("string");
Either<string, int> val2 = val; either<string, int> val2 = val;
EXPECT_LEFT_IS("string", val); EXPECT_LEFT_IS("string", val);
} }
TEST_F(EitherTest, RightCanBeCopied) { TEST_F(EitherTest, RightCanBeCopied) {
Either<int, string> val = string("string"); either<int, string> val = string("string");
Either<int, string> val2 = val; either<int, string> val2 = val;
EXPECT_RIGHT_IS("string", val2); EXPECT_RIGHT_IS("string", val2);
} }
TEST_F(EitherTest, CopyingRightDoesntChangeSource) { TEST_F(EitherTest, CopyingRightDoesntChangeSource) {
Either<int, string> val = string("string"); either<int, string> val = string("string");
Either<int, string> val2 = val; either<int, string> val2 = val;
EXPECT_RIGHT_IS("string", val); EXPECT_RIGHT_IS("string", val);
} }
TEST_F(EitherTest, LeftCanBeMoved) { TEST_F(EitherTest, LeftCanBeMoved) {
Either<OnlyMoveable, int> val = OnlyMoveable(5); either<OnlyMoveable, int> val = OnlyMoveable(5);
Either<OnlyMoveable, int> val2 = std::move(val); either<OnlyMoveable, int> val2 = std::move(val);
EXPECT_LEFT_IS(OnlyMoveable(5), val2); EXPECT_LEFT_IS(OnlyMoveable(5), val2);
} }
TEST_F(EitherTest, RightCanBeMoved) { TEST_F(EitherTest, RightCanBeMoved) {
Either<int, OnlyMoveable> val = OnlyMoveable(5); either<int, OnlyMoveable> val = OnlyMoveable(5);
Either<int, OnlyMoveable> val2 = std::move(val); either<int, OnlyMoveable> val2 = std::move(val);
EXPECT_RIGHT_IS(OnlyMoveable(5), val2); EXPECT_RIGHT_IS(OnlyMoveable(5), val2);
} }
TEST_F(EitherTest, LeftCanBeAssigned) { TEST_F(EitherTest, LeftCanBeAssigned) {
Either<string, int> val = string("string"); either<string, int> val = string("string");
Either<string, int> val2 = string("otherstring"); either<string, int> val2 = string("otherstring");
val2 = val; val2 = val;
EXPECT_LEFT_IS("string", val2); EXPECT_LEFT_IS("string", val2);
} }
TEST_F(EitherTest, RightCanBeAssigned) { TEST_F(EitherTest, RightCanBeAssigned) {
Either<int, string> val = string("string"); either<int, string> val = string("string");
Either<int, string> val2 = string("otherstring"); either<int, string> val2 = string("otherstring");
val2 = val; val2 = val;
EXPECT_RIGHT_IS("string", val2); EXPECT_RIGHT_IS("string", val2);
} }
TEST_F(EitherTest, LeftCanBeMoveAssigned) { TEST_F(EitherTest, LeftCanBeMoveAssigned) {
Either<OnlyMoveable, int> val = OnlyMoveable(3); either<OnlyMoveable, int> val = OnlyMoveable(3);
Either<OnlyMoveable, int> val2 = OnlyMoveable(4); either<OnlyMoveable, int> val2 = OnlyMoveable(4);
val2 = std::move(val); val2 = std::move(val);
EXPECT_LEFT_IS(OnlyMoveable(3), val2); EXPECT_LEFT_IS(OnlyMoveable(3), val2);
} }
TEST_F(EitherTest, RightCanBeMoveAssigned) { TEST_F(EitherTest, RightCanBeMoveAssigned) {
Either<int, OnlyMoveable> val = OnlyMoveable(3); either<int, OnlyMoveable> val = OnlyMoveable(3);
Either<int, OnlyMoveable> val2 = OnlyMoveable(4); either<int, OnlyMoveable> val2 = OnlyMoveable(4);
val2 = std::move(val); val2 = std::move(val);
EXPECT_RIGHT_IS(OnlyMoveable(3), val2); EXPECT_RIGHT_IS(OnlyMoveable(3), val2);
} }
TEST_F(EitherTest, LeftCanBeDirectlyAssigned) { TEST_F(EitherTest, LeftCanBeDirectlyAssigned) {
Either<string, int> val = string("string"); either<string, int> val = string("string");
val = string("otherstring"); val = string("otherstring");
EXPECT_LEFT_IS("otherstring", val); EXPECT_LEFT_IS("otherstring", val);
} }
TEST_F(EitherTest, RightCanBeDirectlyAssigned) { TEST_F(EitherTest, RightCanBeDirectlyAssigned) {
Either<int, string> val = string("string"); either<int, string> val = string("string");
val = string("otherstring"); val = string("otherstring");
EXPECT_RIGHT_IS("otherstring", val); EXPECT_RIGHT_IS("otherstring", val);
} }
TEST_F(EitherTest, LeftCanBeDirectlyMoveAssigned) { TEST_F(EitherTest, LeftCanBeDirectlyMoveAssigned) {
Either<OnlyMoveable, int> val = OnlyMoveable(3); either<OnlyMoveable, int> val = OnlyMoveable(3);
val = OnlyMoveable(5); val = OnlyMoveable(5);
EXPECT_LEFT_IS(OnlyMoveable(5), val); EXPECT_LEFT_IS(OnlyMoveable(5), val);
} }
TEST_F(EitherTest, RightCanBeDirectlyMoveAssigned) { TEST_F(EitherTest, RightCanBeDirectlyMoveAssigned) {
Either<int, OnlyMoveable> val = OnlyMoveable(3); either<int, OnlyMoveable> val = OnlyMoveable(3);
val = OnlyMoveable(5); val = OnlyMoveable(5);
EXPECT_RIGHT_IS(OnlyMoveable(5), val); EXPECT_RIGHT_IS(OnlyMoveable(5), val);
} }
TEST_F(EitherTest, ModifyLeft) { TEST_F(EitherTest, ModifyLeft) {
Either<string, int> val = string("mystring1"); either<string, int> val = string("mystring1");
val.left() = "mystring2"; val.left() = "mystring2";
EXPECT_LEFT_IS("mystring2", val); EXPECT_LEFT_IS("mystring2", val);
} }
TEST_F(EitherTest, ModifyRight) { TEST_F(EitherTest, ModifyRight) {
Either<int, string> val = string("mystring1"); either<int, string> val = string("mystring1");
val.right() = "mystring2"; val.right() = "mystring2";
EXPECT_RIGHT_IS("mystring2", val); EXPECT_RIGHT_IS("mystring2", val);
} }
TEST_F(EitherTest, ModifyLeftOpt) { TEST_F(EitherTest, ModifyLeftOpt) {
Either<string, int> val = string("mystring1"); either<string, int> val = string("mystring1");
val.left_opt().get() = "mystring2"; val.left_opt().get() = "mystring2";
EXPECT_LEFT_IS("mystring2", val); EXPECT_LEFT_IS("mystring2", val);
} }
TEST_F(EitherTest, ModifyRightOpt) { TEST_F(EitherTest, ModifyRightOpt) {
Either<int, string> val = string("mystring1"); either<int, string> val = string("mystring1");
val.right_opt().get() = "mystring2"; val.right_opt().get() = "mystring2";
EXPECT_RIGHT_IS("mystring2", val); EXPECT_RIGHT_IS("mystring2", val);
} }
TEST_F(EitherTest, LeftEquals) { TEST_F(EitherTest, LeftEquals) {
Either<string, int> val1 = string("mystring"); either<string, int> val1 = string("mystring");
Either<string, int> val2 = string("mystring"); either<string, int> val2 = string("mystring");
EXPECT_TRUE(val1 == val2); EXPECT_TRUE(val1 == val2);
EXPECT_TRUE(val2 == val1); EXPECT_TRUE(val2 == val1);
EXPECT_FALSE(val1 != val2); EXPECT_FALSE(val1 != val2);
@ -264,8 +264,8 @@ TEST_F(EitherTest, LeftEquals) {
} }
TEST_F(EitherTest, LeftNotEquals) { TEST_F(EitherTest, LeftNotEquals) {
Either<string, int> val1 = string("mystring"); either<string, int> val1 = string("mystring");
Either<string, int> val2 = string("mystring2"); either<string, int> val2 = string("mystring2");
EXPECT_TRUE(val1 != val2); EXPECT_TRUE(val1 != val2);
EXPECT_TRUE(val2 != val1); EXPECT_TRUE(val2 != val1);
EXPECT_FALSE(val1 == val2); EXPECT_FALSE(val1 == val2);
@ -273,8 +273,8 @@ TEST_F(EitherTest, LeftNotEquals) {
} }
TEST_F(EitherTest, RightEquals) { TEST_F(EitherTest, RightEquals) {
Either<int, string> val1 = string("mystring"); either<int, string> val1 = string("mystring");
Either<int, string> val2 = string("mystring"); either<int, string> val2 = string("mystring");
EXPECT_TRUE(val1 == val2); EXPECT_TRUE(val1 == val2);
EXPECT_TRUE(val2 == val1); EXPECT_TRUE(val2 == val1);
EXPECT_FALSE(val1 != val2); EXPECT_FALSE(val1 != val2);
@ -282,8 +282,8 @@ TEST_F(EitherTest, RightEquals) {
} }
TEST_F(EitherTest, RightNotEquals) { TEST_F(EitherTest, RightNotEquals) {
Either<int, string> val1 = string("mystring"); either<int, string> val1 = string("mystring");
Either<int, string> val2 = string("mystring2"); either<int, string> val2 = string("mystring2");
EXPECT_TRUE(val1 != val2); EXPECT_TRUE(val1 != val2);
EXPECT_TRUE(val2 != val1); EXPECT_TRUE(val2 != val1);
EXPECT_FALSE(val1 == val2); EXPECT_FALSE(val1 == val2);
@ -291,8 +291,8 @@ TEST_F(EitherTest, RightNotEquals) {
} }
TEST_F(EitherTest, LeftNotEqualsRight) { TEST_F(EitherTest, LeftNotEqualsRight) {
Either<string, int> val1 = string("mystring"); either<string, int> val1 = string("mystring");
Either<string, int> val2 = 3; either<string, int> val2 = 3;
EXPECT_TRUE(val1 != val2); EXPECT_TRUE(val1 != val2);
EXPECT_TRUE(val2 != val1); EXPECT_TRUE(val2 != val1);
EXPECT_FALSE(val1 == val2); EXPECT_FALSE(val1 == val2);
@ -312,32 +312,32 @@ TEST_F(EitherTest, OutputRight) {
} }
TEST_F(EitherTest, MakeLeft) { TEST_F(EitherTest, MakeLeft) {
Either<string, int> var = make_left<string, int>("mystring"); either<string, int> var = make_left<string, int>("mystring");
EXPECT_LEFT_IS("mystring", var); EXPECT_LEFT_IS("mystring", var);
} }
TEST_F(EitherTest, MakeLeft_OnlyMoveable) { TEST_F(EitherTest, MakeLeft_OnlyMoveable) {
Either<OnlyMoveable, int> var = make_left<OnlyMoveable, int>(4); either<OnlyMoveable, int> var = make_left<OnlyMoveable, int>(4);
EXPECT_LEFT_IS(OnlyMoveable(4), var); EXPECT_LEFT_IS(OnlyMoveable(4), var);
} }
TEST_F(EitherTest, MakeLeft_MultiParam) { TEST_F(EitherTest, MakeLeft_MultiParam) {
Either<pair<int, int>, int> var = make_left<pair<int, int>, int>(4, 5); either<pair<int, int>, int> var = make_left<pair<int, int>, int>(4, 5);
EXPECT_LEFT_IS(make_pair(4,5), var); EXPECT_LEFT_IS(make_pair(4,5), var);
} }
TEST_F(EitherTest, MakeRight) { TEST_F(EitherTest, MakeRight) {
Either<int, string> var = make_right<int, string>("mystring"); either<int, string> var = make_right<int, string>("mystring");
EXPECT_RIGHT_IS("mystring", var); EXPECT_RIGHT_IS("mystring", var);
} }
TEST_F(EitherTest, MakeRight_OnlyMoveable) { TEST_F(EitherTest, MakeRight_OnlyMoveable) {
Either<int, OnlyMoveable> var = make_right<int, OnlyMoveable>(4); either<int, OnlyMoveable> var = make_right<int, OnlyMoveable>(4);
EXPECT_RIGHT_IS(OnlyMoveable(4), var); EXPECT_RIGHT_IS(OnlyMoveable(4), var);
} }
TEST_F(EitherTest, MakeRight_MultiParam) { TEST_F(EitherTest, MakeRight_MultiParam) {
Either<int, pair<int, int>> var = make_right<int, pair<int, int>>(4, 5); either<int, pair<int, int>> var = make_right<int, pair<int, int>>(4, 5);
EXPECT_RIGHT_IS(make_pair(4,5), var); EXPECT_RIGHT_IS(make_pair(4,5), var);
} }
@ -403,7 +403,7 @@ TEST_F(EitherTest_Destructor, LeftDestructorIsCalled) {
destructorCallback.EXPECT_CALLED(2); //Once for the temp object, once when the either class destructs destructorCallback.EXPECT_CALLED(2); //Once for the temp object, once when the either class destructs
ClassWithDestructorCallback temp(&destructorCallback); ClassWithDestructorCallback temp(&destructorCallback);
Either<ClassWithDestructorCallback, string> var = temp; either<ClassWithDestructorCallback, string> var = temp;
} }
TEST_F(EitherTest_Destructor, RightDestructorIsCalled) { TEST_F(EitherTest_Destructor, RightDestructorIsCalled) {
@ -411,7 +411,7 @@ TEST_F(EitherTest_Destructor, RightDestructorIsCalled) {
destructorCallback.EXPECT_CALLED(2); //Once for the temp object, once when the either class destructs destructorCallback.EXPECT_CALLED(2); //Once for the temp object, once when the either class destructs
ClassWithDestructorCallback temp(&destructorCallback); ClassWithDestructorCallback temp(&destructorCallback);
Either<string, ClassWithDestructorCallback> var = temp; either<string, ClassWithDestructorCallback> var = temp;
} }
TEST_F(EitherTest_Destructor, LeftDestructorIsCalledAfterCopying) { TEST_F(EitherTest_Destructor, LeftDestructorIsCalledAfterCopying) {
@ -419,8 +419,8 @@ TEST_F(EitherTest_Destructor, LeftDestructorIsCalledAfterCopying) {
destructorCallback.EXPECT_CALLED(3); //Once for the temp object, once for var1 and once for var2 destructorCallback.EXPECT_CALLED(3); //Once for the temp object, once for var1 and once for var2
ClassWithDestructorCallback temp(&destructorCallback); ClassWithDestructorCallback temp(&destructorCallback);
Either<ClassWithDestructorCallback, string> var1 = temp; either<ClassWithDestructorCallback, string> var1 = temp;
Either<ClassWithDestructorCallback, string> var2 = var1; either<ClassWithDestructorCallback, string> var2 = var1;
} }
TEST_F(EitherTest_Destructor, RightDestructorIsCalledAfterCopying) { TEST_F(EitherTest_Destructor, RightDestructorIsCalledAfterCopying) {
@ -428,8 +428,8 @@ TEST_F(EitherTest_Destructor, RightDestructorIsCalledAfterCopying) {
destructorCallback.EXPECT_CALLED(3); //Once for the temp object, once for var1 and once for var2 destructorCallback.EXPECT_CALLED(3); //Once for the temp object, once for var1 and once for var2
ClassWithDestructorCallback temp(&destructorCallback); ClassWithDestructorCallback temp(&destructorCallback);
Either<string, ClassWithDestructorCallback> var1 = temp; either<string, ClassWithDestructorCallback> var1 = temp;
Either<string, ClassWithDestructorCallback> var2 = var1; either<string, ClassWithDestructorCallback> var2 = var1;
} }
TEST_F(EitherTest_Destructor, LeftDestructorIsCalledAfterMoving) { TEST_F(EitherTest_Destructor, LeftDestructorIsCalledAfterMoving) {
@ -437,8 +437,8 @@ TEST_F(EitherTest_Destructor, LeftDestructorIsCalledAfterMoving) {
destructorCallback.EXPECT_CALLED(3); //Once for the temp object, once for var1 and once for var2 destructorCallback.EXPECT_CALLED(3); //Once for the temp object, once for var1 and once for var2
OnlyMoveableClassWithDestructorCallback temp(&destructorCallback); OnlyMoveableClassWithDestructorCallback temp(&destructorCallback);
Either<OnlyMoveableClassWithDestructorCallback, string> var1 = std::move(temp); either<OnlyMoveableClassWithDestructorCallback, string> var1 = std::move(temp);
Either<OnlyMoveableClassWithDestructorCallback, string> var2 = std::move(var1); either<OnlyMoveableClassWithDestructorCallback, string> var2 = std::move(var1);
} }
TEST_F(EitherTest_Destructor, RightDestructorIsCalledAfterMoving) { TEST_F(EitherTest_Destructor, RightDestructorIsCalledAfterMoving) {
@ -446,8 +446,8 @@ TEST_F(EitherTest_Destructor, RightDestructorIsCalledAfterMoving) {
destructorCallback.EXPECT_CALLED(3); //Once for the temp object, once for var1 and once for var2 destructorCallback.EXPECT_CALLED(3); //Once for the temp object, once for var1 and once for var2
OnlyMoveableClassWithDestructorCallback temp(&destructorCallback); OnlyMoveableClassWithDestructorCallback temp(&destructorCallback);
Either<string, OnlyMoveableClassWithDestructorCallback> var1 = std::move(temp); either<string, OnlyMoveableClassWithDestructorCallback> var1 = std::move(temp);
Either<string, OnlyMoveableClassWithDestructorCallback> var2 = std::move(var1); either<string, OnlyMoveableClassWithDestructorCallback> var2 = std::move(var1);
} }
TEST_F(EitherTest_Destructor, LeftDestructorIsCalledAfterAssignment) { TEST_F(EitherTest_Destructor, LeftDestructorIsCalledAfterAssignment) {
@ -457,9 +457,9 @@ TEST_F(EitherTest_Destructor, LeftDestructorIsCalledAfterAssignment) {
destructorCallback2.EXPECT_CALLED(3); //Once for the temp2 object, once in destructor of var2, once in destructor of var1 destructorCallback2.EXPECT_CALLED(3); //Once for the temp2 object, once in destructor of var2, once in destructor of var1
ClassWithDestructorCallback temp1(&destructorCallback1); ClassWithDestructorCallback temp1(&destructorCallback1);
Either<ClassWithDestructorCallback, string> var1 = temp1; either<ClassWithDestructorCallback, string> var1 = temp1;
ClassWithDestructorCallback temp2(&destructorCallback2); ClassWithDestructorCallback temp2(&destructorCallback2);
Either<ClassWithDestructorCallback, string> var2 = temp2; either<ClassWithDestructorCallback, string> var2 = temp2;
var1 = var2; var1 = var2;
} }
@ -470,9 +470,9 @@ TEST_F(EitherTest_Destructor, RightDestructorIsCalledAfterAssignment) {
destructorCallback2.EXPECT_CALLED(3); //Once for the temp2 object, once in destructor of var2, once in destructor of var1 destructorCallback2.EXPECT_CALLED(3); //Once for the temp2 object, once in destructor of var2, once in destructor of var1
ClassWithDestructorCallback temp1(&destructorCallback1); ClassWithDestructorCallback temp1(&destructorCallback1);
Either<string, ClassWithDestructorCallback> var1 = temp1; either<string, ClassWithDestructorCallback> var1 = temp1;
ClassWithDestructorCallback temp2(&destructorCallback2); ClassWithDestructorCallback temp2(&destructorCallback2);
Either<string, ClassWithDestructorCallback> var2 = temp2; either<string, ClassWithDestructorCallback> var2 = temp2;
var1 = var2; var1 = var2;
} }
@ -483,9 +483,9 @@ TEST_F(EitherTest_Destructor, LeftDestructorIsCalledAfterMoveAssignment) {
destructorCallback2.EXPECT_CALLED(3); //Once for the temp2 object, once in destructor of var2, once in destructor of var1 destructorCallback2.EXPECT_CALLED(3); //Once for the temp2 object, once in destructor of var2, once in destructor of var1
OnlyMoveableClassWithDestructorCallback temp1(&destructorCallback1); OnlyMoveableClassWithDestructorCallback temp1(&destructorCallback1);
Either<OnlyMoveableClassWithDestructorCallback, string> var1 = std::move(temp1); either<OnlyMoveableClassWithDestructorCallback, string> var1 = std::move(temp1);
OnlyMoveableClassWithDestructorCallback temp2(&destructorCallback2); OnlyMoveableClassWithDestructorCallback temp2(&destructorCallback2);
Either<OnlyMoveableClassWithDestructorCallback, string> var2 = std::move(temp2); either<OnlyMoveableClassWithDestructorCallback, string> var2 = std::move(temp2);
var1 = std::move(var2); var1 = std::move(var2);
} }
@ -496,8 +496,8 @@ TEST_F(EitherTest_Destructor, RightDestructorIsCalledAfterMoveAssignment) {
destructorCallback2.EXPECT_CALLED(3); //Once for the temp2 object, once in destructor of var2, once in destructor of var1 destructorCallback2.EXPECT_CALLED(3); //Once for the temp2 object, once in destructor of var2, once in destructor of var1
OnlyMoveableClassWithDestructorCallback temp1(&destructorCallback1); OnlyMoveableClassWithDestructorCallback temp1(&destructorCallback1);
Either<string, OnlyMoveableClassWithDestructorCallback> var1 = std::move(temp1); either<string, OnlyMoveableClassWithDestructorCallback> var1 = std::move(temp1);
OnlyMoveableClassWithDestructorCallback temp2(&destructorCallback2); OnlyMoveableClassWithDestructorCallback temp2(&destructorCallback2);
Either<string, OnlyMoveableClassWithDestructorCallback> var2 = std::move(temp2); either<string, OnlyMoveableClassWithDestructorCallback> var2 = std::move(temp2);
var1 = std::move(var2); var1 = std::move(var2);
} }