BooleanTest.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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-24
  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 ls::std::boxing::Boolean;
  13. using ls::std::core::IllegalArgumentException;
  14. using std::ostringstream;
  15. using testing::Test;
  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. }
  77. TEST_F(BooleanTest, operator_or)
  78. {
  79. Boolean expressionA{false};
  80. Boolean expressionB{true};
  81. ASSERT_TRUE(expressionA || expressionB);
  82. ASSERT_TRUE(expressionB || expressionA);
  83. ASSERT_TRUE(1 || expressionA.getValue());
  84. }
  85. TEST_F(BooleanTest, operator_or_with_false_result)
  86. {
  87. Boolean expressionA{false};
  88. Boolean expressionB{false};
  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.getValue());
  94. }
  95. // implementation
  96. TEST_F(BooleanTest, parse_true_value)
  97. {
  98. Boolean expression{};
  99. expression.parse("true");
  100. ASSERT_TRUE(expression.getValue());
  101. expression.parse("tRue");
  102. ASSERT_TRUE(expression.getValue());
  103. expression.parse("TRUE");
  104. ASSERT_TRUE(expression.getValue());
  105. }
  106. TEST_F(BooleanTest, parse_false_value)
  107. {
  108. 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. {
  120. try
  121. {
  122. Boolean expression{};
  123. expression.parse("hello");
  124. }
  125. catch (const IllegalArgumentException &_exception)
  126. {
  127. throw;
  128. }
  129. },
  130. IllegalArgumentException);
  131. }
  132. TEST_F(BooleanTest, toString_true)
  133. {
  134. Boolean expression{2 < 3};
  135. ASSERT_STREQ("true", expression.toString().c_str());
  136. }
  137. TEST_F(BooleanTest, toString_false)
  138. {
  139. Boolean expression{4 < 3};
  140. ASSERT_STREQ("false", expression.toString().c_str());
  141. }
  142. // additional functionality
  143. TEST_F(BooleanTest, getValue)
  144. {
  145. Boolean x{2 < 3};
  146. ASSERT_TRUE(x.getValue());
  147. }
  148. TEST_F(BooleanTest, XOR_with_positive_result)
  149. {
  150. Boolean x{2 < 3};
  151. Boolean y{4 < 3};
  152. ASSERT_TRUE(Boolean::XOR(x, y));
  153. ASSERT_TRUE(Boolean::XOR(y, x));
  154. ASSERT_TRUE(Boolean::XOR(y, true));
  155. ASSERT_TRUE(Boolean::XOR(true, y));
  156. ASSERT_TRUE(Boolean::XOR(true, false));
  157. ASSERT_TRUE(Boolean::XOR(false, true));
  158. }
  159. TEST_F(BooleanTest, XOR_with_negative_result)
  160. {
  161. Boolean w{};
  162. Boolean x{true};
  163. Boolean y{};
  164. Boolean z{true};
  165. ASSERT_FALSE(Boolean::XOR(x, z));
  166. ASSERT_FALSE(Boolean::XOR(w, y));
  167. ASSERT_FALSE(Boolean::XOR(true, true));
  168. ASSERT_FALSE(Boolean::XOR(false, false));
  169. ASSERT_FALSE(Boolean::XOR(w, false));
  170. ASSERT_FALSE(Boolean::XOR(false, w));
  171. }
  172. }