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