diff --git a/modules/core/test/tools/io/testNPZ.cpp b/modules/core/test/tools/io/testNPZ.cpp index 443c98b96f..f384783487 100644 --- a/modules/core/test/tools/io/testNPZ.cpp +++ b/modules/core/test/tools/io/testNPZ.cpp @@ -35,11 +35,13 @@ #include #include +#include #if defined(VISP_HAVE_CATCH2) && \ - (defined(_WIN32) || (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))) -// #define CATCH_CONFIG_RUNNER -// #include + (defined(_WIN32) || (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))) && \ + defined(VISP_LITTLE_ENDIAN) +#define CATCH_CONFIG_RUNNER +#include #include @@ -62,141 +64,97 @@ std::string createTmpDir() } } -// TEST_CASE("Test visp::cnpy::npy_load/npz_save", "[visp::cnpy I/O]") -// { -// std::string directory_filename = createTmpDir(); -// REQUIRE(vpIoTools::checkDirectory(directory_filename)); -// std::string npz_filename = directory_filename + "/test_npz_read_write.npz"; - -// SECTION("Read/Save string data") -// { -// const std::string save_string = "Open Source Visual Servoing Platform"; -// std::vector vec_save_string(save_string.begin(), save_string.end()); -// // Manually add a null-terminated character -// // https://stackoverflow.com/a/8247804 -// vec_save_string.push_back('\0'); -// const std::string identifier = "String"; -// visp::cnpy::npz_save(npz_filename, identifier, &vec_save_string[0], { vec_save_string.size() }, "w"); - -// visp::cnpy::npz_t npz_data = visp::cnpy::npz_load(npz_filename); -// visp::cnpy::NpyArray arr_string_data = npz_data[identifier]; -// const std::string read_string = std::string(arr_string_data.data()); -// CHECK(save_string == read_string); -// } - -// SECTION("Read/Save multi-dimensional array") -// { -// size_t height = 5, width = 7, channels = 3; -// std::vector save_vec; -// save_vec.reserve(height*width*channels); -// for (size_t i = 0; i < height*width*channels; i++) { -// save_vec.push_back(i); -// } - -// const std::string identifier = "Array"; -// visp::cnpy::npz_save(npz_filename, identifier, &save_vec[0], { height, width, channels }, "a"); // append -// visp::cnpy::npz_t npz_data = visp::cnpy::npz_load(npz_filename); -// visp::cnpy::NpyArray arr_vec_data = npz_data[identifier]; -// std::vector read_vec = arr_vec_data.as_vec(); - -// REQUIRE(save_vec.size() == read_vec.size()); -// for (size_t i = 0; i < read_vec.size(); i++) { -// CHECK(save_vec[i] == read_vec[i]); -// } -// } - -// REQUIRE(vpIoTools::remove(directory_filename)); -// REQUIRE(!vpIoTools::checkDirectory(directory_filename)); -// } - -// // https://en.cppreference.com/w/cpp/types/integer -// // https://github.com/catchorg/Catch2/blob/devel/docs/test-cases-and-sections.md#type-parametrised-test-cases -// using BasicTypes = std::tuple; -// TEMPLATE_LIST_TEST_CASE("Test visp::cnpy::npy_load/npz_save", "[BasicTypes][list]", BasicTypes) -// { -// std::string directory_filename = createTmpDir(); -// REQUIRE(vpIoTools::checkDirectory(directory_filename)); -// std::string npz_filename = directory_filename + "/test_npz_read_write.npz"; - -// const std::string identifier = "data"; -// TestType save_data = std::numeric_limits::min(); -// visp::cnpy::npz_save(npz_filename, identifier, &save_data, { 1 }, "w"); - -// visp::cnpy::npz_t npz_data = visp::cnpy::npz_load(npz_filename); -// visp::cnpy::NpyArray arr_data = npz_data[identifier]; -// TestType read_data = *arr_data.data(); -// CHECK(save_data == read_data); - -// save_data = std::numeric_limits::max(); -// visp::cnpy::npz_save(npz_filename, identifier, &save_data, { 1 }, "a"); // append - -// npz_data = visp::cnpy::npz_load(npz_filename); -// arr_data = npz_data[identifier]; -// read_data = *arr_data.data(); -// CHECK(save_data == read_data); - -// REQUIRE(vpIoTools::remove(directory_filename)); -// REQUIRE(!vpIoTools::checkDirectory(directory_filename)); -// } - -int main(int argc, char *argv[]) +TEST_CASE("Test visp::cnpy::npy_load/npz_save", "[visp::cnpy I/O]") { - // DEBUG: + std::string directory_filename = createTmpDir(); + REQUIRE(vpIoTools::checkDirectory(directory_filename)); + std::string npz_filename = directory_filename + "/test_npz_read_write.npz"; + + SECTION("Read/Save string data") + { + const std::string save_string = "Open Source Visual Servoing Platform"; + std::vector vec_save_string(save_string.begin(), save_string.end()); + const std::string identifier = "String"; + visp::cnpy::npz_save(npz_filename, identifier, &vec_save_string[0], { vec_save_string.size() }, "w"); + + visp::cnpy::npz_t npz_data = visp::cnpy::npz_load(npz_filename); + visp::cnpy::NpyArray arr_string_data = npz_data[identifier]; + std::vector vec_arr_string_data = arr_string_data.as_vec(); + // For null-terminated character handling, see: + // https://stackoverflow.com/a/8247804 + // https://stackoverflow.com/a/45491652 + const std::string read_string = std::string(vec_arr_string_data.begin(), vec_arr_string_data.end()); + CHECK(save_string == read_string); + } + + SECTION("Read/Save multi-dimensional array") { - std::string directory_filename = createTmpDir(); - std::string npz_filename = directory_filename + "/test_npz_read_write.npz"; - - { - const std::string save_string = "Open Source Visual Servoing Platform"; - std::vector vec_save_string(save_string.begin(), save_string.end()); - // Manually add a null-terminated character - // https://stackoverflow.com/a/8247804 - // vec_save_string.push_back('\0'); - const std::string identifier = "String"; - visp::cnpy::npz_save(npz_filename, identifier, &vec_save_string[0], { vec_save_string.size() }, "w"); - - visp::cnpy::npz_t npz_data = visp::cnpy::npz_load(npz_filename); - visp::cnpy::NpyArray arr_string_data = npz_data[identifier]; - std::vector vec_arr_string_data = arr_string_data.as_vec(); - const std::string read_string = std::string(vec_arr_string_data.begin(), vec_arr_string_data.end()); - std::cout << "(save_string == read_string)=" << (save_string == read_string) << std::endl; + size_t height = 5, width = 7, channels = 3; + std::vector save_vec; + save_vec.reserve(height*width*channels); + for (size_t i = 0; i < height*width*channels; i++) { + save_vec.push_back(i); } - { - size_t height = 5, width = 7, channels = 3; - std::vector save_vec; - save_vec.reserve(height*width*channels); - for (size_t i = 0; i < height*width*channels; i++) { - save_vec.push_back(i); - } - - const std::string identifier = "Array"; - visp::cnpy::npz_save(npz_filename, identifier, &save_vec[0], { height, width, channels }, "a"); // append - visp::cnpy::npz_t npz_data = visp::cnpy::npz_load(npz_filename); - visp::cnpy::NpyArray arr_vec_data = npz_data[identifier]; - std::vector read_vec = arr_vec_data.as_vec(); - - std::cout << "(save_vec.size() == read_vec.size())=" << (save_vec.size() == read_vec.size()) << std::endl; - for (size_t i = 0; i < read_vec.size(); i++) { - if (save_vec[i] != read_vec[i]) { - std::cerr << "save_vec[i]=" << save_vec[i] << " ; read_vec[i]=" << read_vec[i] << std::endl; - } - } + + const std::string identifier = "Array"; + visp::cnpy::npz_save(npz_filename, identifier, &save_vec[0], { height, width, channels }, "a"); // append + visp::cnpy::npz_t npz_data = visp::cnpy::npz_load(npz_filename); + visp::cnpy::NpyArray arr_vec_data = npz_data[identifier]; + std::vector read_vec = arr_vec_data.as_vec(); + + REQUIRE(save_vec.size() == read_vec.size()); + for (size_t i = 0; i < read_vec.size(); i++) { + CHECK(save_vec[i] == read_vec[i]); } } - return 0; + REQUIRE(vpIoTools::remove(directory_filename)); + REQUIRE(!vpIoTools::checkDirectory(directory_filename)); +} - // Catch::Session session; // There must be exactly one instance +// https://en.cppreference.com/w/cpp/types/integer +// https://github.com/catchorg/Catch2/blob/devel/docs/test-cases-and-sections.md#type-parametrised-test-cases +using BasicTypes = std::tuple; +TEMPLATE_LIST_TEST_CASE("Test visp::cnpy::npy_load/npz_save", "[BasicTypes][list]", BasicTypes) +{ + std::string directory_filename = createTmpDir(); + REQUIRE(vpIoTools::checkDirectory(directory_filename)); + std::string npz_filename = directory_filename + "/test_npz_read_write.npz"; + + const std::string identifier = "data"; + TestType save_data = std::numeric_limits::min(); + visp::cnpy::npz_save(npz_filename, identifier, &save_data, { 1 }, "w"); + + visp::cnpy::npz_t npz_data = visp::cnpy::npz_load(npz_filename); + visp::cnpy::NpyArray arr_data = npz_data[identifier]; + TestType read_data = *arr_data.data(); + CHECK(save_data == read_data); + + save_data = std::numeric_limits::max(); + visp::cnpy::npz_save(npz_filename, identifier, &save_data, { 1 }, "a"); // append + + npz_data = visp::cnpy::npz_load(npz_filename); + arr_data = npz_data[identifier]; + read_data = *arr_data.data(); + CHECK(save_data == read_data); + + REQUIRE(vpIoTools::remove(directory_filename)); + REQUIRE(!vpIoTools::checkDirectory(directory_filename)); +} + +int main(int argc, char *argv[]) +{ + Catch::Session session; // There must be exactly one instance - // // Let Catch (using Clara) parse the command line - // session.applyCommandLine(argc, argv); + // Let Catch (using Clara) parse the command line + session.applyCommandLine(argc, argv); - // int numFailed = session.run(); + int numFailed = session.run(); - // // numFailed is clamped to 255 as some unices only use the lower 8 bits. - // // This clamping has already been applied, so just return it here - // // You can also do any post run clean-up here - // return numFailed; + // numFailed is clamped to 255 as some unices only use the lower 8 bits. + // This clamping has already been applied, so just return it here + // You can also do any post run clean-up here + return numFailed; } #else int main() { return EXIT_SUCCESS; }