BooleanTest.cpp 5.7 KB

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