forked from gsl-lite/gsl-lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemulation.t.cpp
119 lines (103 loc) · 5.99 KB
/
emulation.t.cpp
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
//
// gsl-lite is based on GSL: Guidelines Support Library.
// For more information see https://github.com/gsl-lite/gsl-lite
//
// Copyright (c) 2015-2019 Martin Moene
// Copyright (c) 2019-2021 Moritz Beutel
// Copyright (c) 2015-2018 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include "gsl-lite.t.hpp"
using namespace gsl;
#if gsl_HAVE( TYPE_TRAITS ) && gsl_HAVE( VARIADIC_TEMPLATE )
template <int> struct True : std::true_type { };
template <int> struct False : std::false_type { };
struct Incomplete;
#endif // gsl_HAVE( TYPE_TRAITS ) && gsl_HAVE( VARIADIC_TEMPLATE )
CASE( "equal()" )
{
char const* arg1 = "foo";
char const* arg2 = "fou";
EXPECT( detail::equal( arg1, arg1 + std::strlen( arg1 ), arg1 ) );
EXPECT( ! detail::equal( arg1, arg1 + std::strlen( arg1 ), arg2 ) );
EXPECT( ! detail::equal( arg2, arg2 + std::strlen( arg2 ), arg1 ) );
std::istringstream sstr1a1( "foo" ), sstr1a2( "foo" ), sstr1b( "foo" ), sstr1c( "foo" );
std::istringstream sstr2b( "fou" ), sstr2c( "fou" );
EXPECT( detail::equal( std::istreambuf_iterator<char>( sstr1a1 ), std::istreambuf_iterator<char>(), std::istreambuf_iterator<char>( sstr1a2 ) ) );
EXPECT( ! detail::equal( std::istreambuf_iterator<char>( sstr1b ), std::istreambuf_iterator<char>(), std::istreambuf_iterator<char>( sstr2b ) ) );
EXPECT( ! detail::equal( std::istreambuf_iterator<char>( sstr2c ), std::istreambuf_iterator<char>(), std::istreambuf_iterator<char>( sstr1c ) ) );
}
template < typename T, std::size_t N >
std::size_t arraySize( T (&)[N] )
{
return N;
}
CASE( "lexicographical_compare()" )
{
std::pair<char const*, char const*> less[] = {
std::make_pair("", "foo"),
std::make_pair("fa", "foo"),
std::make_pair("foe", "foo"),
std::make_pair("fond", "foo")
};
for ( std::size_t i = 0, n = arraySize( less ); i != n; ++i )
{
EXPECT( detail::lexicographical_compare( less[i].first, less[i].first + std::strlen( less[i].first ), less[i].second, less[i].second + std::strlen( less[i].second ) ));
std::istringstream sstr1( less[i].first );
std::istringstream sstr2( less[i].second );
std::istreambuf_iterator<char> it1( sstr1 ), it1End;
std::istreambuf_iterator<char> it2( sstr2 ), it2End;
EXPECT( detail::lexicographical_compare( it1, it1End, it2, it2End ));
}
std::pair<char const*, char const*> notLess[] = {
std::make_pair("g", "foo"),
std::make_pair("fu", "foo"),
std::make_pair("foo", "foo"),
std::make_pair("fou", "foo"),
std::make_pair("fool", "foo"),
std::make_pair("foul", "foo")
};
for ( std::size_t i = 0, n = arraySize( less ); i != n; ++i )
{
EXPECT( ! detail::lexicographical_compare( notLess[i].first, notLess[i].first + std::strlen( notLess[i].first ), notLess[i].second, notLess[i].second + std::strlen( notLess[i].second ) ));
std::istringstream sstr1( notLess[i].first );
std::istringstream sstr2( notLess[i].second );
std::istreambuf_iterator<char> it1( sstr1 ), it1End;
std::istreambuf_iterator<char> it2( sstr2 ), it2End;
EXPECT( ! detail::lexicographical_compare( it1, it1End, it2, it2End ));
}
}
CASE( "conjunction<> and disjunction<>: Short-circuiting is handled correctly" )
{
#if gsl_HAVE( TYPE_TRAITS ) && gsl_HAVE( VARIADIC_TEMPLATE )
static_assert(std::is_base_of<True<0>, std17::disjunction<True<0>, Incomplete>>::value, "static assertion failed");
static_assert(std::is_base_of<False<0>, std17::conjunction<False<0>, Incomplete>>::value, "static assertion failed");
#endif // gsl_HAVE( TYPE_TRAITS ) && gsl_HAVE( VARIADIC_TEMPLATE )
}
CASE( "conjunction<> and disjunction<>: First suitable type is chosen as base" )
{
#if gsl_HAVE( TYPE_TRAITS ) && gsl_HAVE( VARIADIC_TEMPLATE )
static_assert(std::is_base_of<std::false_type, std17::disjunction<>>::value, "static assertion failed");
static_assert(std::is_base_of<True<0>, std17::disjunction<True<0>>>::value, "static assertion failed");
static_assert(std::is_base_of<False<0>, std17::disjunction<False<0>>>::value, "static assertion failed");
static_assert(std::is_base_of<True<0>, std17::disjunction<True<0>, True<1>>>::value, "static assertion failed");
static_assert(std::is_base_of<True<0>, std17::disjunction<True<0>, True<1>, True<2>>>::value, "static assertion failed");
static_assert(std::is_base_of<True<1>, std17::disjunction<False<0>, True<1>, True<2>>>::value, "static assertion failed");
static_assert(std::is_base_of<False<2>, std17::disjunction<False<0>, False<1>, False<2>>>::value, "static assertion failed");
static_assert(std::is_base_of<std::true_type, std17::conjunction<>>::value, "static assertion failed");
static_assert(std::is_base_of<True<0>, std17::conjunction<True<0>>>::value, "static assertion failed");
static_assert(std::is_base_of<False<0>, std17::conjunction<False<0>>>::value, "static assertion failed");
static_assert(std::is_base_of<True<1>, std17::conjunction<True<0>, True<1>>>::value, "static assertion failed");
static_assert(std::is_base_of<True<2>, std17::conjunction<True<0>, True<1>, True<2>>>::value, "static assertion failed");
static_assert(std::is_base_of<False<1>, std17::conjunction<True<0>, False<1>, False<2>>>::value, "static assertion failed");
static_assert(std::is_base_of<False<0>, std17::conjunction<False<0>, False<1>, False<2>>>::value, "static assertion failed");
#endif // gsl_HAVE( TYPE_TRAITS ) && gsl_HAVE( VARIADIC_TEMPLATE )
}