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.
|
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include <UnitTest++/UnitTest++.h>
|
2017-04-17 20:35:40 -04:00
|
|
|
|
2016-08-10 12:24:00 -04:00
|
|
|
#include <gsl/gsl_byte>
|
2016-06-24 07:54:09 -04:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <list>
|
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace gsl;
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
SUITE(byte_tests)
|
|
|
|
{
|
|
|
|
TEST(construction)
|
|
|
|
{
|
|
|
|
{
|
2017-04-02 15:30:49 -04:00
|
|
|
const byte b = static_cast<byte>(4);
|
2016-06-24 07:54:09 -04:00
|
|
|
CHECK(static_cast<unsigned char>(b) == 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-04-02 15:30:49 -04:00
|
|
|
const byte b = byte(12);
|
2016-06-24 07:54:09 -04:00
|
|
|
CHECK(static_cast<unsigned char>(b) == 12);
|
|
|
|
}
|
2016-10-17 15:36:11 -04:00
|
|
|
|
2016-08-28 16:55:58 -04:00
|
|
|
{
|
2017-04-02 15:30:49 -04:00
|
|
|
const byte b = to_byte<12>();
|
2016-09-07 16:38:43 -04:00
|
|
|
CHECK(static_cast<unsigned char>(b) == 12);
|
|
|
|
}
|
|
|
|
{
|
2017-04-02 15:30:49 -04:00
|
|
|
const unsigned char uc = 12;
|
|
|
|
const byte b = to_byte(uc);
|
2016-08-28 16:55:58 -04:00
|
|
|
CHECK(static_cast<unsigned char>(b) == 12);
|
|
|
|
}
|
2016-06-24 07:54:09 -04:00
|
|
|
|
|
|
|
// waiting for C++17 enum class direct initializer support
|
|
|
|
//{
|
|
|
|
// byte b { 14 };
|
|
|
|
// CHECK(static_cast<unsigned char>(b) == 14);
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(bitwise_operations)
|
|
|
|
{
|
2017-04-02 15:30:49 -04:00
|
|
|
const byte b = to_byte<0xFF>();
|
2016-06-24 07:54:09 -04:00
|
|
|
|
2016-09-07 16:38:43 -04:00
|
|
|
byte a = to_byte<0x00>();
|
|
|
|
CHECK((b | a) == to_byte<0xFF>());
|
|
|
|
CHECK(a == to_byte<0x00>());
|
2016-06-24 07:54:09 -04:00
|
|
|
|
|
|
|
a |= b;
|
2016-09-07 16:38:43 -04:00
|
|
|
CHECK(a == to_byte<0xFF>());
|
2016-06-24 07:54:09 -04:00
|
|
|
|
2016-09-07 16:38:43 -04:00
|
|
|
a = to_byte<0x01>();
|
|
|
|
CHECK((b & a) == to_byte<0x01>());
|
2016-06-24 07:54:09 -04:00
|
|
|
|
|
|
|
a &= b;
|
2016-09-07 16:38:43 -04:00
|
|
|
CHECK(a == to_byte<0x01>());
|
2016-06-24 07:54:09 -04:00
|
|
|
|
2016-09-07 16:38:43 -04:00
|
|
|
CHECK((b ^ a) == to_byte<0xFE>());
|
2017-04-02 15:30:49 -04:00
|
|
|
|
2016-09-07 16:38:43 -04:00
|
|
|
CHECK(a == to_byte<0x01>());
|
2016-06-24 07:54:09 -04:00
|
|
|
a ^= b;
|
2016-09-07 16:38:43 -04:00
|
|
|
CHECK(a == to_byte<0xFE>());
|
2016-06-24 07:54:09 -04:00
|
|
|
|
2016-09-07 16:38:43 -04:00
|
|
|
a = to_byte<0x01>();
|
|
|
|
CHECK(~a == to_byte<0xFE>());
|
2016-06-24 07:54:09 -04:00
|
|
|
|
2016-09-07 16:38:43 -04:00
|
|
|
a = to_byte<0xFF>();
|
|
|
|
CHECK((a << 4) == to_byte<0xF0>());
|
|
|
|
CHECK((a >> 4) == to_byte<0x0F>());
|
2016-06-24 07:54:09 -04:00
|
|
|
|
|
|
|
a <<= 4;
|
2016-09-07 16:38:43 -04:00
|
|
|
CHECK(a == to_byte<0xF0>());
|
2016-06-24 07:54:09 -04:00
|
|
|
a >>= 4;
|
2016-09-07 16:38:43 -04:00
|
|
|
CHECK(a == to_byte<0x0F>());
|
2016-06-24 07:54:09 -04:00
|
|
|
}
|
2016-09-17 03:16:15 -04:00
|
|
|
|
|
|
|
TEST(to_integer)
|
|
|
|
{
|
2017-04-02 15:30:49 -04:00
|
|
|
const byte b = to_byte<0x12>();
|
2016-09-17 03:16:15 -04:00
|
|
|
|
|
|
|
CHECK(0x12 == gsl::to_integer<char>(b));
|
|
|
|
CHECK(0x12 == gsl::to_integer<short>(b));
|
|
|
|
CHECK(0x12 == gsl::to_integer<long>(b));
|
|
|
|
CHECK(0x12 == gsl::to_integer<long long>(b));
|
|
|
|
|
|
|
|
CHECK(0x12 == gsl::to_integer<unsigned char>(b));
|
|
|
|
CHECK(0x12 == gsl::to_integer<unsigned short>(b));
|
|
|
|
CHECK(0x12 == gsl::to_integer<unsigned long>(b));
|
|
|
|
CHECK(0x12 == gsl::to_integer<unsigned long long>(b));
|
|
|
|
|
2017-04-17 20:35:40 -04:00
|
|
|
// CHECK(0x12 == gsl::to_integer<float>(b)); // expect compile-time error
|
|
|
|
// CHECK(0x12 == gsl::to_integer<double>(b)); // expect compile-time error
|
2016-09-17 03:16:15 -04:00
|
|
|
}
|
2016-10-17 15:36:11 -04:00
|
|
|
|
2017-04-17 20:35:40 -04:00
|
|
|
int modify_both(gsl::byte & b, int& i)
|
2016-10-17 15:36:11 -04:00
|
|
|
{
|
|
|
|
i = 10;
|
|
|
|
b = to_byte<5>();
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(aliasing)
|
|
|
|
{
|
2017-04-17 20:35:40 -04:00
|
|
|
int i{0};
|
2017-04-02 15:30:49 -04:00
|
|
|
const int res = modify_both(reinterpret_cast<byte&>(i), i);
|
2016-10-17 15:36:11 -04:00
|
|
|
CHECK(res == i);
|
|
|
|
}
|
2016-06-24 07:54:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int, const char* []) { return UnitTest::RunAllTests(); }
|