BooleanTest.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-09
  6. * Changed: 2020-11-29
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls_std/ls_std.hpp>
  11. #include <TestHelper.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, load)
  86. {
  87. // preparation
  88. std::shared_ptr<ls_std::Boolean> x = std::make_shared<ls_std::Boolean>();
  89. std::string path = TestHelper::getResourcesFolderLocation() + "tmp_storable_bool.json";
  90. ls_std::File file {path};
  91. file.createNewFile();
  92. ls_std::FileWriter writer {file};
  93. writer.write(R"({"value":true})");
  94. auto serializable = std::make_shared<ls_std::SerializableJSONBoolean>(x);
  95. x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
  96. auto storable = std::make_shared<ls_std::StorableFile>(path);
  97. x->setStorable(std::dynamic_pointer_cast<ls_std::IStorable>(storable));
  98. // check
  99. x->load();
  100. ASSERT_TRUE(*x);
  101. file.remove();
  102. }
  103. TEST_F(BooleanTest, marshal)
  104. {
  105. std::shared_ptr<ls_std::Boolean> x = std::make_shared<ls_std::Boolean>(true);
  106. auto serializable = std::make_shared<ls_std::SerializableJSONBoolean>(x);
  107. x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
  108. ASSERT_STREQ(R"({"value":true})", x->marshal().c_str());
  109. *x = false;
  110. ASSERT_STREQ(R"({"value":false})", x->marshal().c_str());
  111. }
  112. TEST_F(BooleanTest, parse)
  113. {
  114. ls_std::Boolean expression {};
  115. expression.parse("true");
  116. ASSERT_TRUE(expression);
  117. expression.parse("tRue");
  118. ASSERT_TRUE(expression);
  119. expression.parse("TRUE");
  120. ASSERT_TRUE(expression);
  121. }
  122. TEST_F(BooleanTest, parseNegative)
  123. {
  124. ls_std::Boolean expression {};
  125. expression.parse("false");
  126. ASSERT_FALSE(expression);
  127. expression.parse("fAlSe");
  128. ASSERT_FALSE(expression);
  129. expression.parse("FALSE");
  130. ASSERT_FALSE(expression);
  131. }
  132. TEST_F(BooleanTest, toString)
  133. {
  134. ls_std::Boolean expression { 2 < 3};
  135. ASSERT_STREQ("true", expression.toString().c_str());
  136. expression = 4 < 3;
  137. ASSERT_STREQ("false", expression.toString().c_str());
  138. }
  139. TEST_F(BooleanTest, unmarshal)
  140. {
  141. std::shared_ptr<ls_std::Boolean> x = std::make_shared<ls_std::Boolean>(false);
  142. ASSERT_FALSE(*x);
  143. auto serializable = std::make_shared<ls_std::SerializableJSONBoolean>(x);
  144. x->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
  145. x->unmarshal(R"({"value":true})");
  146. ASSERT_TRUE(*x);
  147. }
  148. // additional functionality
  149. TEST_F(BooleanTest, getValue)
  150. {
  151. ls_std::Boolean x {2 < 3};
  152. ASSERT_TRUE(x.getValue());
  153. }
  154. TEST_F(BooleanTest, XOR)
  155. {
  156. ls_std::Boolean x {2 < 3};
  157. ls_std::Boolean y {4 < 3};
  158. ASSERT_TRUE(ls_std::Boolean::XOR(x, y));
  159. ASSERT_TRUE(ls_std::Boolean::XOR(y, x));
  160. ASSERT_TRUE(ls_std::Boolean::XOR(y, true));
  161. ASSERT_TRUE(ls_std::Boolean::XOR(true, y));
  162. ASSERT_TRUE(ls_std::Boolean::XOR(true, false));
  163. ASSERT_TRUE(ls_std::Boolean::XOR(false, true));
  164. }
  165. TEST_F(BooleanTest, XORNegative)
  166. {
  167. ls_std::Boolean w {};
  168. ls_std::Boolean x {true};
  169. ls_std::Boolean y {};
  170. ls_std::Boolean z {true};
  171. ASSERT_FALSE(ls_std::Boolean::XOR(x, z));
  172. ASSERT_FALSE(ls_std::Boolean::XOR(w, y));
  173. ASSERT_FALSE(ls_std::Boolean::XOR(true, true));
  174. ASSERT_FALSE(ls_std::Boolean::XOR(false, false));
  175. ASSERT_FALSE(ls_std::Boolean::XOR(w, false));
  176. ASSERT_FALSE(ls_std::Boolean::XOR(false, w));
  177. }
  178. }