DoubleTest.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-14
  6. * Changed: 2022-11-09
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls_std/ls_std_core.hpp>
  11. #include <ls_std/ls_std_boxing.hpp>
  12. using namespace ls::std::boxing;
  13. using namespace ls::std::core;
  14. using namespace ::std;
  15. namespace
  16. {
  17. class DoubleTest : public ::testing::Test
  18. {
  19. protected:
  20. DoubleTest() = default;
  21. ~DoubleTest() override = default;
  22. void SetUp() override
  23. {}
  24. void TearDown() override
  25. {}
  26. };
  27. // assignment operators
  28. TEST_F(DoubleTest, operator_assignment)
  29. {
  30. Double x{};
  31. x = 44.22;
  32. ASSERT_EQ(44.22, x);
  33. }
  34. // arithmetic operators
  35. TEST_F(DoubleTest, operator_negative)
  36. {
  37. Double x{3.25};
  38. ASSERT_DOUBLE_EQ(-3.25, -x);
  39. }
  40. TEST_F(DoubleTest, operator_addition_with_reference)
  41. {
  42. Double x{3.1415};
  43. Double y{2.223};
  44. Double z{x + y};
  45. ASSERT_DOUBLE_EQ(5.3645, z);
  46. }
  47. TEST_F(DoubleTest, operator_addition_with_value)
  48. {
  49. Double x{3.1415};
  50. ASSERT_DOUBLE_EQ(5.3645, x + 2.223);
  51. }
  52. TEST_F(DoubleTest, operator_multiplication_with_reference)
  53. {
  54. Double x{3.14};
  55. Double y{2.22};
  56. Double z{x * y};
  57. ASSERT_DOUBLE_EQ(6.9708, z);
  58. }
  59. TEST_F(DoubleTest, operator_multiplication_with_value)
  60. {
  61. Double x{3.14};
  62. ASSERT_DOUBLE_EQ(6.9708, x * 2.22);
  63. }
  64. TEST_F(DoubleTest, operator_substraction_with_reference)
  65. {
  66. Double x{3.1415};
  67. Double y{2.225};
  68. Double z{x - y};
  69. ASSERT_DOUBLE_EQ(0.9165, z);
  70. }
  71. TEST_F(DoubleTest, operator_substraction_with_value)
  72. {
  73. Double x{3.1415};
  74. ASSERT_DOUBLE_EQ(0.9165, x - 2.225);
  75. }
  76. TEST_F(DoubleTest, operator_division_with_reference)
  77. {
  78. Double x{2.25};
  79. Double y{0.5};
  80. Double z{x / y};
  81. ASSERT_DOUBLE_EQ(4.5, z);
  82. }
  83. TEST_F(DoubleTest, operator_division_with_value)
  84. {
  85. Double x{2.25};
  86. ASSERT_DOUBLE_EQ(4.5, x / 0.5);
  87. }
  88. // compound operators
  89. TEST_F(DoubleTest, operator_add_assign_with_reference)
  90. {
  91. Double x{2.25000000};
  92. Double y{3.14000000};
  93. x += y;
  94. ASSERT_DOUBLE_EQ(5.39000000, x);
  95. }
  96. TEST_F(DoubleTest, operator_add_assign_with_value)
  97. {
  98. Double x{2.25000000};
  99. x += 3.14000000;
  100. ASSERT_DOUBLE_EQ(5.39000000, x);
  101. }
  102. TEST_F(DoubleTest, operator_sub_assign_with_reference)
  103. {
  104. Double x{2.25};
  105. Double y{0.04};
  106. x -= y;
  107. ASSERT_DOUBLE_EQ(2.21, x);
  108. }
  109. TEST_F(DoubleTest, operator_sub_assign_with_value)
  110. {
  111. Double x{2.25};
  112. x -= 0.04;
  113. ASSERT_DOUBLE_EQ(2.21, x);
  114. }
  115. TEST_F(DoubleTest, operator_mul_assign_with_reference)
  116. {
  117. Double x{2.25000000};
  118. Double y{0.04000000};
  119. x *= y;
  120. ASSERT_DOUBLE_EQ(0.09000000, x);
  121. }
  122. TEST_F(DoubleTest, operator_mul_assign_with_value)
  123. {
  124. Double x{2.25000000};
  125. x *= 0.04000000;
  126. ASSERT_DOUBLE_EQ(0.09000000, x);
  127. }
  128. TEST_F(DoubleTest, operator_division_assign_with_reference)
  129. {
  130. Double x{2.25};
  131. Double y{0.05};
  132. x /= y;
  133. ASSERT_DOUBLE_EQ(45.0, x);
  134. }
  135. TEST_F(DoubleTest, operator_division_assign_with_value)
  136. {
  137. Double x{2.25};
  138. x /= 0.05;
  139. ASSERT_DOUBLE_EQ(45.0, x);
  140. }
  141. // comparison operators
  142. TEST_F(DoubleTest, operator_equals_with_reference)
  143. {
  144. Double x{3.14159};
  145. Double y{3.14159};
  146. ASSERT_TRUE(x == y);
  147. ASSERT_TRUE(y == x);
  148. }
  149. TEST_F(DoubleTest, operator_equals_with_value)
  150. {
  151. Double x{3.14159};
  152. ASSERT_TRUE(x == 3.14159);
  153. ASSERT_TRUE(3.14159 == x);
  154. }
  155. TEST_F(DoubleTest, operator_not_equal_with_reference)
  156. {
  157. Double x{3.1415};
  158. Double y{3.1414};
  159. ASSERT_TRUE(x != y);
  160. ASSERT_TRUE(y != x);
  161. }
  162. TEST_F(DoubleTest, operator_not_equal_with_value)
  163. {
  164. Double x{3.1415};
  165. ASSERT_TRUE(x != 3.1414);
  166. ASSERT_TRUE(3.1414 != x);
  167. }
  168. TEST_F(DoubleTest, operator_greater_than_with_reference)
  169. {
  170. Double x{3.1415};
  171. Double y{3.1414};
  172. ASSERT_TRUE(x > y);
  173. }
  174. TEST_F(DoubleTest, operator_greater_than_with_value)
  175. {
  176. Double x{3.1415};
  177. Double y{3.1414};
  178. ASSERT_TRUE(x > 3.1414);
  179. }
  180. TEST_F(DoubleTest, operator_greater_than_equals_with_reference)
  181. {
  182. Double x{3.1414};
  183. Double y{3.1414};
  184. Double z{3.1415};
  185. ASSERT_TRUE(x >= y);
  186. ASSERT_TRUE(z >= y);
  187. }
  188. TEST_F(DoubleTest, operator_greater_than_equals_with_value)
  189. {
  190. Double x{3.1414};
  191. ASSERT_TRUE(x >= 3.1414);
  192. }
  193. TEST_F(DoubleTest, operator_less_than_with_reference)
  194. {
  195. Double x{3.1413};
  196. Double y{3.1414};
  197. ASSERT_TRUE(x < y);
  198. }
  199. TEST_F(DoubleTest, operator_less_than_with_value)
  200. {
  201. Double x{3.1413};
  202. Double y{3.1414};
  203. ASSERT_TRUE(x < 3.1414);
  204. }
  205. TEST_F(DoubleTest, operator_less_than_equals_with_reference)
  206. {
  207. Double x{3.1414};
  208. Double y{3.1414};
  209. Double z{3.1415};
  210. ASSERT_TRUE(x <= y);
  211. ASSERT_TRUE(x <= z);
  212. }
  213. TEST_F(DoubleTest, operator_less_than_equals_with_value)
  214. {
  215. Double x{3.1414};
  216. ASSERT_TRUE(x <= 3.1414);
  217. }
  218. // increment / decrement operator
  219. TEST_F(DoubleTest, operator_increment)
  220. {
  221. Double x{3.1415};
  222. ++x;
  223. ASSERT_DOUBLE_EQ(4.1415, x);
  224. }
  225. TEST_F(DoubleTest, operator_decrement)
  226. {
  227. Double x{3.1415};
  228. --x;
  229. ASSERT_DOUBLE_EQ(2.1415, x);
  230. }
  231. // implementation
  232. TEST_F(DoubleTest, parse_with_positive_value)
  233. {
  234. Double x{};
  235. x.parse("3.1415");
  236. ASSERT_DOUBLE_EQ(3.1415, x);
  237. }
  238. TEST_F(DoubleTest, parse_with_negative_value)
  239. {
  240. Double x{};
  241. x.parse("-2.1415");
  242. ASSERT_DOUBLE_EQ(-2.1415, x);
  243. }
  244. TEST_F(DoubleTest, toString)
  245. {
  246. Double x{13.1543};
  247. ASSERT_TRUE(x.toString().find("13.1543") != string::npos);
  248. }
  249. // additional functionality
  250. TEST_F(DoubleTest, getEpsilon)
  251. {
  252. Double x{};
  253. ASSERT_DOUBLE_EQ(0.00000001, x.getEpsilon());
  254. }
  255. TEST_F(DoubleTest, getValue)
  256. {
  257. Double x{3.1415};
  258. ASSERT_DOUBLE_EQ(3.1415, x.getValue());
  259. }
  260. TEST_F(DoubleTest, setEpsilon)
  261. {
  262. Double x{};
  263. x.setEpsilon(0.01);
  264. ASSERT_DOUBLE_EQ(0.01, x.getEpsilon());
  265. }
  266. TEST_F(DoubleTest, setEpsilon_invalid_value)
  267. {
  268. Double x{};
  269. EXPECT_THROW({
  270. try
  271. {
  272. x.setEpsilon(0.0);
  273. }
  274. catch (const IllegalArgumentException &_exception)
  275. {
  276. throw;
  277. }
  278. }, IllegalArgumentException);
  279. }
  280. }