forked from gsl-lite/gsl-lite
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbyte.t.cpp
More file actions
210 lines (168 loc) · 5.95 KB
/
byte.t.cpp
File metadata and controls
210 lines (168 loc) · 5.95 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
210
//
// 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"
// Use gsl::byte instead of plain byte to prevent collisions with
// other byte declarations, such as in rpcndr.h (Windows kit).
// We have a chicken & egg problem here:
// verifying operations via to_integer() that has yet to verified itself...
CASE( "byte: Allows to construct from integral via static cast (C++17)" )
{
#if gsl_HAVE_ENUM_CLASS_CONSTRUCTION_FROM_UNDERLYING_TYPE
gsl::byte b = static_cast<gsl::byte>( 4 );
EXPECT( static_cast<unsigned char>(b) == 4 );
EXPECT( to_integer<int>( b ) == 4 );
#else
EXPECT( !!"enum class is not constructible from underlying type (no C++17)" );
#endif
}
CASE( "byte: Allows to construct from integral via byte() (C++17)" )
{
#if gsl_HAVE_ENUM_CLASS_CONSTRUCTION_FROM_UNDERLYING_TYPE
gsl::byte b = gsl::byte( 4 );
EXPECT( to_integer<int>( b ) == 4 );
#else
EXPECT( !!"enum class is not constructible from underlying type (no C++17)" );
#endif
}
CASE( "byte: Allows to construct from integral via to_byte()" )
{
gsl::byte b = to_byte( 4 );
EXPECT( to_integer<int>( b ) == 4 );
}
CASE( "byte: Allows to convert to integral via to_integer()" )
{
gsl::byte b = to_byte( 4 );
EXPECT( to_integer<int>( b ) == 4 );
}
CASE( "byte: Allows comparison operations" )
{
gsl::byte a = to_byte( 3 );
gsl::byte b = to_byte( 7 );
EXPECT( a == a );
EXPECT( a != b );
EXPECT( a < b );
EXPECT( a <= a );
EXPECT( a <= b );
EXPECT( b > a );
EXPECT( b >= a );
EXPECT( b >= b );
EXPECT_NOT( a == b );
EXPECT_NOT( a != a );
EXPECT_NOT( b < a );
EXPECT_NOT( a > b );
}
CASE( "byte: Allows bitwise or operation" )
{
gsl::byte const b = to_byte( 0xa5 );
EXPECT( ( b | b ) == b );
EXPECT( ( b | to_byte( 0x00 ) ) == b );
EXPECT( ( b | to_byte( 0xff ) ) == to_byte( 0xff ) );
}
CASE( "byte: Allows bitwise and operation" )
{
gsl::byte const b = to_byte( 0xa5 );
EXPECT( ( b & b ) == b );
EXPECT( ( b & to_byte( 0xff ) ) == b );
EXPECT( ( b & to_byte( 0x00 ) ) == to_byte( 0x00 ) );
}
CASE( "byte: Allows bitwise x-or operation" )
{
gsl::byte const b = to_byte( 0xa5 );
EXPECT( ( b ^ b ) == to_byte( 0 ) );
EXPECT( ( b ^ to_byte( 0x00 ) ) == b );
EXPECT( ( b ^ to_byte( 0xff ) ) == ~b );
}
CASE( "byte: Allows bitwise or assignment" )
{
SETUP("") {
gsl::byte const b_org = to_byte( 0xa5 );
gsl::byte b = b_org;
SECTION("Identity") { EXPECT( ( b |= b ) == b_org ); }
SECTION("Identity") { EXPECT( ( b |= to_byte( 0x00 ) ) == b_org ); }
SECTION("Saturate") { EXPECT( ( b |= to_byte( 0xff ) ) == to_byte( 0xff ) ); }
}
}
CASE( "byte: Allows bitwise and assignment" )
{
SETUP("") {
gsl::byte const b_org = to_byte( 0xa5 );
gsl::byte b = b_org;
SECTION("Identity") { EXPECT( ( b &= b ) == b_org ); }
SECTION("Identity") { EXPECT( ( b &= to_byte( 0xff ) ) == b_org ); }
SECTION("Clear" ) { EXPECT( ( b &= to_byte( 0x00 ) ) == to_byte( 0x00 ) ); }
}
}
CASE( "byte: Allows bitwise x-or assignment" )
{
SETUP("") {
gsl::byte const b_org = to_byte( 0xa5 );
gsl::byte b = b_org;
SECTION("Identity") { EXPECT( ( b ^= b ) == to_byte( 0 ) ); }
SECTION("Identity") { EXPECT( ( b ^= to_byte( 0x00 ) ) == b_org ); }
SECTION("Invert" ) { EXPECT( ( b ^= to_byte( 0xff ) ) == ~b_org ); }
}
}
CASE( "byte: Allows shift-left operation" )
{
gsl::byte const b = to_byte( 0xa5 );
EXPECT( ( b << 3 ) == to_byte( to_uchar( b ) << 3 ) );
}
CASE( "byte: Allows shift-right operation" )
{
gsl::byte const b = to_byte( 0xa5 );
EXPECT( ( b >> 3 ) == to_byte( to_uchar( b ) >> 3 ) );
}
CASE( "byte: Allows shift-left assignment" )
{
gsl::byte const b_org = to_byte( 0xa5 );
gsl::byte b = b_org;
EXPECT( ( b <<= 3 ) == to_byte( to_uchar( b_org ) << 3 ) );
}
CASE( "byte: Allows shift-right assignment" )
{
gsl::byte const b_org = to_byte( 0xa5 );
gsl::byte b = b_org;
EXPECT( ( b >>= 3 ) == to_byte( to_uchar( b_org ) >> 3 ) );
}
CASE( "byte: Provides constexpr non-assignment operations (C++11)" )
{
#if gsl_HAVE_CONSTEXPR_11
static_assert( to_byte( 0xa5 ) == to_byte( 0xa5 ) , "" );
static_assert( 0xa5 == to_integer<int>( to_byte( 0xa5 ) ), "" );
static_assert( to_byte( 0x02 ) == ( to_byte( 0x01 ) << 1 ), "" );
static_assert( to_byte( 0x01 ) == ( to_byte( 0x02 ) >> 1 ), "" );
static_assert( to_byte( 0x01 ) == ( to_byte( 0x03 ) & to_byte( 0x01 ) ), "" );
static_assert( to_byte( 0x01 ) == ( to_byte( 0x00 ) | to_byte( 0x01 ) ), "" );
static_assert( to_byte( 0x00 ) == ( to_byte( 0x01 ) ^ to_byte( 0x01 ) ), "" );
static_assert( to_byte( 0xff ) == ~to_byte( 0x00 ), "" );
#endif
}
CASE( "byte: Provides constexpr assignment operations (C++14)" )
{
#if gsl_HAVE_CONSTEXPR_14
// ...
#endif
}
CASE( "byte: Provides hash support (C++11)" )
{
#if gsl_CPP11_OR_GREATER
EXPECT_NO_THROW( std::hash<gsl::byte>{}( to_byte( 42 ) ) );
#else
EXPECT( !!"hash support is not available (no C++11)" );
#endif
}
// end of file