Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
al_concepts_test.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
39#include <gtest/gtest.h>
40
41#include <gmpxx.h>
42
43#include <ostream>
44#include <string>
45#include <type_traits>
46
47#include <al-matrix.H>
48
49// GoogleTest prints the values of a failed check, and it would print an
50// mpq_class with the operator << that libgmpxx compiles, which is not there
51// for libc++ on Linux, where libgmpxx is built with libstdc++, nor on
52// Windows, where gmpxx.h imports it from a DLL built by MinGW. get_str()
53// calls only the C library of GMP.
54void PrintTo(const mpq_class & q, std::ostream * os)
55{
56 *os << q.get_str();
57}
58
59using namespace Aleph;
60
61namespace
62{
63 // A key without <: hashed by its bytes and compared with ==
64 struct Unordered
65 {
66 int x = 0;
67 bool operator ==(const Unordered &) const = default;
68 };
69
70 // A key without ==
71 struct NoEquality
72 {
73 int x = 0;
74 };
75
76 // A key without a default constructor
77 struct NoDefault
78 {
79 int x;
80 NoDefault(int v) : x(v) {}
81 bool operator ==(const NoDefault &) const = default;
82 bool operator <(const NoDefault & o) const { return x < o.x; }
83 };
84
85 // A key that cannot be copied
86 struct NoCopy
87 {
88 int x = 0;
89 NoCopy() = default;
90 NoCopy(const NoCopy &) = delete;
91 NoCopy & operator =(const NoCopy &) = delete;
92 bool operator ==(const NoCopy &) const = default;
93 bool operator <(const NoCopy & o) const { return x < o.x; }
94 };
95
96 // A number that adds, subtracts and multiplies, but does not divide
97 struct NoDivision
98 {
99 double v = 0;
100 NoDivision() = default;
101 NoDivision(int i) : v(i) {}
102 NoDivision(double d) : v(d) {}
103 NoDivision operator -() const { return NoDivision(-v); }
104 NoDivision & operator +=(const NoDivision & o) { v += o.v; return *this; }
105 NoDivision & operator -=(const NoDivision & o) { v -= o.v; return *this; }
106 NoDivision & operator *=(const NoDivision & o) { v *= o.v; return *this; }
107 friend NoDivision operator +(NoDivision a, const NoDivision & b) { return a += b; }
108 friend NoDivision operator -(NoDivision a, const NoDivision & b) { return a -= b; }
109 friend NoDivision operator *(NoDivision a, const NoDivision & b) { return a *= b; }
110 friend bool operator ==(const NoDivision & a, const NoDivision & b) { return a.v == b.v; }
111 friend bool operator <(const NoDivision & a, const NoDivision & b) { return a.v < b.v; }
112 friend bool operator <=(const NoDivision & a, const NoDivision & b) { return a.v <= b.v; }
113 friend bool operator >=(const NoDivision & a, const NoDivision & b) { return a.v >= b.v; }
114 };
115
116 // A number that only compares: an entry can be set and read, nothing more
117 struct OnlyCompares
118 {
119 double v = 0;
120 OnlyCompares() = default;
121 OnlyCompares(int i) : v(i) {}
122 OnlyCompares(double d) : v(d) {}
123 OnlyCompares operator -() const { return OnlyCompares(-v); }
124 friend bool operator <(const OnlyCompares & a, const OnlyCompares & b) { return a.v < b.v; }
125 friend bool operator <=(const OnlyCompares & a, const OnlyCompares & b) { return a.v <= b.v; }
126 friend bool operator >=(const OnlyCompares & a, const OnlyCompares & b) { return a.v >= b.v; }
127 };
128
129 // A requires-expression outside a template is ill-formed where its
130 // requirements are not valid, instead of false: the checks go through
131 // these concepts, whose parameters make them dependent
132 template <class K> concept makes_a_domain = requires { typename AlDomain<K>; };
133 template <class K, class N> concept makes_a_vector = requires { typename Vector<K, N>; };
134 template <class R, class C, class N>
135 concept makes_a_matrix = requires { typename Matrix<R, C, N>; };
136 template <class D> concept has_keys = requires (const D & d) { d.keys(); };
137 template <class D> concept has_to_list = requires (const D & d) { d.to_list(); };
138 template <class D> concept has_to_str = requires (const D & d) { d.to_str(); };
139 template <class D> concept has_print = requires (const D & d) { d.print(); };
140 template <class M>
141 concept has_row_domain_list = requires (const M & m) { m.row_domain_list(); };
142 template <class V, class N> concept can_divide = requires (const V & v, const N & x) { v / x; };
143 template <class V, class N>
144 concept can_divide_by_scalar = requires (V & v, const N & x) { v.divide_by_scalar(x); };
145 template <class V> concept can_add = requires (const V & v) { v + v; };
146 template <class V> concept can_multiply = requires (const V & v) { v * v; };
147 template <class V> concept can_compare = requires (const V & v) { v == v; };
148 template <class V> concept can_negate = requires (const V & v) { -v; };
149 template <class M, class V>
150 concept can_multiply_a_vector = requires (const M & m, const V & v) { m * v; };
151}
152
153TEST(AlConcepts, TheKeysOfADomain)
154{
155 static_assert(AlDomainKey<int> and AlDomainKey<char> and AlDomainKey<std::string>);
156 static_assert(AlDomainKey<Unordered>);
157 static_assert(not AlDomainKey<NoEquality>);
158 static_assert(not AlDomainKey<NoDefault>);
159 static_assert(not AlDomainKey<NoCopy>);
160
161 static_assert(AlOrderedDomainKey<int> and AlOrderedDomainKey<std::string>);
162 static_assert(not AlOrderedDomainKey<Unordered>);
163
164 static_assert(AlStreamable<int> and AlStreamable<std::string>);
165 static_assert(not AlStreamable<Unordered>);
166
167 // The classes take only those keys
168 static_assert(makes_a_domain<Unordered>);
169 static_assert(not makes_a_domain<NoEquality>);
170 static_assert(not makes_a_vector<NoEquality, double>);
171 static_assert(not makes_a_matrix<int, NoEquality, double>);
172 static_assert(not makes_a_matrix<NoDefault, int, double>);
173 SUCCEED();
174}
175
176TEST(AlConcepts, TheNumbersOfAVector)
177{
178 static_assert(AlNumber<int> and AlNumber<float> and AlNumber<double>
179 and AlNumber<long double> and AlNumber<mpq_class>);
180 static_assert(AlNumber<NoDivision> and AlNumber<OnlyCompares>);
181 static_assert(not AlNumber<std::string>);
182 static_assert(not AlNumber<Unordered>);
183
184 static_assert(AlAdditiveNumber<double> and AlAdditiveNumber<mpq_class>
185 and AlAdditiveNumber<NoDivision>);
186 static_assert(not AlAdditiveNumber<OnlyCompares>);
187
188 static_assert(AlMultiplicativeNumber<double> and AlMultiplicativeNumber<mpq_class>
189 and AlMultiplicativeNumber<NoDivision>);
190 static_assert(not AlMultiplicativeNumber<OnlyCompares>);
191
192 static_assert(AlDivisibleNumber<double> and AlDivisibleNumber<int>
193 and AlDivisibleNumber<mpq_class>);
194 static_assert(not AlDivisibleNumber<NoDivision>);
195
196 // The classes take only those numbers
197 static_assert(not makes_a_vector<int, std::string>);
198 static_assert(not makes_a_matrix<int, int, std::string>);
199 SUCCEED();
200}
201
202TEST(AlConcepts, KeysWithoutOrderSetAndAddEntries)
203{
204 // A key without < makes a domain and vectors that set, read and add
205 // entries; what takes the keys in their order is not there
207 (void) d.insert(Unordered{1});
208 (void) d.insert(Unordered{2});
210 v.set_entry(Unordered{1}, 2.0);
211 w.set_entry(Unordered{1}, 3.0);
212 w.set_entry(Unordered{2}, 1.0);
213 v += w;
214 EXPECT_EQ(v.get_entry(Unordered{1}), 5.0);
215 EXPECT_EQ(v.get_entry(Unordered{2}), 1.0);
216 EXPECT_EQ(v * w, 16.0); // 5*3 + 1*1
217 v[Unordered{2}] = 4.0;
218 EXPECT_EQ(v(Unordered{2}), 4.0);
219
220 static_assert(has_keys<AlDomain<int>> and has_to_str<Vector<int, double>>);
221 static_assert(not has_keys<AlDomain<Unordered>>);
222 static_assert(not has_to_list<AlDomain<Unordered>>);
223 static_assert(not has_to_list<Vector<Unordered, double>>);
224 static_assert(not has_to_str<Vector<Unordered, double>>);
225 static_assert(not has_print<Vector<Unordered, double>>);
226 static_assert(not std::is_constructible_v<Vector<Unordered, double>,
227 const AlDomain<Unordered> &,
228 const DynList<double> &>);
229
231 m.set_entry(Unordered{1}, Unordered{2}, 7.0);
232 EXPECT_EQ(m.get_row_vector(Unordered{1}).get_entry(Unordered{2}), 7.0);
233 EXPECT_EQ(m.transpose().get_entry(Unordered{2}, Unordered{1}), 7.0);
234 static_assert(not has_row_domain_list<Matrix<Unordered, Unordered, double>>);
235 static_assert(not has_to_str<Matrix<Unordered, Unordered, double>>);
236}
237
238TEST(AlConcepts, NumbersWithoutDivisionAddAndMultiply)
239{
240 // A number without / adds and multiplies; the division is not there
241 const IntRange d(3);
242 Vector<int, NoDivision> v(d), w(d);
243 v.set_entry(0, NoDivision(2.0));
244 w.set_entry(0, NoDivision(3.0));
245 w.set_entry(2, NoDivision(1.0));
246 EXPECT_EQ((v + w).get_entry(0).v, 5.0);
247 EXPECT_EQ((w - v).get_entry(0).v, 1.0);
248 EXPECT_EQ((v * w).v, 6.0);
249 EXPECT_EQ((NoDivision(2.0) * w).get_entry(2).v, 2.0);
250
251 static_assert(can_divide<Vector<int, double>, double>);
252 static_assert(not can_divide<Vector<int, NoDivision>, NoDivision>);
253 static_assert(not can_divide_by_scalar<Vector<int, NoDivision>, NoDivision>);
254 // to_str() writes the numbers with std::to_string(), and print() with <<
255 static_assert(not has_to_str<Vector<int, NoDivision>>);
256 static_assert(not has_print<Vector<int, NoDivision>>);
257 static_assert(has_to_list<Vector<int, NoDivision>>);
258}
259
260TEST(AlConcepts, NumbersThatOnlyCompareSetAndReadEntries)
261{
262 // A number without arithmetic makes a vector and a matrix whose entries
263 // are set and read; sums, products and comparisons are not there
264 const IntRange d(2);
266 v.set_entry(1, OnlyCompares(4.0));
267 EXPECT_EQ(v.get_entry(1).v, 4.0);
268 EXPECT_EQ(v.get_entry(0).v, 0.0);
269 v.set_entry(1, OnlyCompares(0.0));
270 EXPECT_EQ(v.search_entry(1), nullptr);
271
272 static_assert(can_add<Vector<int, double>> and can_multiply<Vector<int, double>>
273 and can_compare<Vector<int, double>> and can_negate<Vector<int, double>>);
274 static_assert(not can_add<Vector<int, OnlyCompares>>);
275 static_assert(not can_multiply<Vector<int, OnlyCompares>>);
276 static_assert(not can_compare<Vector<int, OnlyCompares>>);
277 static_assert(not can_negate<Vector<int, OnlyCompares>>);
278
280 m.set_entry(0, 1, OnlyCompares(2.0));
281 EXPECT_EQ(m.get_entry(0, 1).v, 2.0);
282 static_assert(not can_add<Matrix<int, int, OnlyCompares>>);
283 static_assert(not can_multiply_a_vector<Matrix<int, int, OnlyCompares>,
285}
286
287TEST(AlConcepts, ExactRationalsComputeExactly)
288{
289 // mpq_class is an AlNumber: the entries add, multiply and divide exactly
290 const IntRange d(2);
291 Vector<int, mpq_class> v(d), w(d);
292 v.set_entry(0, mpq_class(1, 3));
293 w.set_entry(0, mpq_class(1, 6));
294 w.set_entry(1, mpq_class(2, 5));
295 EXPECT_EQ((v + w).get_entry(0), mpq_class(1, 2));
296 EXPECT_EQ(v * w, mpq_class(1, 18));
297 EXPECT_EQ((w / mpq_class(2)).get_entry(1), mpq_class(1, 5));
298
300 m.set_entry(0, 0, mpq_class(1, 2));
301 m.set_entry(1, 0, mpq_class(3));
302 const Vector<int, mpq_class> p = m * v;
303 EXPECT_EQ(p.get_entry(0), mpq_class(1, 6));
304 EXPECT_EQ(p.get_entry(1), mpq_class(1));
305
306 // to_str() writes the numbers with std::to_string(), which has none for
307 // mpq_class
308 static_assert(not has_to_str<Vector<int, mpq_class>>);
309}
bool operator>=(const Time &l, const Time &r)
Definition ah-time.H:173
bool operator<(const Time &l, const Time &r)
Definition ah-time.H:143
bool operator<=(const Time &l, const Time &r)
Definition ah-time.H:155
Sparse matrix with generic domains.
void PrintTo(const mpq_class &q, std::ostream *os)
long double w
Definition btreepic.C:153
Generic domain class based on hash set.
Definition al-domain.H:132
T * insert(const T &key)
Insert key, as HashSet::insert(), and forget the sorted keys.
Definition al-domain.H:162
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
Sparse matrix with generic row and column domains.
Definition al-matrix.H:70
Number Theoretic Transform over Z / MOD Z.
Definition ntt.H:115
Sparse vector implementation with domain-based indexing.
Definition al-vector.H:155
void set_entry(const T &i, const NumType &value)
Set a vector entry at given index.
Definition al-vector.H:381
NumType * search_entry(const T &i) noexcept
Search for an entry and return pointer to its value.
Definition al-vector.H:512
NumType get_entry(const T &i)
Get vector entry at given index (non-const version)
Definition al-vector.H:474
std::string get_str(int base=10) const
Definition gmpfrxx.h:2225
#define TEST(name)
__gmp_expr< mpq_t, mpq_t > mpq_class
Definition gmpfrxx.h:2270
Main namespace for Aleph-w library functions.
Gen_MultiPolynomial< C, M > operator+(const C &s, const Gen_MultiPolynomial< C, M > &p)
Scalar + polynomial (commutative).
bool operator==(const DynList< T > &l1, const DynList< T > &l2)
Equality operator for DynList.
Gen_MultiPolynomial< C, M > operator-(const C &s, const Gen_MultiPolynomial< C, M > &p)
Scalar - polynomial.
and
Check uniqueness with explicit hash + equality functors.
Matrix< Trow, Tcol, NumType > operator*(const NumType &scalar, const Matrix< Trow, Tcol, NumType > &m)
Scalar-matrix multiplication (scalar * matrix).
Definition al-matrix.H:1228
Integer range domain [start, end] with optional step.
Definition al-domain.H:311
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
size_t V