BooleanTest.cpp 5.2 KB

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