aboutsummaryrefslogtreecommitdiff
path: root/tests/byte_tests.cpp
blob: d6634bb63d406672c9e0ac089090eba0241c4288 (plain)
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
///////////////////////////////////////////////////////////////////////////////
//
// 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.
//
///////////////////////////////////////////////////////////////////////////////

#ifdef _MSC_VER
// blanket turn off warnings from CppCoreCheck from catch
// so people aren't annoyed by them when running the tool.
#pragma warning(disable : 26440 26426) // from catch
#endif

#include <catch/catch.hpp> // for AssertionHandler, StringRef, CHECK, TEST_...

#include <gsl/gsl_byte> // for to_byte, to_integer, byte, operator&, ope...

using namespace std;
using namespace gsl;

namespace
{
TEST_CASE("construction")
{
    {
        const byte b = static_cast<byte>(4);
        CHECK(static_cast<unsigned char>(b) == 4);
    }

    GSL_SUPPRESS(es.49)
    {
        const byte b = byte(12);
        CHECK(static_cast<unsigned char>(b) == 12);
    }

    {
        const byte b = to_byte<12>();
        CHECK(static_cast<unsigned char>(b) == 12);
    }
    {
        const unsigned char uc = 12;
        const byte b = to_byte(uc);
        CHECK(static_cast<unsigned char>(b) == 12);
    }

#if defined(__cplusplus) && (__cplusplus >= 201703L)
    {
        const byte b { 14 };
        CHECK(static_cast<unsigned char>(b) == 14);
    }
#endif
}

TEST_CASE("bitwise_operations")
{
    const byte b = to_byte<0xFF>();

    byte a = to_byte<0x00>();
    CHECK((b | a) == to_byte<0xFF>());
    CHECK(a == to_byte<0x00>());

    a |= b;
    CHECK(a == to_byte<0xFF>());

    a = to_byte<0x01>();
    CHECK((b & a) == to_byte<0x01>());

    a &= b;
    CHECK(a == to_byte<0x01>());

    CHECK((b ^ a) == to_byte<0xFE>());

    CHECK(a == to_byte<0x01>());
    a ^= b;
    CHECK(a == to_byte<0xFE>());

    a = to_byte<0x01>();
    CHECK(~a == to_byte<0xFE>());

    a = to_byte<0xFF>();
    CHECK((a << 4) == to_byte<0xF0>());
    CHECK((a >> 4) == to_byte<0x0F>());

    a <<= 4;
    CHECK(a == to_byte<0xF0>());
    a >>= 4;
    CHECK(a == to_byte<0x0F>());
}

TEST_CASE("to_integer")
{
    const byte b = to_byte<0x12>();

    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));

    //      CHECK(0x12 == gsl::to_integer<float>(b));   // expect compile-time error
    //      CHECK(0x12 == gsl::to_integer<double>(b));  // expect compile-time error
}

int modify_both(gsl::byte & b, int& i)
{
    i = 10;
    b = to_byte<5>();
    return i;
}

GSL_SUPPRESS(type.1)
TEST_CASE("aliasing")
{
    int i{0};
    const int res = modify_both(reinterpret_cast<byte&>(i), i);
    CHECK(res == i);
}

}

#ifdef CONFIRM_COMPILATION_ERRORS
copy(src_span_static, dst_span_static);
#endif