166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman//===- unittests/Support/EndianTest.cpp - Endian.h tests ------------------===// 266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman// 366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman// The LLVM Compiler Infrastructure 466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman// 566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman// This file is distributed under the University of Illinois Open Source 666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman// License. See LICENSE.TXT for details. 766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman// 866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman//===----------------------------------------------------------------------===// 966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman 1066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#include "gtest/gtest.h" 1166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#include "llvm/Support/Endian.h" 1266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#include "llvm/Support/DataTypes.h" 1366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#include <cstdlib> 1466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#include <ctime> 1566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Baumanusing namespace llvm; 1666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Baumanusing namespace support; 1766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman 1866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#undef max 1966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman 2066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Baumannamespace { 2166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman 2266b8ab22586debccb1f787d4d52b7f042d4ddeb8John BaumanTEST(Endian, Read) { 2366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman // These are 5 bytes so we can be sure at least one of the reads is unaligned. 2466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman unsigned char big[] = {0x00, 0x01, 0x02, 0x03, 0x04}; 2566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman unsigned char little[] = {0x00, 0x04, 0x03, 0x02, 0x01}; 2666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman int32_t BigAsHost = 0x00010203; 2766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman EXPECT_EQ(BigAsHost, (endian::read_be<int32_t, unaligned>(big))); 2866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman int32_t LittleAsHost = 0x02030400; 2966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman EXPECT_EQ(LittleAsHost, (endian::read_le<int32_t, unaligned>(little))); 3066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman 3166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman EXPECT_EQ((endian::read_be<int32_t, unaligned>(big + 1)), 3266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman (endian::read_le<int32_t, unaligned>(little + 1))); 3366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman} 3466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman 3566b8ab22586debccb1f787d4d52b7f042d4ddeb8John BaumanTEST(Endian, Write) { 3666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman unsigned char data[5]; 3766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman endian::write_be<int32_t, unaligned>(data, -1362446643); 3866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman EXPECT_EQ(data[0], 0xAE); 3966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman EXPECT_EQ(data[1], 0xCA); 4066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman EXPECT_EQ(data[2], 0xB6); 4166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman EXPECT_EQ(data[3], 0xCD); 4266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman endian::write_be<int32_t, unaligned>(data + 1, -1362446643); 4366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman EXPECT_EQ(data[1], 0xAE); 4466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman EXPECT_EQ(data[2], 0xCA); 4566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman EXPECT_EQ(data[3], 0xB6); 4666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman EXPECT_EQ(data[4], 0xCD); 4766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman 4866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman endian::write_le<int32_t, unaligned>(data, -1362446643); 4966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman EXPECT_EQ(data[0], 0xCD); 5066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman EXPECT_EQ(data[1], 0xB6); 5166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman EXPECT_EQ(data[2], 0xCA); 5266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman EXPECT_EQ(data[3], 0xAE); 5366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman endian::write_le<int32_t, unaligned>(data + 1, -1362446643); 5466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman EXPECT_EQ(data[1], 0xCD); 5566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman EXPECT_EQ(data[2], 0xB6); 5666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman EXPECT_EQ(data[3], 0xCA); 5766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman EXPECT_EQ(data[4], 0xAE); 5866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman} 5966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman 6066b8ab22586debccb1f787d4d52b7f042d4ddeb8John BaumanTEST(Endian, PackedEndianSpecificIntegral) { 6166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman // These are 5 bytes so we can be sure at least one of the reads is unaligned. 6266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman unsigned char big[] = {0x00, 0x01, 0x02, 0x03, 0x04}; 6366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman unsigned char little[] = {0x00, 0x04, 0x03, 0x02, 0x01}; 6466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman big32_t *big_val = 6566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman reinterpret_cast<big32_t *>(big + 1); 6666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman little32_t *little_val = 6766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman reinterpret_cast<little32_t *>(little + 1); 6866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman 6966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman EXPECT_EQ(*big_val, *little_val); 7066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman} 7166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman 7266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman} 73