Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_ca_lattice_test.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
40# include <cstdint>
41# include <stdexcept>
42
43# include <gtest/gtest.h>
44
45# include <ca-traits.H>
46# include <tpl_ca_concepts.H>
47# include <tpl_ca_storage.H>
48# include <tpl_ca_bit_storage.H>
49# include <tpl_ca_lattice.H>
50
51using namespace Aleph;
52using namespace Aleph::CA;
53
54namespace
55{
58 template <typename Boundary>
61 {
62 Lattice<Dense_Cell_Storage<int, 2>, Boundary> lat({ 3, 3 }, 0);
63 for (ca_index_t i = 0; i < 3; ++i)
64 for (ca_index_t j = 0; j < 3; ++j)
65 lat.set({ i, j }, static_cast<int>(i * 10 + j));
66 return lat;
67 }
68}
69
70// ---------------------------------------------------------------------------
71// Concept conformance: a fully-formed Lattice still satisfies LatticeLike.
72// ---------------------------------------------------------------------------
73
78
79// ---------------------------------------------------------------------------
80// OpenBoundary: out-of-range cells read as state_type{} (zero).
81// ---------------------------------------------------------------------------
82
84{
86 for (ca_index_t i = 0; i < 3; ++i)
87 for (ca_index_t j = 0; j < 3; ++j)
88 EXPECT_EQ(lat.at_safe({ i, j }), i * 10 + j);
89}
90
92{
94 EXPECT_EQ(lat.at_safe({ -1, 0 }), 0);
95 EXPECT_EQ(lat.at_safe({ 0, -1 }), 0);
96 EXPECT_EQ(lat.at_safe({ 3, 0 }), 0);
97 EXPECT_EQ(lat.at_safe({ 0, 3 }), 0);
98 EXPECT_EQ(lat.at_safe({ -1, -1 }), 0);
99 EXPECT_EQ(lat.at_safe({ 3, 3 }), 0);
100}
101
102// ---------------------------------------------------------------------------
103// ToroidalBoundary: out-of-range cells wrap around on every axis.
104// ---------------------------------------------------------------------------
105
107{
109
110 // Single-axis wrap: -1 → 2 on i.
111 EXPECT_EQ(lat.at_safe({ -1, 1 }), lat.at({ 2, 1 }));
112 // Single-axis wrap: 3 → 0 on j.
113 EXPECT_EQ(lat.at_safe({ 1, 3 }), lat.at({ 1, 0 }));
114 // Both axes wrap simultaneously (corner).
115 EXPECT_EQ(lat.at_safe({ -1, -1 }), lat.at({ 2, 2 }));
116 EXPECT_EQ(lat.at_safe({ 3, 3 }), lat.at({ 0, 0 }));
117 // Multi-period wrap: -4 ≡ -1 (mod 3) ≡ 2.
118 EXPECT_EQ(lat.at_safe({ -4, 1 }), lat.at({ 2, 1 }));
119 EXPECT_EQ(lat.at_safe({ 7, 1 }), lat.at({ 1, 1 }));
120}
121
123{
125
126 EXPECT_EQ(wrap_into_range(-1, 0), 0u);
127 EXPECT_EQ(wrap_into_range( 1, 0), 0u);
128}
129
130// ---------------------------------------------------------------------------
131// ReflectiveBoundary: -1 → 0, -2 → 1, n → n-1, n+1 → n-2.
132// ---------------------------------------------------------------------------
133
135{
137
138 EXPECT_EQ(reflect_into_range(-1, 0), 0u);
139 EXPECT_EQ(reflect_into_range( 1, 0), 0u);
140 EXPECT_EQ(reflect_into_range(-1, 3), 0u);
141 EXPECT_EQ(reflect_into_range(-2, 3), 1u);
142 EXPECT_EQ(reflect_into_range(3, 3), 2u);
143 EXPECT_EQ(reflect_into_range(4, 3), 1u);
144 EXPECT_EQ(reflect_into_range(-3, 3), 2u);
145 EXPECT_EQ(reflect_into_range(5, 3), 0u);
146 EXPECT_EQ(reflect_into_range(6, 3), 0u);
147 EXPECT_EQ(reflect_into_range(-7, 3), 0u);
148
149 EXPECT_EQ(reflect_into_range(-1, 5), 0u);
150 EXPECT_EQ(reflect_into_range(-2, 5), 1u);
151 EXPECT_EQ(reflect_into_range(5, 5), 4u);
152 EXPECT_EQ(reflect_into_range(6, 5), 3u);
153 EXPECT_EQ(reflect_into_range(19, 5), 0u);
154 EXPECT_EQ(reflect_into_range(-19, 5), 1u);
155}
156
158{
160
161 EXPECT_EQ(lat.at_safe({ -1, 1 }), lat.at({ 0, 1 }));
162 EXPECT_EQ(lat.at_safe({ -2, 1 }), lat.at({ 1, 1 }));
163 EXPECT_EQ(lat.at_safe({ 3, 1 }), lat.at({ 2, 1 }));
164 EXPECT_EQ(lat.at_safe({ 4, 1 }), lat.at({ 1, 1 }));
165 EXPECT_EQ(lat.at_safe({ 1, -1 }), lat.at({ 1, 0 }));
166 EXPECT_EQ(lat.at_safe({ 1, 3 }), lat.at({ 1, 2 }));
167}
168
170{
172
173 EXPECT_EQ(lat.at_safe({ -1, -1 }), lat.at({ 0, 0 }));
174 EXPECT_EQ(lat.at_safe({ -1, 3 }), lat.at({ 0, 2 }));
175 EXPECT_EQ(lat.at_safe({ 3, -1 }), lat.at({ 2, 0 }));
176 EXPECT_EQ(lat.at_safe({ 3, 3 }), lat.at({ 2, 2 }));
177}
178
179// ---------------------------------------------------------------------------
180// ConstantBoundary<T, V>: out-of-range cells read as V.
181// ---------------------------------------------------------------------------
182
184{
185 using Boundary = ConstantBoundary<int, 99>;
186 Lattice<Dense_Cell_Storage<int, 2>, Boundary> lat({ 3, 3 }, 0);
187 for (ca_index_t i = 0; i < 3; ++i)
188 for (ca_index_t j = 0; j < 3; ++j)
189 lat.set({ i, j }, static_cast<int>(i * 10 + j));
190
191 EXPECT_EQ(lat.at_safe({ 0, 0 }), 0);
192 EXPECT_EQ(lat.at_safe({ -1, 0 }), 99);
193 EXPECT_EQ(lat.at_safe({ 0, 3 }), 99);
194 EXPECT_EQ(lat.at_safe({ -2, -2 }), 99);
195}
196
198{
199 using Boundary = ConstantBoundary<bool, true>;
200 Lattice<Bit_Cell_Storage<2>, Boundary> lat({ 3, 3 }, false);
201 // Inner cells are false, outside cells must read true.
202 EXPECT_FALSE(lat.at_safe({ 0, 0 }));
203 EXPECT_TRUE(lat.at_safe({ -1, 0 }));
204 EXPECT_TRUE(lat.at_safe({ 3, 3 }));
205}
206
207// ---------------------------------------------------------------------------
208// Common surface: extents, swap, fill, strict at/set semantics.
209// ---------------------------------------------------------------------------
210
212{
214 EXPECT_EQ(lat.dimension(), 3u);
215 EXPECT_EQ(lat.size(), 24u);
216 EXPECT_EQ(lat.size(0), 2u);
217 EXPECT_EQ(lat.size(1), 3u);
218 EXPECT_EQ(lat.size(2), 4u);
219 EXPECT_EQ(lat.extents()[2], 4u);
220}
221
223{
225 lat.set({ 1, 1 }, 7);
226 lat.fill(42);
227 for (ca_index_t i = 0; i < 3; ++i)
228 for (ca_index_t j = 0; j < 3; ++j)
229 EXPECT_EQ(lat.at({ i, j }), 42);
230}
231
233{
236 a.swap(b);
237 EXPECT_EQ(a.size(0), 3u);
238 EXPECT_EQ(b.size(0), 2u);
239 EXPECT_EQ(a.at({ 0, 0 }), 9);
240 EXPECT_EQ(b.at({ 0, 0 }), 1);
241}
242
244{
246 EXPECT_THROW(lat.at({ -1, 0 }), std::out_of_range);
247 EXPECT_THROW(lat.set({ 3, 0 }, 1), std::out_of_range);
248}
249
251{
253 EXPECT_EQ(lat.dimension(), 2u);
254 EXPECT_EQ(lat.size(), 0u);
255 EXPECT_EQ(lat.size(0), 0u);
256 EXPECT_EQ(lat.size(1), 3u);
257 EXPECT_THROW(lat.at({ 0, 0 }), std::out_of_range);
258 EXPECT_THROW(lat.set({ 0, 0 }, 1), std::out_of_range);
259 EXPECT_NO_THROW((void) lat.at_safe({ 0, 0 }));
260 EXPECT_EQ(lat.at_safe({ 0, 0 }), 0);
261
263 EXPECT_EQ(cube.dimension(), 3u);
264 EXPECT_EQ(cube.size(), 0u);
265 EXPECT_EQ(cube.size(0), 0u);
266 EXPECT_EQ(cube.size(1), 0u);
267 EXPECT_THROW(cube.at({ 0, 0, 0 }), std::out_of_range);
268 EXPECT_NO_THROW((void) cube.at_safe({ 0, 0, 0 }));
269}
Common typedefs and tag types for the Cellular Automata module.
Lattice that adds boundary-aware access on top of a storage.
void set(const coord_type &c, const state_type &v)
Strict write: throws if c is out of range.
#define TEST(name)
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
ca_size_t reflect_into_range(const ca_index_t c, const ca_size_t n) noexcept
Reflect a signed coordinate into [0, n) (no-repeat triangle wave).
ca_size_t wrap_into_range(ca_index_t c, const ca_size_t n) noexcept
Wrap a signed coordinate into [0, n) with positive modulo.
std::ptrdiff_t ca_index_t
Signed coordinate component used by lattices and neighborhoods.
Definition ca-traits.H:60
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Constant-value boundary.
Definition ca-traits.H:138
Out-of-range neighbours behave as if the lattice ended.
Definition ca-traits.H:119
Out-of-range coordinates mirror back into the lattice.
Definition ca-traits.H:129
The lattice wraps around on every axis.
Definition ca-traits.H:124
Bit-packed dense storage for boolean cellular automata.
C++20 concepts for the Cellular Automata module.
Cellular automata lattice with pluggable boundary policies.
Dense, contiguous storage for cellular automata cells (1D/2D/3D).