DoubleTest.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-14
  6. * Changed: 2020-08-14
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include "../../../source/boxing/Double.hpp"
  11. namespace {
  12. class DoubleTest : public ::testing::Test {
  13. protected:
  14. DoubleTest() = default;
  15. ~DoubleTest() override = default;
  16. void SetUp() override {}
  17. void TearDown() override {}
  18. };
  19. // assignment operators
  20. TEST_F(DoubleTest, operatorAssignment)
  21. {
  22. ls_std::Double x {13.023};
  23. ASSERT_EQ(13.023, x);
  24. x = 44.22;
  25. ASSERT_EQ(44.22, x);
  26. ls_std::Double y {3.0};
  27. x = y;
  28. ASSERT_EQ(3.0, x);
  29. }
  30. // arithmetic operators
  31. TEST_F(DoubleTest, operatorHyphen)
  32. {
  33. ls_std::Double x {3.25};
  34. ASSERT_DOUBLE_EQ(-3.25, -x);
  35. }
  36. TEST_F(DoubleTest, operatorAddition)
  37. {
  38. ls_std::Double x {3.1415};
  39. ls_std::Double y {2.223};
  40. ls_std::Double z {x + y};
  41. ASSERT_DOUBLE_EQ(5.3645, z);
  42. ASSERT_DOUBLE_EQ(5.3645, x + 2.223);
  43. }
  44. TEST_F(DoubleTest, operatorMultiplication)
  45. {
  46. ls_std::Double x {3.14};
  47. ls_std::Double y {2.22};
  48. ls_std::Double z {x * y};
  49. ASSERT_DOUBLE_EQ(6.9708, z);
  50. ASSERT_DOUBLE_EQ(6.9708, x * 2.22);
  51. }
  52. TEST_F(DoubleTest, operatorSubstraction)
  53. {
  54. ls_std::Double x {3.1415};
  55. ls_std::Double y {2.225};
  56. ls_std::Double z {x - y};
  57. ASSERT_DOUBLE_EQ(0.9165, z);
  58. ASSERT_DOUBLE_EQ(0.9165, x - 2.225);
  59. }
  60. TEST_F(DoubleTest, operatorDivision)
  61. {
  62. ls_std::Double x {2.25};
  63. ls_std::Double y {0.5};
  64. ls_std::Double z {x / y};
  65. ASSERT_DOUBLE_EQ(4.5, z);
  66. ASSERT_DOUBLE_EQ(4.5, x / 0.5);
  67. }
  68. // compound operators
  69. TEST_F(DoubleTest, operatorAddEqual)
  70. {
  71. ls_std::Double x {2.25000000};
  72. ls_std::Double y {-0.39000000};
  73. ASSERT_DOUBLE_EQ(2.25000000, x);
  74. x += 3.14000000;
  75. ASSERT_DOUBLE_EQ(5.39000000, x);
  76. x += y;
  77. ASSERT_DOUBLE_EQ(5.0000000, x);
  78. }
  79. TEST_F(DoubleTest, operatorSubEqual)
  80. {
  81. ls_std::Double x {2.25};
  82. ls_std::Double y {-0.04};
  83. ASSERT_DOUBLE_EQ(2.25, x);
  84. x -= 1.14;
  85. ASSERT_DOUBLE_EQ(1.11, x);
  86. x -= y;
  87. ASSERT_DOUBLE_EQ(1.15, x);
  88. }
  89. TEST_F(DoubleTest, operatorMulEqual)
  90. {
  91. ls_std::Double x {2.25000000};
  92. ls_std::Double y {0.04000000};
  93. ASSERT_DOUBLE_EQ(2.25000000, x);
  94. x *= 1.14000000;
  95. ASSERT_DOUBLE_EQ(2.56500000, x);
  96. x *= y;
  97. ASSERT_DOUBLE_EQ(0.102600000, x);
  98. }
  99. TEST_F(DoubleTest, operatorDivEqual)
  100. {
  101. ls_std::Double x {2.25};
  102. ls_std::Double y {1.5};
  103. ASSERT_DOUBLE_EQ(2.25, x);
  104. x /= 0.05;
  105. ASSERT_DOUBLE_EQ(45.0, x);
  106. x /= y;
  107. ASSERT_DOUBLE_EQ(30.0, x);
  108. }
  109. // comparison operators
  110. TEST_F(DoubleTest, operatorEqual)
  111. {
  112. ls_std::Double x {3.14159};
  113. ls_std::Double y {3.14159};
  114. ASSERT_TRUE(x == y);
  115. ASSERT_TRUE(y == x);
  116. ASSERT_TRUE(x == 3.14159);
  117. ASSERT_TRUE(3.14159 == x);
  118. }
  119. TEST_F(DoubleTest, operatorNotEqual)
  120. {
  121. ls_std::Double x {3.1415};
  122. ls_std::Double y {3.1414};
  123. ASSERT_TRUE(x != y);
  124. ASSERT_TRUE(y != x);
  125. ASSERT_TRUE(x != 3.1414);
  126. ASSERT_TRUE(3.1414 != x);
  127. }
  128. TEST_F(DoubleTest, operatorGreaterThan)
  129. {
  130. ls_std::Double x {3.1415};
  131. ls_std::Double y {3.1414};
  132. ASSERT_TRUE(x > y);
  133. ASSERT_TRUE(x > 3.1414);
  134. }
  135. TEST_F(DoubleTest, operatorGreaterThanNegative)
  136. {
  137. ls_std::Double x {3.1414};
  138. ls_std::Double y {3.1414};
  139. ASSERT_FALSE(x > y);
  140. ASSERT_FALSE(x > 3.1414);
  141. }
  142. TEST_F(DoubleTest, operatorGreaterThanEqual)
  143. {
  144. ls_std::Double x {3.1414};
  145. ls_std::Double y {3.1414};
  146. ls_std::Double z {3.1415};
  147. ASSERT_TRUE(x >= y);
  148. ASSERT_TRUE(z >= y);
  149. ASSERT_TRUE(x >= 3.1414);
  150. ASSERT_TRUE(z >= 3.1414);
  151. }
  152. TEST_F(DoubleTest, operatorGreaterThanEqualNegative)
  153. {
  154. ls_std::Double x {3.1414};
  155. ls_std::Double y {3.1415};
  156. ASSERT_FALSE(x >= y);
  157. ASSERT_FALSE(x >= 3.1415);
  158. }
  159. TEST_F(DoubleTest, operatorLessThan)
  160. {
  161. ls_std::Double x {3.1413};
  162. ls_std::Double y {3.1414};
  163. ASSERT_TRUE(x < y);
  164. ASSERT_TRUE(x < 3.1414);
  165. }
  166. TEST_F(DoubleTest, operatorLessThanNegative)
  167. {
  168. ls_std::Double x {3.1414};
  169. ls_std::Double y {3.1414};
  170. ASSERT_FALSE(x < y);
  171. ASSERT_FALSE(x < 3.1414);
  172. }
  173. TEST_F(DoubleTest, operatorLessThanEqual)
  174. {
  175. ls_std::Double x {3.1414};
  176. ls_std::Double y {3.1414};
  177. ls_std::Double z {3.1415};
  178. ASSERT_TRUE(x <= y);
  179. ASSERT_TRUE(x <= z);
  180. ASSERT_TRUE(x <= 3.1414);
  181. ASSERT_TRUE(x <= 3.1415);
  182. }
  183. TEST_F(DoubleTest, operatorLessThanEqualNegative)
  184. {
  185. ls_std::Double x {3.1415};
  186. ls_std::Double y {3.1414};
  187. ASSERT_FALSE(x <= y);
  188. ASSERT_FALSE(x <= 3.1414);
  189. }
  190. // increment / decrement operator
  191. TEST_F(DoubleTest, operatorIncrement)
  192. {
  193. ls_std::Double x {3.1415};
  194. ASSERT_DOUBLE_EQ(3.1415, x);
  195. ++x;
  196. ASSERT_DOUBLE_EQ(4.1415, x);
  197. ++x;
  198. ASSERT_DOUBLE_EQ(5.1415, x);
  199. }
  200. TEST_F(DoubleTest, operatorDecrement)
  201. {
  202. ls_std::Double x {3.1415};
  203. ASSERT_DOUBLE_EQ(3.1415, x);
  204. --x;
  205. ASSERT_DOUBLE_EQ(2.1415, x);
  206. --x;
  207. ASSERT_DOUBLE_EQ(1.1415, x);
  208. }
  209. // implementation
  210. TEST_F(DoubleTest, parse)
  211. {
  212. ls_std::Double x {};
  213. x.parse("3.1415");
  214. ASSERT_DOUBLE_EQ(3.1415, x);
  215. x.parse("-2.1415");
  216. ASSERT_DOUBLE_EQ(-2.1415, x);
  217. }
  218. TEST_F(DoubleTest, toString)
  219. {
  220. ls_std::Double x {13.1543};
  221. ASSERT_TRUE(x.toString().find("13.1543") != std::string::npos);
  222. }
  223. // additional functionality
  224. TEST_F(DoubleTest, getEpsilon)
  225. {
  226. ls_std::Double x {};
  227. ASSERT_DOUBLE_EQ(0.00000001, x.getEpsilon());
  228. }
  229. TEST_F(DoubleTest, getValue)
  230. {
  231. ls_std::Double x {3.1415};
  232. ASSERT_DOUBLE_EQ(3.1415, x.getValue());
  233. }
  234. TEST_F(DoubleTest, setEpsilon)
  235. {
  236. ls_std::Double x {};
  237. x.setEpsilon(0.01);
  238. ASSERT_DOUBLE_EQ(0.01, x.getEpsilon());
  239. }
  240. }