BooleanTest.cpp 5.2 KB

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