ctkCoordinatesWidgetValueProxyTest.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. // CTK includes
  17. #include "ctkCoordinatesWidget.h"
  18. #include "ctkLinearValueProxy.h"
  19. #include "ctkTest.h"
  20. #include "ctkValueProxy.h"
  21. namespace
  22. {
  23. class Spy : public QObject
  24. {
  25. Q_OBJECT
  26. public:
  27. explicit Spy()
  28. {
  29. AcknowledgedSignals = 0;
  30. }
  31. void getSpyReport(QString coordinatesString)
  32. {
  33. QCOMPARE(AcknowledgedSignals, 1);
  34. AcknowledgedSignals = 0;
  35. QStringList coordinatesList = coordinatesString.split(',');
  36. QCOMPARE(coordinatesList.count(), this->Coordinates.size());
  37. for (int i = 0; i < this->Coordinates.size(); ++i)
  38. {
  39. QCOMPARE(this->Coordinates[i], coordinatesList[i].toDouble());
  40. }
  41. this->Coordinates.clear();
  42. };
  43. public slots:
  44. void onCoordinatesChanged(double* coordinates)
  45. {
  46. ctkCoordinatesWidget* coordWidget =
  47. qobject_cast<ctkCoordinatesWidget*>(this->sender());
  48. QVERIFY(coordWidget != 0);
  49. for (int i = 0; i < coordWidget->dimension(); ++i)
  50. {
  51. this->Coordinates.append(coordinates[i]);
  52. }
  53. ++AcknowledgedSignals;
  54. }
  55. public:
  56. QList<double> Coordinates;
  57. int AcknowledgedSignals;
  58. };
  59. // ----------------------------------------------------------------------------
  60. QString coordinatesFromValue(double val)
  61. {
  62. return QString("%1,%1,%1").arg(val);
  63. }
  64. } // end namespace
  65. // ----------------------------------------------------------------------------
  66. class ctkCoordinatesWidgetValueProxyTester: public QObject
  67. {
  68. Q_OBJECT
  69. private slots:
  70. void testSetValue();
  71. void testSetValue_data();
  72. void testPrecision();
  73. void testPrecision_data();
  74. };
  75. //-----------------------------------------------------------------------------
  76. void ctkCoordinatesWidgetValueProxyTester::testSetValue()
  77. {
  78. ctkCoordinatesWidget coordinatesWidget;
  79. coordinatesWidget.setMinimum(-200);
  80. coordinatesWidget.setMaximum(200);
  81. coordinatesWidget.setCoordinatesAsString("13.2, 13.2, 13.2");
  82. QFETCH(double, coefficient);
  83. QFETCH(double, offset);
  84. ctkLinearValueProxy proxy;
  85. proxy.setCoefficient(coefficient);
  86. proxy.setOffset(offset);
  87. coordinatesWidget.setValueProxy(&proxy);
  88. // Spy
  89. Spy valueSpy;
  90. QObject::connect(&coordinatesWidget, SIGNAL(coordinatesChanged(double*)),
  91. &valueSpy, SLOT(onCoordinatesChanged(double*)));
  92. // Test
  93. QFETCH(QString, coordinates);
  94. coordinatesWidget.setCoordinatesAsString(coordinates);
  95. QFETCH(QString, expectedCoordinates);
  96. valueSpy.getSpyReport(expectedCoordinates);
  97. QCOMPARE(coordinatesWidget.coordinatesAsString(), expectedCoordinates);
  98. }
  99. //-----------------------------------------------------------------------------
  100. void ctkCoordinatesWidgetValueProxyTester::testSetValue_data()
  101. {
  102. QTest::addColumn<double>("coefficient");
  103. QTest::addColumn<double>("offset");
  104. QTest::addColumn<QString>("coordinates");
  105. QTest::addColumn<QString>("expectedCoordinates");
  106. //---------------------------------------------------------------------------
  107. // Offset
  108. QTest::newRow("Offset only") << 1. << 42.19 << "0.1,0.2,0.3"
  109. << "0.1,0.2,0.3";
  110. QTest::newRow("Offset only: less than min")
  111. << 1. << 42.19 << "-250.,-900.,-3000." << "-200,-200,-200";
  112. QTest::newRow("Offset only: less than min but ok with offset")
  113. << 1. << 42.19 << "-240.3,-232.1,-200.01" << "-200,-200,-200";
  114. QTest::newRow("Offset only: less than min with offset")
  115. << 1. << -42.19 << "-160.15,-199.99,-159." << "-160.15,-199.99,-159";
  116. QTest::newRow("Offset only: more than max with offset")
  117. << 1. << 42.19 << "160.,199.9,163.32" << "160,199.9,163.32";
  118. QTest::newRow("Offset only: more than max")
  119. << 1. << -42.19 << "4830.,250.01,1e6" << "200,200,200";
  120. QTest::newRow("Offset only: less than max but ok with offset")
  121. << 1. << -42.19 << "210.3,200.01,241.03" << "200,200,200";
  122. //---------------------------------------------------------------------------
  123. // Coefficient
  124. QTest::newRow("Coeff only") << 5.0 << 0.0 << "0.1,0.2,0.3"
  125. << "0.1,0.2,0.3";
  126. QTest::newRow("Coeff only: less than min")
  127. << 5. << 0. << "-510.08,-2000,-1000000." << "-200,-200,-200";
  128. QTest::newRow("Coeff only: less than min but ok with coeff")
  129. << 0.5 << 0. << "-250.08,-399.99,-120" << "-200,-200,-120";
  130. QTest::newRow("Coeff only: less than min with coeff")
  131. << 5. << 0.<< "-42.08,-199.99,-40.01" << "-42.08,-199.99,-40.01";
  132. QTest::newRow("Coeff only: more than max with coeff")
  133. << 5. << 0. << "160.08,40.01,199.99" << "160.08,40.01,199.99";
  134. QTest::newRow("Coeff only: more than max")
  135. << 5. << 0. << "510.08,2000,1000000." << "200,200,200";
  136. QTest::newRow("Offset only: more than max but ok with coeff")
  137. << 0.5 << 0. << "380.08,399.99,200.01" << "200,200,200";
  138. //---------------------------------------------------------------------------
  139. // Linear
  140. QTest::newRow("Linear") << 5.0 << 12.0 << "0.1,0.2,0.3"
  141. << "0.1,0.2,0.3";
  142. QTest::newRow("Linear: less than min")
  143. << 5.0 << 12.0 << "-510.08,-2000,-1000000." << "-200,-200,-200";
  144. QTest::newRow("Linear: less than min but ok with function")
  145. << 0.5 << 12.0 << "-250.08,-411.99,-120" << "-200,-200,-120";
  146. QTest::newRow("Linear: less than min with function")
  147. << 5.0 << 12.0 << "-64.08,-199.99,-52.01" << "-64.08,-199.99,-52.01";
  148. QTest::newRow("Linear: more than max with function")
  149. << 5.0 << 12.0 << "64.08,189.99,37.61" << "64.08,189.99,37.61";
  150. QTest::newRow("Linear: more than max")
  151. << 5.0 << 12.0 << "200.01,900000.0,411.99" << "200,200,200";
  152. QTest::newRow("Linear: more than max but ok with function")
  153. << 0.5 << 12.0 << "209.01,356.9,350.9" << "200,200,200";
  154. }
  155. //-----------------------------------------------------------------------------
  156. void ctkCoordinatesWidgetValueProxyTester::testPrecision()
  157. {
  158. ctkCoordinatesWidget coordinatesWidget;
  159. coordinatesWidget.setDecimalsOption(ctkDoubleSpinBox::DecimalsByValue);
  160. double coordinates[3] = {0., 0., 0.};
  161. coordinatesWidget.setCoordinates(coordinates);
  162. coordinatesWidget.setDecimals(3);
  163. coordinatesWidget.setSingleStep(0.001);
  164. coordinatesWidget.setRange(-10000., 10000.);
  165. coordinatesWidget.setDecimals(3);
  166. coordinatesWidget.setSingleStep(0.001);
  167. coordinates[0] = 1.;
  168. coordinates[1] = 1.;
  169. coordinates[1] = 1.;
  170. // coordinatesWidget.setCoordinates(coordinates);
  171. QFETCH(double, coefficient);
  172. ctkLinearValueProxy proxy;
  173. proxy.setCoefficient(coefficient);
  174. coordinatesWidget.setValueProxy(&proxy);
  175. coordinatesWidget.setCoordinates(coordinates);
  176. coordinates[2] = 1.3;
  177. coordinatesWidget.setCoordinates(coordinates);
  178. const double* res = coordinatesWidget.coordinates();
  179. QCOMPARE(coordinates[0], res[0]);
  180. QCOMPARE(coordinates[1], res[1]);
  181. QCOMPARE(coordinates[2], res[2]);
  182. }
  183. //-----------------------------------------------------------------------------
  184. void ctkCoordinatesWidgetValueProxyTester::testPrecision_data()
  185. {
  186. QTest::addColumn<double>("coefficient");
  187. QTest::newRow("1000000.") << 1000000.;
  188. }
  189. // ----------------------------------------------------------------------------
  190. CTK_TEST_MAIN(ctkCoordinatesWidgetValueProxyTest)
  191. #include "moc_ctkCoordinatesWidgetValueProxyTest.cpp"