44# include <type_traits>
47# include <gtest/gtest.h>
61static_assert(LatticeLike<Dense_Cell_Storage<std::uint8_t, 1>>);
62static_assert(LatticeLike<Dense_Cell_Storage<std::uint8_t, 2>>);
63static_assert(LatticeLike<Dense_Cell_Storage<std::uint8_t, 3>>);
64static_assert(LatticeLike<Bit_Cell_Storage<1>>);
65static_assert(LatticeLike<Bit_Cell_Storage<2>>);
66static_assert(LatticeLike<Bit_Cell_Storage<3>>);
72TEST(CADenseCellStorage, DefaultConstructedIsEmpty)
75 EXPECT_EQ(s.
size(), 0u);
79 EXPECT_EQ(s.
data(),
nullptr);
82TEST(CADenseCellStorage, ConstructorAllocatesAndInitialises)
85 EXPECT_EQ(s.size(), 12u);
86 EXPECT_EQ(s.size(0), 3u);
87 EXPECT_EQ(s.size(1), 4u);
90 EXPECT_EQ(s.at(i, j), 7);
93TEST(CADenseCellStorage, RowMajorLinearIndex)
97 EXPECT_EQ(s.linear_index({ 0, 0 }), 0u);
98 EXPECT_EQ(s.linear_index({ 0, 3 }), 3u);
99 EXPECT_EQ(s.linear_index({ 1, 0 }), 4u);
100 EXPECT_EQ(s.linear_index({ 2, 3 }), 11u);
103TEST(CADenseCellStorage, SetAndAtRoundTrip2D)
107 EXPECT_EQ(s.at(1, 2), 42);
108 EXPECT_EQ(s.at(0, 0), 0);
110 EXPECT_EQ(s.data()[1 * 4 + 2], 42);
113TEST(CADenseCellStorage, SetAndAt3D)
118 EXPECT_EQ(s.at(1, 0, 1), 9);
119 EXPECT_EQ(s.at(0, 1, 1), 5);
120 EXPECT_EQ(s.size(), 8u);
123TEST(CADenseCellStorage, OneDimensional)
127 EXPECT_EQ(s.at(i), -1);
129 EXPECT_EQ(s.at(2), 100);
132TEST(CADenseCellStorage, OutOfRangeThrows)
135 EXPECT_THROW(s.at(3, 0), std::out_of_range);
136 EXPECT_THROW(s.at(0, 4), std::out_of_range);
137 EXPECT_THROW(s.at(-1, 0), std::out_of_range);
138 EXPECT_THROW(s.at(0, -1), std::out_of_range);
139 EXPECT_THROW(s.set({ -1, 0 }, 1), std::out_of_range);
140 EXPECT_THROW(s.size(2), std::out_of_range);
143TEST(CADenseCellStorage, FillResetsAllCells)
146 s.
set(0, 0, 1); s.set(1, 1, 2); s.set(2, 2, 3);
150 EXPECT_EQ(s.at(i, j), 99);
153TEST(CADenseCellStorage, SwapIsConstantTimeAndExchanges)
157 const int * pa = a.
data();
158 const int * pb = b.data();
162 EXPECT_EQ(a.size(0), 4u);
163 EXPECT_EQ(a.size(1), 4u);
164 EXPECT_EQ(b.size(0), 3u);
165 EXPECT_EQ(b.size(1), 3u);
167 EXPECT_EQ(a.data(), pb);
168 EXPECT_EQ(b.data(), pa);
169 EXPECT_EQ(a.at(0, 0), 2);
170 EXPECT_EQ(b.at(0, 0), 1);
173TEST(CADenseCellStorage, FreeFunctionSwap)
178 EXPECT_EQ(a.at(0, 0), 9);
179 EXPECT_EQ(b.at(0, 0), 1);
182TEST(CADenseCellStorage, ViewMatchesShapeAndAliasesTheSameBuffer)
186 EXPECT_EQ(v.
rank(), 2u);
187 EXPECT_EQ(v.extent(0), 3u);
188 EXPECT_EQ(v.extent(1), 4u);
189 EXPECT_EQ(v.data_handle(), s.
data());
192TEST(CADenseCellStorage, EmptyStorageViewReturnsEmptyView)
196 EXPECT_EQ(v.size(), 0u);
197 EXPECT_EQ(v.data_handle(),
nullptr);
200TEST(CADenseCellStorage, WriteThroughViewIsVisibleThroughStorageApi)
205 EXPECT_EQ(s.at(1, 2), 42);
208TEST(CADenseCellStorage, WriteThroughStorageIsVisibleThroughView)
213 EXPECT_EQ(v(0, 0), 7);
216TEST(CADenseCellStorage, ConstStorageYieldsReadOnlyView)
221 EXPECT_EQ(cv(0, 0), 5);
222 static_assert(std::is_const_v<std::remove_reference_t<
decltype(cv(0, 0))>>);
225TEST(CADenseCellStorage, ViewRowMajorOrderMatchesLinearIndex)
230 for (
ca_size_t i = 0; i < v.extent(0); ++i)
231 for (
ca_size_t j = 0; j < v.extent(1); ++j)
237 EXPECT_EQ(s.at(i, j),
next++);
240TEST(CADenseCellStorage, ViewOverThreeDimensionalStorage)
245 EXPECT_EQ(s.at(1, 0, 1), 99);
246 EXPECT_EQ(v.
size(), s.size());
249TEST(CADenseCellStorage, MoveTransfersOwnership)
252 const int * p = a.
data();
254 EXPECT_EQ(b.
size(), 9u);
255 EXPECT_EQ(b.
data(), p);
256 EXPECT_EQ(b.
at(0, 0), 4);
263TEST(CABitCellStorage, DefaultConstructedIsEmpty)
266 EXPECT_EQ(s.
size(), 0u);
270TEST(CABitCellStorage, ConstructorInitialisesValue)
273 EXPECT_EQ(s.size(), 35u);
276 EXPECT_TRUE(s.at(i, j));
279TEST(CABitCellStorage, SetAndAtRoundTrip)
286 EXPECT_EQ(s.at(i, j),
287 (i == 1
and j == 2) or (i == 3
and j == 3));
290TEST(CABitCellStorage, RowMajorLinearIndex)
293 EXPECT_EQ(s.linear_index({ 0, 0 }), 0u);
294 EXPECT_EQ(s.linear_index({ 0, 4 }), 4u);
295 EXPECT_EQ(s.linear_index({ 2, 4 }), 14u);
298TEST(CABitCellStorage, FillToggles)
303 EXPECT_TRUE(s.at(i));
306 EXPECT_FALSE(s.at(i));
309TEST(CABitCellStorage, SwapExchangesContentsAndExtents)
314 EXPECT_EQ(a.size(0), 4u);
315 EXPECT_EQ(b.size(0), 3u);
316 EXPECT_FALSE(a.at(0, 0));
317 EXPECT_TRUE(b.at(0, 0));
320TEST(CABitCellStorage, OutOfRangeThrows)
323 EXPECT_THROW(s.at(3, 0), std::out_of_range);
324 EXPECT_THROW(s.at(-1, 0), std::out_of_range);
325 EXPECT_THROW(s.set({ 0, 3 },
true), std::out_of_range);
328TEST(CABitCellStorage, ThreeDimensional)
331 s.
set(1, 1, 1,
true);
332 EXPECT_TRUE(s.at(1, 1, 1));
333 EXPECT_FALSE(s.at(0, 0, 0));
334 EXPECT_EQ(s.size(), 8u);
337TEST(CABitCellStorage, ExtentsProductOverflowThrows)
339 constexpr ca_size_t max = std::numeric_limits<ca_size_t>::max();
341 std::overflow_error);
Bit-packed row-major storage for N-dimensional binary CAs.
void fill(const bool value)
Set every cell to value.
ca_size_t size() const noexcept
static constexpr std::size_t dimension() noexcept
void swap(Bit_Cell_Storage &other) noexcept
Swap contents with another Bit_Cell_Storage instance.
void set(const coord_type &c, bool v)
Write the cell at coordinate c.
Row-major dense storage for N-dimensional cellular automata.
ca_size_t size() const noexcept
static constexpr std::size_t dimension() noexcept
view_type view() noexcept
Return a mutable Aleph::mdspan view of this storage.
const extents_type & extents() const noexcept
T at(const coord_type &c) const
Read access through a multi-index.
void set(const coord_type &c, const T &v)
Write access through a multi-index.
static constexpr std::size_t rank
Number of axes known at compile time.
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_max_function > > max(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
std::ptrdiff_t ca_index_t
Signed coordinate component used by lattices and neighborhoods.
std::size_t ca_size_t
Unsigned size component used for extents and counts.
Main namespace for Aleph-w library functions.
T & swap(T &t1, T &t2)
Generic swap using object's swap method.
and
Check uniqueness with explicit hash + equality functors.
void next()
Advance all underlying iterators (bounds-checked).
Bit-packed dense storage for boolean cellular automata.
C++20 concepts for the Cellular Automata module.
Dense, contiguous storage for cellular automata cells (1D/2D/3D).