Home | History | Annotate | Download | only in tests
      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 <UnitTest++/UnitTest++.h>
     18 #include <gsl/gsl_byte>
     19 
     20 #include <iostream>
     21 #include <list>
     22 #include <map>
     23 #include <memory>
     24 #include <string>
     25 #include <vector>
     26 
     27 using namespace std;
     28 using namespace gsl;
     29 
     30 namespace
     31 {
     32 
     33 SUITE(byte_tests)
     34 {
     35     TEST(construction)
     36     {
     37         {
     38             byte b = static_cast<byte>(4);
     39             CHECK(static_cast<unsigned char>(b) == 4);
     40         }
     41 
     42         {
     43             byte b = byte(12);
     44             CHECK(static_cast<unsigned char>(b) == 12);
     45         }
     46 
     47         {
     48             byte b = to_byte<12>();
     49             CHECK(static_cast<unsigned char>(b) == 12);
     50         }
     51         {
     52             unsigned char uc = 12;
     53             byte b = to_byte(uc);
     54             CHECK(static_cast<unsigned char>(b) == 12);
     55         }
     56 
     57         // waiting for C++17 enum class direct initializer support
     58         //{
     59         //    byte b { 14 };
     60         //    CHECK(static_cast<unsigned char>(b) == 14);
     61         //}
     62     }
     63 
     64     TEST(bitwise_operations)
     65     {
     66         byte b = to_byte<0xFF>();
     67 
     68         byte a = to_byte<0x00>();
     69         CHECK((b | a) == to_byte<0xFF>());
     70         CHECK(a == to_byte<0x00>());
     71 
     72         a |= b;
     73         CHECK(a == to_byte<0xFF>());
     74 
     75         a = to_byte<0x01>();
     76         CHECK((b & a) == to_byte<0x01>());
     77 
     78         a &= b;
     79         CHECK(a == to_byte<0x01>());
     80 
     81         CHECK((b ^ a) == to_byte<0xFE>());
     82 
     83         CHECK(a == to_byte<0x01>());
     84         a ^= b;
     85         CHECK(a == to_byte<0xFE>());
     86 
     87         a = to_byte<0x01>();
     88         CHECK(~a == to_byte<0xFE>());
     89 
     90         a = to_byte<0xFF>();
     91         CHECK((a << 4) == to_byte<0xF0>());
     92         CHECK((a >> 4) == to_byte<0x0F>());
     93 
     94         a <<= 4;
     95         CHECK(a == to_byte<0xF0>());
     96         a >>= 4;
     97         CHECK(a == to_byte<0x0F>());
     98     }
     99 
    100     TEST(to_integer)
    101     {
    102         byte b = to_byte<0x12>();
    103 
    104         CHECK(0x12 == gsl::to_integer<char>(b));
    105         CHECK(0x12 == gsl::to_integer<short>(b));
    106         CHECK(0x12 == gsl::to_integer<long>(b));
    107         CHECK(0x12 == gsl::to_integer<long long>(b));
    108 
    109         CHECK(0x12 == gsl::to_integer<unsigned char>(b));
    110         CHECK(0x12 == gsl::to_integer<unsigned short>(b));
    111         CHECK(0x12 == gsl::to_integer<unsigned long>(b));
    112         CHECK(0x12 == gsl::to_integer<unsigned long long>(b));
    113 
    114 //      CHECK(0x12 == gsl::to_integer<float>(b));   // expect compile-time error
    115 //      CHECK(0x12 == gsl::to_integer<double>(b));  // expect compile-time error
    116     }
    117 
    118     int modify_both(gsl::byte& b, int& i)
    119     {
    120         i = 10;
    121         b = to_byte<5>();
    122         return i;
    123     }
    124 
    125     TEST(aliasing)
    126     {
    127         int i{ 0 };
    128         int res = modify_both(reinterpret_cast<byte&>(i), i);
    129         CHECK(res == i);
    130     }
    131 }
    132 
    133 }
    134 
    135 int main(int, const char* []) { return UnitTest::RunAllTests(); }
    136