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 #ifdef _MSC_VER 18 // blanket turn off warnings from CppCoreCheck from catch 19 // so people aren't annoyed by them when running the tool. 20 #pragma warning(disable : 26440 26426) // from catch 21 #endif 22 23 #include <catch/catch.hpp> // for AssertionHandler, StringRef, CHECK, TEST_... 24 25 #include <gsl/gsl_byte> // for to_byte, to_integer, byte, operator&, ope... 26 27 using namespace std; 28 using namespace gsl; 29 30 namespace 31 { 32 TEST_CASE("construction") 33 { 34 { 35 const byte b = static_cast<byte>(4); 36 CHECK(static_cast<unsigned char>(b) == 4); 37 } 38 39 GSL_SUPPRESS(es.49) 40 { 41 const byte b = byte(12); 42 CHECK(static_cast<unsigned char>(b) == 12); 43 } 44 45 { 46 const byte b = to_byte<12>(); 47 CHECK(static_cast<unsigned char>(b) == 12); 48 } 49 { 50 const unsigned char uc = 12; 51 const byte b = to_byte(uc); 52 CHECK(static_cast<unsigned char>(b) == 12); 53 } 54 55 #if defined(__cplusplus) && (__cplusplus >= 201703L) 56 { 57 const byte b { 14 }; 58 CHECK(static_cast<unsigned char>(b) == 14); 59 } 60 #endif 61 } 62 63 TEST_CASE("bitwise_operations") 64 { 65 const byte b = to_byte<0xFF>(); 66 67 byte a = to_byte<0x00>(); 68 CHECK((b | a) == to_byte<0xFF>()); 69 CHECK(a == to_byte<0x00>()); 70 71 a |= b; 72 CHECK(a == to_byte<0xFF>()); 73 74 a = to_byte<0x01>(); 75 CHECK((b & a) == to_byte<0x01>()); 76 77 a &= b; 78 CHECK(a == to_byte<0x01>()); 79 80 CHECK((b ^ a) == to_byte<0xFE>()); 81 82 CHECK(a == to_byte<0x01>()); 83 a ^= b; 84 CHECK(a == to_byte<0xFE>()); 85 86 a = to_byte<0x01>(); 87 CHECK(~a == to_byte<0xFE>()); 88 89 a = to_byte<0xFF>(); 90 CHECK((a << 4) == to_byte<0xF0>()); 91 CHECK((a >> 4) == to_byte<0x0F>()); 92 93 a <<= 4; 94 CHECK(a == to_byte<0xF0>()); 95 a >>= 4; 96 CHECK(a == to_byte<0x0F>()); 97 } 98 99 TEST_CASE("to_integer") 100 { 101 const byte b = to_byte<0x12>(); 102 103 CHECK(0x12 == gsl::to_integer<char>(b)); 104 CHECK(0x12 == gsl::to_integer<short>(b)); 105 CHECK(0x12 == gsl::to_integer<long>(b)); 106 CHECK(0x12 == gsl::to_integer<long long>(b)); 107 108 CHECK(0x12 == gsl::to_integer<unsigned char>(b)); 109 CHECK(0x12 == gsl::to_integer<unsigned short>(b)); 110 CHECK(0x12 == gsl::to_integer<unsigned long>(b)); 111 CHECK(0x12 == gsl::to_integer<unsigned long long>(b)); 112 113 // CHECK(0x12 == gsl::to_integer<float>(b)); // expect compile-time error 114 // CHECK(0x12 == gsl::to_integer<double>(b)); // expect compile-time error 115 } 116 117 int modify_both(gsl::byte & b, int& i) 118 { 119 i = 10; 120 b = to_byte<5>(); 121 return i; 122 } 123 124 GSL_SUPPRESS(type.1) 125 TEST_CASE("aliasing") 126 { 127 int i{0}; 128 const int res = modify_both(reinterpret_cast<byte&>(i), i); 129 CHECK(res == i); 130 } 131 132 } 133 134 #ifdef CONFIRM_COMPILATION_ERRORS 135 copy(src_span_static, dst_span_static); 136 #endif 137