You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
4.0 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://sam.zoy.org/projects/COPYING.WTFPL for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <cmath>
  14. #include "core.h"
  15. #include "lol/unit.h"
  16. namespace lol
  17. {
  18. LOLUNIT_FIXTURE(RealTest)
  19. {
  20. LOLUNIT_TEST(FloatToReal)
  21. {
  22. float a1 = real(0.0f);
  23. float a2 = real(-0.0f);
  24. float a3 = real(1.0f);
  25. float a4 = real(-1.0f);
  26. float a5 = real(1.5f);
  27. float a6 = real(12345678.0f);
  28. LOLUNIT_ASSERT_EQUAL(a1, 0.0f);
  29. LOLUNIT_ASSERT_EQUAL(a2, -0.0f);
  30. LOLUNIT_ASSERT_EQUAL(a3, 1.0f);
  31. LOLUNIT_ASSERT_EQUAL(a4, -1.0f);
  32. LOLUNIT_ASSERT_EQUAL(a5, 1.5f);
  33. LOLUNIT_ASSERT_EQUAL(a6, 12345678.0f);
  34. }
  35. LOLUNIT_TEST(DoubleToReal)
  36. {
  37. double a1 = real(0.0);
  38. double a2 = real(-0.0);
  39. double a3 = real(1.0);
  40. double a4 = real(-1.0);
  41. double a5 = real(1.5);
  42. double a6 = real(1234567876543210.0);
  43. LOLUNIT_ASSERT_DOUBLES_EQUAL(a1, 0.0, 0.0);
  44. LOLUNIT_ASSERT_DOUBLES_EQUAL(a2, -0.0, 0.0);
  45. LOLUNIT_ASSERT_DOUBLES_EQUAL(a3, 1.0, 0.0);
  46. LOLUNIT_ASSERT_DOUBLES_EQUAL(a4, -1.0, 0.0);
  47. LOLUNIT_ASSERT_DOUBLES_EQUAL(a5, 1.5, 0.0);
  48. LOLUNIT_ASSERT_DOUBLES_EQUAL(a6, 1234567876543210.0, 0.0);
  49. }
  50. LOLUNIT_TEST(UnaryMinus)
  51. {
  52. float a1 = - real(1.0f);
  53. float a2 = - real(-1.0f);
  54. float a3 = - real(0.0f);
  55. float a4 = - real(-0.0f);
  56. LOLUNIT_ASSERT_EQUAL(a1, -1.0f);
  57. LOLUNIT_ASSERT_EQUAL(a2, 1.0f);
  58. LOLUNIT_ASSERT_EQUAL(a3, -0.0f);
  59. LOLUNIT_ASSERT_EQUAL(a4, 0.0f);
  60. }
  61. LOLUNIT_TEST(RealComparison)
  62. {
  63. LOLUNIT_ASSERT(real(1.0f) > real(0.5f));
  64. LOLUNIT_ASSERT(real(1.0f) >= real(0.5f));
  65. LOLUNIT_ASSERT(real(1.0f) >= real(1.0f));
  66. LOLUNIT_ASSERT(real(-1.0f) < real(-0.5f));
  67. LOLUNIT_ASSERT(real(-1.0f) <= real(-0.5f));
  68. LOLUNIT_ASSERT(real(-1.0f) <= real(-1.0f));
  69. LOLUNIT_ASSERT(real(-1.0f) < real(0.5f));
  70. LOLUNIT_ASSERT(real(-0.5f) < real(1.0f));
  71. LOLUNIT_ASSERT(real(-1.0f) <= real(0.5f));
  72. LOLUNIT_ASSERT(real(-0.5f) <= real(1.0f));
  73. LOLUNIT_ASSERT(real(1.0f) > real(-0.5f));
  74. LOLUNIT_ASSERT(real(0.5f) > real(-1.0f));
  75. LOLUNIT_ASSERT(real(1.0f) >= real(-0.5f));
  76. LOLUNIT_ASSERT(real(0.5f) >= real(-1.0f));
  77. }
  78. LOLUNIT_TEST(RealAddition)
  79. {
  80. float a1 = real(1.0f) + real(0.0f);
  81. float a2 = real(0.0f) + real(1.0f);
  82. float a3 = real(1.0f) + real(1.0f);
  83. float a4 = real(-1.0f) + real(-1.0f);
  84. float a5 = real(1.0f) + real(0.125f);
  85. LOLUNIT_ASSERT_EQUAL(a1, 1.0f);
  86. LOLUNIT_ASSERT_EQUAL(a2, 1.0f);
  87. LOLUNIT_ASSERT_EQUAL(a3, 2.0f);
  88. LOLUNIT_ASSERT_EQUAL(a4, -2.0f);
  89. LOLUNIT_ASSERT_EQUAL(a5, 1.125f);
  90. }
  91. LOLUNIT_TEST(RealSubtraction)
  92. {
  93. float a1 = real(1.0f) + real(1e20f) - real(1e20f);
  94. LOLUNIT_ASSERT_EQUAL(a1, 1.0f);
  95. }
  96. LOLUNIT_TEST(RealMultiplication)
  97. {
  98. real x(1.25f);
  99. real y(1.5f);
  100. real z(1.99999f);
  101. real w(-1.5f);
  102. float m1 = x * x;
  103. float m2 = y * y;
  104. float m3 = z * z;
  105. float m4 = w * w;
  106. LOLUNIT_ASSERT_EQUAL(m1, 1.25f * 1.25f);
  107. LOLUNIT_ASSERT_EQUAL(m2, 1.5f * 1.5f);
  108. LOLUNIT_ASSERT_EQUAL(m3, 1.99999f * 1.99999f);
  109. LOLUNIT_ASSERT_EQUAL(m4, -1.5f * -1.5f);
  110. }
  111. LOLUNIT_TEST(RealDivision)
  112. {
  113. real a1(1.0f);
  114. real a2(2.0f);
  115. float m1 = a1 / a1;
  116. float m2 = a2 / a1;
  117. float m3 = a1 / a2;
  118. float m4 = a2 / a2;
  119. LOLUNIT_ASSERT_EQUAL(m1, 1.0f);
  120. LOLUNIT_ASSERT_EQUAL(m2, 2.0f);
  121. LOLUNIT_ASSERT_EQUAL(m3, 0.5f);
  122. LOLUNIT_ASSERT_EQUAL(m4, 1.0f);
  123. }
  124. };
  125. } /* namespace lol */