-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdaw_container_algorithm_test.cpp
More file actions
180 lines (155 loc) · 5.15 KB
/
daw_container_algorithm_test.cpp
File metadata and controls
180 lines (155 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright (c) Darrell Wright
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/beached/header_libraries
//
#include "daw/daw_algorithm.h"
#include "daw/daw_benchmark.h"
#include "daw/daw_container_algorithm.h"
#include <algorithm>
#include <array>
#include <cstddef>
#include <cstdint>
#include <ctime>
#include <functional>
#include <iostream>
#include <iterator>
#include <vector>
void container_algorithm_accumulate( ) {
std::vector<int> test( 100, 1 );
auto const sum = daw::container::accumulate( test, 0 );
daw::expecting( sum >= 0 );
daw::expecting( test.size( ), static_cast<size_t>( sum ) );
auto const product =
daw::container::accumulate( test, 1, []( auto lhs, auto rhs ) {
return lhs * rhs;
} );
daw::expecting( product, 1 );
}
void container_algorithm_transform( ) {
auto test_vec = std::vector<int>( 100 );
std::iota( test_vec.begin( ), test_vec.end( ), 1 );
auto result = std::vector<int>( test_vec.size( ) );
daw::container::transform( test_vec, result.data( ), []( int const &val ) {
return 2 * val;
} );
auto const sum1 = daw::container::accumulate( test_vec, 0 );
auto const sum2 = daw::container::accumulate( result, 0 );
daw::expecting( sum1 * 2, sum2 );
}
void daw_container_algorithm_test_sort( ) {
std::array<int64_t, 1000> v1{ };
daw::algorithm::iota( std::begin( v1 ), std::end( v1 ), 1 );
daw::container::sort( v1, []( auto lhs, auto rhs ) {
return lhs < rhs;
} );
daw::expecting( std::is_sorted( v1.cbegin( ), v1.cend( ) ) );
}
void daw_container_algorithm_test_stable_sort( ) {
std::array<int64_t, 1000> v1{ };
daw::algorithm::iota( std::begin( v1 ), std::end( v1 ), 1 );
daw::container::stable_sort( v1, []( auto lhs, auto rhs ) {
return lhs < rhs;
} );
daw::expecting( std::is_sorted( v1.cbegin( ), v1.cend( ) ) );
}
constexpr bool daw_container_algorithm_test_max_element( ) {
std::array<int64_t, 10> v1{ };
daw::algorithm::iota( std::begin( v1 ), std::end( v1 ), 1 );
auto ans = daw::container::max_element( v1, []( auto lhs, auto rhs ) {
return lhs < rhs;
} );
daw::expecting( 10, *ans );
return true;
}
static_assert( daw_container_algorithm_test_max_element( ) );
constexpr bool daw_container_algorithm_test_copy_001( ) {
std::array<int, 100> a1{ };
daw::algorithm::iota( std::begin( a1 ), std::end( a1 ), 2 );
std::array<int, 100> a2{ };
daw::container::copy( a1, a2.begin( ) );
daw::expecting( daw::algorithm::equal(
std::cbegin( a1 ), std::cend( a1 ), std::cbegin( a2 ), std::cend( a2 ) ) );
return true;
}
static_assert( daw_container_algorithm_test_copy_001( ) );
constexpr bool daw_container_algorithm_test_copy_n_001( ) {
std::array<int, 100> a1{ };
daw::algorithm::iota( std::begin( a1 ), std::end( a1 ), 2 );
std::array<int, 100> a2{ };
daw::container::copy_n( a1, 100, a2.begin( ) );
daw::expecting( daw::algorithm::equal(
std::cbegin( a1 ), std::cend( a1 ), std::cbegin( a2 ), std::cend( a2 ) ) );
return true;
}
static_assert( daw_container_algorithm_test_copy_n_001( ) );
void daw_for_each_pos_001( ) {
std::array<int, 5> const blah = { 0, 1, 2, 3, 4 };
daw::container::for_each_with_pos( blah, []( auto const &value, size_t pos ) {
std::cout << pos << ": " << value << '\n';
} );
}
namespace daw_for_each_with_pos_002_ns {
struct summer_t {
int *sum;
constexpr summer_t( int *ptr ) noexcept
: sum{ ptr } {}
template<typename Value>
constexpr void operator( )( Value const &value,
size_t pos ) const noexcept {
*sum += value * static_cast<Value>( pos );
}
};
} // namespace daw_for_each_with_pos_002_ns
void daw_for_each_subset_001( ) {
std::array<int, 5> const blah = { 0, 1, 2, 3, 4 };
daw::container::for_each_subset(
blah, 1, 4, []( auto &container, size_t pos ) {
std::cout << container[pos] << '\n';
} );
}
namespace daw_for_each_subset_002_ns {
struct summer_t {
int *sum;
constexpr summer_t( int *ptr ) noexcept
: sum{ ptr } {}
template<typename Container>
constexpr void operator( )( Container &container,
size_t pos ) const noexcept {
*sum += container[pos];
}
};
template<size_t N>
constexpr int find_sum( int const ( &ptr )[N] ) noexcept {
int sum = 0;
daw::container::for_each_subset( ptr, 1, N - 1, summer_t{ &sum } );
return sum;
}
constexpr bool daw_for_each_subset_002( ) {
int blah[] = { 0, 1, 2, 3, 4 };
auto sum = find_sum( blah );
daw::expecting( 6, sum );
return true;
}
static_assert( daw_for_each_subset_002( ) );
} // namespace daw_for_each_subset_002_ns
void daw_append_001( ) {
std::vector<int> a = { 1, 2, 3 };
std::vector<int> const b = { 4, 5, 6 };
std::vector<int> const expect = { 1, 2, 3, 4, 5, 6 };
daw::container::append( b, a );
auto const tst = daw::algorithm::equal(
expect.cbegin( ), expect.cend( ), a.cbegin( ), a.cend( ) );
daw::expecting( tst );
}
int main( ) {
container_algorithm_accumulate( );
container_algorithm_transform( );
daw_container_algorithm_test_sort( );
daw_container_algorithm_test_stable_sort( );
daw_for_each_pos_001( );
daw_for_each_subset_001( );
daw_append_001( );
}