BooleanTest.cpp 4.6 KB

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