1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2015 Microsoft Corporation. All rights reserved.
4 //
5 // This code is licensed under the MIT License (MIT).
6 //
7 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
8 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
10 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
11 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
12 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
13 // THE SOFTWARE.
14 //
15 ///////////////////////////////////////////////////////////////////////////////
16
17 #include <catch/catch.hpp>
18
19 #include <gsl/gsl_byte>
20
21 #include <iostream>
22 #include <list>
23 #include <map>
24 #include <memory>
25 #include <string>
26 #include <vector>
27
28 using namespace std;
29 using namespace gsl;
30
31 namespace
32 {
33
34 TEST_CASE("construction")
35 {
36 {
37 const byte b = static_cast<byte>(4);
38 CHECK(static_cast<unsigned char>(b) == 4);
39 }
40
41 {
42 const byte b = byte(12);
43 CHECK(static_cast<unsigned char>(b) == 12);
44 }
45
46 {
47 const byte b = to_byte<12>();
48 CHECK(static_cast<unsigned char>(b) == 12);
49 }
50 {
51 const unsigned char uc = 12;
52 const byte b = to_byte(uc);
53 CHECK(static_cast<unsigned char>(b) == 12);
54 }
55
56 // waiting for C++17 enum class direct initializer support
57 //{
58 // byte b { 14 };
59 // CHECK(static_cast<unsigned char>(b) == 14);
60 //}
61 }
62
63 TEST_CASE("bitwise_operations")
64 {
65 const byte b = to_byte<0xFF>();
66
67 byte a = to_byte<0x00>();
68 CHECK((b | a) == to_byte<0xFF>());
69 CHECK(a == to_byte<0x00>());
70
71 a |= b;
72 CHECK(a == to_byte<0xFF>());
73
74 a = to_byte<0x01>();
75 CHECK((b & a) == to_byte<0x01>());
76
77 a &= b;
78 CHECK(a == to_byte<0x01>());
79
80 CHECK((b ^ a) == to_byte<0xFE>());
81
82 CHECK(a == to_byte<0x01>());
83 a ^= b;
84 CHECK(a == to_byte<0xFE>());
85
86 a = to_byte<0x01>();
87 CHECK(~a == to_byte<0xFE>());
88
89 a = to_byte<0xFF>();
90 CHECK((a << 4) == to_byte<0xF0>());
91 CHECK((a >> 4) == to_byte<0x0F>());
92
93 a <<= 4;
94 CHECK(a == to_byte<0xF0>());
95 a >>= 4;
96 CHECK(a == to_byte<0x0F>());
97 }
98
99 TEST_CASE("to_integer")
100 {
101 const byte b = to_byte<0x12>();
102
103 CHECK(0x12 == gsl::to_integer<char>(b));
104 CHECK(0x12 == gsl::to_integer<short>(b));
105 CHECK(0x12 == gsl::to_integer<long>(b));
106 CHECK(0x12 == gsl::to_integer<long long>(b));
107
108 CHECK(0x12 == gsl::to_integer<unsigned char>(b));
109 CHECK(0x12 == gsl::to_integer<unsigned short>(b));
110 CHECK(0x12 == gsl::to_integer<unsigned long>(b));
111 CHECK(0x12 == gsl::to_integer<unsigned long long>(b));
112
113 // CHECK(0x12 == gsl::to_integer<float>(b)); // expect compile-time error
114 // CHECK(0x12 == gsl::to_integer<double>(b)); // expect compile-time error
115 }
116
modify_both(gsl::byte & b,int & i)117 int modify_both(gsl::byte & b, int& i)
118 {
119 i = 10;
120 b = to_byte<5>();
121 return i;
122 }
123
124 TEST_CASE("aliasing")
125 {
126 int i{0};
127 const int res = modify_both(reinterpret_cast<byte&>(i), i);
128 CHECK(res == i);
129 }
130
131 }
132