ctkCoreTestingUtilities.tpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 <QDebug>
  16. namespace ctkCoreTestingUtilities
  17. {
  18. //----------------------------------------------------------------------------
  19. template<typename TYPE>
  20. bool Check(int line, const QString& description,
  21. TYPE current, TYPE expected,
  22. const QString& _testName,
  23. bool errorIfDifferent = true)
  24. {
  25. QString testName = _testName.isEmpty() ? "Check" : _testName;
  26. if (errorIfDifferent)
  27. {
  28. if(current != expected)
  29. {
  30. qWarning() << "\nLine " << line << " - " << description
  31. << " : " << testName << " failed"
  32. << "\n\tcurrent :" << current
  33. << "\n\texpected:" << expected;
  34. return false;
  35. }
  36. }
  37. else
  38. {
  39. if(current == expected)
  40. {
  41. qWarning() << "\nLine " << line << " - " << description
  42. << " : " << testName << " failed"
  43. << "\n\tcurrent :" << current
  44. << "\n\texpected to be different from:" << expected;
  45. return false;
  46. }
  47. }
  48. return true;
  49. }
  50. //----------------------------------------------------------------------------
  51. template<typename TYPE>
  52. bool CheckList(int line, const QString& description,
  53. const QList<TYPE>& current, const QList<TYPE>& expected,
  54. const QString& testName)
  55. {
  56. QString msg;
  57. if (current.count() != expected.count())
  58. {
  59. qWarning() << "\nLine " << line << " - " << description
  60. << " : " << testName << " failed"
  61. << "\nCompared lists have different sizes."
  62. << "\n\tcurrent size :" << current.count()
  63. << "\n\texpected size:" << expected.count()
  64. << "\n\tcurrent:" << current
  65. << "\n\texpected:" << expected;
  66. return false;
  67. }
  68. for (int idx = 0; idx < current.count(); ++idx)
  69. {
  70. if (current.at(idx) != expected.at(idx))
  71. {
  72. qWarning() << "\nLine " << line << " - " << description
  73. << " : " << testName << " failed"
  74. << "\nCompared lists differ at index " << idx
  75. << "\n\tcurrent[" << idx << "] :" << current.at(idx)
  76. << "\n\texpected[" << idx << "]:" << expected.at(idx);
  77. return false;
  78. }
  79. }
  80. return true;
  81. }
  82. } // namespace ctkCoreTestingUtilities