|
| 1 | +//------------------------------------------------------------------------------ |
| 2 | +// © 2021-2025. Triad National Security, LLC. All rights reserved. This |
| 3 | +// program was produced under U.S. Government contract 89233218CNA000001 |
| 4 | +// for Los Alamos National Laboratory (LANL), which is operated by Triad |
| 5 | +// National Security, LLC for the U.S. Department of Energy/National |
| 6 | +// Nuclear Security Administration. All rights in the program are |
| 7 | +// reserved by Triad National Security, LLC, and the U.S. Department of |
| 8 | +// Energy/National Nuclear Security Administration. The Government is |
| 9 | +// granted for itself and others acting on its behalf a nonexclusive, |
| 10 | +// paid-up, irrevocable worldwide license in this material to reproduce, |
| 11 | +// prepare derivative works, distribute copies to the public, perform |
| 12 | +// publicly and display publicly, and to permit others to do so. |
| 13 | +//------------------------------------------------------------------------------ |
| 14 | + |
| 15 | +#include <array> |
| 16 | + |
| 17 | +#include <ports-of-call/portability.hpp> |
| 18 | +#include <singularity-eos/base/generic_indexer.hpp> |
| 19 | + |
| 20 | +#ifndef CATCH_CONFIG_FAST_COMPILE |
| 21 | +#define CATCH_CONFIG_FAST_COMPILE |
| 22 | +#include <catch2/catch_test_macros.hpp> |
| 23 | +#endif |
| 24 | +#include <catch2/matchers/catch_matchers_floating_point.hpp> |
| 25 | + |
| 26 | +SCENARIO("Generic Indexer", "[GenericIndexer]") { |
| 27 | + GIVEN("An array of arays populated with data") { |
| 28 | + constexpr std::size_t n = 5; |
| 29 | + constexpr std::size_t m = 7; |
| 30 | + auto data = std::array<std::array<Real, m>, n>{}; |
| 31 | + for (std::size_t i = 0; i < n; i++) { |
| 32 | + for (std::size_t j = 0; j < m; j++) { |
| 33 | + data[i][j] = (i + 1) * (j + 1); |
| 34 | + } |
| 35 | + } |
| 36 | + WHEN("A flattened array is created with the same size and populated with " |
| 37 | + "the same data") { |
| 38 | + auto flat_data = std::array<Real, n * m>{}; |
| 39 | + for (std::size_t i = 0; i < n; i++) { |
| 40 | + for (std::size_t j = 0; j < m; j++) { |
| 41 | + flat_data[i * m + j] = data[i][j]; |
| 42 | + } |
| 43 | + } |
| 44 | + WHEN("We use the generic indexer to index into material data") { |
| 45 | + struct constant_offset { |
| 46 | + std::size_t offset; |
| 47 | + constexpr constant_offset(std::size_t offset_) : offset{offset_} {} |
| 48 | + constexpr std::size_t operator[](std::size_t i) { return i * offset; } |
| 49 | + }; |
| 50 | + auto mat_indexer = |
| 51 | + singularity::GenericIndexer(flat_data.data(), constant_offset{m}); |
| 52 | + THEN("The data returned should be same as if we accessed the array of " |
| 53 | + "arrays") { |
| 54 | + for (std::size_t i = 0; i < n; i++) { |
| 55 | + auto *mat_array = &mat_indexer[i]; |
| 56 | + for (std::size_t j = 0; j < m; j++) { |
| 57 | + INFO("i: " << i << " j: " << j); |
| 58 | + CHECK_THAT(data[i][j], Catch::Matchers::WithinRel(mat_array[j], 1.0e-12)); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | +} |
0 commit comments