[llvm] [cmake] Add possibility to use ChooseMSVCCRT.cmake when include LLVM library
[llvm-core.git] / unittests / Support / EndianTest.cpp
blobb5e4a9c8d14a359fad11107a7f966ea1c162a2ff
1 //===- unittests/Support/EndianTest.cpp - Endian.h tests ------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/Endian.h"
10 #include "llvm/Support/DataTypes.h"
11 #include "gtest/gtest.h"
12 #include <cstdlib>
13 #include <ctime>
14 using namespace llvm;
15 using namespace support;
17 #undef max
19 namespace {
21 TEST(Endian, Read) {
22 // These are 5 bytes so we can be sure at least one of the reads is unaligned.
23 unsigned char bigval[] = {0x00, 0x01, 0x02, 0x03, 0x04};
24 unsigned char littleval[] = {0x00, 0x04, 0x03, 0x02, 0x01};
25 int32_t BigAsHost = 0x00010203;
26 EXPECT_EQ(BigAsHost, (endian::read<int32_t, big, unaligned>(bigval)));
27 int32_t LittleAsHost = 0x02030400;
28 EXPECT_EQ(LittleAsHost,(endian::read<int32_t, little, unaligned>(littleval)));
30 EXPECT_EQ((endian::read<int32_t, big, unaligned>(bigval + 1)),
31 (endian::read<int32_t, little, unaligned>(littleval + 1)));
34 TEST(Endian, ReadBitAligned) {
35 // Simple test to make sure we properly pull out the 0x0 word.
36 unsigned char littleval[] = {0x3f, 0x00, 0x00, 0x00, 0xc0, 0xff, 0xff, 0xff};
37 unsigned char bigval[] = {0x00, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xc0};
38 EXPECT_EQ(
39 (endian::readAtBitAlignment<int, little, unaligned>(&littleval[0], 6)),
40 0x0);
41 EXPECT_EQ((endian::readAtBitAlignment<int, big, unaligned>(&bigval[0], 6)),
42 0x0);
43 // Test to make sure that signed right shift of 0xf0000000 is masked
44 // properly.
45 unsigned char littleval2[] = {0x00, 0x00, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x00};
46 unsigned char bigval2[] = {0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
47 EXPECT_EQ(
48 (endian::readAtBitAlignment<int, little, unaligned>(&littleval2[0], 4)),
49 0x0f000000);
50 EXPECT_EQ((endian::readAtBitAlignment<int, big, unaligned>(&bigval2[0], 4)),
51 0x0f000000);
52 // Test to make sure left shift of start bit doesn't overflow.
53 EXPECT_EQ(
54 (endian::readAtBitAlignment<int, little, unaligned>(&littleval2[0], 1)),
55 0x78000000);
56 EXPECT_EQ((endian::readAtBitAlignment<int, big, unaligned>(&bigval2[0], 1)),
57 0x78000000);
58 // Test to make sure 64-bit int doesn't overflow.
59 unsigned char littleval3[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0,
60 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
61 unsigned char bigval3[] = {0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
62 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
63 EXPECT_EQ((endian::readAtBitAlignment<int64_t, little, unaligned>(
64 &littleval3[0], 4)),
65 0x0f00000000000000);
66 EXPECT_EQ(
67 (endian::readAtBitAlignment<int64_t, big, unaligned>(&bigval3[0], 4)),
68 0x0f00000000000000);
71 TEST(Endian, WriteBitAligned) {
72 // This test ensures that signed right shift of 0xffffaa is masked
73 // properly.
74 unsigned char bigval[8] = {0x00};
75 endian::writeAtBitAlignment<int32_t, big, unaligned>(bigval, (int)0xffffaaaa,
76 4);
77 EXPECT_EQ(bigval[0], 0xff);
78 EXPECT_EQ(bigval[1], 0xfa);
79 EXPECT_EQ(bigval[2], 0xaa);
80 EXPECT_EQ(bigval[3], 0xa0);
81 EXPECT_EQ(bigval[4], 0x00);
82 EXPECT_EQ(bigval[5], 0x00);
83 EXPECT_EQ(bigval[6], 0x00);
84 EXPECT_EQ(bigval[7], 0x0f);
86 unsigned char littleval[8] = {0x00};
87 endian::writeAtBitAlignment<int32_t, little, unaligned>(littleval,
88 (int)0xffffaaaa, 4);
89 EXPECT_EQ(littleval[0], 0xa0);
90 EXPECT_EQ(littleval[1], 0xaa);
91 EXPECT_EQ(littleval[2], 0xfa);
92 EXPECT_EQ(littleval[3], 0xff);
93 EXPECT_EQ(littleval[4], 0x0f);
94 EXPECT_EQ(littleval[5], 0x00);
95 EXPECT_EQ(littleval[6], 0x00);
96 EXPECT_EQ(littleval[7], 0x00);
98 // This test makes sure 1<<31 doesn't overflow.
99 // Test to make sure left shift of start bit doesn't overflow.
100 unsigned char bigval2[8] = {0x00};
101 endian::writeAtBitAlignment<int32_t, big, unaligned>(bigval2, (int)0xffffffff,
103 EXPECT_EQ(bigval2[0], 0xff);
104 EXPECT_EQ(bigval2[1], 0xff);
105 EXPECT_EQ(bigval2[2], 0xff);
106 EXPECT_EQ(bigval2[3], 0xfe);
107 EXPECT_EQ(bigval2[4], 0x00);
108 EXPECT_EQ(bigval2[5], 0x00);
109 EXPECT_EQ(bigval2[6], 0x00);
110 EXPECT_EQ(bigval2[7], 0x01);
112 unsigned char littleval2[8] = {0x00};
113 endian::writeAtBitAlignment<int32_t, little, unaligned>(littleval2,
114 (int)0xffffffff, 1);
115 EXPECT_EQ(littleval2[0], 0xfe);
116 EXPECT_EQ(littleval2[1], 0xff);
117 EXPECT_EQ(littleval2[2], 0xff);
118 EXPECT_EQ(littleval2[3], 0xff);
119 EXPECT_EQ(littleval2[4], 0x01);
120 EXPECT_EQ(littleval2[5], 0x00);
121 EXPECT_EQ(littleval2[6], 0x00);
122 EXPECT_EQ(littleval2[7], 0x00);
124 // Test to make sure 64-bit int doesn't overflow.
125 unsigned char bigval64[16] = {0x00};
126 endian::writeAtBitAlignment<int64_t, big, unaligned>(
127 bigval64, (int64_t)0xffffffffffffffff, 1);
128 EXPECT_EQ(bigval64[0], 0xff);
129 EXPECT_EQ(bigval64[1], 0xff);
130 EXPECT_EQ(bigval64[2], 0xff);
131 EXPECT_EQ(bigval64[3], 0xff);
132 EXPECT_EQ(bigval64[4], 0xff);
133 EXPECT_EQ(bigval64[5], 0xff);
134 EXPECT_EQ(bigval64[6], 0xff);
135 EXPECT_EQ(bigval64[7], 0xfe);
136 EXPECT_EQ(bigval64[8], 0x00);
137 EXPECT_EQ(bigval64[9], 0x00);
138 EXPECT_EQ(bigval64[10], 0x00);
139 EXPECT_EQ(bigval64[11], 0x00);
140 EXPECT_EQ(bigval64[12], 0x00);
141 EXPECT_EQ(bigval64[13], 0x00);
142 EXPECT_EQ(bigval64[14], 0x00);
143 EXPECT_EQ(bigval64[15], 0x01);
145 unsigned char littleval64[16] = {0x00};
146 endian::writeAtBitAlignment<int64_t, little, unaligned>(
147 littleval64, (int64_t)0xffffffffffffffff, 1);
148 EXPECT_EQ(littleval64[0], 0xfe);
149 EXPECT_EQ(littleval64[1], 0xff);
150 EXPECT_EQ(littleval64[2], 0xff);
151 EXPECT_EQ(littleval64[3], 0xff);
152 EXPECT_EQ(littleval64[4], 0xff);
153 EXPECT_EQ(littleval64[5], 0xff);
154 EXPECT_EQ(littleval64[6], 0xff);
155 EXPECT_EQ(littleval64[7], 0xff);
156 EXPECT_EQ(littleval64[8], 0x01);
157 EXPECT_EQ(littleval64[9], 0x00);
158 EXPECT_EQ(littleval64[10], 0x00);
159 EXPECT_EQ(littleval64[11], 0x00);
160 EXPECT_EQ(littleval64[12], 0x00);
161 EXPECT_EQ(littleval64[13], 0x00);
162 EXPECT_EQ(littleval64[14], 0x00);
163 EXPECT_EQ(littleval64[15], 0x00);
166 TEST(Endian, Write) {
167 unsigned char data[5];
168 endian::write<int32_t, big, unaligned>(data, -1362446643);
169 EXPECT_EQ(data[0], 0xAE);
170 EXPECT_EQ(data[1], 0xCA);
171 EXPECT_EQ(data[2], 0xB6);
172 EXPECT_EQ(data[3], 0xCD);
173 endian::write<int32_t, big, unaligned>(data + 1, -1362446643);
174 EXPECT_EQ(data[1], 0xAE);
175 EXPECT_EQ(data[2], 0xCA);
176 EXPECT_EQ(data[3], 0xB6);
177 EXPECT_EQ(data[4], 0xCD);
179 endian::write<int32_t, little, unaligned>(data, -1362446643);
180 EXPECT_EQ(data[0], 0xCD);
181 EXPECT_EQ(data[1], 0xB6);
182 EXPECT_EQ(data[2], 0xCA);
183 EXPECT_EQ(data[3], 0xAE);
184 endian::write<int32_t, little, unaligned>(data + 1, -1362446643);
185 EXPECT_EQ(data[1], 0xCD);
186 EXPECT_EQ(data[2], 0xB6);
187 EXPECT_EQ(data[3], 0xCA);
188 EXPECT_EQ(data[4], 0xAE);
191 TEST(Endian, PackedEndianSpecificIntegral) {
192 // These are 5 bytes so we can be sure at least one of the reads is unaligned.
193 unsigned char big[] = {0x00, 0x01, 0x02, 0x03, 0x04};
194 unsigned char little[] = {0x00, 0x04, 0x03, 0x02, 0x01};
195 big32_t *big_val =
196 reinterpret_cast<big32_t *>(big + 1);
197 little32_t *little_val =
198 reinterpret_cast<little32_t *>(little + 1);
200 EXPECT_EQ(*big_val, *little_val);
203 TEST(Endian, PacketEndianSpecificIntegralAsEnum) {
204 enum class Test : uint16_t { ONETWO = 0x0102, TWOONE = 0x0201 };
205 unsigned char bytes[] = {0x01, 0x02};
206 using LittleTest = little_t<Test>;
207 using BigTest = big_t<Test>;
208 EXPECT_EQ(Test::TWOONE, *reinterpret_cast<LittleTest *>(bytes));
209 EXPECT_EQ(Test::ONETWO, *reinterpret_cast<BigTest *>(bytes));
212 } // end anon namespace