From a92947314b8d63fe21fd9c09146387d61d8cd761 Mon Sep 17 00:00:00 2001 From: Sam Yates <sam@quux.dropbear.id.au> Date: Thu, 9 Jun 2016 15:58:03 +0200 Subject: [PATCH] Better support tests in out-of-tree build. Add path to data directory by preprocessor definition so that unit tests can be run from any directory. --- tests/CMakeLists.txt | 1 + tests/test_swcio.cpp | 11 +++++++++-- tests/test_tree.cpp | 10 +++++++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index cb33af4d..51d0aa9c 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -39,6 +39,7 @@ set(VALIDATION_SOURCES validate.cpp ) +add_definitions("-DDATADIR=\"${CMAKE_SOURCE_DIR}/data\"") add_executable(test.exe ${TEST_SOURCES} ${HEADERS}) add_executable(validate.exe ${VALIDATION_SOURCES} ${HEADERS}) diff --git a/tests/test_swcio.cpp b/tests/test_swcio.cpp index 6968fd43..f5039713 100644 --- a/tests/test_swcio.cpp +++ b/tests/test_swcio.cpp @@ -11,6 +11,11 @@ #include <cell.hpp> #include <swcio.hpp> +// Path to data directory can be overriden at compile time. +#if !defined(DATADIR) +#define DATADIR "../data" +#endif + // SWC tests void expect_record_equals(const nest::mc::io::swc_record &expected, const nest::mc::io::swc_record &actual) @@ -227,7 +232,8 @@ TEST(swc_parser, from_allen_db) { using namespace nest::mc; - auto fname = "../data/example.swc"; + std::string datadir{DATADIR}; + auto fname = datadir+"/example.swc"; std::ifstream fid(fname); if(!fid.is_open()) { std::cerr << "unable to open file " << fname << "... skipping test\n"; @@ -493,7 +499,8 @@ TEST(swc_io, cell_construction) // the one generated with the C++ interface TEST(swc_parser, from_file_ball_and_stick) { - auto fname = "../data/ball_and_stick.swc"; + std::string datadir{DATADIR}; + auto fname = datadir+"/ball_and_stick.swc"; std::ifstream fid(fname); if(!fid.is_open()) { std::cerr << "unable to open file " << fname << "... skipping test\n"; diff --git a/tests/test_tree.cpp b/tests/test_tree.cpp index a468d80c..ecb324be 100644 --- a/tests/test_tree.cpp +++ b/tests/test_tree.cpp @@ -8,6 +8,11 @@ #include <cell_tree.hpp> #include "json/src/json.hpp" +// Path to data directory can be overriden at compile time. +#if !defined(DATADIR) +#define DATADIR "../data" +#endif + using json = nlohmann::json; using range = memory::Range; @@ -287,7 +292,10 @@ TEST(cell_tree, balance) { TEST(cell_tree, json_load) { json cell_data; - std::ifstream("../data/cells_small.json") >> cell_data; + std::string path{DATADIR}; + + path += "/cells_small.json"; + std::ifstream(path) >> cell_data; for(auto c : range(0,cell_data.size())) { std::vector<int> parent_index = cell_data[c]["parent_index"]; -- GitLab