42# include <gtest/gtest.h>
57static_assert(NeighborhoodLike<Moore<2, 1>>);
58static_assert(NeighborhoodLike<Moore<2, 2>>);
59static_assert(NeighborhoodLike<Moore<3, 1>>);
60static_assert(NeighborhoodLike<Von_Neumann<2, 1>>);
61static_assert(NeighborhoodLike<Von_Neumann<3, 1>>);
62static_assert(NeighborhoodLike<Hex_Neighborhood>);
63static_assert(NeighborhoodLike<Triangular_Neighborhood>);
64static_assert(NeighborhoodLike<Custom_Neighborhood<2, 4>>);
90TEST(CAMoore, OffsetsAreUniqueNonZeroAndBounded)
93 EXPECT_EQ(nh.
size(), 8u);
94 EXPECT_EQ(nh.
radius(), 1u);
96 std::set<std::pair<int, int>> seen;
97 std::size_t calls = 0;
101 EXPECT_FALSE(o[0] == 0
and o[1] == 0);
102 EXPECT_GE(o[0], -1); EXPECT_LE(o[0], 1);
103 EXPECT_GE(o[1], -1); EXPECT_LE(o[1], 1);
104 seen.emplace(
static_cast<int>(o[0]),
static_cast<int>(o[1]));
106 EXPECT_EQ(calls, 8u);
107 EXPECT_EQ(seen.size(), 8u);
110TEST(CAMoore, R2In2DProduces24Offsets)
113 std::size_t calls = 0;
116 EXPECT_FALSE(o[0] == 0
and o[1] == 0);
117 EXPECT_LE(std::max(std::abs((
int)o[0]), std::abs((
int)o[1])), 2);
119 EXPECT_EQ(calls, 24u);
126TEST(CAVonNeumann, R1In2DGivesFourCardinalOffsets)
129 EXPECT_EQ(nh.
size(), 4u);
131 std::set<std::pair<int, int>> seen;
133 seen.emplace(
static_cast<int>(o[0]),
static_cast<int>(o[1]));
135 EXPECT_EQ(seen.count({ -1, 0 }), 1u);
136 EXPECT_EQ(seen.count({ 1, 0 }), 1u);
137 EXPECT_EQ(seen.count({ 0, -1 }), 1u);
138 EXPECT_EQ(seen.count({ 0, 1 }), 1u);
141TEST(CAVonNeumann, R2In2DOffsetsRespectL1Bound)
144 std::size_t calls = 0;
147 const int s = std::abs((
int)o[0]) + std::abs((
int)o[1]);
148 EXPECT_GE(s, 1); EXPECT_LE(s, 2);
150 EXPECT_EQ(calls, 12u);
157TEST(CAHexNeighborhood, SixAxialOffsets)
160 EXPECT_EQ(nh.
size(), 6u);
162 std::set<std::pair<int, int>> seen;
164 seen.emplace(
static_cast<int>(o[0]),
static_cast<int>(o[1]));
166 EXPECT_EQ(seen.size(), 6u);
167 EXPECT_EQ(seen.count({ 1, 0 }), 1u);
168 EXPECT_EQ(seen.count({ -1, 0 }), 1u);
169 EXPECT_EQ(seen.count({ 0, 1 }), 1u);
170 EXPECT_EQ(seen.count({ 0, -1 }), 1u);
171 EXPECT_EQ(seen.count({ 1, -1 }), 1u);
172 EXPECT_EQ(seen.count({ -1, 1 }), 1u);
179TEST(CATriangularNeighborhood, ParityDependentThirdOffset)
182 EXPECT_EQ(nh.
size(), 3u);
185 std::set<std::pair<int, int>> seen_even;
187 seen_even.emplace(
static_cast<int>(o[0]),
static_cast<int>(o[1]));
189 EXPECT_EQ(seen_even.count({ 0, -1 }), 1u);
190 EXPECT_EQ(seen_even.count({ 0, 1 }), 1u);
191 EXPECT_EQ(seen_even.count({ 1, 0 }), 1u);
194 std::set<std::pair<int, int>> seen_odd;
196 seen_odd.emplace(
static_cast<int>(o[0]),
static_cast<int>(o[1]));
198 EXPECT_EQ(seen_odd.count({ 0, -1 }), 1u);
199 EXPECT_EQ(seen_odd.count({ 0, 1 }), 1u);
200 EXPECT_EQ(seen_odd.count({ -1, 0 }), 1u);
207TEST(CACustomNeighborhood, EnumeratesProvidedOffsets)
212 EXPECT_EQ(nh.size(), 3u);
213 EXPECT_EQ(nh.radius(), 3u);
215 std::vector<std::pair<int, int>> seen;
216 nh.for_each_offset({ 0, 0 }, [&](
const auto & o) {
217 seen.emplace_back(
static_cast<int>(o[0]),
static_cast<int>(o[1]));
219 ASSERT_EQ(seen.size(), 3u);
220 EXPECT_EQ(seen[0].first, 2); EXPECT_EQ(seen[0].second, 0);
221 EXPECT_EQ(seen[1].first, 0); EXPECT_EQ(seen[1].second, 2);
222 EXPECT_EQ(seen[2].first, -3); EXPECT_EQ(seen[2].second, 1);
229TEST(CAGatherNeighbors, MooreOnToroidalLattice)
234 lat.set({ i, j },
static_cast<int>(i * 10 + j));
237 std::array<int, Moore<2, 1>::size_v> buf { };
239 std::span<int>(buf.data(), buf.size()));
245 for (
int v : buf) total += v;
246 EXPECT_EQ(total, 99);
249TEST(CAGatherNeighbors, MooreOnOpenLatticeReadsZeroOutsideBorders)
253 std::array<int, Moore<2, 1>::size_v> buf { };
255 std::span<int>(buf.data(), buf.size()));
260 for (
int v : buf)
if (v != 0) ++alive;
264TEST(CAGatherNeighbors, ThrowsWhenOutputSpanIsTooSmall)
268 std::array<int, 2> buf { };
270 std::span<int>(buf.data(), buf.size())),
Common typedefs and tag types for the Cellular Automata module.
User-supplied list of offsets for arbitrary connectivity.
Six-neighbour hex pattern in axial coordinates over a 2D lattice.
static constexpr void for_each_offset(const Coord_Vec< 2 > ¢er, F &&f)
Apply a functor to each of the six hex offsets.
static constexpr std::size_t size() noexcept
Return the number of neighbours (always 6).
static constexpr std::size_t size_v
Number of neighbours (always 6).
Lattice that adds boundary-aware access on top of a storage.
Moore (Chebyshev) neighborhood of radius R in N dimensions.
constexpr std::size_t size() const noexcept
Return the number of neighbours in the neighborhood.
constexpr void for_each_offset(const Coord_Vec< N > ¢er, F &&f) const
Apply a functor to each offset in the neighborhood.
static constexpr std::size_t radius() noexcept
Return the Chebyshev radius of the neighborhood.
Three edge-neighbours over a 2D lattice with parity coupling.
static constexpr std::size_t size_v
Number of neighbours (always 3).
static constexpr std::size_t size() noexcept
Return the number of neighbours (always 3).
static constexpr void for_each_offset(const Coord_Vec< 2 > ¢er, F &&f)
Apply a functor to the three parity-dependent triangle offsets.
Von Neumann (L1) neighborhood of radius R in N dimensions.
constexpr void for_each_offset(const Coord_Vec< N > ¢er, F &&f) const
Apply a functor to each offset in the neighborhood.
static constexpr std::size_t size() noexcept
Return the number of neighbours in the neighborhood.
std::ptrdiff_t ca_index_t
Signed coordinate component used by lattices and neighborhoods.
std::array< ca_index_t, N > Coord_Vec
Default coordinate vector.
void gather_neighbors(const Nbh &nh, const L &lat, const typename L::coord_type ¢er, std::span< T > out)
Populate out[0..nh.size()) with neighbour values of center.
Coord_Vec< N > Offset_Vec
Default offset vector (aliases Coord_Vec).
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
Out-of-range neighbours behave as if the lattice ended.
The lattice wraps around on every axis.
C++20 concepts for the Cellular Automata module.
Cellular automata lattice with pluggable boundary policies.
Neighborhoods catalogue for Aleph::CA.
Dense, contiguous storage for cellular automata cells (1D/2D/3D).