ctkDoubleSpinBoxValueProxyTest.cpp 12 KB

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