ctkDoubleSliderValueProxyTest.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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.commontk.org/LICENSE
  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. // Qt includes
  15. #include <QApplication>
  16. #include <QTest>
  17. // CTK includes
  18. #include "ctkDoubleSlider.h"
  19. #include "ctkLinearValueProxy.h"
  20. #include "ctkTest.h"
  21. #include "ctkValueProxy.h"
  22. // STD includes
  23. #include <limits>
  24. namespace
  25. {
  26. //-----------------------------------------------------------------------------
  27. void getSpyReport(QSignalSpy& spy, double expectedValue)
  28. {
  29. QCOMPARE(spy.count(), 1);
  30. QList<QVariant> arguments = spy.takeFirst(); // take the first signal
  31. ctkTest::COMPARE(arguments.at(0).toDouble(), expectedValue);
  32. }
  33. } // end namespace
  34. //-----------------------------------------------------------------------------
  35. class ctkDoubleSliderValueProxyTester: public QObject
  36. {
  37. Q_OBJECT
  38. private slots:
  39. void testSetValue();
  40. void testSetValue_data();
  41. void testSetSliderPosition();
  42. void testSetSliderPosition_data();
  43. };
  44. //-----------------------------------------------------------------------------
  45. void ctkDoubleSliderValueProxyTester::testSetValue()
  46. {
  47. // Setup
  48. ctkDoubleSlider slider;
  49. slider.setMinimum(-200);
  50. slider.setMaximum(200);
  51. slider.setSingleStep(0.01);
  52. slider.setValue(-32.6);
  53. QFETCH(double, coefficient);
  54. QFETCH(double, offset);
  55. ctkLinearValueProxy proxy;
  56. proxy.setCoefficient(coefficient);
  57. proxy.setOffset(offset);
  58. slider.setValueProxy(&proxy);
  59. // Spy
  60. QSignalSpy valueSpy(&slider, SIGNAL(valueChanged(double)));
  61. // Test
  62. QFETCH(double, value);
  63. slider.setValue(value);
  64. QFETCH(double, expectedValue);
  65. getSpyReport(valueSpy, expectedValue);
  66. ctkTest::COMPARE(slider.value(), expectedValue);
  67. }
  68. //-----------------------------------------------------------------------------
  69. void ctkDoubleSliderValueProxyTester::testSetValue_data()
  70. {
  71. QTest::addColumn<double>("coefficient");
  72. QTest::addColumn<double>("offset");
  73. QTest::addColumn<double>("value");
  74. QTest::addColumn<double>("expectedValue");
  75. //---------------------------------------------------------------------------
  76. // Offset
  77. QTest::newRow("Offset only") << 1.0 << 42.19176 << 0.1 << 0.1;
  78. QTest::newRow("Offset only: less than min")
  79. << 1.0 << 42.19 << -510.0 << -242.19;
  80. QTest::newRow("Offset only: less than min but ok with offset")
  81. << 1.0 << 42.19 << -230.0 << -230.0;
  82. QTest::newRow("Offset only: less than min with offset")
  83. << 1.0 << -42.19 << -190.0 << -157.81;
  84. QTest::newRow("Offset only: more than max with offset")
  85. << 1.0 << 42.19 << 160.0 << 157.81;
  86. QTest::newRow("Offset only: more than max")
  87. << 1.0 << -42.19 << 65010.0 << 242.19;
  88. QTest::newRow("Offset only: less than max but ok with offset")
  89. << 1.0 << -42.19 << 229.1 << 229.1;
  90. QTest::newRow("Offset only: max")
  91. << 1.0 << 42.19 << std::numeric_limits<double>::max() << 157.81;
  92. QTest::newRow("Offset only: min")
  93. << 1.0 << 42.19 << -std::numeric_limits<double>::max() << -242.19;
  94. QTest::newRow("Offset only: infinity")
  95. << 1.0 << 42.19 << std::numeric_limits<double>::infinity() << 157.81;
  96. QTest::newRow("Offset only: - infinity")
  97. << 1.0 << 42.19 << -std::numeric_limits<double>::infinity() << -242.19;
  98. QTest::newRow("Offset only: Nan")
  99. << 1.0 << 42.19 << std::numeric_limits<double>::quiet_NaN() << -242.19;
  100. // coeff // offset // value // expectedValue
  101. //---------------------------------------------------------------------------
  102. // Coefficient
  103. QTest::newRow("Coeff only") << 5.0 << 0.0 << 0.1 << 0.1;
  104. QTest::newRow("Coeff only: less than min")
  105. << 5.0 << 0.0 << -510.0 << -40.0;
  106. QTest::newRow("Coeff only: less than min but ok with coeff")
  107. << 0.5 << 0.0 << -230.0 << -230.0;
  108. QTest::newRow("Coeff only: less than min with coeff")
  109. << 5.0 << 0.0 << -190.0 << -40.0;
  110. QTest::newRow("Coeff only: more than max with coeff")
  111. << 5.0 << 0.0 << 160.0 << 40.0;
  112. QTest::newRow("Coeff only: more than max")
  113. << 5.0 << 0.0 << 65010.0 << 40.0;
  114. QTest::newRow("Offset only: less than max but ok with coeff")
  115. << 0.5 << 0.0 << 229.2 << 229.2;
  116. QTest::newRow("Coeff only: max")
  117. << 5.0 << 0.0 << std::numeric_limits<double>::max() << 40.0;
  118. QTest::newRow("Coeff only: min")
  119. << 5.0 << 0.0 << -std::numeric_limits<double>::max() << -40.0;
  120. QTest::newRow("Coeff only: infinity")
  121. << 5.0 << 0.0 << std::numeric_limits<double>::infinity() << 40.0;
  122. QTest::newRow("Coeff only: - infinity")
  123. << 5.0 << 0.0 << -std::numeric_limits<double>::infinity() << -40.0;
  124. QTest::newRow("Coeff only: Nan")
  125. << 5.0 << 0.0 << std::numeric_limits<double>::quiet_NaN() << -40.0;
  126. // coeff // offset // value // expectedValue
  127. //---------------------------------------------------------------------------
  128. // Linear
  129. QTest::newRow("Linear") << 5.0 << 0.0 << 0.1 << 0.1;
  130. QTest::newRow("Linear: less than min")
  131. << 5.0 << 12.0 << -510.0 << -42.4;
  132. QTest::newRow("Linear: less than min but ok with function")
  133. << 0.5 << 12.0 << -230.0 << -230.0;
  134. QTest::newRow("Linear: less than min with function")
  135. << 5.0 << 12.0 << -61.5 << -42.4;
  136. QTest::newRow("Linear: more than max with function")
  137. << 5.0 << 12.0 << 160.0 << 37.6;
  138. QTest::newRow("Linear: more than max")
  139. << 5.0 << 12.0 << 65010.0 << 37.6;
  140. QTest::newRow("Offset only: less than max but ok with function")
  141. << 0.5 << 12.0 << 229.2 << 229.2;
  142. QTest::newRow("Linear: max")
  143. << 5.0 << 12.0 << std::numeric_limits<double>::max() << 37.6;
  144. QTest::newRow("Linear: min")
  145. << 5.0 << 12.0 << -std::numeric_limits<double>::max() << -42.4;
  146. QTest::newRow("Linear: infinity")
  147. << 5.0 << 12.0 << std::numeric_limits<double>::infinity() << 37.6;
  148. QTest::newRow("Linear: - infinity")
  149. << 5.0 << 12.0 << -std::numeric_limits<double>::infinity() << -42.4;
  150. QTest::newRow("Linear: Nan")
  151. << 5.0 << 12.0 << std::numeric_limits<double>::quiet_NaN() << -42.4;
  152. }
  153. //-----------------------------------------------------------------------------
  154. void ctkDoubleSliderValueProxyTester::testSetSliderPosition()
  155. {
  156. // Setup
  157. ctkDoubleSlider slider;
  158. slider.setMinimum(-200);
  159. slider.setMaximum(200);
  160. slider.setSingleStep(0.01);
  161. slider.setValue(-32.6);
  162. QFETCH(double, coefficient);
  163. QFETCH(double, offset);
  164. ctkLinearValueProxy proxy;
  165. proxy.setCoefficient(coefficient);
  166. proxy.setOffset(offset);
  167. slider.setValueProxy(&proxy);
  168. // Spy
  169. QSignalSpy valueSpy(&slider, SIGNAL(valueChanged(double)));
  170. // Test
  171. QFETCH(double, sliderPosition);
  172. slider.setSliderPosition(sliderPosition);
  173. QFETCH(double, expectedSliderPosition);
  174. QFETCH(double, expectedValue);
  175. getSpyReport(valueSpy, expectedValue);
  176. ctkTest::COMPARE(slider.value(), expectedValue);
  177. ctkTest::COMPARE(slider.sliderPosition(), expectedSliderPosition);
  178. }
  179. //-----------------------------------------------------------------------------
  180. void ctkDoubleSliderValueProxyTester::testSetSliderPosition_data()
  181. {
  182. QTest::addColumn<double>("coefficient");
  183. QTest::addColumn<double>("offset");
  184. QTest::addColumn<double>("sliderPosition");
  185. QTest::addColumn<double>("expectedValue");
  186. QTest::addColumn<double>("expectedSliderPosition");
  187. //---------------------------------------------------------------------------
  188. // Offset
  189. QTest::newRow("Offset only") << 1.0 << 42.19 << 0.1 << -42.09 << 0.1;
  190. QTest::newRow("Offset only: less than min")
  191. << 1.0 << 42.19 << -510.0 << -242.19 << -200.0;
  192. QTest::newRow("Offset only: more than max")
  193. << 1.0 << -42.19 << 65010.0 << 242.19 << 200.0;
  194. QTest::newRow("Offset only: max")
  195. << 1.0 << 42.19 << std::numeric_limits<double>::max()
  196. << 157.81 << 200.0;
  197. QTest::newRow("Offset only: min")
  198. << 1.0 << 42.19 << -std::numeric_limits<double>::max()
  199. << -242.19 << -200.0;
  200. QTest::newRow("Offset only: infinity")
  201. << 1.0 << 42.19 << std::numeric_limits<double>::infinity()
  202. << 157.81 << 200.0;
  203. QTest::newRow("Offset only: - infinity")
  204. << 1.0 << 42.19 << -std::numeric_limits<double>::infinity()
  205. << -242.19 << -200.0;
  206. QTest::newRow("Offset only: Nan")
  207. << 1.0 << 42.19 << std::numeric_limits<double>::quiet_NaN()
  208. << -242.19 << -200.0;
  209. //---------------------------------------------------------------------------
  210. // Coefficient
  211. QTest::newRow("Coeff only") << 5.0 << 0.0 << 5.0 << 1.0 << 5.0;
  212. QTest::newRow("Coeff only: less than min")
  213. << 5.0 << 0.0 << -1010.0 << -40.0 << -200.0;
  214. QTest::newRow("Coeff only: more than max")
  215. << 5.0 << 0.0 << 65010.0 << 40.0 << 200.0;
  216. QTest::newRow("Coeff only: max")
  217. << 5.0 << 0.0 << std::numeric_limits<double>::max() << 40.0 << 200.0;
  218. QTest::newRow("Coeff only: min")
  219. << 5.0 << 0.0 << -std::numeric_limits<double>::max() << -40.0 << -200.0;
  220. QTest::newRow("Coeff only: infinity")
  221. << 5.0 << 0.0 << std::numeric_limits<double>::infinity() << 40.0 << 200.0;
  222. QTest::newRow("Coeff only: - infinity")
  223. << 5.0 << 0.0 << -std::numeric_limits<double>::infinity()
  224. << -40.0 << -200.0;
  225. QTest::newRow("Coeff only: Nan")
  226. << 5.0 << 0.0 << std::numeric_limits<double>::quiet_NaN()
  227. << -40.0 << -200.0;
  228. //---------------------------------------------------------------------------
  229. // Linear
  230. QTest::newRow("Linear") << 5.0 << 12.0 << 42.0 << 6.0 << 42.0;
  231. QTest::newRow("Linear: less than min")
  232. << 5.0 << 12.0 << -5010.0 << -42.4 << -200.0;
  233. QTest::newRow("Linear: more than max")
  234. << 5.0 << 12.0 << 65010.0 << 37.6 << 200.0;
  235. QTest::newRow("Linear: max")
  236. << 5.0 << 12.0 << std::numeric_limits<double>::max() << 37.6 << 200.0;
  237. QTest::newRow("Linear: min")
  238. << 5.0 << 12.0 << -std::numeric_limits<double>::max() << -42.4 << -200.0;
  239. QTest::newRow("Linear: infinity")
  240. << 5.0 << 12.0 << std::numeric_limits<double>::infinity() << 37.6 << 200.0;
  241. QTest::newRow("Linear: - infinity")
  242. << 5.0 << 12.0 << -std::numeric_limits<double>::infinity()
  243. << -42.4 << -200.0;
  244. QTest::newRow("Linear: Nan")
  245. << 5.0 << 12.0 << std::numeric_limits<double>::quiet_NaN()
  246. << -42.4 << -200.0;
  247. }
  248. // ----------------------------------------------------------------------------
  249. CTK_TEST_MAIN(ctkDoubleSliderValueProxyTest)
  250. #include "moc_ctkDoubleSliderValueProxyTest.cpp"