FloatTest.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-14
  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 FloatTest : public ::testing::Test
  15. {
  16. protected:
  17. FloatTest() = default;
  18. ~FloatTest() override = default;
  19. void SetUp() override
  20. {}
  21. void TearDown() override
  22. {}
  23. static std::pair<std::shared_ptr<ls_std::File>, std::shared_ptr<ls_std::Float>> createPersistentTestFloat()
  24. {
  25. std::shared_ptr<ls_std::Float> number = std::make_shared<ls_std::Float>();
  26. std::string path = TestHelper::getResourcesFolderLocation() + "tmp_storable_float.json";
  27. std::shared_ptr<ls_std::File> file = std::make_shared<ls_std::File>(path);
  28. file->createNewFile();
  29. ls_std::FileWriter writer{*file};
  30. writer.write(R"({"value":3.14159})");
  31. auto serializable = std::make_shared<ls_std::SerializableJsonFloat>(number);
  32. number->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
  33. auto storable = std::make_shared<ls_std::StorableFile>(path);
  34. number->setStorable(std::dynamic_pointer_cast<ls_std::IStorable>(storable));
  35. return std::pair<std::shared_ptr<ls_std::File>, std::shared_ptr<ls_std::Float>>(file, number);
  36. }
  37. };
  38. // assignment operators
  39. TEST_F(FloatTest, operator_assignment)
  40. {
  41. ls_std::Float x{13.023f};
  42. x = 44.22f;
  43. ASSERT_EQ(44.22f, x);
  44. }
  45. // arithmetic operators
  46. TEST_F(FloatTest, operator_negative)
  47. {
  48. ls_std::Float x{3.25f};
  49. ASSERT_FLOAT_EQ(-3.25f, -x);
  50. }
  51. TEST_F(FloatTest, operator_addition_with_reference)
  52. {
  53. ls_std::Float x{3.1415f};
  54. ls_std::Float y{2.223f};
  55. ls_std::Float z{x + y};
  56. ASSERT_FLOAT_EQ(5.3645f, z);
  57. }
  58. TEST_F(FloatTest, operator_addition_with_value)
  59. {
  60. ls_std::Float x{3.1415f};
  61. ASSERT_FLOAT_EQ(5.3645f, x + 2.223f);
  62. }
  63. TEST_F(FloatTest, operator_multiplication_with_reference)
  64. {
  65. ls_std::Float x{3.14f};
  66. ls_std::Float y{2.22f};
  67. ls_std::Float z{x * y};
  68. ASSERT_FLOAT_EQ(6.9708f, z);
  69. }
  70. TEST_F(FloatTest, operator_multiplication_with_value)
  71. {
  72. ls_std::Float x{3.14f};
  73. ASSERT_FLOAT_EQ(6.9708f, x * 2.22f);
  74. }
  75. TEST_F(FloatTest, operator_substraction_with_reference)
  76. {
  77. ls_std::Float x{3.1415f};
  78. ls_std::Float y{2.225f};
  79. ls_std::Float z{x - y};
  80. ASSERT_FLOAT_EQ(0.9165f, z);
  81. }
  82. TEST_F(FloatTest, operator_substraction_with_value)
  83. {
  84. ls_std::Float x{3.1415f};
  85. ASSERT_FLOAT_EQ(0.9165f, x - 2.225f);
  86. }
  87. TEST_F(FloatTest, operator_division_with_reference)
  88. {
  89. ls_std::Float x{2.25f};
  90. ls_std::Float y{0.5f};
  91. ls_std::Float z{x / y};
  92. ASSERT_FLOAT_EQ(4.5f, z);
  93. }
  94. TEST_F(FloatTest, operator_division_with_value)
  95. {
  96. ls_std::Float x{2.25f};
  97. ASSERT_FLOAT_EQ(4.5f, x / 0.5f);
  98. }
  99. // compound operators
  100. TEST_F(FloatTest, operator_add_assign_with_reference)
  101. {
  102. ls_std::Float x{2.25f};
  103. ls_std::Float y{3.14f};
  104. x += y;
  105. ASSERT_FLOAT_EQ(5.39f, x);
  106. }
  107. TEST_F(FloatTest, operator_add_assign_with_value)
  108. {
  109. ls_std::Float x{2.25f};
  110. x += 3.14f;
  111. ASSERT_FLOAT_EQ(5.39f, x);
  112. }
  113. TEST_F(FloatTest, operator_sub_assign_with_reference)
  114. {
  115. ls_std::Float x{2.25f};
  116. ls_std::Float y{1.14f};
  117. x -= y;
  118. ASSERT_FLOAT_EQ(1.11f, x);
  119. }
  120. TEST_F(FloatTest, operator_sub_assign_with_value)
  121. {
  122. ls_std::Float x{2.25f};
  123. x -= 1.14f;
  124. ASSERT_FLOAT_EQ(1.11f, x);
  125. }
  126. TEST_F(FloatTest, operator_mul_assign_with_reference)
  127. {
  128. ls_std::Float x{2.25f};
  129. ls_std::Float y{0.04f};
  130. x *= y;
  131. ASSERT_FLOAT_EQ(0.09f, x);
  132. }
  133. TEST_F(FloatTest, operator_mul_assign_with_value)
  134. {
  135. ls_std::Float x{2.25f};
  136. x *= 1.14f;
  137. ASSERT_FLOAT_EQ(2.565f, x);
  138. }
  139. TEST_F(FloatTest, operator_division_assign_with_reference)
  140. {
  141. ls_std::Float x{2.25f};
  142. ls_std::Float y{1.5f};
  143. x /= y;
  144. ASSERT_FLOAT_EQ(1.5f, x);
  145. }
  146. TEST_F(FloatTest, operator_division_assign_with_value)
  147. {
  148. ls_std::Float x{2.25f};
  149. x /= 0.05f;
  150. ASSERT_FLOAT_EQ(45.0f, x);
  151. }
  152. // comparison operators
  153. TEST_F(FloatTest, operator_equals_with_reference)
  154. {
  155. ls_std::Float x{3.14159f};
  156. ls_std::Float y{3.14159f};
  157. ASSERT_TRUE(x == y);
  158. ASSERT_TRUE(y == x);
  159. }
  160. TEST_F(FloatTest, operator_equals_with_value)
  161. {
  162. ls_std::Float x{3.14159f};
  163. ASSERT_TRUE(x == 3.14159f);
  164. }
  165. TEST_F(FloatTest, operator_not_equals_with_reference)
  166. {
  167. ls_std::Float x{3.1415f};
  168. ls_std::Float y{3.1414f};
  169. ASSERT_TRUE(x != y);
  170. ASSERT_TRUE(y != x);
  171. }
  172. TEST_F(FloatTest, operator_not_equals_with_value)
  173. {
  174. ls_std::Float x{3.1415f};
  175. ASSERT_TRUE(x != 3.1414f);
  176. }
  177. TEST_F(FloatTest, operator_greater_than_with_reference)
  178. {
  179. ls_std::Float x{3.1415f};
  180. ls_std::Float y{3.1414f};
  181. ASSERT_TRUE(x > y);
  182. ASSERT_TRUE(x > 3.1414f);
  183. }
  184. TEST_F(FloatTest, operator_greater_than_with_value)
  185. {
  186. ls_std::Float x{3.1415f};
  187. ASSERT_TRUE(x > 3.1414f);
  188. }
  189. TEST_F(FloatTest, operator_greater_than_equals_with_reference)
  190. {
  191. ls_std::Float x{3.1414f};
  192. ls_std::Float y{3.1414f};
  193. ls_std::Float z{3.1415f};
  194. ASSERT_TRUE(x >= y);
  195. ASSERT_TRUE(z >= y);
  196. }
  197. TEST_F(FloatTest, operator_greater_than_equals_with_value)
  198. {
  199. ls_std::Float x{3.1414f};
  200. ls_std::Float z{3.1415f};
  201. ASSERT_TRUE(x >= 3.1414f);
  202. ASSERT_TRUE(z >= 3.1414f);
  203. }
  204. TEST_F(FloatTest, operator_less_than_with_reference)
  205. {
  206. ls_std::Float x{3.1413f};
  207. ls_std::Float y{3.1414f};
  208. ASSERT_TRUE(x < y);
  209. }
  210. TEST_F(FloatTest, operator_less_than_with_value)
  211. {
  212. ls_std::Float x{3.1413f};
  213. ASSERT_TRUE(x < 3.1414f);
  214. }
  215. TEST_F(FloatTest, operator_less_than_equals_with_reference)
  216. {
  217. ls_std::Float x{3.1414f};
  218. ls_std::Float y{3.1414f};
  219. ls_std::Float z{3.1415f};
  220. ASSERT_TRUE(x <= y);
  221. ASSERT_TRUE(x <= z);
  222. }
  223. TEST_F(FloatTest, operator_less_than_equals_with_value)
  224. {
  225. ls_std::Float x{3.1414f};
  226. ASSERT_TRUE(x <= 3.1414f);
  227. ASSERT_TRUE(x <= 3.1415f);
  228. }
  229. // increment / decrement operator
  230. TEST_F(FloatTest, operator_increment)
  231. {
  232. ls_std::Float x{3.1415f};
  233. ++x;
  234. ASSERT_FLOAT_EQ(4.1415f, x);
  235. }
  236. TEST_F(FloatTest, operator_decrement)
  237. {
  238. ls_std::Float x{3.1415f};
  239. --x;
  240. ASSERT_FLOAT_EQ(2.1415f, x);
  241. }
  242. // implementation
  243. TEST_F(FloatTest, load)
  244. {
  245. // preparation
  246. auto storableFloat = createPersistentTestFloat();
  247. // check
  248. storableFloat.second->load();
  249. ASSERT_FLOAT_EQ(3.14159f, *storableFloat.second);
  250. storableFloat.first->remove();
  251. }
  252. TEST_F(FloatTest, marshal)
  253. {
  254. std::shared_ptr<ls_std::Float> number = std::make_shared<ls_std::Float>(3.14159f);
  255. auto serializable = std::make_shared<ls_std::SerializableJsonFloat>(number);
  256. number->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
  257. ls_std::String jsonString{number->marshal()};
  258. ASSERT_TRUE(jsonString.contains(R"({"value":3.14159)"));
  259. }
  260. TEST_F(FloatTest, parse)
  261. {
  262. ls_std::Float number{};
  263. number.parse("3.1415f");
  264. ASSERT_FLOAT_EQ(3.1415f, number);
  265. }
  266. TEST_F(FloatTest, toString)
  267. {
  268. ls_std::Float x{13.1543f};
  269. ASSERT_TRUE(x.toString().find("13.1543") != std::string::npos);
  270. }
  271. TEST_F(FloatTest, unmarshal)
  272. {
  273. std::shared_ptr<ls_std::Float> number = std::make_shared<ls_std::Float>(3.14159f);
  274. auto serializable = std::make_shared<ls_std::SerializableJsonFloat>(number);
  275. number->setSerializable(std::dynamic_pointer_cast<ls_std::ISerializable>(serializable));
  276. number->unmarshal(R"({"value":17.4132})");
  277. ASSERT_FLOAT_EQ(17.4132, *number);
  278. }
  279. // additional functionality
  280. TEST_F(FloatTest, getEpsilon)
  281. {
  282. ls_std::Float x{};
  283. ASSERT_FLOAT_EQ(0.00001f, x.getEpsilon());
  284. }
  285. TEST_F(FloatTest, getValue)
  286. {
  287. ls_std::Float x{3.1415f};
  288. ASSERT_FLOAT_EQ(3.1415f, x.getValue());
  289. }
  290. TEST_F(FloatTest, setEpsilon)
  291. {
  292. ls_std::Float x{};
  293. x.setEpsilon(0.01f);
  294. ASSERT_FLOAT_EQ(0.01f, x.getEpsilon());
  295. }
  296. TEST_F(FloatTest, setEpsilon_invalid_value)
  297. {
  298. ls_std::Float x{};
  299. EXPECT_THROW({
  300. try
  301. {
  302. x.setEpsilon(0.0f);
  303. }
  304. catch (const ls_std::IllegalArgumentException &_exception)
  305. {
  306. throw;
  307. }
  308. }, ls_std::IllegalArgumentException);
  309. }
  310. TEST_F(FloatTest, setSerializable_no_reference)
  311. {
  312. ls_std::Float x{};
  313. EXPECT_THROW({
  314. try
  315. {
  316. x.setSerializable(nullptr);
  317. }
  318. catch (const ls_std::IllegalArgumentException &_exception)
  319. {
  320. throw;
  321. }
  322. }, ls_std::IllegalArgumentException);
  323. }
  324. TEST_F(FloatTest, setStorable_no_reference)
  325. {
  326. ls_std::Float x{};
  327. EXPECT_THROW({
  328. try
  329. {
  330. x.setStorable(nullptr);
  331. }
  332. catch (const ls_std::IllegalArgumentException &_exception)
  333. {
  334. throw;
  335. }
  336. }, ls_std::IllegalArgumentException);
  337. }
  338. }