38#include <gtest/gtest.h>
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);
80 class Gmp_Alloc_Counter
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;
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)
92 return def_realloc(p, old_n, n);
94 static void count_free(
void *p,
size_t n) { def_free(p, n); }
99 mpfr_mp_memory_cleanup();
100 mp_get_memory_functions(&def_alloc, &def_realloc, &def_free);
101 mp_set_memory_functions(count_alloc, count_realloc, count_free);
107 mpfr_mp_memory_cleanup();
108 mp_set_memory_functions(def_alloc, def_realloc, def_free);
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; }
118TEST(Gmpfrxx, noexcept_specifications)
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>);
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>);
141TEST(Gmpfrxx, move_construction_takes_the_value_and_leaves_zero)
146 EXPECT_EQ(z2, z_copy);
154 EXPECT_EQ(q2, q_copy);
162 EXPECT_EQ(f2.
get_d(), 1.5);
164 EXPECT_EQ(f.
get_d(), 0.0);
167 EXPECT_EQ(f.
get_d(), 2.5);
170TEST(Gmpfrxx, move_assignment_exchanges_the_values)
175 EXPECT_EQ(b, a_copy);
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);
188TEST(Gmpfrxx, self_move_assignment_is_harmless)
194 EXPECT_EQ(q, q_copy);
198 f = std::move(same_f);
199 EXPECT_EQ(f.
get_d(), 1.5);
208 EXPECT_EQ(b, big_integer());
210 EXPECT_EQ(a, big_integer());
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);
221TEST(Gmpfrxx, moving_does_not_copy_the_limbs)
231 EXPECT_EQ(
counter.num_reallocs(), 0);
232 EXPECT_LE(
counter.num_allocs(), 2);
235 dst = std::move(moved);
245#if __GNU_MP_VERSION > 6 || (__GNU_MP_VERSION == 6 && __GNU_MP_VERSION_MINOR >= 2)
248 EXPECT_EQ(
counter.num_reallocs(), 0);
257 EXPECT_EQ(
counter.num_reallocs(), 0);
258 EXPECT_LE(
counter.num_allocs(), 1);
264TEST(Gmpfrxx, rational_from_an_rvalue_integer_takes_its_limbs)
271 EXPECT_EQ(
counter.num_reallocs(), 0);
272 EXPECT_EQ(q.
get_num(), z_copy);
281 EXPECT_EQ(q, z_copy);
285TEST(Gmpfrxx, array_growth_moves_the_items)
289 std::vector<mpq_class> values(100, big_rational());
292 for (
auto & v : values)
294 EXPECT_EQ(
counter.num_reallocs(), 0);
295 EXPECT_EQ(a.
size(), 100u);
296 EXPECT_EQ(a[99], big_rational());
299TEST(Gmpfrxx, list_append_moves_the_items)
301 std::vector<mpq_class> values(100, big_rational());
304 for (
auto & v : values)
306 EXPECT_EQ(
counter.num_reallocs(), 0);
307 EXPECT_EQ(
l.
size(), 100u);
311TEST(Gmpfrxx, sorting_moves_the_items)
314 for (
int i = 99; i >= 0; --i)
315 a.
append(big_rational() + i);
318 EXPECT_EQ(
counter.num_reallocs(), 0);
319 for (
int i = 0; i < 100; ++i)
320 EXPECT_EQ(a[i], big_rational() + i);
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
T & append(const T &data)
Append a copy of data
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
T & get_last()
Return the last item of the list.
size_t size() const noexcept
Return the number of items of the list, in constant time.
unsigned long int get_prec() const
const mpz_class & get_num() const
const mpz_class & get_den() const
mpq_srcptr get_mpq_t() const
mpz_srcptr get_mpz_t() const
__gmp_expr< mpq_t, mpq_t > mpq_class
bool tiny_keys
Global flag to enable tiny font size for keys/labels.
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
T & swap(T &t1, T &t2)
Generic swap using object's swap method.
void quicksort_op(C< T > &a, const Compare &cmp=Compare(), const size_t threshold=Quicksort_Threshold)
Optimized quicksort for containers using operator().
Dynamic array container with automatic resizing.
Comprehensive sorting algorithms and search utilities for Aleph-w.