BooleanTest.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-09
  6. * Changed: 2023-02-22
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls-std/ls-std-boxing.hpp>
  11. #include <ls-std/ls-std-core.hpp>
  12. using namespace ls::std::boxing;
  13. using namespace ls::std::core;
  14. using namespace ::std;
  15. using namespace ::testing;
  16. namespace
  17. {
  18. class BooleanTest : public Test
  19. {
  20. protected:
  21. BooleanTest() = default;
  22. ~BooleanTest() override = default;
  23. void SetUp() override
  24. {}
  25. void TearDown() override
  26. {}
  27. };
  28. // assignment operators
  29. TEST_F(BooleanTest, operator_assignment_boolean)
  30. {
  31. Boolean expression{};
  32. expression = true;
  33. ASSERT_TRUE(expression.getValue());
  34. }
  35. TEST_F(BooleanTest, operator_assignment_integer)
  36. {
  37. Boolean expression{};
  38. expression = 1;
  39. ASSERT_TRUE(expression.getValue());
  40. }
  41. // stream operators
  42. TEST_F(BooleanTest, operator_output_stream)
  43. {
  44. Boolean expression{true};
  45. ostringstream _stream{};
  46. _stream << expression;
  47. ASSERT_STREQ("true", _stream.str().c_str());
  48. }
  49. // logical operators
  50. TEST_F(BooleanTest, operator_negation_negative_value)
  51. {
  52. Boolean expression{};
  53. ASSERT_TRUE(!expression);
  54. }
  55. TEST_F(BooleanTest, operator_negation_positive_value)
  56. {
  57. Boolean expression{true};
  58. ASSERT_FALSE(!expression);
  59. }
  60. TEST_F(BooleanTest, operator_and)
  61. {
  62. Boolean expressionA{true};
  63. Boolean expressionB{true};
  64. ASSERT_TRUE(expressionA && expressionB);
  65. ASSERT_TRUE(expressionB && expressionA);
  66. ASSERT_TRUE(expressionA && true);
  67. ASSERT_TRUE(expressionA && 1);
  68. ASSERT_TRUE(1 && expressionB.getValue());
  69. }
  70. TEST_F(BooleanTest, operator_and_with_false_result)
  71. {
  72. Boolean expressionA{true};
  73. Boolean expressionB{false};
  74. ASSERT_FALSE(expressionA && expressionB);
  75. ASSERT_FALSE(expressionB && expressionA);
  76. ASSERT_FALSE(expressionB && (3 == 4));
  77. ASSERT_FALSE(expressionB && 0);
  78. ASSERT_FALSE(expressionB && false);
  79. }
  80. TEST_F(BooleanTest, operator_or)
  81. {
  82. Boolean expressionA{false};
  83. Boolean expressionB{true};
  84. ASSERT_TRUE(expressionA || expressionB);
  85. ASSERT_TRUE(expressionB || expressionA);
  86. ASSERT_TRUE(expressionA || 1);
  87. ASSERT_TRUE(expressionA || true);
  88. ASSERT_TRUE(1 || expressionA.getValue());
  89. }
  90. TEST_F(BooleanTest, operator_or_with_false_result)
  91. {
  92. Boolean expressionA{false};
  93. Boolean expressionB{false};
  94. ASSERT_FALSE(expressionA || expressionB);
  95. ASSERT_FALSE(expressionB || expressionA);
  96. ASSERT_FALSE(expressionA || 0);
  97. ASSERT_FALSE(expressionA || false);
  98. ASSERT_FALSE(0 || expressionA.getValue());
  99. }
  100. // implementation
  101. TEST_F(BooleanTest, parse_true_value)
  102. {
  103. Boolean expression{};
  104. expression.parse("true");
  105. ASSERT_TRUE(expression.getValue());
  106. expression.parse("tRue");
  107. ASSERT_TRUE(expression.getValue());
  108. expression.parse("TRUE");
  109. ASSERT_TRUE(expression.getValue());
  110. }
  111. TEST_F(BooleanTest, parse_false_value)
  112. {
  113. Boolean expression{};
  114. expression.parse("false");
  115. ASSERT_FALSE(expression);
  116. expression.parse("fAlSe");
  117. ASSERT_FALSE(expression);
  118. expression.parse("FALSE");
  119. ASSERT_FALSE(expression);
  120. }
  121. TEST_F(BooleanTest, parse_with_invalid_parameter)
  122. {
  123. EXPECT_THROW(
  124. {
  125. try
  126. {
  127. Boolean expression{};
  128. expression.parse("hello");
  129. }
  130. catch (const IllegalArgumentException &_exception)
  131. {
  132. throw;
  133. }
  134. },
  135. IllegalArgumentException);
  136. }
  137. TEST_F(BooleanTest, toString_true)
  138. {
  139. Boolean expression{2 < 3};
  140. ASSERT_STREQ("true", expression.toString().c_str());
  141. }
  142. TEST_F(BooleanTest, toString_false)
  143. {
  144. Boolean expression{4 < 3};
  145. ASSERT_STREQ("false", expression.toString().c_str());
  146. }
  147. // additional functionality
  148. TEST_F(BooleanTest, getValue)
  149. {
  150. Boolean x{2 < 3};
  151. ASSERT_TRUE(x.getValue());
  152. }
  153. TEST_F(BooleanTest, XOR_with_positive_result)
  154. {
  155. Boolean x{2 < 3};
  156. Boolean y{4 < 3};
  157. ASSERT_TRUE(Boolean::XOR(x, y));
  158. ASSERT_TRUE(Boolean::XOR(y, x));
  159. ASSERT_TRUE(Boolean::XOR(y, true));
  160. ASSERT_TRUE(Boolean::XOR(true, y));
  161. ASSERT_TRUE(Boolean::XOR(true, false));
  162. ASSERT_TRUE(Boolean::XOR(false, true));
  163. }
  164. TEST_F(BooleanTest, XOR_with_negative_result)
  165. {
  166. Boolean w{};
  167. Boolean x{true};
  168. Boolean y{};
  169. Boolean z{true};
  170. ASSERT_FALSE(Boolean::XOR(x, z));
  171. ASSERT_FALSE(Boolean::XOR(w, y));
  172. ASSERT_FALSE(Boolean::XOR(true, true));
  173. ASSERT_FALSE(Boolean::XOR(false, false));
  174. ASSERT_FALSE(Boolean::XOR(w, false));
  175. ASSERT_FALSE(Boolean::XOR(false, w));
  176. }
  177. }