-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdaw_iterator_counting_iterators_test.cpp
More file actions
95 lines (77 loc) · 2.31 KB
/
Copy pathdaw_iterator_counting_iterators_test.cpp
File metadata and controls
95 lines (77 loc) · 2.31 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
// 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/iterator/daw_counting_iterators.h>
#include <daw/daw_benchmark.h>
#include <daw/daw_random.h>
#include <array>
#include <vector>
constexpr bool fwd_ary_test_001( ) {
std::array nums = { 1, 2, 3, 4, 5, 6, 7 };
auto first = daw::forward_counting_iterator( nums.begin( ) );
while( first != nums.end( ) ) {
++first;
}
daw::expecting( first.distance( ) == static_cast<int>( nums.size( ) ) );
return true;
}
static_assert( fwd_ary_test_001( ) );
constexpr bool fwd_ary_test_002( ) {
std::array nums = { 1, 2, 3, 4, 5, 6, 7 };
auto first = daw::forward_counting_iterator( nums.begin( ) );
while( first != nums.end( ) ) {
first++;
}
daw::expecting( first.distance( ) == static_cast<int>( nums.size( ) ) );
return true;
}
static_assert( fwd_ary_test_002( ) );
constexpr bool bidir_ary_test_001( ) {
std::array nums = { 1, 2, 3, 4, 5, 6, 7 };
auto first = daw::bidirectional_counting_iterator( nums.begin( ) );
while( first != nums.end( ) ) {
++first;
}
daw::expecting( first.distance( ) == static_cast<int>( nums.size( ) ) );
while( first != nums.begin( ) ) {
--first;
}
daw::expecting( first.distance( ) == 0 );
return true;
}
static_assert( bidir_ary_test_001( ) );
constexpr bool bidir_ary_test_002( ) {
std::array nums = { 1, 2, 3, 4, 5, 6, 7 };
auto first = daw::bidirectional_counting_iterator( nums.begin( ) );
while( first != nums.end( ) ) {
first++;
}
daw::expecting( first.distance( ) == static_cast<int>( nums.size( ) ) );
while( first != nums.begin( ) ) {
first--;
}
daw::expecting( first.distance( ) == 0 );
return true;
}
static_assert( bidir_ary_test_002( ) );
int main( ) {
auto data = daw::make_random_data<int>( 1'000'000 );
daw::bench_n_test<100>(
"Average Calc via counting iterator",
[]( std::vector<int> values ) {
daw::do_not_optimize( values );
auto first = daw::forward_counting_iterator( values.begin( ) );
auto result = 0LL;
while( first != values.end( ) ) {
result += *first;
++first;
}
daw::do_not_optimize( result );
return result / first.distance( );
},
data );
}