Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
gmpfrxx_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
38#include <gtest/gtest.h>
39
40#include <gmpfrxx.h>
41#include <htlist.H>
42#include <tpl_array.H>
43#include <tpl_sort_utils.H>
44
45#include <type_traits>
46#include <utility>
47#include <vector>
48
49// eepicgeom.C, inside libAleph, declares tiny_keys extern: every program that
50// links it defines the variable. This test does not use eepicgeom, but some
51// linkers (AppleClang) take a GMP template instantiation from eepicgeom.C.o,
52// which then needs tiny_keys.
53bool tiny_keys = false;
54
55using namespace Aleph;
56
57namespace
58{
59 // Values with many limbs, so that copying one must allocate.
60 mpz_class big_integer()
61 {
62 mpz_class z;
63 mpz_ui_pow_ui(z.get_mpz_t(), 3, 4000);
64 return z;
65 }
66
67 mpq_class big_rational()
68 {
69 mpq_class q;
70 mpz_ui_pow_ui(mpq_numref(q.get_mpq_t()), 3, 4000);
71 mpz_ui_pow_ui(mpq_denref(q.get_mpq_t()), 5, 3000);
72 return q;
73 }
74
75 /* Counts the allocations GMP and MPFR make, through the memory hook of
76 GMP. The counting functions wrap the ones installed before, so memory
77 taken with one set is freed correctly with the other. A copy of a value
78 that does not fit in the destination shows up as a reallocation; moves
79 and swaps make none. */
80 class Gmp_Alloc_Counter
81 {
82 static inline void *(*def_alloc)(size_t) = nullptr;
83 static inline void *(*def_realloc)(void *, size_t, size_t) = nullptr;
84 static inline void (*def_free)(void *, size_t) = nullptr;
85 static inline long allocs = 0;
86 static inline long reallocs = 0;
87
88 static void *count_alloc(size_t n) { ++allocs; return def_alloc(n); }
89 static void *count_realloc(void *p, size_t old_n, size_t n)
90 {
91 ++reallocs;
92 return def_realloc(p, old_n, n);
93 }
94 static void count_free(void *p, size_t n) { def_free(p, n); }
95
96 public:
97 Gmp_Alloc_Counter()
98 {
99 mpfr_mp_memory_cleanup(); // MPFR caches the memory functions of GMP
100 mp_get_memory_functions(&def_alloc, &def_realloc, &def_free);
101 mp_set_memory_functions(count_alloc, count_realloc, count_free);
102 reset();
103 }
104
105 ~Gmp_Alloc_Counter()
106 {
107 mpfr_mp_memory_cleanup();
108 mp_set_memory_functions(def_alloc, def_realloc, def_free);
109 }
110
111 void reset() { allocs = reallocs = 0; }
112 long num_allocs() const { return allocs; }
113 long num_reallocs() const { return reallocs; }
114 long total() const { return allocs + reallocs; }
115 };
116}
117
118TEST(Gmpfrxx, noexcept_specifications)
119{
120 // Move assignment and swap exchange the structs: nothing can throw.
121 static_assert(std::is_nothrow_move_assignable_v<mpz_class>);
122 static_assert(std::is_nothrow_move_assignable_v<mpq_class>);
123 static_assert(std::is_nothrow_move_assignable_v<mpfr_class>);
124 static_assert(std::is_nothrow_swappable_v<mpz_class>);
125 static_assert(std::is_nothrow_swappable_v<mpq_class>);
126 static_assert(std::is_nothrow_swappable_v<mpfr_class>);
127
128 // Move construction re-initializes the source. For a rational (the limb of
129 // the denominator) and a float (the significand) that allocates, so they
130 // are not noexcept, as in gmpxx.h. GMP 6.2 initializes integers lazily.
131 static_assert(std::is_move_constructible_v<mpq_class>);
132 static_assert(not std::is_nothrow_move_constructible_v<mpq_class>);
133 static_assert(std::is_move_constructible_v<mpfr_class>);
134 static_assert(not std::is_nothrow_move_constructible_v<mpfr_class>);
135#if __GNU_MP_VERSION > 6 || (__GNU_MP_VERSION == 6 && __GNU_MP_VERSION_MINOR >= 2)
136 static_assert(std::is_nothrow_default_constructible_v<mpz_class>);
137 static_assert(std::is_nothrow_move_constructible_v<mpz_class>);
138#endif
139}
140
141TEST(Gmpfrxx, move_construction_takes_the_value_and_leaves_zero)
142{
143 mpz_class z = big_integer();
144 const mpz_class z_copy = z;
145 mpz_class z2(std::move(z));
146 EXPECT_EQ(z2, z_copy);
147 EXPECT_EQ(z, 0);
148 z = 5; // the moved-from object is usable
149 EXPECT_EQ(z, 5);
150
151 mpq_class q = big_rational();
152 const mpq_class q_copy = q;
153 mpq_class q2(std::move(q));
154 EXPECT_EQ(q2, q_copy);
155 EXPECT_EQ(q, 0);
156 EXPECT_EQ(q.get_den(), 1);
157 q = mpq_class("2/3");
158 EXPECT_EQ(q, mpq_class("2/3"));
159
160 mpfr_class f(1.5, 256);
161 mpfr_class f2(std::move(f));
162 EXPECT_EQ(f2.get_d(), 1.5);
163 EXPECT_EQ(f2.get_prec(), 256ul);
164 EXPECT_EQ(f.get_d(), 0.0);
165 EXPECT_EQ(f.get_prec(), 256ul); // zero, with the same precision
166 f = 2.5;
167 EXPECT_EQ(f.get_d(), 2.5);
168}
169
170TEST(Gmpfrxx, move_assignment_exchanges_the_values)
171{
172 mpq_class a = big_rational(), b("2/3");
173 const mpq_class a_copy = a;
174 b = std::move(a);
175 EXPECT_EQ(b, a_copy);
176 EXPECT_EQ(a, mpq_class("2/3"));
177
178 // The precision travels with the value; copy assignment would keep the
179 // precision of the destination.
180 mpfr_class x(1.5, 64), y(2.5, 256);
181 y = std::move(x);
182 EXPECT_EQ(y.get_d(), 1.5);
183 EXPECT_EQ(y.get_prec(), 64ul);
184 EXPECT_EQ(x.get_d(), 2.5);
185 EXPECT_EQ(x.get_prec(), 256ul);
186}
187
188TEST(Gmpfrxx, self_move_assignment_is_harmless)
189{
190 mpq_class q = big_rational();
191 const mpq_class q_copy = q;
192 mpq_class & same = q;
193 q = std::move(same);
194 EXPECT_EQ(q, q_copy);
195
196 mpfr_class f(1.5, 128);
197 mpfr_class & same_f = f;
198 f = std::move(same_f);
199 EXPECT_EQ(f.get_d(), 1.5);
200 EXPECT_EQ(f.get_prec(), 128ul);
201}
202
203TEST(Gmpfrxx, swap)
204{
205 mpz_class a = big_integer(), b = 7;
206 swap(a, b); // the overload of gmpfrxx.h, found by ADL
207 EXPECT_EQ(a, 7);
208 EXPECT_EQ(b, big_integer());
209 std::swap(a, b); // the generic one works too
210 EXPECT_EQ(a, big_integer());
211 EXPECT_EQ(b, 7);
212
213 mpfr_class x(1.5, 64), y(2.5, 256);
214 x.swap(y);
215 EXPECT_EQ(x.get_d(), 2.5);
216 EXPECT_EQ(x.get_prec(), 256ul);
217 EXPECT_EQ(y.get_d(), 1.5);
218 EXPECT_EQ(y.get_prec(), 64ul);
219}
220
221TEST(Gmpfrxx, moving_does_not_copy_the_limbs)
222{
223 mpq_class src = big_rational(), dst;
224 Gmp_Alloc_Counter counter;
225
226 dst = src; // a copy must grow the limbs of the destination
227 EXPECT_GT(counter.total(), 0);
228
229 counter.reset();
230 mpq_class moved(std::move(src)); // re-initializing src: one allocation
231 EXPECT_EQ(counter.num_reallocs(), 0);
232 EXPECT_LE(counter.num_allocs(), 2);
233
234 counter.reset();
235 dst = std::move(moved); // an exchange
236 EXPECT_EQ(counter.total(), 0);
237
238 counter.reset();
239 swap(dst, src);
240 EXPECT_EQ(counter.total(), 0);
241
242 mpz_class z = big_integer();
243 counter.reset();
244 mpz_class z2(std::move(z));
245#if __GNU_MP_VERSION > 6 || (__GNU_MP_VERSION == 6 && __GNU_MP_VERSION_MINOR >= 2)
246 EXPECT_EQ(counter.total(), 0);
247#else
248 EXPECT_EQ(counter.num_reallocs(), 0);
249#endif
250 counter.reset();
251 z = std::move(z2);
252 EXPECT_EQ(counter.total(), 0);
253
254 mpfr_class f(1.5, 4096);
255 counter.reset();
256 mpfr_class f2(std::move(f)); // re-initializing f: one allocation
257 EXPECT_EQ(counter.num_reallocs(), 0);
258 EXPECT_LE(counter.num_allocs(), 1);
259 counter.reset();
260 f = std::move(f2);
261 EXPECT_EQ(counter.total(), 0);
262}
263
264TEST(Gmpfrxx, rational_from_an_rvalue_integer_takes_its_limbs)
265{
266 mpz_class z = big_integer();
267 const mpz_class z_copy = z;
268 Gmp_Alloc_Counter counter;
269
270 mpq_class q(std::move(z));
271 EXPECT_EQ(counter.num_reallocs(), 0);
272 EXPECT_EQ(q.get_num(), z_copy);
273 EXPECT_EQ(q.get_den(), 1);
274 EXPECT_EQ(z, 0);
275
276 z = z_copy;
277 q = mpq_class("2/3");
278 counter.reset();
279 q = std::move(z); // by exchange with the numerator
280 EXPECT_EQ(counter.total(), 0);
281 EXPECT_EQ(q, z_copy);
282 EXPECT_EQ(z, 2);
283}
284
285TEST(Gmpfrxx, array_growth_moves_the_items)
286{
287 // MemArray reallocates its storage by move assignment: growing an Array of
288 // big rationals copies no limb. The values are built before counting.
289 std::vector<mpq_class> values(100, big_rational());
291 Gmp_Alloc_Counter counter;
292 for (auto & v : values)
293 a.append(std::move(v));
294 EXPECT_EQ(counter.num_reallocs(), 0);
295 EXPECT_EQ(a.size(), 100u);
296 EXPECT_EQ(a[99], big_rational());
297}
298
299TEST(Gmpfrxx, list_append_moves_the_items)
300{
301 std::vector<mpq_class> values(100, big_rational());
303 Gmp_Alloc_Counter counter;
304 for (auto & v : values)
305 l.append(std::move(v));
306 EXPECT_EQ(counter.num_reallocs(), 0);
307 EXPECT_EQ(l.size(), 100u);
308 EXPECT_EQ(l.get_last(), big_rational());
309}
310
311TEST(Gmpfrxx, sorting_moves_the_items)
312{
314 for (int i = 99; i >= 0; --i)
315 a.append(big_rational() + i);
316 Gmp_Alloc_Counter counter;
317 quicksort_op(a);
318 EXPECT_EQ(counter.num_reallocs(), 0);
319 for (int i = 0; i < 100; ++i)
320 EXPECT_EQ(a[i], big_rational() + i);
321}
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:450
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:331
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
T & append(const T &item)
Definition htlist.H:1362
T & get_last()
Return the last item of the list.
Definition htlist.H:1529
size_t size() const noexcept
Return the number of items of the list, in constant time.
Definition htlist.H:1264
unsigned long int get_prec() const
Definition gmpfrxx.h:2282
const mpz_class & get_num() const
Definition gmpfrxx.h:2236
const mpz_class & get_den() const
Definition gmpfrxx.h:2240
mpq_srcptr get_mpq_t() const
Definition gmpfrxx.h:2247
mpz_srcptr get_mpz_t() const
Definition gmpfrxx.h:2031
#define TEST(name)
__gmp_expr< mpq_t, mpq_t > mpq_class
Definition gmpfrxx.h:2270
bool tiny_keys
Global flag to enable tiny font size for keys/labels.
Singly linked list implementations with head-tail access.
static mpfr_t y
Definition mpfr_mul_d.c:3
Main namespace for Aleph-w library functions.
T & swap(T &t1, T &t2)
Generic swap using object's swap method.
Definition ahTypes.H:123
void quicksort_op(C< T > &a, const Compare &cmp=Compare(), const size_t threshold=Quicksort_Threshold)
Optimized quicksort for containers using operator().
static long counter
Definition test-splice.C:40
Dynamic array container with automatic resizing.
Comprehensive sorting algorithms and search utilities for Aleph-w.
DynList< int > l