1 ///////////////////////////////////////////////////////////////////////////////
3 // Copyright (c) 2015 Microsoft Corporation. All rights reserved.
5 // This code is licensed under the MIT License (MIT).
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
15 ///////////////////////////////////////////////////////////////////////////////
17 #include <gtest/gtest.h>
19 #define GSL_USE_STD_BYTE 0
20 #include <gsl/byte> // for to_byte, to_integer, byte, operator&, ope...
22 #include <type_traits>
30 int modify_both(gsl::byte
& b
, int& i
)
37 TEST(byte_tests
, construction
)
40 const gsl::byte b
= static_cast<gsl::byte
>(4);
41 EXPECT_TRUE(static_cast<unsigned char>(b
) == 4);
45 const gsl::byte b
= gsl::byte(12);
46 EXPECT_TRUE(static_cast<unsigned char>(b
) == 12);
50 const gsl::byte b
= to_byte
<12>();
51 EXPECT_TRUE(static_cast<unsigned char>(b
) == 12);
54 const unsigned char uc
= 12;
55 const gsl::byte b
= to_byte(uc
);
56 EXPECT_TRUE(static_cast<unsigned char>(b
) == 12);
59 #if defined(__cplusplus) && (__cplusplus >= 201703L)
61 const gsl::byte b
{14};
62 EXPECT_TRUE(static_cast<unsigned char>(b
) == 14);
66 #ifdef CONFIRM_COMPILATION_ERRORS
75 TEST(byte_tests
, bitwise_operations
)
77 const gsl::byte b
= to_byte
<0xFF>();
79 gsl::byte a
= to_byte
<0x00>();
80 EXPECT_TRUE((b
| a
) == to_byte
<0xFF>());
81 EXPECT_TRUE(a
== to_byte
<0x00>());
84 EXPECT_TRUE(a
== to_byte
<0xFF>());
87 EXPECT_TRUE((b
& a
) == to_byte
<0x01>());
90 EXPECT_TRUE(a
== to_byte
<0x01>());
92 EXPECT_TRUE((b
^ a
) == to_byte
<0xFE>());
94 EXPECT_TRUE(a
== to_byte
<0x01>());
96 EXPECT_TRUE(a
== to_byte
<0xFE>());
99 EXPECT_TRUE(~a
== to_byte
<0xFE>());
102 EXPECT_TRUE((a
<< 4) == to_byte
<0xF0>());
103 EXPECT_TRUE((a
>> 4) == to_byte
<0x0F>());
106 EXPECT_TRUE(a
== to_byte
<0xF0>());
108 EXPECT_TRUE(a
== to_byte
<0x0F>());
111 TEST(byte_tests
, to_integer
)
113 const gsl::byte b
= to_byte
<0x12>();
115 EXPECT_TRUE(0x12 == gsl::to_integer
<char>(b
));
116 EXPECT_TRUE(0x12 == gsl::to_integer
<short>(b
));
117 EXPECT_TRUE(0x12 == gsl::to_integer
<long>(b
));
118 EXPECT_TRUE(0x12 == gsl::to_integer
<long long>(b
));
120 EXPECT_TRUE(0x12 == gsl::to_integer
<unsigned char>(b
));
121 EXPECT_TRUE(0x12 == gsl::to_integer
<unsigned short>(b
));
122 EXPECT_TRUE(0x12 == gsl::to_integer
<unsigned long>(b
));
123 EXPECT_TRUE(0x12 == gsl::to_integer
<unsigned long long>(b
));
125 // EXPECT_TRUE(0x12 == gsl::to_integer<float>(b)); // expect compile-time error
126 // EXPECT_TRUE(0x12 == gsl::to_integer<double>(b)); // expect compile-time error
129 TEST(byte_tests
, aliasing
)
132 const int res
= modify_both(reinterpret_cast<gsl::byte
&>(i
), i
);
133 EXPECT_TRUE(res
== i
);
136 #if __cplusplus >= 201703l
138 #else // __cplusplus >= 201703l
141 #endif // __cplusplus < 201703l
143 template <typename U
, typename
= void>
144 static constexpr bool LShiftCompilesFor
= false;
145 template <typename U
>
146 static constexpr bool LShiftCompilesFor
<
147 U
, void_t
<decltype(gsl::operator<< <float>(declval
<gsl::byte
>(), declval
<U
>()))>> = true;
148 static_assert(!LShiftCompilesFor
<float>, "!LShiftCompilesFor<float>");
150 template <typename U
, typename
= void>
151 static constexpr bool RShiftCompilesFor
= false;
152 template <typename U
>
153 static constexpr bool RShiftCompilesFor
<
154 U
, void_t
<decltype(gsl::operator>> <U
>(declval
<gsl::byte
>(), declval
<U
>()))>> = true;
155 static_assert(!RShiftCompilesFor
<float>, "!RShiftCompilesFor<float>");
157 template <typename U
, typename
= void>
158 static constexpr bool LShiftAssignCompilesFor
= false;
159 template <typename U
>
160 static constexpr bool LShiftAssignCompilesFor
<
161 U
, void_t
<decltype(gsl::operator<<= <U
>(declval
<gsl::byte
&>(), declval
<U
>()))>> = true;
162 static_assert(!LShiftAssignCompilesFor
<float>, "!LShiftAssignCompilesFor<float>");
164 template <typename U
, typename
= void>
165 static constexpr bool RShiftAssignCompilesFor
= false;
166 template <typename U
>
167 static constexpr bool RShiftAssignCompilesFor
<
168 U
, void_t
<decltype(gsl::operator>>= <U
>(declval
<gsl::byte
&>(), declval
<U
>()))>> = true;
169 static_assert(!RShiftAssignCompilesFor
<float>, "!RShiftAssignCompilesFor<float>");
171 template <typename U
, typename
= void>
172 static constexpr bool ToIntegerCompilesFor
= false;
173 template <typename U
>
174 static constexpr bool
175 ToIntegerCompilesFor
<U
, void_t
<decltype(gsl::to_integer
<U
>(gsl::byte
{}))>> = true;
176 static_assert(!ToIntegerCompilesFor
<float>, "!ToIntegerCompilesFor<float>");