From 9786d9d5c2584f0a74015f6ef3bcf71c401ff49b Mon Sep 17 00:00:00 2001 From: Stephen Nicholas Swatman Date: Fri, 12 Jan 2024 19:41:06 +0100 Subject: [PATCH] Add tests for convertibility of interpolators This commit adds a test for the convertability of interpolation types at binary IO time, which is a problem faced by @beomki-yeo. --- tests/core/CMakeLists.txt | 4 + tests/core/test_atlas_like_io.cpp | 183 ++++++++++++++++++++++++++++++ 2 files changed, 187 insertions(+) create mode 100644 tests/core/test_atlas_like_io.cpp diff --git a/tests/core/CMakeLists.txt b/tests/core/CMakeLists.txt index 0f584ff..2853610 100644 --- a/tests/core/CMakeLists.txt +++ b/tests/core/CMakeLists.txt @@ -33,6 +33,10 @@ add_executable( test_array_binary_io.cpp test_build.cpp test_canonical.cpp +<<<<<<< HEAD +======= + test_atlas_like_io.cpp +>>>>>>> 621b797 (Add tests for convertibility of interpolators) ) # Ensure that the tests are linked against the required libraries. diff --git a/tests/core/test_atlas_like_io.cpp b/tests/core/test_atlas_like_io.cpp new file mode 100644 index 0000000..3f9efb1 --- /dev/null +++ b/tests/core/test_atlas_like_io.cpp @@ -0,0 +1,183 @@ +/* + * This file is part of covfie, a part of the ACTS project + * + * Copyright (c) 2024 CERN + * + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +using basic_backend_t = covfie::backend::strided< + covfie::vector::size3, + covfie::backend::array>; + +using field_nn_backend_t = covfie::backend::affine< + covfie::backend::nearest_neighbour>; + +using field_li_backend_t = + covfie::backend::affine>; + +TEST(TestAtlasLikeIO, WriteReadAtlasLike) +{ + covfie::field bf( + covfie::make_parameter_pack_for>( + {2, 2, 2}, {8} + ) + ); + covfie::field::view_t bv(bf); + + for (int x = 0; x < 2; x++) { + for (int y = 0; y < 2; y++) { + for (int z = 0; z < 2; z++) { + bv.at(x, y, z) = { + static_cast(x), + static_cast(y), + static_cast(z)}; + } + } + } + + for (int x = 0; x < 2; x++) { + for (int y = 0; y < 2; y++) { + for (int z = 0; z < 2; z++) { + covfie::field::output_t & p = + bv.at(x, y, z); + EXPECT_EQ(p[0], static_cast(x)); + EXPECT_EQ(p[1], static_cast(y)); + EXPECT_EQ(p[2], static_cast(z)); + } + } + } + + covfie::algebra::affine<3> translation = + covfie::algebra::affine<3>::translation(0.0f, 0.0f, 0.0f); + + covfie::field nnf(covfie::make_parameter_pack( + field_nn_backend_t::configuration_t(translation), + field_nn_backend_t::backend_t::configuration_t{}, + std::move(bf.backend()) + )); + + boost::filesystem::path ofile = get_tmp_file(); + + std::ofstream ofs(ofile.native(), std::ofstream::binary); + + if (!ofs.good()) { + throw std::runtime_error("Invalid file was somehow opened!"); + } + + nnf.dump(ofs); + ofs.close(); + + std::ifstream ifs(ofile.native(), std::ifstream::binary); + + if (!ifs.good()) { + throw std::runtime_error("Invalid file was somehow opened!"); + } + + covfie::field nnnf(ifs); + ifs.close(); + + covfie::field::view_t nnnv(nnnf); + + for (int x = 0; x < 2; x++) { + for (int y = 0; y < 2; y++) { + for (int z = 0; z < 2; z++) { + covfie::field::output_t & p = + nnnv.at(x, y, z); + EXPECT_EQ(p[0], static_cast(x)); + EXPECT_EQ(p[1], static_cast(y)); + EXPECT_EQ(p[2], static_cast(z)); + } + } + } +} + +TEST(TestAtlasLikeIO, WriteReadAtlasLikeChangeInterpolation) +{ + covfie::field bf( + covfie::make_parameter_pack_for>( + {2, 2, 2}, {8} + ) + ); + covfie::field::view_t bv(bf); + + for (int x = 0; x < 2; x++) { + for (int y = 0; y < 2; y++) { + for (int z = 0; z < 2; z++) { + bv.at(x, y, z) = { + static_cast(x), + static_cast(y), + static_cast(z)}; + } + } + } + + for (int x = 0; x < 2; x++) { + for (int y = 0; y < 2; y++) { + for (int z = 0; z < 2; z++) { + covfie::field::output_t p = bv.at(x, y, z); + EXPECT_EQ(p[0], static_cast(x)); + EXPECT_EQ(p[1], static_cast(y)); + EXPECT_EQ(p[2], static_cast(z)); + } + } + } + + covfie::algebra::affine<3> translation = + covfie::algebra::affine<3>::translation(0.0f, 0.0f, 0.0f); + + covfie::field nnf(covfie::make_parameter_pack( + field_nn_backend_t::configuration_t(translation), + field_nn_backend_t::backend_t::configuration_t{}, + std::move(bf.backend()) + )); + + boost::filesystem::path ofile = get_tmp_file(); + + std::ofstream ofs(ofile.native(), std::ofstream::binary); + + if (!ofs.good()) { + throw std::runtime_error("Invalid file was somehow opened!"); + } + + nnf.dump(ofs); + ofs.close(); + + std::ifstream ifs(ofile.native(), std::ifstream::binary); + + if (!ifs.good()) { + throw std::runtime_error("Invalid file was somehow opened!"); + } + + covfie::field nnnf(ifs); + ifs.close(); + + covfie::field::view_t nnnv(nnnf); + + for (int x = 0; x < 2; x++) { + for (int y = 0; y < 2; y++) { + for (int z = 0; z < 2; z++) { + covfie::field::output_t p = + nnnv.at(x, y, z); + EXPECT_EQ(p[0], static_cast(x)); + EXPECT_EQ(p[1], static_cast(y)); + EXPECT_EQ(p[2], static_cast(z)); + } + } + } +}