BooleanTest.cpp 4.4 KB

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