diff --git a/Core/Code/Algorithms/mitkUIDGenerator.cpp b/Core/Code/Algorithms/mitkUIDGenerator.cpp index 8bc4ca6c37..38b9eb885c 100644 --- a/Core/Code/Algorithms/mitkUIDGenerator.cpp +++ b/Core/Code/Algorithms/mitkUIDGenerator.cpp @@ -1,83 +1,87 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include #include #include #include #include #include #include #include namespace mitk { UIDGenerator::UIDGenerator(const char* prefix, unsigned int lengthOfRandomPart) :m_Prefix(prefix), m_LengthOfRandomPart(lengthOfRandomPart) { if (lengthOfRandomPart < 5) { MITK_ERROR << lengthOfRandomPart << " are not really unique, right?" << std::endl; throw std::invalid_argument("To few digits requested"); } - std::srand((unsigned int) time( (time_t *)0 )); + static int instanceID = 0; + unsigned int seed = ((time( (time_t *)0 )) + getpid()) * 10 + instanceID++; + std::srand(seed); } std::string UIDGenerator::GetUID() { std::ostringstream s; s << m_Prefix; time_t tt = time(0); tm* t = gmtime(&tt); if (t) { + + s << t->tm_year + 1900; if (t->tm_mon < 9) s << "0"; // add a 0 for months 1 to 9 s << t->tm_mon + 1; if (t->tm_mday < 10) s << "0"; // add a 0 for days 1 to 9 s << t->tm_mday; if (t->tm_hour < 10) s << "0"; // add a 0 for hours 1 to 9 s << t->tm_hour; if (t->tm_min < 10) s << "0"; // add a 0 for minutes 1 to 9 s << t->tm_min; if (t->tm_sec < 10) s << "0"; // add a 0 for seconds 1 to 9 s << t->tm_sec; std::ostringstream rs; rs << (long int)( pow(10.0, double(m_LengthOfRandomPart)) / double(RAND_MAX) * double(rand()) ); for (size_t i = rs.str().length(); i < m_LengthOfRandomPart; ++i) { s << "X"; } s << rs.str(); } return s.str(); } } diff --git a/Core/Code/Testing/files.cmake b/Core/Code/Testing/files.cmake index 3efce83274..52a2412056 100644 --- a/Core/Code/Testing/files.cmake +++ b/Core/Code/Testing/files.cmake @@ -1,122 +1,123 @@ # tests with no extra command line parameter set(MODULE_TESTS mitkAccessByItkTest.cpp mitkCoreObjectFactoryTest.cpp mitkMaterialTest.cpp mitkActionTest.cpp mitkEnumerationPropertyTest.cpp mitkEventTest.cpp mitkFocusManagerTest.cpp mitkGenericPropertyTest.cpp mitkGeometry3DTest.cpp mitkGeometryDataToSurfaceFilterTest.cpp mitkGlobalInteractionTest.cpp mitkImageDataItemTest.cpp #mitkImageMapper2DTest.cpp mitkImageGeneratorTest.cpp mitkBaseDataTest.cpp #mitkImageToItkTest.cpp mitkInstantiateAccessFunctionTest.cpp mitkInteractorTest.cpp mitkITKThreadingTest.cpp # mitkLevelWindowManagerTest.cpp mitkLevelWindowTest.cpp mitkMessageTest.cpp #mitkPipelineSmartPointerCorrectnessTest.cpp mitkPixelTypeTest.cpp mitkPlaneGeometryTest.cpp mitkPointSetFileIOTest.cpp mitkPointSetTest.cpp mitkPointSetWriterTest.cpp mitkPointSetReaderTest.cpp mitkPointSetInteractorTest.cpp mitkPropertyTest.cpp mitkPropertyListTest.cpp #mitkRegistrationBaseTest.cpp #mitkSegmentationInterpolationTest.cpp mitkSlicedGeometry3DTest.cpp mitkSliceNavigationControllerTest.cpp mitkStateMachineTest.cpp mitkStateTest.cpp mitkSurfaceTest.cpp mitkSurfaceToSurfaceFilterTest.cpp mitkTimeSlicedGeometryTest.cpp mitkTransitionTest.cpp mitkUndoControllerTest.cpp mitkVtkWidgetRenderingTest.cpp mitkVerboseLimitedLinearUndoTest.cpp mitkWeakPointerTest.cpp mitkTransferFunctionTest.cpp #mitkAbstractTransformGeometryTest.cpp mitkStepperTest.cpp itkTotalVariationDenoisingImageFilterTest.cpp mitkRenderingManagerTest.cpp vtkMitkThickSlicesFilterTest.cpp mitkNodePredicateSourceTest.cpp mitkVectorTest.cpp mitkClippedSurfaceBoundsCalculatorTest.cpp #QmitkRenderingTestHelper.cpp mitkExceptionTest.cpp mitkExtractSliceFilterTest.cpp mitkLogTest.cpp mitkImageDimensionConverterTest.cpp mitkLoggingAdapterTest.cpp + mitkUIDGeneratorTest.cpp ) # test with image filename as an extra command line parameter set(MODULE_IMAGE_TESTS mitkPlanePositionManagerTest.cpp mitkSurfaceVtkWriterTest.cpp #mitkImageSliceSelectorTest.cpp mitkImageTimeSelectorTest.cpp # mitkVtkPropRendererTest.cpp mitkDataNodeFactoryTest.cpp #mitkSTLFileReaderTest.cpp mitkImageAccessorTest.cpp ) # list of images for which the tests are run set(MODULE_TESTIMAGES # Pic-Factory no more available in Core, test images now in .nrrd format US4DCyl.nrrd Pic3D.nrrd Pic2DplusT.nrrd BallBinary30x30x30.nrrd binary.stl ball.stl ) set(MODULE_CUSTOM_TESTS #mitkLabeledImageToSurfaceFilterTest.cpp #mitkExternalToolsTest.cpp mitkDataStorageTest.cpp mitkDataNodeTest.cpp mitkDicomSeriesReaderTest.cpp mitkDICOMLocaleTest.cpp mitkEventMapperTest.cpp mitkNodeDependentPointSetInteractorTest.cpp mitkStateMachineFactoryTest.cpp mitkPointSetLocaleTest.cpp mitkImageTest.cpp mitkImageWriterTest.cpp mitkImageVtkMapper2DTest.cpp mitkImageVtkMapper2DLevelWindowTest.cpp mitkImageVtkMapper2DOpacityTest.cpp mitkImageVtkMapper2DColorTest.cpp mitkImageVtkMapper2DSwivelTest.cpp mitkIOUtilTest.cpp mitkSurfaceVtkMapper3DTest mitkSurfaceVtkMapper3DTexturedSphereTest.cpp ) # Create an artificial module initializing class for # the usServiceListenerTest.cpp usFunctionGenerateModuleInit(testdriver_init_file NAME ${MODULE_NAME}TestDriver DEPENDS "Mitk" VERSION "0.1.0" EXECUTABLE ) set(TEST_CPP_FILES ${testdriver_init_file} mitkRenderingTestHelper.cpp) diff --git a/Core/Code/Testing/mitkUIDGeneratorTest.cpp b/Core/Code/Testing/mitkUIDGeneratorTest.cpp new file mode 100644 index 0000000000..bd0288cdd9 --- /dev/null +++ b/Core/Code/Testing/mitkUIDGeneratorTest.cpp @@ -0,0 +1,86 @@ +/*=================================================================== + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center, +Division of Medical and Biological Informatics. +All rights reserved. + +This software is distributed WITHOUT ANY WARRANTY; without +even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. + +See LICENSE.txt or http://www.mitk.org for details. + +===================================================================*/ + +#include "mitkUIDGenerator.h" +#include +#include + +void newGeneratorInstancesHeapTest() +{ + mitk::UIDGenerator* uidGen1 = new mitk::UIDGenerator("UID_",8); + mitk::UIDGenerator* uidGen2 = uidGen1; + std::string uid1_1, uid2_1; + + uid1_1 = uidGen1->GetUID(); + + uidGen1 = new mitk::UIDGenerator("UID_",8); + + uid2_1 = uidGen1->GetUID(); + + MITK_INFO << uid1_1; + MITK_INFO << uid2_1; + + delete uidGen1; + delete uidGen2; + + MITK_TEST_CONDITION(uid1_1 != uid2_1,"Different UIDs are not allowed to be equal"); + +} + + +void newGeneratorInstancesTest() +{ + mitk::UIDGenerator uidGen1("UID_",8); + std::string uid1_1, uid2_1; + + uid1_1 = uidGen1.GetUID(); + + uidGen1 = mitk::UIDGenerator("UID_",8); + + uid2_1 = uidGen1.GetUID(); + + MITK_INFO << uid1_1; + MITK_INFO << uid2_1; + + MITK_TEST_CONDITION(uid1_1 != uid2_1,"Different UIDs are not allowed to be equal"); + +} + +void severalGeneratorInstancesTest() +{ + mitk::UIDGenerator uidGen1("UID_",8); + mitk::UIDGenerator uidGen2("UID_",8); + std::string uid1_1, uid2_1; + + uid1_1 = uidGen1.GetUID(); + uid2_1 = uidGen2.GetUID(); + + MITK_INFO << uid1_1; + MITK_INFO << uid2_1; + + MITK_TEST_CONDITION(uid1_1 != uid2_1,"Different UIDs are not allowed to be equal"); + +} + +int mitkUIDGeneratorTest(int /*argc*/, char* /*argv*/[]) +{ + MITK_TEST_BEGIN("mitkUIDGeneratorTest"); + severalGeneratorInstancesTest(); + newGeneratorInstancesTest(); + newGeneratorInstancesHeapTest(); + + MITK_TEST_END(); +}