Home | History | Annotate | Download | only in Support
      1 //===- unittests/Support/SwapByteOrderTest.cpp - swap byte order test -----===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 #include "gtest/gtest.h"
     11 #include "llvm/Support/SwapByteOrder.h"
     12 #include <cstdlib>
     13 #include <ctime>
     14 using namespace llvm;
     15 
     16 #undef max
     17 
     18 namespace {
     19 
     20 // In these first two tests all of the origional_uintx values are truncated
     21 // except for 64. We could avoid this, but there's really no point.
     22 
     23 TEST(SwapByteOrder, UnsignedRoundTrip) {
     24   // The point of the bit twiddling of magic is to test with and without bits
     25   // in every byte.
     26   uint64_t value = 1;
     27   for (std::size_t i = 0; i <= sizeof(value); ++i) {
     28     uint8_t origional_uint8 = static_cast<uint8_t>(value);
     29     EXPECT_EQ(origional_uint8,
     30       sys::SwapByteOrder(sys::SwapByteOrder(origional_uint8)));
     31 
     32     uint16_t origional_uint16 = static_cast<uint16_t>(value);
     33     EXPECT_EQ(origional_uint16,
     34       sys::SwapByteOrder(sys::SwapByteOrder(origional_uint16)));
     35 
     36     uint32_t origional_uint32 = static_cast<uint32_t>(value);
     37     EXPECT_EQ(origional_uint32,
     38       sys::SwapByteOrder(sys::SwapByteOrder(origional_uint32)));
     39 
     40     uint64_t origional_uint64 = static_cast<uint64_t>(value);
     41     EXPECT_EQ(origional_uint64,
     42       sys::SwapByteOrder(sys::SwapByteOrder(origional_uint64)));
     43 
     44     value = (value << 8) | 0x55; // binary 0101 0101.
     45   }
     46 }
     47 
     48 TEST(SwapByteOrder, SignedRoundTrip) {
     49   // The point of the bit twiddling of magic is to test with and without bits
     50   // in every byte.
     51   uint64_t value = 1;
     52   for (std::size_t i = 0; i <= sizeof(value); ++i) {
     53     int8_t origional_int8 = static_cast<int8_t>(value);
     54     EXPECT_EQ(origional_int8,
     55       sys::SwapByteOrder(sys::SwapByteOrder(origional_int8)));
     56 
     57     int16_t origional_int16 = static_cast<int16_t>(value);
     58     EXPECT_EQ(origional_int16,
     59       sys::SwapByteOrder(sys::SwapByteOrder(origional_int16)));
     60 
     61     int32_t origional_int32 = static_cast<int32_t>(value);
     62     EXPECT_EQ(origional_int32,
     63       sys::SwapByteOrder(sys::SwapByteOrder(origional_int32)));
     64 
     65     int64_t origional_int64 = static_cast<int64_t>(value);
     66     EXPECT_EQ(origional_int64,
     67       sys::SwapByteOrder(sys::SwapByteOrder(origional_int64)));
     68 
     69     // Test other sign.
     70     value *= -1;
     71 
     72     origional_int8 = static_cast<int8_t>(value);
     73     EXPECT_EQ(origional_int8,
     74       sys::SwapByteOrder(sys::SwapByteOrder(origional_int8)));
     75 
     76     origional_int16 = static_cast<int16_t>(value);
     77     EXPECT_EQ(origional_int16,
     78       sys::SwapByteOrder(sys::SwapByteOrder(origional_int16)));
     79 
     80     origional_int32 = static_cast<int32_t>(value);
     81     EXPECT_EQ(origional_int32,
     82       sys::SwapByteOrder(sys::SwapByteOrder(origional_int32)));
     83 
     84     origional_int64 = static_cast<int64_t>(value);
     85     EXPECT_EQ(origional_int64,
     86       sys::SwapByteOrder(sys::SwapByteOrder(origional_int64)));
     87 
     88     // Return to normal sign and twiddle.
     89     value *= -1;
     90     value = (value << 8) | 0x55; // binary 0101 0101.
     91   }
     92 }
     93 
     94 TEST(SwapByteOrder, uint8_t) {
     95   EXPECT_EQ(uint8_t(0x11), sys::SwapByteOrder(uint8_t(0x11)));
     96 }
     97 
     98 TEST(SwapByteOrder, uint16_t) {
     99   EXPECT_EQ(uint16_t(0x1122), sys::SwapByteOrder(uint16_t(0x2211)));
    100 }
    101 
    102 TEST(SwapByteOrder, uint32_t) {
    103   EXPECT_EQ(uint32_t(0x11223344), sys::SwapByteOrder(uint32_t(0x44332211)));
    104 }
    105 
    106 TEST(SwapByteOrder, uint64_t) {
    107   EXPECT_EQ(uint64_t(0x1122334455667788ULL),
    108     sys::SwapByteOrder(uint64_t(0x8877665544332211ULL)));
    109 }
    110 
    111 TEST(SwapByteOrder, int8_t) {
    112   EXPECT_EQ(int8_t(0x11), sys::SwapByteOrder(int8_t(0x11)));
    113 }
    114 
    115 TEST(SwapByteOrder, int16_t) {
    116   EXPECT_EQ(int16_t(0x1122), sys::SwapByteOrder(int16_t(0x2211)));
    117 }
    118 
    119 TEST(SwapByteOrder, int32_t) {
    120   EXPECT_EQ(int32_t(0x11223344), sys::SwapByteOrder(int32_t(0x44332211)));
    121 }
    122 
    123 TEST(SwapByteOrder, int64_t) {
    124   EXPECT_EQ(int64_t(0x1122334455667788LL),
    125     sys::SwapByteOrder(int64_t(0x8877665544332211LL)));
    126 }
    127 
    128 }
    129