BooleanTest.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-09
  6. * Changed: 2020-08-14
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include "../../../source/boxing/Boolean.hpp"
  11. namespace {
  12. class BooleanTest : public ::testing::Test {
  13. protected:
  14. BooleanTest() = default;
  15. ~BooleanTest() override = default;
  16. void SetUp() override
  17. {}
  18. void TearDown() override
  19. {}
  20. };
  21. // assignment operators
  22. TEST_F(BooleanTest, operatorAssignment)
  23. {
  24. ls_std::Boolean expression {};
  25. expression = true;
  26. ASSERT_TRUE(expression);
  27. expression = 1;
  28. ASSERT_TRUE(expression);
  29. }
  30. // stream operators
  31. TEST_F(BooleanTest, operatorOutputStream)
  32. {
  33. ls_std::Boolean expression {true};
  34. std::ostringstream _stream {};
  35. _stream << expression;
  36. ASSERT_STREQ("true", _stream.str().c_str());
  37. }
  38. // logical operators
  39. TEST_F(BooleanTest, operatorNot) {
  40. ls_std::Boolean expression {};
  41. ASSERT_TRUE(!expression);
  42. }
  43. TEST_F(BooleanTest, operatorNotNegative) {
  44. ls_std::Boolean expression {true};
  45. ASSERT_FALSE(!expression);
  46. }
  47. TEST_F(BooleanTest, operatorAnd) {
  48. ls_std::Boolean expressionA {true};
  49. ls_std::Boolean expressionB {3 == 3};
  50. ASSERT_TRUE(expressionA && expressionB);
  51. ASSERT_TRUE(expressionB && expressionA);
  52. ASSERT_TRUE(expressionA && true);
  53. ASSERT_TRUE(expressionA && 1);
  54. ASSERT_TRUE(1 && expressionB);
  55. }
  56. TEST_F(BooleanTest, operatorAndNegative) {
  57. ls_std::Boolean expressionA {true};
  58. ls_std::Boolean expressionB {3 == 4};
  59. ASSERT_FALSE(expressionA && expressionB);
  60. ASSERT_FALSE(expressionB && expressionA);
  61. ASSERT_FALSE(expressionB && (3 == 4));
  62. ASSERT_FALSE(expressionB && 0);
  63. ASSERT_FALSE(expressionB && false);
  64. }
  65. TEST_F(BooleanTest, operatorOr) {
  66. ls_std::Boolean expressionA {false};
  67. ls_std::Boolean expressionB {3 == 3};
  68. ASSERT_TRUE(expressionA || expressionB);
  69. ASSERT_TRUE(expressionB || expressionA);
  70. ASSERT_TRUE(expressionA || 1);
  71. ASSERT_TRUE(expressionA || true);
  72. ASSERT_TRUE(1 || expressionA);
  73. }
  74. TEST_F(BooleanTest, operatorOrNegative) {
  75. ls_std::Boolean expressionA {false};
  76. ls_std::Boolean expressionB {3 == 4};
  77. ASSERT_FALSE(expressionA || expressionB);
  78. ASSERT_FALSE(expressionB || expressionA);
  79. ASSERT_FALSE(expressionA || 0);
  80. ASSERT_FALSE(expressionA || false);
  81. ASSERT_FALSE(0 || expressionA);
  82. }
  83. // implementation
  84. TEST_F(BooleanTest, parse)
  85. {
  86. ls_std::Boolean expression {};
  87. expression.parse("true");
  88. ASSERT_TRUE(expression);
  89. expression.parse("tRue");
  90. ASSERT_TRUE(expression);
  91. expression.parse("TRUE");
  92. ASSERT_TRUE(expression);
  93. }
  94. TEST_F(BooleanTest, parseNegative)
  95. {
  96. ls_std::Boolean expression {};
  97. expression.parse("false");
  98. ASSERT_FALSE(expression);
  99. expression.parse("fAlSe");
  100. ASSERT_FALSE(expression);
  101. expression.parse("FALSE");
  102. ASSERT_FALSE(expression);
  103. }
  104. TEST_F(BooleanTest, toString)
  105. {
  106. ls_std::Boolean expression { 2 < 3};
  107. ASSERT_STREQ("true", expression.toString().c_str());
  108. expression = 4 < 3;
  109. ASSERT_STREQ("false", expression.toString().c_str());
  110. }
  111. // additional functionality
  112. TEST_F(BooleanTest, getValue)
  113. {
  114. ls_std::Boolean x {2 < 3};
  115. ASSERT_TRUE(x.getValue());
  116. }
  117. TEST_F(BooleanTest, XOR)
  118. {
  119. ls_std::Boolean x {2 < 3};
  120. ls_std::Boolean y {4 < 3};
  121. ASSERT_TRUE(ls_std::Boolean::XOR(x, y));
  122. ASSERT_TRUE(ls_std::Boolean::XOR(y, x));
  123. ASSERT_TRUE(ls_std::Boolean::XOR(y, true));
  124. ASSERT_TRUE(ls_std::Boolean::XOR(true, y));
  125. ASSERT_TRUE(ls_std::Boolean::XOR(true, false));
  126. ASSERT_TRUE(ls_std::Boolean::XOR(false, true));
  127. }
  128. TEST_F(BooleanTest, XORNegative)
  129. {
  130. ls_std::Boolean w {};
  131. ls_std::Boolean x {true};
  132. ls_std::Boolean y {};
  133. ls_std::Boolean z {true};
  134. ASSERT_FALSE(ls_std::Boolean::XOR(x, z));
  135. ASSERT_FALSE(ls_std::Boolean::XOR(w, y));
  136. ASSERT_FALSE(ls_std::Boolean::XOR(true, true));
  137. ASSERT_FALSE(ls_std::Boolean::XOR(false, false));
  138. ASSERT_FALSE(ls_std::Boolean::XOR(w, false));
  139. ASSERT_FALSE(ls_std::Boolean::XOR(false, w));
  140. }
  141. }