Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ah-utils.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
32
38#include <gtest/gtest.h>
39#include <ahUtils.H>
40#include <vector>
41#include <fstream>
42#include <cstdint>
43#include <cstdio>
44#include <limits>
45#include <stdexcept>
46
47// Tests for is_even() and is_odd()
48TEST(AhUtilsTest, EvenOdd) {
49 EXPECT_TRUE(Aleph::is_even(0));
50 EXPECT_FALSE(Aleph::is_odd(0));
51 EXPECT_TRUE(Aleph::is_even(2));
52 EXPECT_FALSE(Aleph::is_odd(2));
53 EXPECT_FALSE(Aleph::is_even(1));
54 EXPECT_TRUE(Aleph::is_odd(1));
55 EXPECT_TRUE(Aleph::is_even(-4));
56 EXPECT_FALSE(Aleph::is_odd(-4));
57 EXPECT_FALSE(Aleph::is_even(-5));
58 EXPECT_TRUE(Aleph::is_odd(-5));
59}
60
61// Tests for nibble_to_char() and char_to_nibble()
62TEST(AhUtilsTest, NibbleConversion) {
63 for (int i = 0; i < 16; ++i) {
64 char c = Aleph::nibble_to_char(i);
65 EXPECT_EQ(i, Aleph::char_to_nibble(c));
66 }
67 EXPECT_EQ(Aleph::nibble_to_char(10), 'A');
68 EXPECT_EQ(Aleph::nibble_to_char(15), 'F');
69 EXPECT_EQ(Aleph::char_to_nibble('9'), 9);
70 EXPECT_EQ(Aleph::char_to_nibble('B'), 11);
71
72 // Assertion test (only in debug)
73 #ifndef NDEBUG
74 EXPECT_DEATH(Aleph::nibble_to_char(16), "");
75 EXPECT_DEATH(Aleph::char_to_nibble('G'), "");
76 #endif
77}
78
79// Tests for is_power_of_2() and next_power_of_2()
80TEST(AhUtilsTest, PowerOf2) {
81 EXPECT_TRUE(Aleph::is_power_of_2(1));
82 EXPECT_TRUE(Aleph::is_power_of_2(2));
83 EXPECT_TRUE(Aleph::is_power_of_2(16));
84 EXPECT_TRUE(Aleph::is_power_of_2(1024));
85 EXPECT_FALSE(Aleph::is_power_of_2(0));
86 EXPECT_FALSE(Aleph::is_power_of_2(3));
87 EXPECT_FALSE(Aleph::is_power_of_2(15));
88
89 EXPECT_EQ(Aleph::next_power_of_2(1), 1);
90 EXPECT_EQ(Aleph::next_power_of_2(2), 2);
91 EXPECT_EQ(Aleph::next_power_of_2(3), 4);
92 EXPECT_EQ(Aleph::next_power_of_2(7), 8);
93 EXPECT_EQ(Aleph::next_power_of_2(16), 16);
94 EXPECT_EQ(Aleph::next_power_of_2(17), 32);
95
96#if SIZE_MAX > UINT32_MAX
97 // Sizes beyond 32 bits, which they took as an unsigned long: on Windows
98 // it has 32 bits, so 2^32 became 0 and 2^40 + 2^20 became 2^20.
99 constexpr size_t big = size_t(1) << 40;
100 EXPECT_TRUE(Aleph::is_power_of_2(size_t(1) << 32));
101 EXPECT_TRUE(Aleph::is_power_of_2(big));
102 EXPECT_FALSE(Aleph::is_power_of_2(big + (size_t(1) << 20)));
103 EXPECT_EQ(Aleph::next_power_of_2(big - 1), big);
104 EXPECT_EQ(Aleph::next_power_of_2(big + 1), big << 1);
105#endif
106
107 // Above the largest power of 2 that a size_t holds there is none to
108 // return; next_power_of_2() used to loop forever there.
109 constexpr size_t top = (std::numeric_limits<size_t>::max() >> 1) + 1;
110 EXPECT_EQ(Aleph::next_power_of_2(top - 1), top);
111 EXPECT_EQ(Aleph::next_power_of_2(top), top);
112 EXPECT_THROW(Aleph::next_power_of_2(top + 1), std::overflow_error);
113 EXPECT_THROW(Aleph::next_power_of_2(std::numeric_limits<size_t>::max()),
114 std::overflow_error);
115}
116
117// Tests for median()
118TEST(AhUtilsTest, Median) {
120 EXPECT_EQ(*Aleph::median(1, 2, 3, cmp), 2);
121 EXPECT_EQ(*Aleph::median(3, 1, 2, cmp), 2);
122 EXPECT_EQ(*Aleph::median(2, 3, 1, cmp), 2);
123 EXPECT_EQ(*Aleph::median(1, 1, 2, cmp), 1);
124 EXPECT_EQ(*Aleph::median(2, 1, 1, cmp), 1);
125 EXPECT_EQ(*Aleph::median(1, 2, 1, cmp), 1);
126 EXPECT_EQ(*Aleph::median(5, 5, 5, cmp), 5);
127
128 // Test with temporary comparator (thanks to refactoring)
129 EXPECT_EQ(*Aleph::median(10, 20, 15, Aleph::less<int>()), 15);
130}
131
132// Tests for interpolate() and extrapolate_*()
133TEST(AhUtilsTest, Interpolation) {
134 EXPECT_DOUBLE_EQ(Aleph::interpolate(0, 10, 0, 100, 5), 50.0);
135 EXPECT_DOUBLE_EQ(Aleph::interpolate(1, 2, 10, 20, 1.5), 15.0);
136 EXPECT_DOUBLE_EQ(Aleph::extrapolate_left(1, 2, 10, 20, 0), 0.0);
137 EXPECT_DOUBLE_EQ(Aleph::extrapolate_right(1, 2, 10, 20, 3), 30.0);
138
139 EXPECT_DOUBLE_EQ(Aleph::pow2(3.0), 9.0);
140 EXPECT_DOUBLE_EQ(Aleph::pow3(2.0), 8.0);
141}
142
143// Tests for are_near()
144TEST(AhUtilsTest, AreNear) {
145 EXPECT_TRUE(Aleph::are_near(1.0, 1.000001, 1e-5));
146 EXPECT_FALSE(Aleph::are_near(1.0, 1.0001, 1e-5));
147 EXPECT_TRUE(Aleph::are_near(-1.0, -1.000001, 1e-5));
148 EXPECT_FALSE(Aleph::are_near(-1.0, -1.0001, 1e-5));
149
150 // Equal values are within any non-negative tolerance.
151 EXPECT_TRUE(Aleph::are_near(2.5, 2.5, 0.0));
152 // The comparison is inclusive at the tolerance boundary (|v1 - v2| <= e).
153 EXPECT_TRUE(Aleph::are_near(1.0, 1.5, 0.5));
154 // Symmetric in its first two arguments.
155 EXPECT_EQ(Aleph::are_near(3.0, 3.4, 0.5), Aleph::are_near(3.4, 3.0, 0.5));
156
157 // are_near must stay usable in constant expressions: it is constexpr and
158 // computes the absolute difference by hand because std::fabs is not
159 // constexpr in MSVC's STL under C++20.
160 static_assert(Aleph::are_near(1.0, 1.0005, 1e-3));
161 static_assert(not Aleph::are_near(1.0, 2.0, 1e-3));
162}
163
164// Tests for u_index and l_index
165TEST(AhUtilsTest, Indices) {
166 EXPECT_EQ(Aleph::u_index(10), 5);
167 EXPECT_EQ(Aleph::u_index(11), 5);
168 EXPECT_EQ(Aleph::l_index(5), 10);
169}
170
171// Tests for demangle and CLASSNAME_TO_STRING
172struct MyTestStruct {};
173TEST(AhUtilsTest, Demangle) {
174 std::string name = Aleph::demangle(typeid(int).name());
175 EXPECT_EQ(name, "int");
176
177 MyTestStruct s;
178 std::string className = CLASSNAME_TO_STRING(&s);
179 // The exact name may vary depending on the compiler/namespace, but it must contain MyTestStruct
180 EXPECT_TRUE(className.find("MyTestStruct") != std::string::npos);
181}
182
183// Tests for exists_file
184TEST(AhUtilsTest, ExistsFile) {
185 std::string filename = "test_exists_file.tmp";
186 std::ofstream outfile(filename);
187 outfile << "test";
188 outfile.close();
189
190 EXPECT_TRUE(Aleph::exists_file(filename));
191 std::remove(filename.c_str());
192 EXPECT_FALSE(Aleph::exists_file(filename));
193}
194
195// Tests for next_value, prev_value, is_normal_number
196TEST(AhUtilsTest, FloatUtils) {
197 double val = 1.0;
198 EXPECT_GT(Aleph::next_value(val), val);
199 EXPECT_LT(Aleph::prev_value(val), val);
200
201 EXPECT_TRUE(Aleph::is_normal_number(1.0));
202 EXPECT_TRUE(Aleph::is_normal_number(0.0));
203 EXPECT_TRUE(Aleph::is_normal_number(-0.0));
204
205 double inf = std::numeric_limits<double>::infinity();
206 double nan = std::numeric_limits<double>::quiet_NaN();
207
208 EXPECT_FALSE(Aleph::is_normal_number(inf));
209 EXPECT_FALSE(Aleph::is_normal_number(nan));
210}
211
212// Tests for error_msg
213TEST(AhUtilsTest, ErrorMsg) {
214 EXPECT_DEATH(Aleph::error_msg("Test Error"), "Test Error");
215}
216
217// Mock container to test Rvector
219 std::vector<int> data;
220
221 MockContainer(std::initializer_list<int> l) : data(l) {}
222
223 const int& get_first() const { return data.front(); }
224 const int& get_last() const { return data.back(); }
225
226 struct Iterator {
227 std::vector<int>::const_iterator it;
228 std::vector<int>::const_iterator end;
229
230 Iterator(const MockContainer& c) : it(c.data.begin()), end(c.data.end()) {}
231
232 bool has_curr() const { return it != end; }
233 const int& get_curr() const { return *it; }
234 void next() { ++it; }
235 };
236
237 Iterator get_it() const { return Iterator(*this); }
238 // For the second overload of Rvector that uses get_it(1)
239 Iterator get_it(int) const { return Iterator(*this); }
240};
241
242TEST(AhUtilsTest, Rvector) {
243 MockContainer c{1, 2, 3};
244
245 std::string r1 = Aleph::Rvector("vec", c);
246 EXPECT_EQ(r1, "vec <- c(1, 2, 3)");
247
248 // Note: The second overload of Rvector uses c.get_first() as the name
249 // and c.get_it(1) to iterate. In our mock, get_first returns int,
250 // so the name will be "1".
251 std::string r2 = Aleph::Rvector(c);
252 EXPECT_EQ(r2, "1 <- c(1, 2, 3)");
253}
254
255int main(int argc, char **argv) {
256 ::testing::InitGoogleTest(&argc, argv);
257 return RUN_ALL_TESTS();
258}
General utility functions and helpers.
#define CLASSNAME_TO_STRING(class_ptr)
Given a pointer, it returns the class name.
Definition ahUtils.H:282
int main()
#define TEST(name)
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4196
double next_value(const double val)
Return the next representable floating-point value to val
Definition ahUtils.H:445
size_t next_power_of_2(size_t x)
Return the smallest power of 2 that is not less than x.
Definition ahUtils.H:243
void error_msg(const std::string &msg)
Display message and abort program execution.
Definition ahUtils.H:288
bool is_power_of_2(size_t x)
Taken from http://stackoverflow.com/questions/3638431/determine-if-an-int-is-a-power-of-2-or-not-in-a...
Definition ahUtils.H:229
const T * median(const T &a, const T &b, const T &c, const Compare &cmp=Compare())
Return a pointer to the median value among three elements.
Definition ahUtils.H:85
constexpr bool are_near(const double v1, const double v2, const double e) noexcept
Return true if v1 is within absolute distance e of v2.
Definition ahUtils.H:468
double extrapolate_right(const double x1, const double x2, const double y1, const double y2, const double x)
Basic linear extrapolation.
Definition ahUtils.H:431
double interpolate(const double x1, const double x2, const double y1, const double y2, const double x)
Basic linear interpolation.
Definition ahUtils.H:367
constexpr size_t l_index(const size_t i)
Map a binary heap index to the index of its left child.
Definition ahUtils.H:218
constexpr size_t u_index(const size_t &i)
Map a binary heap index to the index of its parent.
Definition ahUtils.H:204
double prev_value(const double val)
Return the next representable floating-point value of val towards the smallest positive normal number...
Definition ahUtils.H:458
bool is_even(const long n)
Return true if n is even.
Definition ahUtils.H:106
bool is_odd(const long n)
Return true if n is odd.
Definition ahUtils.H:115
double pow2(const double x)
Return x^2.
Definition ahUtils.H:404
char nibble_to_char(const int i)
Convert a 4-bit nibble stored in an int to its hex character.
Definition ahUtils.H:126
bool exists_file(const std::string &name)
Return true if it exists a file of name
Definition ahUtils.H:298
int char_to_nibble(const char c)
Convert a hex character in 0..9A..F to its 4-bit nibble value.
Definition ahUtils.H:141
std::string Rvector(const std::string &name, const C &c)
Return a string with R specification of a vector with name and data stored in container c
Definition ahUtils.H:310
bool is_normal_number(const double n)
Return true if a floating-point number is normal or zero.
Definition ahUtils.H:509
std::string demangle(const char *name)
Given a linker symbol name generated by a c++ compiler, this functions decodes it into a user level n...
Definition ahUtils.H:263
double extrapolate_left(const double x1, const double x2, const double y1, const double y2, const double x)
Basic linear extrapolation.
Definition ahUtils.H:391
double pow3(const double x)
Return x^3.
Definition ahUtils.H:412
std::vector< int >::const_iterator end
Definition ah-utils.cc:228
Iterator(const MockContainer &c)
Definition ah-utils.cc:230
std::vector< int >::const_iterator it
Definition ah-utils.cc:227
bool has_curr() const
Definition ah-utils.cc:232
const int & get_curr() const
Definition ah-utils.cc:233
Iterator get_it() const
Definition ah-utils.cc:237
const int & get_last() const
Definition ah-utils.cc:224
MockContainer(std::initializer_list< int > l)
Definition ah-utils.cc:221
std::vector< int > data
Definition ah-utils.cc:219
const int & get_first() const
Definition ah-utils.cc:223
Iterator get_it(int) const
Definition ah-utils.cc:239
DynList< int > l