forked from gsl-lite/gsl-lite
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathat.t.cpp
More file actions
133 lines (108 loc) · 3.38 KB
/
at.t.cpp
File metadata and controls
133 lines (108 loc) · 3.38 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
//
// gsl-lite is based on GSL: Guideline Support Library.
// For more information see https://github.com/martinmoene/gsl-lite
//
// Copyright (c) 2015 Martin Moene
// Copyright (c) 2015 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.h"
CASE( "at(): Terminates access to non-existing C-array elements" )
{
int a[] = { 1, 2, 3, 4 };
EXPECT_THROWS( at(a, 4) );
}
CASE( "at(): Terminates access to non-existing std::array elements (C++11)" )
{
#if gsl_HAVE_ARRAY
std::array<int, 4> a = {{ 1, 2, 3, 4 }};
EXPECT_THROWS( at(a, 4) );
#else
EXPECT( !!"std::array<> is not available (no C++11)" );
#endif
}
CASE( "at(): Terminates access to non-existing std::vector elements" )
{
std::vector<int> a; // = { 1, 2, 3, 4 };
for ( int i = 0; i < 4; ++i )
{
a.push_back( i + 1 );
}
EXPECT_THROWS( at(a, 4) );
}
CASE( "at(): Terminates access to non-existing std::initializer_list elements (C++11)" )
{
// Note: GCC 4.6.3 has std::initializer_list but selects at(Cont & cont,...) overload.
#if gsl_HAVE_INITIALIZER_LIST && ( !gsl_COMPILER_GNUC_VERSION || gsl_COMPILER_GNUC_VERSION >= 473 )
std::initializer_list<int> a = { 1, 2, 3, 4 };
EXPECT_THROWS( at(a, 4) );
#else
EXPECT( !!"std::initializer_list<> is not available (no C++11)" );
#endif
}
CASE( "at(): Terminates access to non-existing gsl::span elements" )
{
int arr[] = { 1, 2, 3, 4 };
span<int> a( arr );
EXPECT_THROWS( at(a, 4) );
}
CASE( "at(): Allows to access existing C-array elements" )
{
size_t a[] = { 1, 2, 3, 4 };
for ( size_t i = 0; i < 4; ++i )
{
EXPECT( at(a, i) == i + 1 );
}
}
CASE( "at(): Allows to access existing std::array elements (C++11)" )
{
#if gsl_HAVE_ARRAY
std::array<size_t, 4> a = {{ 1, 2, 3, 4 }};
for ( size_t i = 0; i < 4; ++i )
{
EXPECT( at(a, i) == i + 1 );
}
#else
EXPECT( !!"std::array<> is not available (no C++11)" );
#endif
}
CASE( "at(): Allows to access existing std::vector elements" )
{
std::vector<size_t> a; // = { 1, 2, 3, 4 };
for ( size_t i = 0; i < 4; ++i )
{
a.push_back( i + 1 );
EXPECT( at(a, i) == i + 1 );
}
}
CASE( "at(): Allows to access std::initializer_list elements (C++11)" )
{
// Note: GCC 4.6.3 has std::initializer_list but selects at(Cont & cont,...) overload.
#if gsl_HAVE_INITIALIZER_LIST && ( !gsl_COMPILER_GNUC_VERSION || gsl_COMPILER_GNUC_VERSION >= 473 )
std::initializer_list<size_t> a = { 1, 2, 3, 4 };
for ( size_t i = 0; i < 4; ++i )
{
EXPECT( at(a, i) == i + 1 );
}
#else
EXPECT( !!"std::initializer_list<> is not available (no C++11)" );
#endif
}
CASE( "at(): Allows to access gsl::span elements" )
{
size_t arr[] = { 1, 2, 3, 4 };
span<size_t> a( arr );
for ( size_t i = 0; i < 4; ++i )
{
EXPECT( at(a, i) == i + 1 );
}
}
// end of file