浏览代码

Add ctkCoreTestingMacros module

Adapated from Slicer/Libs/MRML/Core/vtkMRMLCoreTestingMacros
Jean-Christophe Fillion-Robin 9 年之前
父节点
当前提交
e99254dfe5

+ 2 - 0
Libs/Core/Testing/Cpp/CMakeLists.txt

@@ -26,6 +26,7 @@ set(KITTests_SRCS
   ctkBooleanMapperTest.cpp
   ctkCallbackTest1.cpp
   ctkCommandLineParserTest1.cpp
+  ctkCoreTestingMacrosTest.cpp
   ctkCoreTestingUtilitiesTest.cpp
   ctkExceptionTest.cpp
   ctkFileLoggerTest.cpp
@@ -143,6 +144,7 @@ endif()
 SIMPLE_TEST( ctkBooleanMapperTest )
 SIMPLE_TEST( ctkCallbackTest1 )
 SIMPLE_TEST( ctkCommandLineParserTest1 )
+SIMPLE_TEST( ctkCoreTestingMacrosTest )
 SIMPLE_TEST( ctkCoreTestingUtilitiesTest )
 SIMPLE_TEST( ctkDependencyGraphTest1 )
 SIMPLE_TEST( ctkDependencyGraphTest2 )

+ 337 - 0
Libs/Core/Testing/Cpp/ctkCoreTestingMacrosTest.cpp

@@ -0,0 +1,337 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0.txt
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+// CTK includes
+#include "ctkCoreTestingMacros.h"
+
+using namespace ctkCoreTestingUtilities;
+
+//----------------------------------------------------------------------------
+int TestCheckNullSuccess();
+int TestCheckNullFailure();
+
+int TestCheckNotNullSuccess();
+int TestCheckNotNullFailure();
+
+int TestCheckExitSuccessSuccess();
+int TestCheckExitSuccessFailure();
+
+int TestCheckIntSuccess();
+int TestCheckIntFailure();
+
+int TestCheckPointerSuccess();
+int TestCheckPointerFailure();
+
+int TestCheckPointerDifferentSuccess();
+int TestCheckPointerDifferentFailure();
+
+int TestCheckBoolSuccess();
+int TestCheckBoolFailure();
+
+int TestCheckStringSuccess();
+int TestCheckStringFailure();
+
+int TestCheckStdStringSuccess();
+int TestCheckStdStringFailure();
+
+int TestCheckQStringSuccess();
+int TestCheckQStringFailure();
+
+int TestCheckStdStringDifferentSuccess();
+int TestCheckStdStringDifferentFailure();
+
+int TestCheckQStringDifferentSuccess();
+int TestCheckQStringDifferentFailure();
+
+//----------------------------------------------------------------------------
+#define TestMacro(MACRO_NAME) \
+  if (Test##MACRO_NAME##Success() != EXIT_SUCCESS) \
+    { \
+    return EXIT_FAILURE; \
+    } \
+  if (Test##MACRO_NAME##Failure() != EXIT_FAILURE) \
+    { \
+    return EXIT_FAILURE; \
+    }
+
+//----------------------------------------------------------------------------
+int ctkCoreTestingMacrosTest(int , char * [])
+{
+  TestMacro(CheckNull)
+  TestMacro(CheckNotNull)
+  TestMacro(CheckExitSuccess)
+  TestMacro(CheckInt)
+  TestMacro(CheckPointer)
+  TestMacro(CheckPointerDifferent)
+  TestMacro(CheckBool)
+  TestMacro(CheckString)
+  TestMacro(CheckStdString)
+  TestMacro(CheckQString)
+  TestMacro(CheckStdStringDifferent)
+  TestMacro(CheckQStringDifferent)
+  return EXIT_SUCCESS;
+}
+
+//----------------------------------------------------------------------------
+// Test CHECK_NULL
+
+//----------------------------------------------------------------------------
+int TestCheckNullSuccess()
+{
+  void * nullPtr = 0;
+  CHECK_NULL(nullPtr);
+  return EXIT_SUCCESS;
+}
+
+//----------------------------------------------------------------------------
+int TestCheckNullFailure()
+{
+  int integer = 42;
+  void* notNullPtr = &integer;
+  CHECK_NULL(notNullPtr);
+  return EXIT_SUCCESS;
+}
+
+//----------------------------------------------------------------------------
+// Test CHECK_NOT_NULL
+
+//----------------------------------------------------------------------------
+int TestCheckNotNullSuccess()
+{
+  int integer = 42;
+  void* notNullPtr = &integer;
+  CHECK_NOT_NULL(notNullPtr);
+  return EXIT_SUCCESS;
+}
+
+//----------------------------------------------------------------------------
+int TestCheckNotNullFailure()
+{
+  void * nullPtr = 0;
+  CHECK_NOT_NULL(nullPtr);
+  return EXIT_SUCCESS;
+}
+
+//----------------------------------------------------------------------------
+// Test CHECK_EXIT_SUCCESS
+
+//----------------------------------------------------------------------------
+int TestCheckExitSuccessSuccess()
+{
+  int status = EXIT_SUCCESS;
+  CHECK_EXIT_SUCCESS(status);
+  return EXIT_SUCCESS;
+}
+
+//----------------------------------------------------------------------------
+int TestCheckExitSuccessFailure()
+{
+  int status = EXIT_FAILURE;
+  CHECK_EXIT_SUCCESS(status);
+  return EXIT_SUCCESS;
+}
+
+//----------------------------------------------------------------------------
+// Test CHECK_INT
+
+//----------------------------------------------------------------------------
+int TestCheckIntSuccess()
+{
+  CHECK_INT(4, 4);
+  return EXIT_SUCCESS;
+}
+
+//----------------------------------------------------------------------------
+int TestCheckIntFailure()
+{
+  CHECK_INT(4, 2);
+  return EXIT_SUCCESS;
+}
+
+//----------------------------------------------------------------------------
+// Test CHECK_POINTER
+
+//----------------------------------------------------------------------------
+int TestCheckPointerSuccess()
+{
+  int integer = 42;
+  void* actual = &integer;
+  void* expected = &integer;
+  CHECK_POINTER(actual, expected);
+  return EXIT_SUCCESS;
+}
+
+//----------------------------------------------------------------------------
+int TestCheckPointerFailure()
+{
+  int integer = 42;
+  void* actual = &integer;
+  int integer2 = 42;
+  void* expected = &integer2;
+  CHECK_POINTER(actual, expected);
+  return EXIT_SUCCESS;
+}
+
+//----------------------------------------------------------------------------
+// Test CHECK_POINTER_DIFFERENT
+
+//----------------------------------------------------------------------------
+int TestCheckPointerDifferentSuccess()
+{
+  int integer = 42;
+  void* actual = &integer;
+  int integer2 = 42;
+  void* expected = &integer2;
+  CHECK_POINTER_DIFFERENT(actual, expected);
+  return EXIT_SUCCESS;
+}
+
+//----------------------------------------------------------------------------
+int TestCheckPointerDifferentFailure()
+{
+  int integer = 42;
+  void* actual = &integer;
+  void* expected = &integer;
+  CHECK_POINTER_DIFFERENT(actual, expected);
+  return EXIT_SUCCESS;
+}
+
+//----------------------------------------------------------------------------
+// Test CHECK_BOOL
+
+//----------------------------------------------------------------------------
+int TestCheckBoolSuccess()
+{
+  CHECK_BOOL(true, true);
+  return EXIT_SUCCESS;
+}
+
+//----------------------------------------------------------------------------
+int TestCheckBoolFailure()
+{
+  CHECK_BOOL(true, false);
+  return EXIT_SUCCESS;
+}
+
+//----------------------------------------------------------------------------
+// Test CHECK_STRING
+
+//----------------------------------------------------------------------------
+int TestCheckStringSuccess()
+{
+  const char* actual = "string";
+  const char* expected = "string";
+  CHECK_STRING(actual, expected);
+  return EXIT_SUCCESS;
+}
+
+//----------------------------------------------------------------------------
+int TestCheckStringFailure()
+{
+  const char* actual = "string";
+  const char* expected = "string2";
+  CHECK_STRING(actual, expected);
+  return EXIT_SUCCESS;
+}
+
+//----------------------------------------------------------------------------
+// Test CHECK_STD_STRING
+
+//----------------------------------------------------------------------------
+int TestCheckStdStringSuccess()
+{
+  std::string actual = "string";
+  std::string expected = "string";
+  CHECK_STD_STRING(actual, expected);
+  return EXIT_SUCCESS;
+}
+
+//----------------------------------------------------------------------------
+int TestCheckStdStringFailure()
+{
+  std::string actual = "string";
+  std::string expected = "string2";
+  CHECK_STD_STRING(actual, expected);
+  return EXIT_SUCCESS;
+}
+
+//----------------------------------------------------------------------------
+// Test CHECK_QSTRING
+
+//----------------------------------------------------------------------------
+int TestCheckQStringSuccess()
+{
+  QString actual = "string";
+  QString expected = "string";
+  CHECK_QSTRING(actual, expected);
+  return EXIT_SUCCESS;
+}
+
+//----------------------------------------------------------------------------
+int TestCheckQStringFailure()
+{
+  QString actual = "string";
+  QString expected = "string2";
+  CHECK_QSTRING(actual, expected);
+  return EXIT_SUCCESS;
+}
+
+//----------------------------------------------------------------------------
+// Test CHECK_STD_STRING_DIFFERENT
+
+//----------------------------------------------------------------------------
+int TestCheckStdStringDifferentSuccess()
+{
+  std::string actual = "string";
+  std::string expected = "string2";
+  CHECK_STD_STRING_DIFFERENT(actual, expected);
+  return EXIT_SUCCESS;
+}
+
+//----------------------------------------------------------------------------
+int TestCheckStdStringDifferentFailure()
+{
+  std::string actual = "string";
+  std::string expected = "string";
+  CHECK_STD_STRING_DIFFERENT(actual, expected);
+  return EXIT_SUCCESS;
+}
+
+//----------------------------------------------------------------------------
+// Test CHECK_QSTRING_DIFFERENT
+
+//----------------------------------------------------------------------------
+int TestCheckQStringDifferentSuccess()
+{
+  QString actual = "string";
+  QString expected = "string2";
+  CHECK_QSTRING_DIFFERENT(actual, expected);
+  return EXIT_SUCCESS;
+}
+
+//----------------------------------------------------------------------------
+int TestCheckQStringDifferentFailure()
+{
+  QString actual = "string";
+  QString expected = "string";
+  CHECK_QSTRING_DIFFERENT(actual, expected);
+  return EXIT_SUCCESS;
+}
+

+ 187 - 0
Libs/Core/ctkCoreTestingMacros.h

@@ -0,0 +1,187 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0.txt
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+#ifndef __ctkCoreTestingMacros_h
+#define __ctkCoreTestingMacros_h
+
+#include "ctkCoreTestingUtilities.h"
+
+/// Convenience macros for unit tests.
+///
+/// The macro returns from the current method with EXIT_FAILURE if the check fails.
+/// Expressions can be passed as arguments, they are guaranteed to be executed only once.
+///
+/// Example:
+///
+/// \code{.cpp}
+/// int testedFunction(int a, int b) { return a+b; }
+///
+/// int MyTest1(int , char * [])
+/// {
+///
+///   int current = 40 + 2;
+///   int expected = 42;
+///   CHECK_INT(current, expected);
+///
+///   CHECK_INT(testedFunction(40,2), 42);
+///   CHECK_INT(testedFunction(35,5), 40);
+///
+///   return EXIT_SUCCESS;
+/// }
+///
+/// \endcode
+
+/// Verifies that pointer is NULL
+#define CHECK_NULL(pointer) \
+  { \
+  const void* pointerValue = (pointer); \
+  if (!ctkCoreTestingUtilities::CheckNull(__LINE__,#pointer " is not NULL", pointerValue)) \
+    { \
+    return EXIT_FAILURE; \
+    } \
+  }
+
+/// Verifies that pointer is not NULL
+#define CHECK_NOT_NULL(pointer) \
+  { \
+  if (!ctkCoreTestingUtilities::CheckNotNull(__LINE__,#pointer " is NULL", (pointer))) \
+    { \
+    return EXIT_FAILURE; \
+    } \
+  }
+
+#define CHECK_EXIT_SUCCESS(actual) \
+  { \
+  if (!ctkCoreTestingUtilities::CheckInt(__LINE__,#actual " != EXIT_SUCCESS", (actual), EXIT_SUCCESS)) \
+    { \
+    return EXIT_FAILURE; \
+    } \
+  }
+
+/// Verifies if actual int value is the same as expected
+#define CHECK_INT(actual, expected) \
+  { \
+  if (!ctkCoreTestingUtilities::CheckInt(__LINE__,#actual " != " #expected, (actual), (expected))) \
+    { \
+    return EXIT_FAILURE; \
+    } \
+  }
+
+/// Verifies if actual pointer value is the same as expected
+#define CHECK_POINTER(actual, expected) \
+  { \
+  if (!ctkCoreTestingUtilities::CheckPointer(__LINE__,#actual " != " #expected, (actual), (expected))) \
+    { \
+    return EXIT_FAILURE; \
+    } \
+  }
+
+/// Verifies if actual pointer value is the same as expected
+#define CHECK_POINTER_DIFFERENT(actual, expected) \
+  { \
+  if (!ctkCoreTestingUtilities::CheckPointer(__LINE__,#actual " == " #expected, (actual), (expected), false)) \
+    { \
+    return EXIT_FAILURE; \
+    } \
+  }
+
+/// Verifies if actual bool value is the same as expected
+#define CHECK_BOOL(actual, expected) \
+  { \
+  if (!ctkCoreTestingUtilities::CheckInt(__LINE__,#actual " != " #expected, (actual)?1:0, (expected)?1:0)) \
+    { \
+    return EXIT_FAILURE; \
+    } \
+  }
+
+/// Verifies if actual const char* value is the same as expected.
+/// It can handle NULL pointer inputs.
+#define CHECK_STRING(actual, expected) \
+  { \
+  if (!ctkCoreTestingUtilities::CheckString(__LINE__,#actual " != " #expected, (actual), (expected))) \
+    { \
+    return EXIT_FAILURE; \
+    } \
+  }
+
+/// Verifies if actual std::string value is the same as expected.
+/// It is safe to use for comparing std::string values.
+/// It cannot handle NULL pointer inputs.
+#define CHECK_STD_STRING(actual, expected) \
+  { \
+  std::string a = (actual); \
+  std::string e = (expected); \
+  if (!ctkCoreTestingUtilities::CheckString(__LINE__,#actual " != " #expected, a.c_str(), e.c_str())) \
+    { \
+    return EXIT_FAILURE; \
+    } \
+  }
+
+/// Verifies if actual QString value is the same as expected.
+/// It is safe to use for comparing QString values.
+/// It cannot handle NULL pointer inputs.
+#define CHECK_QSTRING(actual, expected) \
+  { \
+  QString a = (actual); \
+  QString e = (expected); \
+  if (!ctkCoreTestingUtilities::CheckString(__LINE__,#actual " != " #expected, qPrintable(a), qPrintable(e))) \
+    { \
+    return EXIT_FAILURE; \
+    } \
+  }
+
+/// Verifies if actual const char* value is not the same as expected.
+/// It can handle NULL pointer inputs.
+#define CHECK_STRING_DIFFERENT(actual, expected) \
+  { \
+  if (!ctkCoreTestingUtilities::CheckString(__LINE__,#actual " != " #expected, (actual), (expected), false)) \
+    { \
+    return EXIT_FAILURE; \
+    } \
+  }
+
+/// Verifies if actual std::string value is not the same as expected.
+/// It is safe to use for comparing std::string values.
+/// It cannot handle NULL pointer inputs.
+#define CHECK_STD_STRING_DIFFERENT(actual, expected) \
+  { \
+  std::string a = (actual); \
+  std::string e = (expected); \
+  if (!ctkCoreTestingUtilities::CheckString(__LINE__,#actual " != " #expected, a.c_str(), e.c_str(), false)) \
+    { \
+    return EXIT_FAILURE; \
+    } \
+  }
+
+/// Verifies if actual QString value is not the same as expected.
+/// It is safe to use for comparing QString values.
+/// It cannot handle NULL pointer inputs.
+#define CHECK_QSTRING_DIFFERENT(actual, expected) \
+  { \
+  QString a = (actual); \
+  QString e = (expected); \
+  if (!ctkCoreTestingUtilities::CheckString(__LINE__,#actual " != " #expected, qPrintable(a), qPrintable(e), false)) \
+    { \
+    return EXIT_FAILURE; \
+    } \
+  }
+
+#endif
+

+ 2 - 0
Libs/Core/ctkCoreTestingUtilities.h

@@ -43,6 +43,8 @@
 ///   }
 /// \endcode
 ///
+/// Usually these test methods are used by single-line convenience macros
+/// defined in ctkCoreTestingMacros.h.
 
 namespace ctkCoreTestingUtilities
 {