DoubleTest.cpp 6.5 KB

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