BooleanTest.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-09
  6. * Changed: 2020-09-04
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include "../../../source/boxing/Boolean.hpp"
  11. #include "../../../source/serialization/boxing/SerializableJSONBoolean.hpp"
  12. namespace {
  13. class BooleanTest : public ::testing::Test {
  14. protected:
  15. BooleanTest() = default;
  16. ~BooleanTest() override = default;
  17. void SetUp() override
  18. {}
  19. void TearDown() override
  20. {}
  21. };
  22. // assignment operators
  23. TEST_F(BooleanTest, operatorAssignment)
  24. {
  25. ls_std::Boolean expression {};
  26. expression = true;
  27. ASSERT_TRUE(expression);
  28. expression = 1;
  29. ASSERT_TRUE(expression);
  30. }
  31. // stream operators
  32. TEST_F(BooleanTest, operatorOutputStream)
  33. {
  34. ls_std::Boolean expression {true};
  35. std::ostringstream _stream {};
  36. _stream << expression;
  37. ASSERT_STREQ("true", _stream.str().c_str());
  38. }
  39. // logical operators
  40. TEST_F(BooleanTest, operatorNot) {
  41. ls_std::Boolean expression {};
  42. ASSERT_TRUE(!expression);
  43. }
  44. TEST_F(BooleanTest, operatorNotNegative) {
  45. ls_std::Boolean expression {true};
  46. ASSERT_FALSE(!expression);
  47. }
  48. TEST_F(BooleanTest, operatorAnd) {
  49. ls_std::Boolean expressionA {true};
  50. ls_std::Boolean expressionB {3 == 3};
  51. ASSERT_TRUE(expressionA && expressionB);
  52. ASSERT_TRUE(expressionB && expressionA);
  53. ASSERT_TRUE(expressionA && true);
  54. ASSERT_TRUE(expressionA && 1);
  55. ASSERT_TRUE(1 && expressionB);
  56. }
  57. TEST_F(BooleanTest, operatorAndNegative) {
  58. ls_std::Boolean expressionA {true};
  59. ls_std::Boolean expressionB {3 == 4};
  60. ASSERT_FALSE(expressionA && expressionB);
  61. ASSERT_FALSE(expressionB && expressionA);
  62. ASSERT_FALSE(expressionB && (3 == 4));
  63. ASSERT_FALSE(expressionB && 0);
  64. ASSERT_FALSE(expressionB && false);
  65. }
  66. TEST_F(BooleanTest, operatorOr) {
  67. ls_std::Boolean expressionA {false};
  68. ls_std::Boolean expressionB {3 == 3};
  69. ASSERT_TRUE(expressionA || expressionB);
  70. ASSERT_TRUE(expressionB || expressionA);
  71. ASSERT_TRUE(expressionA || 1);
  72. ASSERT_TRUE(expressionA || true);
  73. ASSERT_TRUE(1 || expressionA);
  74. }
  75. TEST_F(BooleanTest, operatorOrNegative) {
  76. ls_std::Boolean expressionA {false};
  77. ls_std::Boolean expressionB {3 == 4};
  78. ASSERT_FALSE(expressionA || expressionB);
  79. ASSERT_FALSE(expressionB || expressionA);
  80. ASSERT_FALSE(expressionA || 0);
  81. ASSERT_FALSE(expressionA || false);
  82. ASSERT_FALSE(0 || expressionA);
  83. }
  84. // implementation
  85. TEST_F(BooleanTest, marshal)
  86. {
  87. std::shared_ptr<ls_std::Boolean> x = std::make_shared<ls_std::Boolean>(true);
  88. auto serializable = std::make_shared<ls_std::SerializableJSONBoolean>(x);
  89. x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
  90. ASSERT_STREQ(R"({"value":true})", x->marshal().c_str());
  91. *x = false;
  92. ASSERT_STREQ(R"({"value":false})", x->marshal().c_str());
  93. }
  94. TEST_F(BooleanTest, parse)
  95. {
  96. ls_std::Boolean expression {};
  97. expression.parse("true");
  98. ASSERT_TRUE(expression);
  99. expression.parse("tRue");
  100. ASSERT_TRUE(expression);
  101. expression.parse("TRUE");
  102. ASSERT_TRUE(expression);
  103. }
  104. TEST_F(BooleanTest, parseNegative)
  105. {
  106. ls_std::Boolean expression {};
  107. expression.parse("false");
  108. ASSERT_FALSE(expression);
  109. expression.parse("fAlSe");
  110. ASSERT_FALSE(expression);
  111. expression.parse("FALSE");
  112. ASSERT_FALSE(expression);
  113. }
  114. TEST_F(BooleanTest, toString)
  115. {
  116. ls_std::Boolean expression { 2 < 3};
  117. ASSERT_STREQ("true", expression.toString().c_str());
  118. expression = 4 < 3;
  119. ASSERT_STREQ("false", expression.toString().c_str());
  120. }
  121. TEST_F(BooleanTest, unmarshal)
  122. {
  123. std::shared_ptr<ls_std::Boolean> x = std::make_shared<ls_std::Boolean>(false);
  124. ASSERT_FALSE(*x);
  125. auto serializable = std::make_shared<ls_std::SerializableJSONBoolean>(x);
  126. x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
  127. x->unmarshal(R"({"value":true})");
  128. ASSERT_TRUE(*x);
  129. }
  130. // additional functionality
  131. TEST_F(BooleanTest, getValue)
  132. {
  133. ls_std::Boolean x {2 < 3};
  134. ASSERT_TRUE(x.getValue());
  135. }
  136. TEST_F(BooleanTest, XOR)
  137. {
  138. ls_std::Boolean x {2 < 3};
  139. ls_std::Boolean y {4 < 3};
  140. ASSERT_TRUE(ls_std::Boolean::XOR(x, y));
  141. ASSERT_TRUE(ls_std::Boolean::XOR(y, x));
  142. ASSERT_TRUE(ls_std::Boolean::XOR(y, true));
  143. ASSERT_TRUE(ls_std::Boolean::XOR(true, y));
  144. ASSERT_TRUE(ls_std::Boolean::XOR(true, false));
  145. ASSERT_TRUE(ls_std::Boolean::XOR(false, true));
  146. }
  147. TEST_F(BooleanTest, XORNegative)
  148. {
  149. ls_std::Boolean w {};
  150. ls_std::Boolean x {true};
  151. ls_std::Boolean y {};
  152. ls_std::Boolean z {true};
  153. ASSERT_FALSE(ls_std::Boolean::XOR(x, z));
  154. ASSERT_FALSE(ls_std::Boolean::XOR(w, y));
  155. ASSERT_FALSE(ls_std::Boolean::XOR(true, true));
  156. ASSERT_FALSE(ls_std::Boolean::XOR(false, false));
  157. ASSERT_FALSE(ls_std::Boolean::XOR(w, false));
  158. ASSERT_FALSE(ls_std::Boolean::XOR(false, w));
  159. }
  160. }