2016-06-24 07:54:09 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-12-03 17:32:25 -05:00
|
|
|
#include <gtest/gtest.h>
|
2017-04-20 10:51:37 -04:00
|
|
|
|
2020-10-29 20:38:48 -04:00
|
|
|
#include <gsl/byte> // for to_byte, to_integer, byte, operator&, ope...
|
2016-06-24 07:54:09 -04:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace gsl;
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2019-12-03 17:32:25 -05:00
|
|
|
int modify_both(gsl::byte& b, int& i)
|
|
|
|
{
|
|
|
|
i = 10;
|
|
|
|
b = to_byte<5>();
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(byte_tests, construction)
|
2016-06-24 07:54:09 -04:00
|
|
|
{
|
|
|
|
{
|
2017-07-13 16:53:56 -04:00
|
|
|
const byte b = static_cast<byte>(4);
|
2019-12-03 17:32:25 -05:00
|
|
|
EXPECT_TRUE(static_cast<unsigned char>(b) == 4);
|
2016-06-24 07:54:09 -04:00
|
|
|
}
|
|
|
|
|
2021-05-20 21:18:08 -04:00
|
|
|
// clang-format off
|
2018-08-13 00:44:17 -04:00
|
|
|
GSL_SUPPRESS(es.49)
|
2021-05-20 21:18:08 -04:00
|
|
|
// clang-format on
|
2016-06-24 07:54:09 -04:00
|
|
|
{
|
2017-07-13 16:53:56 -04:00
|
|
|
const byte b = byte(12);
|
2019-12-03 17:32:25 -05:00
|
|
|
EXPECT_TRUE(static_cast<unsigned char>(b) == 12);
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2016-06-24 07:54:09 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
const byte b = to_byte<12>();
|
2019-12-03 17:32:25 -05:00
|
|
|
EXPECT_TRUE(static_cast<unsigned char>(b) == 12);
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const unsigned char uc = 12;
|
|
|
|
const byte b = to_byte(uc);
|
2019-12-03 17:32:25 -05:00
|
|
|
EXPECT_TRUE(static_cast<unsigned char>(b) == 12);
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2016-06-24 07:54:09 -04:00
|
|
|
|
2018-08-13 00:44:17 -04:00
|
|
|
#if defined(__cplusplus) && (__cplusplus >= 201703L)
|
|
|
|
{
|
2021-05-20 21:18:08 -04:00
|
|
|
const byte b{14};
|
2019-12-03 17:32:25 -05:00
|
|
|
EXPECT_TRUE(static_cast<unsigned char>(b) == 14);
|
2018-08-13 00:44:17 -04:00
|
|
|
}
|
|
|
|
#endif
|
2023-02-14 17:10:56 -05:00
|
|
|
|
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
|
|
|
to_byte(char{});
|
|
|
|
to_byte(3);
|
|
|
|
to_byte(3u);
|
|
|
|
#endif
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2016-06-24 07:54:09 -04:00
|
|
|
|
2019-12-03 17:32:25 -05:00
|
|
|
TEST(byte_tests, bitwise_operations)
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
const byte b = to_byte<0xFF>();
|
2016-06-24 07:54:09 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
byte a = to_byte<0x00>();
|
2019-12-03 17:32:25 -05:00
|
|
|
EXPECT_TRUE((b | a) == to_byte<0xFF>());
|
|
|
|
EXPECT_TRUE(a == to_byte<0x00>());
|
2016-06-24 07:54:09 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
a |= b;
|
2019-12-03 17:32:25 -05:00
|
|
|
EXPECT_TRUE(a == to_byte<0xFF>());
|
2017-04-02 15:30:49 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
a = to_byte<0x01>();
|
2019-12-03 17:32:25 -05:00
|
|
|
EXPECT_TRUE((b & a) == to_byte<0x01>());
|
2016-06-24 07:54:09 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
a &= b;
|
2019-12-03 17:32:25 -05:00
|
|
|
EXPECT_TRUE(a == to_byte<0x01>());
|
2016-06-24 07:54:09 -04:00
|
|
|
|
2019-12-03 17:32:25 -05:00
|
|
|
EXPECT_TRUE((b ^ a) == to_byte<0xFE>());
|
2016-06-24 07:54:09 -04:00
|
|
|
|
2019-12-03 17:32:25 -05:00
|
|
|
EXPECT_TRUE(a == to_byte<0x01>());
|
2017-07-13 16:53:56 -04:00
|
|
|
a ^= b;
|
2019-12-03 17:32:25 -05:00
|
|
|
EXPECT_TRUE(a == to_byte<0xFE>());
|
2016-09-17 03:16:15 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
a = to_byte<0x01>();
|
2019-12-03 17:32:25 -05:00
|
|
|
EXPECT_TRUE(~a == to_byte<0xFE>());
|
2016-09-17 03:16:15 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
a = to_byte<0xFF>();
|
2019-12-03 17:32:25 -05:00
|
|
|
EXPECT_TRUE((a << 4) == to_byte<0xF0>());
|
|
|
|
EXPECT_TRUE((a >> 4) == to_byte<0x0F>());
|
2016-09-17 03:16:15 -04:00
|
|
|
|
2017-07-13 16:53:56 -04:00
|
|
|
a <<= 4;
|
2019-12-03 17:32:25 -05:00
|
|
|
EXPECT_TRUE(a == to_byte<0xF0>());
|
2017-07-13 16:53:56 -04:00
|
|
|
a >>= 4;
|
2019-12-03 17:32:25 -05:00
|
|
|
EXPECT_TRUE(a == to_byte<0x0F>());
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
2016-09-17 03:16:15 -04:00
|
|
|
|
2019-12-03 17:32:25 -05:00
|
|
|
TEST(byte_tests, to_integer)
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
const byte b = to_byte<0x12>();
|
2016-10-17 15:36:11 -04:00
|
|
|
|
2019-12-03 17:32:25 -05:00
|
|
|
EXPECT_TRUE(0x12 == gsl::to_integer<char>(b));
|
|
|
|
EXPECT_TRUE(0x12 == gsl::to_integer<short>(b));
|
|
|
|
EXPECT_TRUE(0x12 == gsl::to_integer<long>(b));
|
|
|
|
EXPECT_TRUE(0x12 == gsl::to_integer<long long>(b));
|
2016-10-17 15:36:11 -04:00
|
|
|
|
2019-12-03 17:32:25 -05:00
|
|
|
EXPECT_TRUE(0x12 == gsl::to_integer<unsigned char>(b));
|
|
|
|
EXPECT_TRUE(0x12 == gsl::to_integer<unsigned short>(b));
|
|
|
|
EXPECT_TRUE(0x12 == gsl::to_integer<unsigned long>(b));
|
|
|
|
EXPECT_TRUE(0x12 == gsl::to_integer<unsigned long long>(b));
|
2017-07-13 16:53:56 -04:00
|
|
|
|
2019-12-03 17:32:25 -05:00
|
|
|
// EXPECT_TRUE(0x12 == gsl::to_integer<float>(b)); // expect compile-time error
|
|
|
|
// EXPECT_TRUE(0x12 == gsl::to_integer<double>(b)); // expect compile-time error
|
2016-06-24 07:54:09 -04:00
|
|
|
}
|
|
|
|
|
2019-12-03 17:32:25 -05:00
|
|
|
TEST(byte_tests, aliasing)
|
2017-07-13 16:53:56 -04:00
|
|
|
{
|
|
|
|
int i{0};
|
|
|
|
const int res = modify_both(reinterpret_cast<byte&>(i), i);
|
2019-12-03 17:32:25 -05:00
|
|
|
EXPECT_TRUE(res == i);
|
2017-07-13 16:53:56 -04:00
|
|
|
}
|
|
|
|
|
2021-05-20 21:18:08 -04:00
|
|
|
} // namespace
|
2018-08-13 00:44:17 -04:00
|
|
|
|
|
|
|
#ifdef CONFIRM_COMPILATION_ERRORS
|
|
|
|
copy(src_span_static, dst_span_static);
|
|
|
|
#endif
|