Selaa lähdekoodia

Add ctkBinaryFileDescriptorTest

Jean-Christophe Fillion-Robin 14 vuotta sitten
vanhempi
commit
b0355c6eeb

+ 36 - 9
Libs/Core/Testing/Cpp/CMakeLists.txt

@@ -1,4 +1,4 @@
-# Dummy plugin
+# Dummy plugin used by ctkAbstractPluginFactoryTest1
 ADD_DEFINITIONS( -DCTKDummyPlugin)
 ctkMacroBuildLib(
   NAME "CTKDummyPlugin"
@@ -12,10 +12,14 @@ ctkMacroBuildLib(
 GET_TARGET_PROPERTY(ctkDummyPluginPATH "CTKDummyPlugin" LOCATION)
 REMOVE_DEFINITIONS(-DCTKDummyPlugin)
 
-#normal test
+
+
 SET(KIT ${PROJECT_NAME})
 
-CREATE_TEST_SOURCELIST(Tests ${KIT}CppTests.cpp
+#
+# Test sources
+#
+SET(KITTests_SRCS
   ctkAbstractFactoryTest1.cpp
   ctkAbstractLibraryFactoryTest1.cpp
   ctkAbstractObjectFactoryTest1.cpp
@@ -32,6 +36,16 @@ CREATE_TEST_SOURCELIST(Tests ${KIT}CppTests.cpp
   ctkWorkflowTest1.cpp
   ctkWorkflowTest2.cpp
   ctkWorkflowTest3.cpp
+  )
+
+IF(HAVE_BFD)
+  LIST(APPEND KITTests_SRCS
+    ctkBinaryFileDescriptorTest1.cpp
+    )
+ENDIF()
+
+CREATE_TEST_SOURCELIST(Tests ${KIT}CppTests.cpp
+  ${KITTests_SRCS}
   #EXTRA_INCLUDE TestingMacros.h
   )
 
@@ -40,7 +54,10 @@ REMOVE (TestsToRun ${KIT}CppTests.cpp)
 
 SET(LIBRARY_NAME ${PROJECT_NAME})
 
-SET(Tests_SRCS
+#
+# Tests Helpers sources
+#
+SET(Tests_Helpers_SRCS
   ctkBranchingWorkflowStep.h
   ctkExampleDerivedWorkflowStep.cpp
   ctkExampleDerivedWorkflowStep.h
@@ -50,15 +67,21 @@ SET(Tests_SRCS
   ctkSingletonTestHelper.h
   )
 
-SET(Tests_MOC_SRCS
+SET(Tests_Helpers_MOC_SRCS
   ctkExampleWorkflowStepUsingSignalsAndSlots.h
   )
 
-SET(Tests_MOC_CPP)
-QT4_WRAP_CPP(Tests_MOC_CPP ${Tests_MOC_SRCS})
+SET(Tests_Helpers_MOC_CPP)
+QT4_WRAP_CPP(Tests_Helpers_MOC_CPP ${Tests_Helpers_MOC_SRCS})
 
-# Test library
-ADD_EXECUTABLE(${KIT}CppTests ${Tests} ${Tests_SRCS} ${Tests_MOC_CPP})
+IF(HAVE_BFD)
+  ADD_EXECUTABLE(ctkBinaryFileDescriptorTestHelper ctkBinaryFileDescriptorTestHelper.cpp)
+ENDIF()
+
+#
+# Test executable
+#
+ADD_EXECUTABLE(${KIT}CppTests ${Tests} ${Tests_Helpers_SRCS} ${Tests_Helpers_MOC_CPP})
 TARGET_LINK_LIBRARIES(${KIT}CppTests ${LIBRARY_NAME} ${CTK_BASE_LIBRARIES} CTKDummyPlugin)
 
 SET( KIT_TESTS ${CPP_TEST_PATH}/${KIT}CppTests)
@@ -79,6 +102,10 @@ SIMPLE_TEST( ctkAbstractPluginFactoryTest1 "totto")
 ADD_TEST( ctkAbstractPluginFactoryTest1 ${KIT_TESTS} ctkAbstractPluginFactoryTest1 ${ctkDummyPluginPATH})
 SET_PROPERTY(TEST ctkAbstractPluginFactoryTest1 PROPERTY LABELS ${PROJECT_NAME})
 SIMPLE_TEST( ctkAbstractQObjectFactoryTest1 )
+IF(HAVE_BFD)
+  ADD_TEST(NAME ctkBinaryFileDescriptorTest1 COMMAND ${KIT_TESTS} ctkBinaryFileDescriptorTest1 $<TARGET_FILE:ctkBinaryFileDescriptorTestHelper>)
+  SET_PROPERTY(TEST ctkBinaryFileDescriptorTest1 PROPERTY LABELS ${PROJECT_NAME})
+ENDIF()
 SIMPLE_TEST( ctkCommandLineParserTest1 )
 SIMPLE_TEST( ctkDependencyGraphTest1 )
 SIMPLE_TEST( ctkDependencyGraphTest2 )

+ 120 - 0
Libs/Core/Testing/Cpp/ctkBinaryFileDescriptorTest1.cpp

@@ -0,0 +1,120 @@
+/*=========================================================================
+
+  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.commontk.org/LICENSE
+
+  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.
+
+=========================================================================*/
+
+// Qt includes
+#include <QFile>
+
+// CTK includes
+#include "ctkBinaryFileDescriptor.h"
+
+// STD includes
+#include <cstdlib>
+#include <iostream>
+
+//-----------------------------------------------------------------------------
+int ctkBinaryFileDescriptorTest1(int argc, char * argv[])
+{
+  if (argc <= 1)
+    {
+    std::cerr << "Missing argument" << std::endl;
+    return EXIT_FAILURE;
+    }
+  QString filePath(argv[1]);
+
+  if (!QFile::exists(filePath))
+    {
+    std::cerr << "Line " << __LINE__ << " - "
+              << "filePath [" << qPrintable(filePath)
+              << "] do *NOT* exist" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  ctkBinaryFileDescriptor bfd;
+  if (bfd.load())
+    {
+    // Should fail to load without any valid filename
+    std::cerr << "Line " << __LINE__ << " - "
+              << "Problem with load() method" << std::endl;
+    return EXIT_FAILURE;
+    }
+  if (!bfd.unload())
+    {
+    // Unload inconditionnally return True
+    std::cerr << "Line " << __LINE__ << " - "
+              << "Problem with unload() method" << std::endl;
+    return EXIT_FAILURE;
+    }
+  if (bfd.isLoaded())
+    {
+    std::cerr << "Line " << __LINE__ << " - "
+              << "Problem with isLoaded() method" << std::endl;
+    return EXIT_FAILURE;
+    }
+  if (!bfd.fileName().isEmpty())
+    {
+    std::cerr << "Line " << __LINE__ << " - "
+              << "Problem with fileName() method" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  bfd.setFileName(filePath);
+
+  if (bfd.fileName() != filePath)
+    {
+    std::cerr << "Line " << __LINE__ << " - "
+              << "Problem with fileName() method" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  if (!bfd.load())
+    {
+    // Should succeed since a valid filename is provided
+    std::cerr << "Line " << __LINE__ << " - "
+              << "Problem with load() method" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  void * main_pointer = bfd.resolve("main");
+  if (!main_pointer)
+    {
+    std::cerr << "Line " << __LINE__ << " - "
+              << "Problem with resolve() method - "
+              << "Failed to revolve 'main' symbol !" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  void * mtBlancElevationInMeters_pointer = bfd.resolve("MtBlancElevationInMeters");
+  int * mtBlancElevationInMeters = reinterpret_cast<int*>(mtBlancElevationInMeters_pointer);
+  if (!mtBlancElevationInMeters)
+    {
+    std::cerr << "Line " << __LINE__ << " - "
+              << "Failed to case 'mtBlancElevationInMeters' pointer !" << std::endl;
+    return EXIT_FAILURE;
+    }
+  if (*mtBlancElevationInMeters != 4810)
+    {
+    std::cerr << "Line " << __LINE__ << " - "
+              << "Failed to invoke function associated with symbol 'MtBlancElevationInMeters' !" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  return EXIT_SUCCESS;
+}
+

+ 22 - 0
Libs/Core/Testing/Cpp/ctkBinaryFileDescriptorTestHelper.cpp

@@ -0,0 +1,22 @@
+
+// STD includes
+#include <iostream>
+#include <cstdlib>
+
+#ifdef WIN32
+# define BFD_TEST_HELPER_EXPORT __declspec(dllexport)
+#else
+# define BFD_TEST_HELPER_EXPORT
+#endif
+
+extern "C" {
+
+  BFD_TEST_HELPER_EXPORT int MtBlancElevationInMeters = 4810;
+}
+
+int main(int /*argc*/, char* /*argv*/[])
+{
+  std::cout << "Mt Blanc elevation is " << MtBlancElevationInMeters
+            << " meters" << std::endl;
+  return EXIT_SUCCESS;
+}