BooleanTest.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-09
  6. * Changed: 2021-07-08
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls_std/ls_std.hpp>
  11. #include <TestHelper.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::Boolean expression{};
  28. expression = true;
  29. ASSERT_TRUE(expression);
  30. }
  31. TEST_F(BooleanTest, operator_assignment_integer)
  32. {
  33. ls_std::Boolean expression{};
  34. expression = 1;
  35. ASSERT_TRUE(expression);
  36. }
  37. // stream operators
  38. TEST_F(BooleanTest, operator_output_stream)
  39. {
  40. ls_std::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::Boolean expression{};
  49. ASSERT_TRUE(!expression);
  50. }
  51. TEST_F(BooleanTest, operator_negation_positive_value)
  52. {
  53. ls_std::Boolean expression{true};
  54. ASSERT_FALSE(!expression);
  55. }
  56. TEST_F(BooleanTest, operator_and)
  57. {
  58. ls_std::Boolean expressionA{true};
  59. ls_std::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::Boolean expressionA{true};
  69. ls_std::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::Boolean expressionA{false};
  79. ls_std::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::Boolean expressionA{false};
  89. ls_std::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::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::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, toString_true)
  118. {
  119. ls_std::Boolean expression{2 < 3};
  120. ASSERT_STREQ("true", expression.toString().c_str());
  121. }
  122. TEST_F(BooleanTest, toString_false)
  123. {
  124. ls_std::Boolean expression{4 < 3};
  125. ASSERT_STREQ("false", expression.toString().c_str());
  126. }
  127. // additional functionality
  128. TEST_F(BooleanTest, getValue)
  129. {
  130. ls_std::Boolean x{2 < 3};
  131. ASSERT_TRUE(x.getValue());
  132. }
  133. TEST_F(BooleanTest, XOR_with_positive_result)
  134. {
  135. ls_std::Boolean x{2 < 3};
  136. ls_std::Boolean y{4 < 3};
  137. ASSERT_TRUE(ls_std::Boolean::XOR(x, y));
  138. ASSERT_TRUE(ls_std::Boolean::XOR(y, x));
  139. ASSERT_TRUE(ls_std::Boolean::XOR(y, true));
  140. ASSERT_TRUE(ls_std::Boolean::XOR(true, y));
  141. ASSERT_TRUE(ls_std::Boolean::XOR(true, false));
  142. ASSERT_TRUE(ls_std::Boolean::XOR(false, true));
  143. }
  144. TEST_F(BooleanTest, XOR_with_negative_result)
  145. {
  146. ls_std::Boolean w{};
  147. ls_std::Boolean x{true};
  148. ls_std::Boolean y{};
  149. ls_std::Boolean z{true};
  150. ASSERT_FALSE(ls_std::Boolean::XOR(x, z));
  151. ASSERT_FALSE(ls_std::Boolean::XOR(w, y));
  152. ASSERT_FALSE(ls_std::Boolean::XOR(true, true));
  153. ASSERT_FALSE(ls_std::Boolean::XOR(false, false));
  154. ASSERT_FALSE(ls_std::Boolean::XOR(w, false));
  155. ASSERT_FALSE(ls_std::Boolean::XOR(false, w));
  156. }
  157. }