BooleanTest.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-09
  6. * Changed: 2022-11-09
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls_std/ls_std_core.hpp>
  11. #include <ls_std/ls_std_boxing.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. try
  124. {
  125. Boolean expression{};
  126. expression.parse("hello");
  127. } catch (const IllegalArgumentException &_exception)
  128. {
  129. throw;
  130. }
  131. }, IllegalArgumentException);
  132. }
  133. TEST_F(BooleanTest, toString_true)
  134. {
  135. Boolean expression{2 < 3};
  136. ASSERT_STREQ("true", expression.toString().c_str());
  137. }
  138. TEST_F(BooleanTest, toString_false)
  139. {
  140. Boolean expression{4 < 3};
  141. ASSERT_STREQ("false", expression.toString().c_str());
  142. }
  143. // additional functionality
  144. TEST_F(BooleanTest, getValue)
  145. {
  146. Boolean x{2 < 3};
  147. ASSERT_TRUE(x.getValue());
  148. }
  149. TEST_F(BooleanTest, XOR_with_positive_result)
  150. {
  151. Boolean x{2 < 3};
  152. Boolean y{4 < 3};
  153. ASSERT_TRUE(Boolean::XOR(x, y));
  154. ASSERT_TRUE(Boolean::XOR(y, x));
  155. ASSERT_TRUE(Boolean::XOR(y, true));
  156. ASSERT_TRUE(Boolean::XOR(true, y));
  157. ASSERT_TRUE(Boolean::XOR(true, false));
  158. ASSERT_TRUE(Boolean::XOR(false, true));
  159. }
  160. TEST_F(BooleanTest, XOR_with_negative_result)
  161. {
  162. Boolean w{};
  163. Boolean x{true};
  164. Boolean y{};
  165. Boolean z{true};
  166. ASSERT_FALSE(Boolean::XOR(x, z));
  167. ASSERT_FALSE(Boolean::XOR(w, y));
  168. ASSERT_FALSE(Boolean::XOR(true, true));
  169. ASSERT_FALSE(Boolean::XOR(false, false));
  170. ASSERT_FALSE(Boolean::XOR(w, false));
  171. ASSERT_FALSE(Boolean::XOR(false, w));
  172. }
  173. }