Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ah_convert_composition_test.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
42#include <ah-convert.H>
43#include <ah-stl-utils.H>
44
45#include <gtest/gtest.h>
46
47#include <vector>
48
49// Defined in ah_convert_composition_other.cc
50std::vector<int> other_unit_to_vector(const Aleph::DynList<int> & l);
51Aleph::DynList<int> other_unit_vector_to_DynList(const std::vector<int> & v);
52Aleph::DynArray<int> other_unit_vector_to_DynArray(const std::vector<int> & v);
53
54using namespace Aleph;
55
56namespace
57{
58 template <class C>
59 std::vector<int> items(const C & c)
60 {
61 std::vector<int> ret;
62 for (auto it = c.get_it(); it.has_curr(); it.next_ne())
63 ret.push_back(it.get_curr());
64 return ret;
65 }
66
67 // Only get_it(), which ah-convert.H accepted
68 struct Only_Aleph_Iterator
69 {
70 using Item_Type = int;
71
72 struct Iterator
73 {
74 const std::vector<int> * v;
75 size_t i = 0;
76 bool has_curr() const noexcept { return i < v->size(); }
77 void next_ne() noexcept { ++i; }
78 int get_curr() const { return (*v)[i]; }
79 };
80
81 std::vector<int> v;
82 Iterator get_it() const { return Iterator{&v}; }
83 };
84
85 // Only begin() and end(), which ah-stl-utils.H accepted
86 struct Only_Stl_Iterator
87 {
88 using Item_Type = int;
89
90 std::vector<int> v;
91 auto begin() const { return v.begin(); }
92 auto end() const { return v.end(); }
93 };
94}
95
96TEST(AhConvertComposition, BothUnitsConvertTheSameWay)
97{
98 const std::vector<int> v = {5, 3, 8, 1};
99
101 for (int i : v)
102 l.append(i);
103
104 EXPECT_EQ(to_vector(l), v);
105 EXPECT_EQ(other_unit_to_vector(l), v);
106
107 EXPECT_EQ(items(vector_to_DynList(v)), v);
108 EXPECT_EQ(items(other_unit_vector_to_DynList(v)), v);
109
112 ASSERT_EQ(a.size(), v.size());
113 ASSERT_EQ(b.size(), v.size());
114 for (size_t i = 0; i < v.size(); ++i)
115 {
116 EXPECT_EQ(a(i), v[i]);
117 EXPECT_EQ(b(i), v[i]);
118 }
119}
120
121TEST(AhConvertComposition, ToVectorTakesWhatEitherHeaderTook)
122{
123 EXPECT_EQ(to_vector(Only_Aleph_Iterator{{1, 2, 3}}), (std::vector<int>{1, 2, 3}));
124 EXPECT_EQ(to_vector(Only_Stl_Iterator{{4, 5}}), (std::vector<int>{4, 5}));
125
127 for (int i : {3, 1, 2})
128 s.insert(i);
129 EXPECT_EQ(to_vector(s), (std::vector<int>{1, 2, 3}));
130
131 EXPECT_TRUE(to_vector(DynList<int>()).empty());
132}
Conversion utilities between Aleph-w and STL container types.
Conversion utilities between Aleph-w containers and STL containers.
Aleph::DynArray< int > other_unit_vector_to_DynArray(const std::vector< int > &v)
Aleph::DynList< int > other_unit_vector_to_DynList(const std::vector< int > &v)
std::vector< int > other_unit_to_vector(const Aleph::DynList< int > &l)
size_t size() const noexcept
Return the current dimension of array.
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
T & append(const T &item)
Definition htlist.H:1362
Dynamic set backed by balanced binary search trees with automatic memory management.
Key * insert(const Key &key)
Inserts a key into the dynamic set.
#define TEST(name)
Main namespace for Aleph-w library functions.
T Item_Type
Type of elements from the first container.
Definition ah-zip.H:161
auto get_curr() const
Return the current tuple (bounds-checked).
Definition ah-zip.H:192
DynList< T > vector_to_DynList(const std::vector< T > &v)
Convert a std::vector to a DynList.
bool has_curr() const noexcept
Return true if all underlying iterators are positioned on a valid item.
Definition ah-zip.H:173
DynArray< T > vector_to_DynArray(const std::vector< T > &v)
Convert a std::vector to a DynArray.
std::vector< typename C::Item_Type > to_vector(const C &c)
Convert a container to a std::vector.
void next_ne() noexcept(noexcept(std::declval< Aleph::const_iterator_t< HeadC > & >().next_ne()) and noexcept(std::declval< ZipIterator< TailC... > & >().next_ne()))
Advance all underlying iterators (unchecked variant).
Definition ah-zip.H:252
DynList< int > l