-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcpp_17_test.cpp
More file actions
209 lines (178 loc) · 5.59 KB
/
cpp_17_test.cpp
File metadata and controls
209 lines (178 loc) · 5.59 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// 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/cpp_17.h"
#include "daw/cpp_17_iterator.h"
#include "daw/daw_apply.h"
#include "daw/daw_benchmark.h"
#include "daw/daw_bind_front.h"
#include "daw/daw_tuple_traits.h"
#include "daw/daw_utility.h"
#include <cstdint>
#include <tuple>
#include <type_traits>
#include <utility>
constexpr int plus( int a, int b ) {
return a + b;
}
static_assert( daw::apply( plus, std::make_tuple( 1, 2 ) ) == 3 );
/*
namespace void_t_tests {
void test2( int ) {}
struct X {};
constexpr bool fn2( ... ) {
return true;
}
template<typename T, typename = ::std::void_t<decltype( fn( T{} ) )>>
constexpr bool fn2( T ) {
return false;
}
constexpr bool void_t_test_001( ) {
auto const b1 = fn2( X{} );
daw::expecting( b1 );
return true;
}
static_assert( void_t_test_001( ) );
} // namespace void_t_tests
*/
/*
namespace cpp_17_test_01 {
void test( ) {}
struct test2 {};
static_assert( std::is_function_v<decltype( test )>, "" );
static_assert( !std::is_function_v<test2>, "" );
} // namespace cpp_17_test_01
*/
namespace not_fn_test {
struct not_fn_test {
bool value = false;
constexpr bool operator( )( ) const noexcept {
return value;
}
};
static_assert( daw::not_fn<not_fn_test>( )( ) );
static constexpr auto fn1 = not_fn_test{ true };
static constexpr auto const fn2 = daw::not_fn( fn1 );
static_assert( fn1( ) );
static_assert( !fn2( ) );
} // namespace not_fn_test
/*
namespace is_array_v_001 {
struct A {};
static_assert( !std::is_array_v<A>, "" );
static_assert( std::is_array_v<A[]>, "" );
static_assert( std::is_array_v<A[3]>, "" );
static_assert( !std::is_array_v<float>, "" );
} // namespace is_array_v_001
*/
namespace advance_001 {
constexpr bool test_advance_pos( ) {
int a[11] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
auto first = &a[0];
daw::advance( first, 4 );
return *first == 4;
}
static_assert( test_advance_pos( ) );
} // namespace advance_001
namespace advance_002 {
constexpr bool test_advance_neg( ) {
int a[11] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
auto first = &a[0];
daw::advance( first, 4 );
daw::advance( first, -4 );
return *first == 0;
}
static_assert( test_advance_neg( ) );
} // namespace advance_002
namespace advance_003 {
constexpr bool test_advance_zero( ) {
int a[11] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
auto first = &a[0];
daw::advance( first, 0 );
return *first == 0;
}
static_assert( test_advance_zero( ) );
} // namespace advance_003
namespace trivially_destructable_001 {
struct Foo {
~Foo( ) = default;
};
static_assert( std::is_trivially_destructible_v<Foo> );
} // namespace trivially_destructable_001
void bit_cast_001( ) {
constexpr uint32_t const tst = 0x89AB'CDEF;
auto const f = daw::bit_cast<float>( tst );
auto const i = daw::bit_cast<uint32_t>( f );
daw::expecting( tst, i );
}
void bit_cast_002( ) {
constexpr uint32_t const tst = 0x89AB'CDEF;
auto const f = daw::bit_cast<float>( tst );
auto const i = daw::bit_cast<uint32_t>( f );
daw::expecting( tst, i );
}
void bit_cast_003( ) {
constexpr uint64_t const as_bin =
0b0'01111111111'0000000000000000000000000000000000000000000000000000; // double
// 1
constexpr double const as_dbl = 1.0;
bool const b1 = daw::bit_cast<uint64_t>( as_dbl ) == as_bin;
daw::expecting( b1 );
constexpr uint64_t const as_bin2 =
0b1'10000000000'0000000000000000000000000000000000000000000000000000; // double
// -2
constexpr double const as_dbl2 = -2.0;
bool const b2 = daw::bit_cast<uint64_t>( as_dbl2 ) == as_bin2;
::Unused( b2 );
daw::expecting( b1 );
}
namespace is_nothrow_convertible_001 {
struct tmp_t {};
struct tmp2_t {
constexpr tmp2_t( tmp_t ) noexcept {}
constexpr tmp2_t( int ) noexcept( false ) {}
};
static_assert( !daw::is_nothrow_convertible_v<int, tmp_t>,
"int should not be convertible to tmp_t" );
static_assert( daw::is_nothrow_convertible_v<int, double>,
"int should not be convertible to tmp_t" );
static_assert( daw::is_nothrow_convertible_v<tmp_t, tmp2_t>,
"tmp_t should be nothrow convertible to tmp2_t" );
static_assert( !daw::is_nothrow_convertible_v<int, tmp2_t>,
"int should not be nothrow convertible to tmp2_t" );
} // namespace is_nothrow_convertible_001
namespace decay_copy_001 {
template<typename T>
auto dc_func( T &&v ) {
auto x = std::pair<T, int>( std::forward<T>( v ), 99 );
return x;
}
[[maybe_unused]] static double d = 5.6;
[[maybe_unused]] static double const cd = 3.4;
static_assert(
!std::is_same_v<decltype( dc_func( d ) ), decltype( dc_func( cd ) )>,
"Results should be difference, double and double const" );
static_assert( std::is_same_v<decltype( daw::decay_copy( d ) ),
decltype( daw::decay_copy( cd ) )>,
"Results should be the same, double" );
} // namespace decay_copy_001
static_assert( daw::any_true_v<false, false, true> );
static_assert( !daw::any_true_v<false, false, false> );
constexpr int add_numbers( int const &lhs, int const &rhs ) {
return lhs + rhs;
}
static_assert( []( ) {
int a = 5;
auto add_to_5 = ::daw::bind_front( &add_numbers, a );
daw::expecting( add_to_5( 6 ), 11 );
return true;
}( ) );
int main( ) {
bit_cast_001( );
bit_cast_002( );
bit_cast_003( );
}