ctkUtilsTest.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // CTK includes
  15. #include "ctkUtils.h"
  16. #include "ctkTest.h"
  17. // STD includes
  18. #include <iostream>
  19. #include <limits>
  20. // ----------------------------------------------------------------------------
  21. class ctkUtilsTester: public QObject
  22. {
  23. Q_OBJECT
  24. private slots:
  25. void initTestCase();
  26. void testOrderOfMagnitude();
  27. void testOrderOfMagnitude_data();
  28. void testClosestPowerOfTen();
  29. void testClosestPowerOfTen_data();
  30. void testSignificantDecimals();
  31. void testSignificantDecimals_data();
  32. };
  33. // ----------------------------------------------------------------------------
  34. void ctkUtilsTester::initTestCase()
  35. {
  36. std::cout.precision(16);
  37. std::cerr.precision(16);
  38. std::cout << std::fixed;
  39. std::cerr << std::fixed;
  40. }
  41. // ----------------------------------------------------------------------------
  42. void ctkUtilsTester::testOrderOfMagnitude()
  43. {
  44. QFETCH(double, value);
  45. QFETCH(int, expectedOrder);
  46. QCOMPARE(ctk::orderOfMagnitude(value), expectedOrder);
  47. }
  48. // ----------------------------------------------------------------------------
  49. void ctkUtilsTester::testOrderOfMagnitude_data()
  50. {
  51. QTest::addColumn<double>("value");
  52. QTest::addColumn<int>("expectedOrder");
  53. QTest::newRow("1. -> 0") << 1. << 0;
  54. QTest::newRow("2. -> 0") << 2. << 0;
  55. QTest::newRow("10. -> 1") << 10. << 1;
  56. QTest::newRow("11. -> 1") << 11. << 1;
  57. QTest::newRow("0.1 -> -1") << 0.1 << -1;
  58. QTest::newRow("0.11 -> -1") << 0.1 << -1;
  59. QTest::newRow("0.2 -> -1") << 0.2 << -1;
  60. QTest::newRow("0.01 -> -2") << 0.01 << -2;
  61. QTest::newRow("0.0000000001 -> -10") << 0.0000000001 << -10;
  62. QTest::newRow("10.0001 -> 1") << 10.0001 << 1;
  63. QTest::newRow("100000000001.0001 -> 11") << 100000000001.0001 << 11;
  64. QTest::newRow("0. -> min") << 0. << std::numeric_limits<int>::min();
  65. QTest::newRow("inf -> min") << std::numeric_limits<double>::infinity() << std::numeric_limits<int>::min();
  66. QTest::newRow("-inf -> min") << -std::numeric_limits<double>::infinity() << std::numeric_limits<int>::min();
  67. QTest::newRow("nan -> min") << std::numeric_limits<double>::quiet_NaN() << std::numeric_limits<int>::min();
  68. QTest::newRow("min -> min") << std::numeric_limits<double>::min() << std::numeric_limits<int>::min();
  69. QTest::newRow("max -> 308") << std::numeric_limits<double>::max() << 308;
  70. QTest::newRow("denorm -> min") << std::numeric_limits<double>::denorm_min() << std::numeric_limits<int>::min();
  71. }
  72. // ----------------------------------------------------------------------------
  73. void ctkUtilsTester::testClosestPowerOfTen()
  74. {
  75. QFETCH(double, value);
  76. QFETCH(double, expectedValue);
  77. QFETCH(int, compareMode);
  78. const double closestValue = ctk::closestPowerOfTen(value);
  79. switch (compareMode)
  80. {
  81. default:
  82. case 0:
  83. QVERIFY(closestValue == expectedValue);
  84. break;
  85. case 1:
  86. {
  87. const double epsilon = std::numeric_limits<double>::epsilon();
  88. QVERIFY( closestValue > expectedValue - epsilon );
  89. QVERIFY( closestValue < expectedValue + epsilon );
  90. break;
  91. }
  92. case 2:
  93. QVERIFY( closestValue != closestValue );
  94. break;
  95. }
  96. }
  97. // ----------------------------------------------------------------------------
  98. void ctkUtilsTester::testClosestPowerOfTen_data()
  99. {
  100. QTest::addColumn<double>("value");
  101. QTest::addColumn<double>("expectedValue");
  102. /// 0 exact compare
  103. /// 1 compare with epsilon
  104. /// 2 isNaN
  105. QTest::addColumn<int>("compareMode");
  106. QTest::newRow("1. -> 1.") << 1. << 1. << 0;
  107. QTest::newRow("2. -> 1.") << 2. << 1. << 0;
  108. QTest::newRow("10. -> 10.") << 10. << 10. << 0;
  109. QTest::newRow("45. -> 10.") << 45. << 10. << 0;
  110. QTest::newRow("98. -> 100.") << 98. << 100. << 0;
  111. QTest::newRow("50. -> 10.") << 50. << 10. << 0;
  112. QTest::newRow("-1234. -> -1000.") << -1234. << -1000. << 0;
  113. QTest::newRow("0.01 -> 0.01") << 0.01 << 0.01 << 1;
  114. QTest::newRow("0.00000000015 -> 0.0000000001")
  115. << 0.00000000015 << 0.0000000001 << 1;
  116. QTest::newRow("0.1 -> 0.1") << 0.1 << 0.1 << 1;
  117. QTest::newRow("0. -> 0.") << 0. << 0. << 0;
  118. QTest::newRow("inf -> inf") << std::numeric_limits<double>::infinity()
  119. << std::numeric_limits<double>::infinity()
  120. << 0;
  121. QTest::newRow("-inf -> -inf") << -std::numeric_limits<double>::infinity()
  122. << -std::numeric_limits<double>::infinity()
  123. << 0;
  124. QTest::newRow("nan -> nan") << std::numeric_limits<double>::quiet_NaN()
  125. << std::numeric_limits<double>::quiet_NaN()
  126. << 2;
  127. QTest::newRow("min -> min") << std::numeric_limits<double>::min()
  128. << std::numeric_limits<double>::min()
  129. << 0;
  130. //QTest::newRow("max -> max") << std::numeric_limits<double>::max()
  131. // << 1e+308
  132. // << 0;
  133. QTest::newRow("denorm -> denorm") << std::numeric_limits<double>::denorm_min()
  134. << std::numeric_limits<double>::denorm_min()
  135. << 0;
  136. }
  137. // ----------------------------------------------------------------------------
  138. void ctkUtilsTester::testSignificantDecimals()
  139. {
  140. QFETCH(double, value);
  141. QFETCH(int, defaultDecimals);
  142. QFETCH(int, expectedDecimals);
  143. QCOMPARE(ctk::significantDecimals(value, defaultDecimals), expectedDecimals);
  144. }
  145. // ----------------------------------------------------------------------------
  146. void ctkUtilsTester::testSignificantDecimals_data()
  147. {
  148. QTest::addColumn<double>("value");
  149. QTest::addColumn<int>("defaultDecimals");
  150. QTest::addColumn<int>("expectedDecimals");
  151. // Default decimals= -1
  152. QTest::newRow("123456 -> 0") << 123456. << -1 << 0;
  153. QTest::newRow("123456.1 -> 1") << 123456.1 << -1 << 1;
  154. QTest::newRow("123456.12 -> 2") << 123456.12 << -1 << 2;
  155. QTest::newRow("123456.123 -> 3") << 123456.123 << -1 << 3;
  156. QTest::newRow("123456.122 -> 3") << 123456.122 << -1 << 3;
  157. QTest::newRow("123456.1223 -> 4") << 123456.1223 << -1 << 4;
  158. QTest::newRow("123456.1234 -> 4") << 123456.1234 << -1 << 4;
  159. QTest::newRow("123456.0123 -> 4") << 123456.0123 << -1 << 4;
  160. QTest::newRow("123456.0012 -> 4") << 123456.0012 << -1 << 4;
  161. QTest::newRow("123456.001234 -> 6") << 123456.001234 << -1 << 6;
  162. QTest::newRow("123456.000123 -> 6") << 123456.000123 << -1 << 6;
  163. QTest::newRow("123456.0000 -> 0") << 123456.0000 << -1 << 0;
  164. QTest::newRow("123456.0001 -> 4") << 123456.0001 << -1 << 4;
  165. QTest::newRow("123456.3333333 -> 2") << 123456.3333333 << -1 << 2;
  166. QTest::newRow("123456.1333333 -> 3") << 123456.1333333 << -1 << 3;
  167. QTest::newRow("123456.3333334 -> 2") << 123456.3333334 << -1 << 2;
  168. QTest::newRow("123456.00122 -> 5") << 123456.00122 << -1 << 5;
  169. QTest::newRow("123456.00123 -> 5") << 123456.00123 << -1 << 5;
  170. // internally representated as 123456.001109999997425
  171. QTest::newRow("123456.00111 -> 5") << 123456.00111 << -1 << 5;
  172. // internally representated as 123456.270000000004075
  173. QTest::newRow("123456.26999999999999996 -> 2")
  174. << 123456.26999999999999996 << -1 << 2;
  175. QTest::newRow("123456.863899999999987 -> 4") << 123456.863899999999987 << -1 << 4;
  176. QTest::newRow("0.5 -> 1") << 0.5 << -1 << 1;
  177. QTest::newRow("0.25 -> 2") << 0.25 << -1 << 2;
  178. QTest::newRow("0.125 -> 3") << 0.125 << -1 << 3;
  179. QTest::newRow("0.1234567891013151 -> 16") << 0.1234567891013151 << -1 << 16;
  180. QTest::newRow("0. -> 0") << 0. << -1 << 0;
  181. QTest::newRow("inf -> 0") << std::numeric_limits<double>::infinity() << -1 << 0;
  182. QTest::newRow("-inf -> 0") << -std::numeric_limits<double>::infinity() << -1 << 0;
  183. QTest::newRow("nan -> -1") << std::numeric_limits<double>::quiet_NaN() << -1 << -1;
  184. QTest::newRow("min -> 16") << std::numeric_limits<double>::min() << -1 << 16;
  185. QTest::newRow("max -> 0") << std::numeric_limits<double>::max() << -1 << 0;
  186. QTest::newRow("denorm -> 16") << std::numeric_limits<double>::denorm_min()
  187. << -1 << 16;
  188. // Default decimals= 3
  189. QTest::newRow("123456 -> 0") << 123456. << 3 << 0;
  190. QTest::newRow("123456.1 -> 1") << 123456.1 << 3 << 1;
  191. QTest::newRow("123456.12 -> 2") << 123456.12 << 3 << 2;
  192. QTest::newRow("123456.123 -> 3") << 123456.123 << 3 << 3;
  193. QTest::newRow("123456.122 -> 3") << 123456.122 << 3 << 3;
  194. QTest::newRow("123456.1223 -> 4") << 123456.1223 << 3 << 4;
  195. QTest::newRow("123456.1234 -> 4") << 123456.1234 << 3 << 4;
  196. QTest::newRow("123456.0123 -> 4") << 123456.0123 << 3 << 4;
  197. QTest::newRow("123456.0012 -> 4") << 123456.0012 << 3 << 4;
  198. QTest::newRow("123456.001234 -> 6") << 123456.001234 << 3 << 6;
  199. QTest::newRow("123456.000123 -> 6") << 123456.000123 << 3 << 6;
  200. QTest::newRow("123456.0000 -> 0") << 123456.0000 << 3 << 0;
  201. QTest::newRow("123456.0001 -> 4") << 123456.0001 << 3 << 4;
  202. QTest::newRow("123456.3333333 -> 2") << 123456.3333333 << 3 << 2;
  203. QTest::newRow("123456.1333333 -> 3") << 123456.1333333 << 3 << 3;
  204. QTest::newRow("123456.3333334 -> 2") << 123456.3333334 << 3 << 2;
  205. QTest::newRow("123456.00122 -> 5") << 123456.00122 << 3 << 5;
  206. QTest::newRow("123456.00123 -> 5") << 123456.00123 << 3 << 5;
  207. // internally representated as 123456.001109999997425
  208. QTest::newRow("123456.00111 -> 5") << 123456.00111 << 3 << 5;
  209. // internally representated as 123456.270000000004075
  210. QTest::newRow("123456.26999999999999996 -> 2")
  211. << 123456.26999999999999996 << 3 << 2;
  212. QTest::newRow("123456.863899999999987 -> 4") << 123456.863899999999987 << 3 << 4;
  213. QTest::newRow("0.5 -> 1") << 0.5 << 3 << 1;
  214. QTest::newRow("0.25 -> 2") << 0.25 << 3 << 2;
  215. QTest::newRow("0.125 -> 3") << 0.125 << 3 << 3;
  216. QTest::newRow("0.1234567891013151 -> 16") << 0.1234567891013151 << 3 << 3;
  217. QTest::newRow("0. -> 0") << 0. << 3 << 0;
  218. QTest::newRow("inf -> 0") << std::numeric_limits<double>::infinity() << 3 << 0;
  219. QTest::newRow("-inf -> 0") << -std::numeric_limits<double>::infinity() << 3 << 0;
  220. QTest::newRow("nan -> -1") << std::numeric_limits<double>::quiet_NaN() << 3 << -1;
  221. QTest::newRow("min -> 3") << std::numeric_limits<double>::min() << 3 << 3;
  222. QTest::newRow("max -> 0") << std::numeric_limits<double>::max() << 3 << 0;
  223. QTest::newRow("denorm -> 3") << std::numeric_limits<double>::denorm_min()
  224. << 3 << 3;
  225. }
  226. // ----------------------------------------------------------------------------
  227. CTK_TEST_MAIN(ctkUtilsTest)
  228. #include "moc_ctkUtilsTest.cpp"