54#include <gtest/gtest.h>
64using namespace testing;
78 for (
int i = 0; i < 5; ++i) {
82 for (
int i = 0; i < 3; ++i) {
101 for (
int i = 0; i < 5; ++i) {
122 for (
int i = 0; i < 5; ++i) {
123 EXPECT_DOUBLE_EQ(v.
get_entry(i),
static_cast<double>(i + 1));
234 EXPECT_THROW(v.
set_entries({0, 1}, {1.0, 2.0, 3.0}), std::exception);
243 ASSERT_NE(ptr,
nullptr);
244 EXPECT_DOUBLE_EQ(*ptr, 3.14);
247 EXPECT_EQ(null_ptr,
nullptr);
288 EXPECT_TRUE(v.
are_equal(1.0000001, 1.0000002));
389 for (
int i = 0; i < 5; ++i) {
420 EXPECT_THROW(v / 0.0, std::exception);
463 EXPECT_DOUBLE_EQ(result, 32.0);
476 double result = v1 * v2;
478 EXPECT_DOUBLE_EQ(result, 11.0);
490 double result = v1 * v2;
492 EXPECT_DOUBLE_EQ(result, 0.0);
502 double result = v1 * v2;
504 EXPECT_DOUBLE_EQ(result, 0.0);
520 EXPECT_TRUE(v1 == v2);
530 EXPECT_TRUE(v1 == v2);
540 EXPECT_TRUE(v1 != v2);
547 EXPECT_TRUE(v1 == v2);
559 EXPECT_DOUBLE_EQ(val, 3.14);
561 double val_zero = v[1];
562 EXPECT_DOUBLE_EQ(val_zero, 0.0);
588 EXPECT_DOUBLE_EQ(val, 3.14);
612 while (it.has_curr()) {
633 EXPECT_EQ(list.
size(), 3);
642 std::string str = v.
to_str();
644 EXPECT_FALSE(str.empty());
674 EXPECT_DOUBLE_EQ(v3.
get_entry(
"x"), 4.0);
675 EXPECT_DOUBLE_EQ(v3.
get_entry(
"y"), 2.0);
676 EXPECT_DOUBLE_EQ(v3.
get_entry(
"z"), 4.0);
708 EXPECT_DOUBLE_EQ(v.
get_entry(2), -1e100);
723 EXPECT_DOUBLE_EQ(result.
get_entry(0), 20.0);
724 EXPECT_DOUBLE_EQ(result.
get_entry(1), 20.0);
725 EXPECT_DOUBLE_EQ(result.
get_entry(2), 30.0);
757 while (it.has_curr()) {
773 EXPECT_EQ(d.size(), 5);
780TEST(VectorFloatTest, FloatType) {
794TEST(VectorFloatTest, FloatArithmetic) {
824 EXPECT_DOUBLE_EQ(
sum.get_entry(0), 1.0);
825 EXPECT_DOUBLE_EQ(
sum.get_entry(1), 3.0);
826 EXPECT_DOUBLE_EQ(
sum.get_entry(2), 4.0);
827 EXPECT_DOUBLE_EQ(
sum.get_entry(3), 1.0);
831 EXPECT_DOUBLE_EQ(scaled.
get_entry(0), 0.5);
832 EXPECT_DOUBLE_EQ(scaled.
get_entry(1), 1.5);
835 double dot = v1 * v2;
836 EXPECT_DOUBLE_EQ(dot, 2.0 + 3.0);
840 EXPECT_TRUE(
copy == v1);
844 EXPECT_FALSE(
copy == v1);
845 EXPECT_DOUBLE_EQ(
copy.get_entry(0), 10.0);
849 EXPECT_DOUBLE_EQ(neg.
get_entry(0), -1.0);
850 EXPECT_DOUBLE_EQ(neg.
get_entry(1), -2.0);
851 EXPECT_DOUBLE_EQ(neg.
get_entry(2), -3.0);
863 std::vector<double> ret;
864 v.
to_list().for_each([&ret] (
double x) { ret.push_back(x); });
869TEST(VectorProxy, AssigningAZeroEntryClearsTheTarget)
884TEST(VectorProxy, AssignsAnLvalue)
893 const double zero = 0;
897 const double tiny = 1e-9;
902TEST(VectorProxy, KeepsItsOwnKey)
914 auto q =
w[std::string(
"two")];
916 EXPECT_EQ(
w.get_entry(
"two"), 2.0);
919TEST(VectorProxy, ReadsWhatItWasAssigned)
926 EXPECT_EQ(
static_cast<double>(p), 9.0);
933TEST(VectorProxy, SurvivesTheGrowthOfTheTable)
941 for (
int i = 1; i < 5000; ++i)
945 EXPECT_EQ(
static_cast<double>(p), 2.0);
947 for (
int i = 5000; i < 10000; ++i)
949 for (
int i = 5000; i < 10000; ++i)
960 EXPECT_EQ(cv[1], 2.0);
961 EXPECT_EQ(cv(1), 2.0);
962 EXPECT_EQ(cv[0], 0.0);
963 static_assert(std::is_same_v<
decltype(cv[1]),
double>);
971 EXPECT_EQ(values_of(v), (std::vector<double>{3, 1, 2}));
977 EXPECT_EQ(values_of(
w), (std::vector<double>{8, 0, 0, -1, 0}));
992 EXPECT_EQ((v + c).get_entry(3), 3.0);
995 EXPECT_EQ(values_of(
w), (std::vector<double>{1, 2, 3}));
1006 static_assert(not std::is_constructible_v<Vector<int, double>,
int>);
1007 static_assert(not std::is_constructible_v<Vector<int, double>,
size_t>);
1008 static_assert(not std::is_constructible_v<Vector<int, double>, int,
double>);
1009 static_assert(not std::is_constructible_v<Vector<int, double>, int,
1011 static_assert(std::is_constructible_v<Vector<int, double>,
IntRange>);
1012 static_assert(std::is_constructible_v<Vector<int, double>,
const IntRange &>);
1013 static_assert(std::is_constructible_v<Vector<int, double>,
1014 std::shared_ptr<AlDomain<int>>>);
1023 ::testing::InitGoogleTest(&argc, argv);
1024 return RUN_ALL_TESTS();
Exception handling system with formatted messages for Aleph-w.
Integer domain classes for sparse data structures.
Sparse vector with named elements.
Generic domain class based on hash set.
T * insert(const T &key)
Insert key, as HashSet::insert(), and forget the sorted keys.
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
size_t size() const noexcept
Return the number of items of the list, in constant time.
Sparse vector implementation with domain-based indexing.
NumType scalar_product(const Vector &v) const and AlMultiplicativeNumber< NumType >
The scalar product of this vector and v, over the same domain.
const Domain & get_domain() const noexcept
Get the domain over which this vector is defined.
bool are_equal(const NumType &n1, const NumType &n2) const noexcept
Check if two numeric values are equal within epsilon.
void set_entry(const T &i, const NumType &value)
Set a vector entry at given index.
const NumType & get_epsilon() const noexcept
Get the epsilon value used for zero comparisons.
NumType * search_entry(const T &i) noexcept
Search for an entry and return pointer to its value.
DynList< NumType > to_list() const
The values of the vector, in the order of the sorted keys of its domain.
Iterator get_it() noexcept
void set_epsilon(const NumType &e) noexcept
Set the epsilon value for zero comparisons.
NumType get_entry(const T &i)
Get vector entry at given index (non-const version)
void set_entries(std::initializer_list< T > ld, std::initializer_list< NumType > lr)
Set multiple entries using initializer lists.
const DomainPtr & get_domain_ptr() const noexcept
Return the shared pointer to the domain.
std::string to_str() const and AlStreamable< T > and
The keys over their values, in the order of the keys.
AlDomain< std::string > domain_str
Main namespace for Aleph-w library functions.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Integer range domain [start, end] with optional step.
TEST_F(VectorTest, DefaultConstructor)