Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_ca_concepts_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
43# include <array>
44# include <cstdint>
45# include <gtest/gtest.h>
46
47# include <ca-traits.H>
48# include <tpl_ca_concepts.H>
49
50using namespace Aleph;
51using namespace Aleph::CA;
52
53namespace
54{
55
56 // ---------------------------------------------------------------------
57 // Mock types used to validate the Phase 0 concepts. These are kept
58 // purposely minimal: they implement nothing beyond what the concepts
59 // require, so a regression that tightens a concept will surface here
60 // immediately.
61 // ---------------------------------------------------------------------
62
64 template <typename T>
65 struct Mock_Lattice
66 {
67 using state_type = T;
68 using coord_type = Coord_Vec<2>;
69
70 std::array<std::size_t, 2> extents { 0, 0 };
71 std::array<T, 16> cells { }; // up to 4x4 fits
72
73 static std::size_t dimension()
74 { return 2; }
75
76 std::size_t size(std::size_t d) const { return extents[d]; }
77
78 T at(const coord_type & c) const
79 {
80 return cells[c[0] * extents[1] + c[1]];
81 }
82
83 void set(const coord_type & c, const T & v)
84 {
85 cells[c[0] * extents[1] + c[1]] = v;
86 }
87 };
88
90 struct Mock_Neighborhood
91 {
92 using coord_type = Coord_Vec<2>;
93 static constexpr std::size_t rank_v = 2;
94 static constexpr std::size_t size_v = 8;
95
96 std::size_t radius() const { return 1; }
97 std::size_t size() const { return size_v; }
98
99 template <typename F>
100 static void for_each_offset(const coord_type &, F &&)
101 {}
102 };
103
105 struct Identity_Rule
106 {
107 template <typename S>
108 S operator()(S s) const { return s; }
109
110 template <typename S>
111 S operator()(S s, Neighbor_View<S>) const { return s; }
112 };
113
114 // ---------------------------------------------------------------------
115 // Compile-time checks. If any of these fails the file does not compile,
116 // which is exactly the signal we want from the acceptance criteria.
117 // ---------------------------------------------------------------------
118
119 static_assert(CellState<bool>);
120 static_assert(CellState<std::uint8_t>);
121 static_assert(CellState<int>);
122
123 static_assert(Coord<Coord_Vec<1>, 1>);
124 static_assert(Coord<Coord_Vec<2>, 2>);
125 static_assert(Coord<Coord_Vec<3>, 3>);
126
127 static_assert(LatticeLike<Mock_Lattice<bool>>);
129
131
134
135 // Negative checks: things that must NOT satisfy the concepts.
136 struct Not_A_Lattice {};
137 static_assert(not LatticeLike<Not_A_Lattice>);
138
139 struct Not_A_Neighborhood {};
141
142} // namespace
143
144
146{
148 lat.extents = { 4, 4 };
149 for (std::size_t i = 0; i < lat.cells.size(); ++i)
150 lat.cells[i] = 0;
151
152 const Coord_Vec<2> c { 1, 2 };
153 lat.set(c, 7u);
154 EXPECT_EQ(lat.at(c), 7u);
155 EXPECT_EQ(lat.dimension(), 2u);
156 EXPECT_EQ(lat.size(0), 4u);
157 EXPECT_EQ(lat.size(1), 4u);
158}
159
161{
162 Mock_Neighborhood h;
163 EXPECT_EQ(h.radius(), 1u);
164 EXPECT_EQ(h.size(), 8u);
165}
166
168{
169 Identity_Rule r;
170 EXPECT_EQ(r(static_cast<std::uint8_t>(42)), static_cast<std::uint8_t>(42));
171 EXPECT_TRUE(r(true));
172 EXPECT_FALSE(r(false));
173}
174
176{
177 // The boundary tags from ca-traits.H are empty types with sizeof == 1
178 // (the minimum C++ object size). They carry no data members and can be
179 // passed as template tags without any runtime cost.
180 EXPECT_EQ(sizeof(OpenBoundary), 1u);
181 EXPECT_EQ(sizeof(ToroidalBoundary), 1u);
182 EXPECT_EQ(sizeof(ReflectiveBoundary), 1u);
183 EXPECT_EQ(sizeof(RowMajor), 1u);
184 EXPECT_EQ(sizeof(ColumnMajor), 1u);
185
187 EXPECT_EQ(B::value, 0u);
188}
long double h
Definition btreepic.C:154
Common typedefs and tag types for the Cellular Automata module.
Shape (per-axis sizes) of an mdspan, mixing compile-time and run-time extents.
Definition ah-mdspan.H:303
constexpr extents() noexcept
All-static-or-default-initialised extents (dynamic axes start at 0).
Definition ah-mdspan.H:329
#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
@ S
Susceptible.
std::span< const T > Neighbor_View
Read-only view over a contiguous range of neighbour values.
Definition ca-traits.H:90
std::array< ca_index_t, N > Coord_Vec
Default coordinate vector.
Definition ca-traits.H:69
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
size_t size(Node *root) noexcept
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:105
Iterate the lattice in column-major (Fortran) order.
Definition ca-traits.H:183
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
Iterate the lattice in row-major (C) order.
Definition ca-traits.H:178
The lattice wraps around on every axis.
Definition ca-traits.H:124
gsl_rng * r
C++20 concepts for the Cellular Automata module.