123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429 |
- /*
- * Author: Patrick-Christopher Mattulat
- * Company: Lynar Studios
- * E-Mail: webmaster@lynarstudios.com
- * Created: 2020-08-09
- * Changed: 2020-08-25
- *
- * */
- #include <gtest/gtest.h>
- #include "../../../source/boxing/Integer.hpp"
- #include "../../../source/serialization/boxing/SerializableJSONInteger.hpp"
- #include "../../../source/io/StorableFile.hpp"
- #include "../../TestHelper.hpp"
- #include "../../../source/io/FileWriter.hpp"
- namespace {
- class IntegerTest : public ::testing::Test {
- protected:
- IntegerTest() = default;
- ~IntegerTest() override = default;
- void SetUp() override
- {}
- void TearDown() override
- {}
- };
- // assignment operators
- TEST_F(IntegerTest, operatorAssignment)
- {
- ls_std::Integer x {13};
- ASSERT_EQ(13, x);
- x = 44;
- ASSERT_EQ(44, x);
- ls_std::Integer y {3};
- x = y;
- ASSERT_EQ(3, x);
- }
- // arithmetic operators
- TEST_F(IntegerTest, operatorHyphen)
- {
- ls_std::Integer x {13};
- ls_std::Integer y {-13};
- ASSERT_EQ(-13, -x);
- ASSERT_EQ(13, -y);
- }
- TEST_F(IntegerTest, operatorAddition)
- {
- ls_std::Integer x {13};
- ls_std::Integer y {7};
- ls_std::Integer z {-15};
- ASSERT_EQ(20, x + y);
- ASSERT_EQ(8, 1 + y);
- ASSERT_EQ(5, x + y + z);
- ASSERT_EQ(15, x + 2);
- ASSERT_EQ(0, x + 2 + z);
- }
- TEST_F(IntegerTest, operatorMultiplication)
- {
- ls_std::Integer x {3};
- ls_std::Integer y {7};
- ls_std::Integer z {5};
- ASSERT_EQ(21, x * y);
- ASSERT_EQ(105, x * y * z);
- ASSERT_EQ(6, x * 2);
- ASSERT_EQ(71, 1 + y * 2 * z);
- }
- TEST_F(IntegerTest, operatorSubtraction)
- {
- ls_std::Integer x {51};
- ls_std::Integer y {17};
- ASSERT_EQ(34, x - y);
- ASSERT_EQ(30, x - 21);
- ASSERT_EQ(13, x - 21 - y);
- }
- TEST_F(IntegerTest, operatorDivision)
- {
- ls_std::Integer x {81};
- ls_std::Integer y {9};
- ASSERT_EQ(9, x / y);
- ASSERT_EQ(3, 9 / 3);
- ASSERT_EQ(3, x / y / 3);
- ASSERT_EQ(1, 19 / 10);
- }
- TEST_F(IntegerTest, operatorModulus)
- {
- ls_std::Integer x {85};
- ls_std::Integer y {9};
- ASSERT_EQ(4, x % y);
- ASSERT_EQ(1, x % 3);
- }
- // compound operators
- TEST_F(IntegerTest, operatorAddEqual)
- {
- ls_std::Integer x {4};
- ls_std::Integer y {2};
- ASSERT_EQ(4, x);
- x += y;
- ASSERT_EQ(6, x);
- x += 4;
- ASSERT_EQ(10, x);
- x += -5;
- ASSERT_EQ(5, x);
- }
- TEST_F(IntegerTest, operatorSubEqual)
- {
- ls_std::Integer x {14};
- ls_std::Integer y {2};
- ASSERT_EQ(14, x);
- x -= y;
- ASSERT_EQ(12, x);
- x -= 6;
- ASSERT_EQ(6, x);
- x -= -3;
- ASSERT_EQ(9, x);
- }
- TEST_F(IntegerTest, operatorMulEqual)
- {
- ls_std::Integer x {6};
- ls_std::Integer y {3};
- ASSERT_EQ(6, x);
- x *= y;
- ASSERT_EQ(18, x);
- x *= 2;
- ASSERT_EQ(36, x);
- }
- TEST_F(IntegerTest, operatorDivEqual)
- {
- ls_std::Integer x {12};
- ls_std::Integer y {3};
- ASSERT_EQ(12, x);
- x /= y;
- ASSERT_EQ(4, x);
- x /= 2;
- ASSERT_EQ(2, x);
- }
- // comparison operators
- TEST_F(IntegerTest, operatorEqual)
- {
- ls_std::Integer x {12};
- ls_std::Integer y {3};
- ls_std::Integer z {3};
- ASSERT_TRUE(x == 12);
- ASSERT_TRUE(y == z);
- ASSERT_TRUE(z == y);
- }
- TEST_F(IntegerTest, operatorNotEqual)
- {
- ls_std::Integer x {12};
- ls_std::Integer y {3};
- ls_std::Integer z {3};
- ASSERT_TRUE(x != 14);
- ASSERT_TRUE(y != x);
- ASSERT_TRUE(z != x);
- }
- TEST_F(IntegerTest, operatorGreaterThan)
- {
- ls_std::Integer x {12};
- ls_std::Integer y {3};
- ASSERT_TRUE(x > 4);
- ASSERT_TRUE(x > y);
- }
- TEST_F(IntegerTest, operatorGreaterThanNegative)
- {
- ls_std::Integer x {12};
- ls_std::Integer y {3};
- ASSERT_FALSE(x > 14);
- ASSERT_FALSE(x > (y + 20));
- }
- TEST_F(IntegerTest, operatorGreaterThanEqual)
- {
- ls_std::Integer x {12};
- ls_std::Integer y {12};
- ASSERT_TRUE(x >= 12);
- ASSERT_TRUE(x >= y);
- }
- TEST_F(IntegerTest, operatorGreaterThanEqualNegative)
- {
- ls_std::Integer x {12};
- ls_std::Integer y {13};
- ASSERT_FALSE(x >= 13);
- ASSERT_FALSE(x >= y);
- }
- TEST_F(IntegerTest, operatorLessThan)
- {
- ls_std::Integer x {10};
- ls_std::Integer y {12};
- ASSERT_TRUE(x < 12);
- ASSERT_TRUE(x < y);
- }
- TEST_F(IntegerTest, operatorLessThanNegative)
- {
- ls_std::Integer x {10};
- ls_std::Integer y {10};
- ASSERT_FALSE(x < 10);
- ASSERT_FALSE(x < y);
- }
- TEST_F(IntegerTest, operatorLessThanEqual)
- {
- ls_std::Integer x {10};
- ls_std::Integer y {10};
- ASSERT_TRUE(x <= 11);
- ASSERT_TRUE(x <= y);
- }
- TEST_F(IntegerTest, operatorLessThanEqualNegative)
- {
- ls_std::Integer x {11};
- ls_std::Integer y {10};
- ASSERT_FALSE(x <= 10);
- ASSERT_FALSE(x <= y);
- }
- // logical operators
- TEST_F(IntegerTest, operatorNot)
- {
- ls_std::Integer x {};
- ASSERT_TRUE(!x);
- }
- TEST_F(IntegerTest, operatorNotNegative)
- {
- ls_std::Integer x {10};
- ASSERT_FALSE(!x);
- }
- TEST_F(IntegerTest, operatorAnd)
- {
- ls_std::Integer x {1};
- ls_std::Integer y {1};
- ASSERT_TRUE(x && 1);
- ASSERT_TRUE(x && true);
- ASSERT_TRUE(x && y);
- }
- TEST_F(IntegerTest, operatorAndNegative)
- {
- ls_std::Integer x {};
- ls_std::Integer y {1};
- ASSERT_FALSE(x && 1);
- ASSERT_FALSE(x && true);
- ASSERT_FALSE(x && y);
- }
- TEST_F(IntegerTest, operatorOr)
- {
- ls_std::Integer x {};
- ls_std::Integer y {1};
- ASSERT_TRUE(x || 1);
- ASSERT_TRUE(x || true);
- ASSERT_TRUE(x || y);
- }
- TEST_F(IntegerTest, operatorOrNegative)
- {
- ls_std::Integer x {};
- ls_std::Integer y {};
- ASSERT_FALSE(x || 0);
- ASSERT_FALSE(x || false);
- ASSERT_FALSE(x || y);
- }
- // increment / decrement operator
- TEST_F(IntegerTest, operatorIncrement)
- {
- ls_std::Integer x {};
- ++x;
- ASSERT_EQ(1, x);
- ++x;
- ASSERT_EQ(2, x);
- }
- TEST_F(IntegerTest, operatorDecrement)
- {
- ls_std::Integer x {};
- --x;
- ASSERT_EQ(-1, x);
- --x;
- ASSERT_EQ(-2, x);
- }
- // implementation
- TEST_F(IntegerTest, load)
- {
- // preparation
- std::shared_ptr<ls_std::Integer> x = std::make_shared<ls_std::Integer>();
- std::string path = TestHelper::getResourcesFolderLocation() + "tmp_storable_integer.json";
- ls_std::File file {path};
- file.createNewFile();
- ls_std::FileWriter writer {file};
- writer.write(R"({"class":"Integer","value":1990})");
- auto serializable = std::make_shared<ls_std::SerializableJSONInteger>(x);
- x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
- auto storable = std::make_shared<ls_std::StorableFile>(path);
- x->setStorable(std::dynamic_pointer_cast<ls_std::IStorable>(storable));
- // check
- x->load();
- ASSERT_EQ(1990, *x);
- file.remove();
- }
- TEST_F(IntegerTest, marshal)
- {
- std::shared_ptr<ls_std::Integer> x = std::make_shared<ls_std::Integer>(3);
- auto serializable = std::make_shared<ls_std::SerializableJSONInteger>(x);
- x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
- ASSERT_STREQ(R"({"class":"Integer","value":3})", x->marshal().c_str());
- *x = 17;
- ASSERT_STREQ(R"({"class":"Integer","value":17})", x->marshal().c_str());
- }
- TEST_F(IntegerTest, parse)
- {
- ls_std::Integer x {};
- x.parse("1989");
- ASSERT_EQ(1989, x);
- x.parse("-17");
- ASSERT_EQ(-17, x);
- }
- TEST_F(IntegerTest, toString)
- {
- ls_std::Integer x {112};
- ASSERT_STREQ("112", x.toString().c_str());
- }
- TEST_F(IntegerTest, unmarshal)
- {
- std::shared_ptr<ls_std::Integer> x = std::make_shared<ls_std::Integer>(13);
- ASSERT_EQ(13, *x);
- auto serializable = std::make_shared<ls_std::SerializableJSONInteger>(x);
- x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
- x->unmarshal(R"({"class":"Integer","value":1989})");
- ASSERT_EQ(1989, *x);
- }
- // additional functionality
- TEST_F(IntegerTest, getValue)
- {
- ls_std::Integer x {3};
- ASSERT_EQ(3, x.getValue());
- }
- // additional testing
- TEST_F(IntegerTest, constApproach)
- {
- const ls_std::Integer x {3};
- ASSERT_EQ(3, x);
- // x = 4; // wouldn't work
- }
- }
|