zpu: test commit
[llvm/zpu.git] / unittests / Support / SwapByteOrderTest.cpp
blob073824caa4b231f2a1396f2b2b9592d2c1d5c4f1
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 //===----------------------------------------------------------------------===//
10 #include "gtest/gtest.h"
11 #include "llvm/System/SwapByteOrder.h"
12 #include <cstdlib>
13 #include <ctime>
14 using namespace llvm;
16 #undef max
18 namespace {
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.
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)));
32 uint16_t origional_uint16 = static_cast<uint16_t>(value);
33 EXPECT_EQ(origional_uint16,
34 sys::SwapByteOrder(sys::SwapByteOrder(origional_uint16)));
36 uint32_t origional_uint32 = static_cast<uint32_t>(value);
37 EXPECT_EQ(origional_uint32,
38 sys::SwapByteOrder(sys::SwapByteOrder(origional_uint32)));
40 uint64_t origional_uint64 = static_cast<uint64_t>(value);
41 EXPECT_EQ(origional_uint64,
42 sys::SwapByteOrder(sys::SwapByteOrder(origional_uint64)));
44 value = (value << 8) | 0x55; // binary 0101 0101.
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)));
57 int16_t origional_int16 = static_cast<int16_t>(value);
58 EXPECT_EQ(origional_int16,
59 sys::SwapByteOrder(sys::SwapByteOrder(origional_int16)));
61 int32_t origional_int32 = static_cast<int32_t>(value);
62 EXPECT_EQ(origional_int32,
63 sys::SwapByteOrder(sys::SwapByteOrder(origional_int32)));
65 int64_t origional_int64 = static_cast<int64_t>(value);
66 EXPECT_EQ(origional_int64,
67 sys::SwapByteOrder(sys::SwapByteOrder(origional_int64)));
69 // Test other sign.
70 value *= -1;
72 origional_int8 = static_cast<int8_t>(value);
73 EXPECT_EQ(origional_int8,
74 sys::SwapByteOrder(sys::SwapByteOrder(origional_int8)));
76 origional_int16 = static_cast<int16_t>(value);
77 EXPECT_EQ(origional_int16,
78 sys::SwapByteOrder(sys::SwapByteOrder(origional_int16)));
80 origional_int32 = static_cast<int32_t>(value);
81 EXPECT_EQ(origional_int32,
82 sys::SwapByteOrder(sys::SwapByteOrder(origional_int32)));
84 origional_int64 = static_cast<int64_t>(value);
85 EXPECT_EQ(origional_int64,
86 sys::SwapByteOrder(sys::SwapByteOrder(origional_int64)));
88 // Return to normal sign and twiddle.
89 value *= -1;
90 value = (value << 8) | 0x55; // binary 0101 0101.
94 TEST(SwapByteOrder, uint8_t) {
95 EXPECT_EQ(uint8_t(0x11), sys::SwapByteOrder<uint8_t>(0x11));
98 TEST(SwapByteOrder, uint16_t) {
99 EXPECT_EQ(uint16_t(0x1122), sys::SwapByteOrder<uint16_t>(0x2211));
102 TEST(SwapByteOrder, uint32_t) {
103 EXPECT_EQ(uint32_t(0x11223344), sys::SwapByteOrder<uint32_t>(0x44332211));
106 TEST(SwapByteOrder, uint64_t) {
107 EXPECT_EQ(uint64_t(0x1122334455667788ULL),
108 sys::SwapByteOrder<uint64_t>(0x8877665544332211ULL));
111 TEST(SwapByteOrder, int8_t) {
112 EXPECT_EQ(int8_t(0x11), sys::SwapByteOrder<int8_t>(0x11));
115 TEST(SwapByteOrder, int16_t) {
116 EXPECT_EQ(int16_t(0x1122), sys::SwapByteOrder<int16_t>(0x2211));
119 TEST(SwapByteOrder, int32_t) {
120 EXPECT_EQ(int32_t(0x11223344), sys::SwapByteOrder<int32_t>(0x44332211));
123 TEST(SwapByteOrder, int64_t) {
124 EXPECT_EQ(int64_t(0x1122334455667788LL),
125 sys::SwapByteOrder<int64_t>(0x8877665544332211LL));