BooleanTest.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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-30
  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. static std::pair<std::shared_ptr<ls_std::File>, std::shared_ptr<ls_std::Boolean>> createTestExpression()
  24. {
  25. std::shared_ptr<ls_std::Boolean> expression = std::make_shared<ls_std::Boolean>();
  26. std::string path = TestHelper::getResourcesFolderLocation() + "tmp_storable_bool.json";
  27. std::shared_ptr<ls_std::File> file = std::make_shared<ls_std::File>(path);
  28. file->createNewFile();
  29. ls_std::FileWriter writer{*file};
  30. writer.write(R"({"value":true})");
  31. auto serializable = std::make_shared<ls_std::SerializableJsonBoolean>(expression);
  32. expression->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
  33. auto storable = std::make_shared<ls_std::StorableFile>(path);
  34. expression->setStorable(std::dynamic_pointer_cast<ls_std::IStorable>(storable));
  35. return std::pair<std::shared_ptr<ls_std::File>, std::shared_ptr<ls_std::Boolean>>(file, expression);
  36. }
  37. };
  38. // assignment operators
  39. TEST_F(BooleanTest, operator_assignment_boolean)
  40. {
  41. ls_std::Boolean expression{};
  42. expression = true;
  43. ASSERT_TRUE(expression);
  44. }
  45. TEST_F(BooleanTest, operator_assignment_integer)
  46. {
  47. ls_std::Boolean expression{};
  48. expression = 1;
  49. ASSERT_TRUE(expression);
  50. }
  51. // stream operators
  52. TEST_F(BooleanTest, operator_output_stream)
  53. {
  54. ls_std::Boolean expression{true};
  55. std::ostringstream _stream{};
  56. _stream << expression;
  57. ASSERT_STREQ("true", _stream.str().c_str());
  58. }
  59. // logical operators
  60. TEST_F(BooleanTest, operator_negation_negative_value)
  61. {
  62. ls_std::Boolean expression{};
  63. ASSERT_TRUE(!expression);
  64. }
  65. TEST_F(BooleanTest, operator_negation_positive_value)
  66. {
  67. ls_std::Boolean expression{true};
  68. ASSERT_FALSE(!expression);
  69. }
  70. TEST_F(BooleanTest, operator_and)
  71. {
  72. ls_std::Boolean expressionA{true};
  73. ls_std::Boolean expressionB{3 == 3};
  74. ASSERT_TRUE(expressionA && expressionB);
  75. ASSERT_TRUE(expressionB && expressionA);
  76. ASSERT_TRUE(expressionA && true);
  77. ASSERT_TRUE(expressionA && 1);
  78. ASSERT_TRUE(1 && expressionB);
  79. }
  80. TEST_F(BooleanTest, operator_and_with_false_result)
  81. {
  82. ls_std::Boolean expressionA{true};
  83. ls_std::Boolean expressionB{3 == 4};
  84. ASSERT_FALSE(expressionA && expressionB);
  85. ASSERT_FALSE(expressionB && expressionA);
  86. ASSERT_FALSE(expressionB && (3 == 4));
  87. ASSERT_FALSE(expressionB && 0);
  88. ASSERT_FALSE(expressionB && false);
  89. }
  90. TEST_F(BooleanTest, operator_or)
  91. {
  92. ls_std::Boolean expressionA{false};
  93. ls_std::Boolean expressionB{3 == 3};
  94. ASSERT_TRUE(expressionA || expressionB);
  95. ASSERT_TRUE(expressionB || expressionA);
  96. ASSERT_TRUE(expressionA || 1);
  97. ASSERT_TRUE(expressionA || true);
  98. ASSERT_TRUE(1 || expressionA);
  99. }
  100. TEST_F(BooleanTest, operator_or_with_false_result)
  101. {
  102. ls_std::Boolean expressionA{false};
  103. ls_std::Boolean expressionB{3 == 4};
  104. ASSERT_FALSE(expressionA || expressionB);
  105. ASSERT_FALSE(expressionB || expressionA);
  106. ASSERT_FALSE(expressionA || 0);
  107. ASSERT_FALSE(expressionA || false);
  108. ASSERT_FALSE(0 || expressionA);
  109. }
  110. // implementation
  111. TEST_F(BooleanTest, load)
  112. {
  113. // preparation
  114. auto storableExpression = createTestExpression();
  115. // check
  116. storableExpression.second->load();
  117. ASSERT_TRUE(*storableExpression.second);
  118. storableExpression.first->remove();
  119. }
  120. TEST_F(BooleanTest, marshal_positive_value)
  121. {
  122. std::shared_ptr<ls_std::Boolean> expression = std::make_shared<ls_std::Boolean>(true);
  123. auto serializable = std::make_shared<ls_std::SerializableJsonBoolean>(expression);
  124. expression->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
  125. ASSERT_STREQ(R"({"value":true})", expression->marshal().c_str());
  126. }
  127. TEST_F(BooleanTest, marshal_negative_value)
  128. {
  129. std::shared_ptr<ls_std::Boolean> expression = std::make_shared<ls_std::Boolean>(false);
  130. auto serializable = std::make_shared<ls_std::SerializableJsonBoolean>(expression);
  131. expression->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
  132. ASSERT_STREQ(R"({"value":false})", expression->marshal().c_str());
  133. }
  134. TEST_F(BooleanTest, parse_true_value)
  135. {
  136. ls_std::Boolean expression{};
  137. expression.parse("true");
  138. ASSERT_TRUE(expression);
  139. expression.parse("tRue");
  140. ASSERT_TRUE(expression);
  141. expression.parse("TRUE");
  142. ASSERT_TRUE(expression);
  143. }
  144. TEST_F(BooleanTest, parse_false_value)
  145. {
  146. ls_std::Boolean expression{};
  147. expression.parse("false");
  148. ASSERT_FALSE(expression);
  149. expression.parse("fAlSe");
  150. ASSERT_FALSE(expression);
  151. expression.parse("FALSE");
  152. ASSERT_FALSE(expression);
  153. }
  154. TEST_F(BooleanTest, toString_true)
  155. {
  156. ls_std::Boolean expression{2 < 3};
  157. ASSERT_STREQ("true", expression.toString().c_str());
  158. }
  159. TEST_F(BooleanTest, toString_false)
  160. {
  161. ls_std::Boolean expression{4 < 3};
  162. ASSERT_STREQ("false", expression.toString().c_str());
  163. }
  164. TEST_F(BooleanTest, unmarshal)
  165. {
  166. std::shared_ptr<ls_std::Boolean> expression = std::make_shared<ls_std::Boolean>(false);
  167. auto serializable = std::make_shared<ls_std::SerializableJsonBoolean>(expression);
  168. expression->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
  169. expression->unmarshal(R"({"value":true})");
  170. ASSERT_TRUE(*expression);
  171. }
  172. // additional functionality
  173. TEST_F(BooleanTest, getValue)
  174. {
  175. ls_std::Boolean x{2 < 3};
  176. ASSERT_TRUE(x.getValue());
  177. }
  178. TEST_F(BooleanTest, setSerialiable_no_reference)
  179. {
  180. ls_std::Boolean expression{};
  181. EXPECT_THROW({
  182. try
  183. {
  184. expression.setSerializable(nullptr);
  185. }
  186. catch (const ls_std::IllegalArgumentException &_exception)
  187. {
  188. throw;
  189. }
  190. }, ls_std::IllegalArgumentException);
  191. }
  192. TEST_F(BooleanTest, setStorable_no_reference)
  193. {
  194. ls_std::Boolean expression{};
  195. EXPECT_THROW({
  196. try
  197. {
  198. expression.setStorable(nullptr);
  199. }
  200. catch (const ls_std::IllegalArgumentException &_exception)
  201. {
  202. throw;
  203. }
  204. }, ls_std::IllegalArgumentException);
  205. }
  206. TEST_F(BooleanTest, XOR_with_positive_result)
  207. {
  208. ls_std::Boolean x{2 < 3};
  209. ls_std::Boolean y{4 < 3};
  210. ASSERT_TRUE(ls_std::Boolean::XOR(x, y));
  211. ASSERT_TRUE(ls_std::Boolean::XOR(y, x));
  212. ASSERT_TRUE(ls_std::Boolean::XOR(y, true));
  213. ASSERT_TRUE(ls_std::Boolean::XOR(true, y));
  214. ASSERT_TRUE(ls_std::Boolean::XOR(true, false));
  215. ASSERT_TRUE(ls_std::Boolean::XOR(false, true));
  216. }
  217. TEST_F(BooleanTest, XOR_with_negative_result)
  218. {
  219. ls_std::Boolean w{};
  220. ls_std::Boolean x{true};
  221. ls_std::Boolean y{};
  222. ls_std::Boolean z{true};
  223. ASSERT_FALSE(ls_std::Boolean::XOR(x, z));
  224. ASSERT_FALSE(ls_std::Boolean::XOR(w, y));
  225. ASSERT_FALSE(ls_std::Boolean::XOR(true, true));
  226. ASSERT_FALSE(ls_std::Boolean::XOR(false, false));
  227. ASSERT_FALSE(ls_std::Boolean::XOR(w, false));
  228. ASSERT_FALSE(ls_std::Boolean::XOR(false, w));
  229. }
  230. }