BooleanTest.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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-14
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include "../../../source/boxing/Boolean.hpp"
  11. #include "../../../source/serialization/json/boxing/SerializableJSONBoolean.hpp"
  12. #include "../../TestHelper.hpp"
  13. #include "../../../source/io/File.hpp"
  14. #include "../../../source/io/FileWriter.hpp"
  15. #include "../../../source/io/StorableFile.hpp"
  16. namespace {
  17. class BooleanTest : public ::testing::Test {
  18. protected:
  19. BooleanTest() = default;
  20. ~BooleanTest() override = default;
  21. void SetUp() override
  22. {}
  23. void TearDown() override
  24. {}
  25. };
  26. // assignment operators
  27. TEST_F(BooleanTest, operatorAssignment)
  28. {
  29. ls_std::Boolean expression {};
  30. expression = true;
  31. ASSERT_TRUE(expression);
  32. expression = 1;
  33. ASSERT_TRUE(expression);
  34. }
  35. // stream operators
  36. TEST_F(BooleanTest, operatorOutputStream)
  37. {
  38. ls_std::Boolean expression {true};
  39. std::ostringstream _stream {};
  40. _stream << expression;
  41. ASSERT_STREQ("true", _stream.str().c_str());
  42. }
  43. // logical operators
  44. TEST_F(BooleanTest, operatorNot) {
  45. ls_std::Boolean expression {};
  46. ASSERT_TRUE(!expression);
  47. }
  48. TEST_F(BooleanTest, operatorNotNegative) {
  49. ls_std::Boolean expression {true};
  50. ASSERT_FALSE(!expression);
  51. }
  52. TEST_F(BooleanTest, operatorAnd) {
  53. ls_std::Boolean expressionA {true};
  54. ls_std::Boolean expressionB {3 == 3};
  55. ASSERT_TRUE(expressionA && expressionB);
  56. ASSERT_TRUE(expressionB && expressionA);
  57. ASSERT_TRUE(expressionA && true);
  58. ASSERT_TRUE(expressionA && 1);
  59. ASSERT_TRUE(1 && expressionB);
  60. }
  61. TEST_F(BooleanTest, operatorAndNegative) {
  62. ls_std::Boolean expressionA {true};
  63. ls_std::Boolean expressionB {3 == 4};
  64. ASSERT_FALSE(expressionA && expressionB);
  65. ASSERT_FALSE(expressionB && expressionA);
  66. ASSERT_FALSE(expressionB && (3 == 4));
  67. ASSERT_FALSE(expressionB && 0);
  68. ASSERT_FALSE(expressionB && false);
  69. }
  70. TEST_F(BooleanTest, operatorOr) {
  71. ls_std::Boolean expressionA {false};
  72. ls_std::Boolean expressionB {3 == 3};
  73. ASSERT_TRUE(expressionA || expressionB);
  74. ASSERT_TRUE(expressionB || expressionA);
  75. ASSERT_TRUE(expressionA || 1);
  76. ASSERT_TRUE(expressionA || true);
  77. ASSERT_TRUE(1 || expressionA);
  78. }
  79. TEST_F(BooleanTest, operatorOrNegative) {
  80. ls_std::Boolean expressionA {false};
  81. ls_std::Boolean expressionB {3 == 4};
  82. ASSERT_FALSE(expressionA || expressionB);
  83. ASSERT_FALSE(expressionB || expressionA);
  84. ASSERT_FALSE(expressionA || 0);
  85. ASSERT_FALSE(expressionA || false);
  86. ASSERT_FALSE(0 || expressionA);
  87. }
  88. // implementation
  89. TEST_F(BooleanTest, load)
  90. {
  91. // preparation
  92. std::shared_ptr<ls_std::Boolean> x = std::make_shared<ls_std::Boolean>();
  93. std::string path = TestHelper::getResourcesFolderLocation() + "tmp_storable_bool.json";
  94. ls_std::File file {path};
  95. file.createNewFile();
  96. ls_std::FileWriter writer {file};
  97. writer.write(R"({"value":true})");
  98. auto serializable = std::make_shared<ls_std::SerializableJSONBoolean>(x);
  99. x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
  100. auto storable = std::make_shared<ls_std::StorableFile>(path);
  101. x->setStorable(std::dynamic_pointer_cast<ls_std::IStorable>(storable));
  102. // check
  103. x->load();
  104. ASSERT_TRUE(*x);
  105. file.remove();
  106. }
  107. TEST_F(BooleanTest, marshal)
  108. {
  109. std::shared_ptr<ls_std::Boolean> x = std::make_shared<ls_std::Boolean>(true);
  110. auto serializable = std::make_shared<ls_std::SerializableJSONBoolean>(x);
  111. x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
  112. ASSERT_STREQ(R"({"value":true})", x->marshal().c_str());
  113. *x = false;
  114. ASSERT_STREQ(R"({"value":false})", x->marshal().c_str());
  115. }
  116. TEST_F(BooleanTest, parse)
  117. {
  118. ls_std::Boolean expression {};
  119. expression.parse("true");
  120. ASSERT_TRUE(expression);
  121. expression.parse("tRue");
  122. ASSERT_TRUE(expression);
  123. expression.parse("TRUE");
  124. ASSERT_TRUE(expression);
  125. }
  126. TEST_F(BooleanTest, parseNegative)
  127. {
  128. ls_std::Boolean expression {};
  129. expression.parse("false");
  130. ASSERT_FALSE(expression);
  131. expression.parse("fAlSe");
  132. ASSERT_FALSE(expression);
  133. expression.parse("FALSE");
  134. ASSERT_FALSE(expression);
  135. }
  136. TEST_F(BooleanTest, toString)
  137. {
  138. ls_std::Boolean expression { 2 < 3};
  139. ASSERT_STREQ("true", expression.toString().c_str());
  140. expression = 4 < 3;
  141. ASSERT_STREQ("false", expression.toString().c_str());
  142. }
  143. TEST_F(BooleanTest, unmarshal)
  144. {
  145. std::shared_ptr<ls_std::Boolean> x = std::make_shared<ls_std::Boolean>(false);
  146. ASSERT_FALSE(*x);
  147. auto serializable = std::make_shared<ls_std::SerializableJSONBoolean>(x);
  148. x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
  149. x->unmarshal(R"({"value":true})");
  150. ASSERT_TRUE(*x);
  151. }
  152. // additional functionality
  153. TEST_F(BooleanTest, getValue)
  154. {
  155. ls_std::Boolean x {2 < 3};
  156. ASSERT_TRUE(x.getValue());
  157. }
  158. TEST_F(BooleanTest, XOR)
  159. {
  160. ls_std::Boolean x {2 < 3};
  161. ls_std::Boolean y {4 < 3};
  162. ASSERT_TRUE(ls_std::Boolean::XOR(x, y));
  163. ASSERT_TRUE(ls_std::Boolean::XOR(y, x));
  164. ASSERT_TRUE(ls_std::Boolean::XOR(y, true));
  165. ASSERT_TRUE(ls_std::Boolean::XOR(true, y));
  166. ASSERT_TRUE(ls_std::Boolean::XOR(true, false));
  167. ASSERT_TRUE(ls_std::Boolean::XOR(false, true));
  168. }
  169. TEST_F(BooleanTest, XORNegative)
  170. {
  171. ls_std::Boolean w {};
  172. ls_std::Boolean x {true};
  173. ls_std::Boolean y {};
  174. ls_std::Boolean z {true};
  175. ASSERT_FALSE(ls_std::Boolean::XOR(x, z));
  176. ASSERT_FALSE(ls_std::Boolean::XOR(w, y));
  177. ASSERT_FALSE(ls_std::Boolean::XOR(true, true));
  178. ASSERT_FALSE(ls_std::Boolean::XOR(false, false));
  179. ASSERT_FALSE(ls_std::Boolean::XOR(w, false));
  180. ASSERT_FALSE(ls_std::Boolean::XOR(false, w));
  181. }
  182. }