Skip to content
Snippets Groups Projects
Commit c25c57f5 authored by Neil Vaytet's avatar Neil Vaytet
Browse files

Refs #0 : Added tests for NexusIOHelper

parent 40182a1d
No related branches found
No related tags found
No related merge requests found
......@@ -119,7 +119,7 @@ template <typename NumT>
std::vector<NumT> readArray1DCoerce(H5::DataSet &dataset);
// Create a comparable version number as a single integer
uint32_t makeHdf5VersionNumber(uint32_t maj, uint32_t min, uint32_t relnum);
MANTID_DATAHANDLING_DLL uint32_t makeHdf5VersionNumber(uint32_t maj, uint32_t min, uint32_t relnum);
// Check if current version of hdf5 supports variable length strings
MANTID_DATAHANDLING_DLL bool checkVariableLengthStringSupport();
......
......@@ -708,6 +708,7 @@ bool checkVariableLengthStringSupport() {
const auto expected = makeHdf5VersionNumber(1, 8, 16); // Minimum expected
return (actual > expected);
}
} // namespace H5Util
} // namespace DataHandling
} // namespace Mantid
set ( SRC_FILES
src/MuonNexusReader.cpp
src/NexusClasses.cpp
src/NexusFileIO.cpp
src/MuonNexusReader.cpp
src/NexusClasses.cpp
src/NexusFileIO.cpp
)
set ( INC_FILES
inc/MantidNexus/MuonNexusReader.h
inc/MantidNexus/NexusClasses.h
inc/MantidNexus/NexusFileIO.h
inc/MantidNexus/NexusIOHelper.h
inc/MantidNexus/MuonNexusReader.h
inc/MantidNexus/NexusClasses.h
inc/MantidNexus/NexusFileIO.h
inc/MantidNexus/NexusIOHelper.h
)
set ( TEST_FILES
NexusIOHelperTest.h
)
if (COVERALLS)
foreach( loop_var ${SRC_FILES} ${INC_FILES})
set_property(GLOBAL APPEND PROPERTY COVERAGE_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/${loop_var}")
endforeach(loop_var)
foreach( loop_var ${SRC_FILES} ${INC_FILES})
set_property(GLOBAL APPEND PROPERTY COVERAGE_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/${loop_var}")
endforeach(loop_var)
endif()
add_definitions ( -DIN_NEXUS_CPP_LIBRARY )
......@@ -38,6 +42,9 @@ include_directories ( inc )
target_link_libraries ( Nexus LINK_PRIVATE ${TCMALLOC_LIBRARIES_LINKTIME} ${MANTIDLIBS} ${NEXUS_C_LIBRARIES} ${NEXUS_LIBRARIES} )
# Add the unit tests directory
add_subdirectory ( test )
###########################################################################
# Installation settings
###########################################################################
......
......@@ -84,6 +84,8 @@ template <typename T, typename U>
std::vector<T> readNexusAnyVector(::NeXus::File &file, size_t size,
bool close_file) {
if (sizeof(T) < sizeof(U)) {
if (close_file)
file.closeData();
throw std::runtime_error(
"Downcasting is forbidden in NeXusIOHelper::readNexusAnyVector");
} else if (std::is_same<T, U>::value) {
......@@ -108,6 +110,8 @@ std::vector<T>
readNexusAnySlab(::NeXus::File &file, const std::vector<int64_t> &start,
const std::vector<int64_t> &size, bool close_file) {
if (sizeof(T) < sizeof(U)) {
if (close_file)
file.closeData();
throw std::runtime_error(
"Downcasting is forbidden in NeXusIOHelper::readNexusAnySlab");
} else if (std::is_same<T, U>::value) {
......
if ( CXXTEST_FOUND )
include_directories ( SYSTEM ${CXXTEST_INCLUDE_DIR} )
cxxtest_add_test ( NexusTest ${TEST_FILES} )
target_include_directories( NexusTest SYSTEM PRIVATE ${HDF5_INCLUDE_DIRS} )
target_link_libraries( NexusTest LINK_PRIVATE ${TCMALLOC_LIBRARIES_LINKTIME} ${MANTIDLIBS}
Nexus
${NEXUS_LIBRARIES}
${HDF5_LIBRARIES}
${HDF5_HL_LIBRARIES} )
add_dependencies ( FrameworkTests NexusTest )
add_dependencies ( NexusTest StandardTestData )
# Add to the 'FrameworkTests' group in VS
set_property ( TARGET NexusTest PROPERTY FOLDER "UnitTests" )
endif ()
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright &copy; 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source
// & Institut Laue - Langevin
// SPDX - License - Identifier: GPL - 3.0 +
#ifndef NEXUSIOHELPERTEST_H_
#define NEXUSIOHELPERTEST_H_
#include "MantidAPI/FileFinder.h"
#include "MantidNexus/NexusIOHelper.h"
#include <nexus/NeXusFile.hpp>
#include <cxxtest/TestSuite.h>
using namespace Mantid;
using namespace Mantid::NeXus::NeXusIOHelper;
class NexusIOHelperTest : public CxxTest::TestSuite {
public:
void test_nexus_io_helper_readNexusVector() {
const std::string filename =
API::FileFinder::Instance().getFullPath("V20_ESS_example.nxs");
::NeXus::File file(filename);
file.openGroup("entry", "NXentry");
file.openGroup("raw_event_data", "NXevent_data");
auto event_index = readNexusVector<uint64_t>(file, "event_index");
TS_ASSERT_EQUALS(event_index.size(), 1439);
auto event_id = readNexusVector<uint64_t>(file, "event_id");
TS_ASSERT_EQUALS(event_index.size(), 1439);
auto event_time_offset = readNexusVector<float>(file, "event_time_offset");
TS_ASSERT_EQUALS(event_time_offset.size(), 1439);
auto event_time_zero = readNexusVector<double>(file, "event_time_zero");
TS_ASSERT_EQUALS(event_time_zero.size(), 1439);
file.closeGroup();
file.closeGroup();
}
void test_nexus_io_helper_readNexusVector_throws_when_downcasting() {
const std::string filename =
API::FileFinder::Instance().getFullPath("V20_ESS_example.nxs");
::NeXus::File file(filename);
file.openGroup("entry", "NXentry");
file.openGroup("raw_event_data", "NXevent_data");
auto event_index = readNexusVector<uint64_t>(file, "event_index");
TS_ASSERT_EQUALS(event_index.size(), 1439);
TS_ASSERT_THROWS_EQUALS(
auto event_id = readNexusVector<uint16_t>(file, "event_id"),
std::runtime_error & e, std::string(e.what()),
"Downcasting is forbidden in NeXusIOHelper::readNexusAnyVector");
TS_ASSERT_THROWS_EQUALS(
auto event_time_offset =
readNexusVector<uint16_t>(file, "event_time_offset"),
std::runtime_error & e, std::string(e.what()),
"Downcasting is forbidden in NeXusIOHelper::readNexusAnyVector");
TS_ASSERT_THROWS_EQUALS(
auto event_time_zero = readNexusVector<float>(file, "event_time_zero"),
std::runtime_error & e, std::string(e.what()),
"Downcasting is forbidden in NeXusIOHelper::readNexusAnyVector");
file.closeGroup();
file.closeGroup();
}
};
#endif /*NEXUSIOHELPERTEST_H_*/
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment