Brian Silverman | a6f7ce0 | 2018-07-07 15:04:00 -0700 | [diff] [blame^] | 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> // for AssertionHandler, StringRef, CHECK, TEST_... |
| 18 | |
| 19 | #include <gsl/gsl_byte> // for to_byte, to_integer, byte, operator&, ope... |
| 20 | |
| 21 | using namespace std; |
| 22 | using namespace gsl; |
| 23 | |
| 24 | namespace |
| 25 | { |
| 26 | |
| 27 | TEST_CASE("construction") |
| 28 | { |
| 29 | { |
| 30 | const byte b = static_cast<byte>(4); |
| 31 | CHECK(static_cast<unsigned char>(b) == 4); |
| 32 | } |
| 33 | |
| 34 | { |
| 35 | const byte b = byte(12); |
| 36 | CHECK(static_cast<unsigned char>(b) == 12); |
| 37 | } |
| 38 | |
| 39 | { |
| 40 | const byte b = to_byte<12>(); |
| 41 | CHECK(static_cast<unsigned char>(b) == 12); |
| 42 | } |
| 43 | { |
| 44 | const unsigned char uc = 12; |
| 45 | const byte b = to_byte(uc); |
| 46 | CHECK(static_cast<unsigned char>(b) == 12); |
| 47 | } |
| 48 | |
| 49 | // waiting for C++17 enum class direct initializer support |
| 50 | //{ |
| 51 | // byte b { 14 }; |
| 52 | // CHECK(static_cast<unsigned char>(b) == 14); |
| 53 | //} |
| 54 | } |
| 55 | |
| 56 | TEST_CASE("bitwise_operations") |
| 57 | { |
| 58 | const byte b = to_byte<0xFF>(); |
| 59 | |
| 60 | byte a = to_byte<0x00>(); |
| 61 | CHECK((b | a) == to_byte<0xFF>()); |
| 62 | CHECK(a == to_byte<0x00>()); |
| 63 | |
| 64 | a |= b; |
| 65 | CHECK(a == to_byte<0xFF>()); |
| 66 | |
| 67 | a = to_byte<0x01>(); |
| 68 | CHECK((b & a) == to_byte<0x01>()); |
| 69 | |
| 70 | a &= b; |
| 71 | CHECK(a == to_byte<0x01>()); |
| 72 | |
| 73 | CHECK((b ^ a) == to_byte<0xFE>()); |
| 74 | |
| 75 | CHECK(a == to_byte<0x01>()); |
| 76 | a ^= b; |
| 77 | CHECK(a == to_byte<0xFE>()); |
| 78 | |
| 79 | a = to_byte<0x01>(); |
| 80 | CHECK(~a == to_byte<0xFE>()); |
| 81 | |
| 82 | a = to_byte<0xFF>(); |
| 83 | CHECK((a << 4) == to_byte<0xF0>()); |
| 84 | CHECK((a >> 4) == to_byte<0x0F>()); |
| 85 | |
| 86 | a <<= 4; |
| 87 | CHECK(a == to_byte<0xF0>()); |
| 88 | a >>= 4; |
| 89 | CHECK(a == to_byte<0x0F>()); |
| 90 | } |
| 91 | |
| 92 | TEST_CASE("to_integer") |
| 93 | { |
| 94 | const byte b = to_byte<0x12>(); |
| 95 | |
| 96 | CHECK(0x12 == gsl::to_integer<char>(b)); |
| 97 | CHECK(0x12 == gsl::to_integer<short>(b)); |
| 98 | CHECK(0x12 == gsl::to_integer<long>(b)); |
| 99 | CHECK(0x12 == gsl::to_integer<long long>(b)); |
| 100 | |
| 101 | CHECK(0x12 == gsl::to_integer<unsigned char>(b)); |
| 102 | CHECK(0x12 == gsl::to_integer<unsigned short>(b)); |
| 103 | CHECK(0x12 == gsl::to_integer<unsigned long>(b)); |
| 104 | CHECK(0x12 == gsl::to_integer<unsigned long long>(b)); |
| 105 | |
| 106 | // CHECK(0x12 == gsl::to_integer<float>(b)); // expect compile-time error |
| 107 | // CHECK(0x12 == gsl::to_integer<double>(b)); // expect compile-time error |
| 108 | } |
| 109 | |
| 110 | int modify_both(gsl::byte & b, int& i) |
| 111 | { |
| 112 | i = 10; |
| 113 | b = to_byte<5>(); |
| 114 | return i; |
| 115 | } |
| 116 | |
| 117 | TEST_CASE("aliasing") |
| 118 | { |
| 119 | int i{0}; |
| 120 | const int res = modify_both(reinterpret_cast<byte&>(i), i); |
| 121 | CHECK(res == i); |
| 122 | } |
| 123 | |
| 124 | } |