101 lines · cpp
1//===- EndianTest.cpp -----------------------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8//9// Tests for orc-rt's Endian.h APIs.10//11//===----------------------------------------------------------------------===//12 13#include "orc-rt/Endian.h"14#include "gtest/gtest.h"15 16#include <algorithm>17#include <limits>18 19using namespace orc_rt;20 21template <typename T> static void endianRead(T Value, endian E) {22 char Buffer[sizeof(T)];23 memcpy(Buffer, &Value, sizeof(T));24 25 if (E != endian::native)26 std::reverse(Buffer, Buffer + sizeof(T));27 28 T NewVal = endian_read<T>(Buffer, E);29 EXPECT_EQ(NewVal, Value);30}31 32template <typename T> static void endianWrite(T Value, endian E) {33 char Buffer[sizeof(T)];34 35 endian_write(Buffer, Value, E);36 37 if (E != endian::native)38 std::reverse(Buffer, Buffer + sizeof(T));39 40 T NewVal;41 memcpy(&NewVal, Buffer, sizeof(T));42 43 EXPECT_EQ(NewVal, Value);44}45 46template <typename T> static void endianReadAndWrite(T Value, endian E) {47 endianRead(Value, E);48 endianWrite(Value, E);49}50 51template <typename T> static void bothEndiansReadAndWrite(T Value) {52 endianReadAndWrite(Value, endian::little);53 endianReadAndWrite(Value, endian::big);54}55 56// Rotate the given bit pattern through all valid rotations for T, testing that57// the given operation works for the given pattern.58template <typename Op, typename T>59void forAllRotatedValues(Op O, T InitialValue) {60 T V = InitialValue;61 for (size_t I = 0; I != CHAR_BIT * sizeof(T); ++I) {62 O(V);63 V = llvm::rotl(V, 1);64 }65}66 67template <typename Op, typename T>68void forAllShiftedValues(Op O, T InitialValue) {69 T V = InitialValue;70 constexpr T TopValueBit = 1 << (std::numeric_limits<T>::digits - 1);71 for (size_t I = 0; I != CHAR_BIT * sizeof(T); ++I) {72 O(V);73 if (V & TopValueBit)74 break;75 V << 1;76 }77}78 79TEST(EndianTest, ReadWrite) {80 bothEndiansReadAndWrite<uint8_t>(0);81 bothEndiansReadAndWrite<uint8_t>(0xff);82 forAllRotatedValues(bothEndiansReadAndWrite<uint8_t>, uint8_t(1));83 forAllRotatedValues(bothEndiansReadAndWrite<uint8_t>, uint8_t(0x5A));84 85 bothEndiansReadAndWrite<uint16_t>(0);86 bothEndiansReadAndWrite<uint16_t>(0xffff);87 forAllRotatedValues(bothEndiansReadAndWrite<uint16_t>, uint16_t(1));88 forAllRotatedValues(bothEndiansReadAndWrite<uint16_t>, uint16_t(0x5A5A));89 90 bothEndiansReadAndWrite<uint32_t>(0);91 bothEndiansReadAndWrite<uint32_t>(0xffffffff);92 forAllRotatedValues(bothEndiansReadAndWrite<uint32_t>, uint32_t(1));93 forAllRotatedValues(bothEndiansReadAndWrite<uint32_t>, uint32_t(0x5A5A5A5A));94 95 bothEndiansReadAndWrite<uint64_t>(0);96 bothEndiansReadAndWrite<uint64_t>(0xffffffffffffffff);97 forAllRotatedValues(bothEndiansReadAndWrite<uint64_t>, uint64_t(1));98 forAllRotatedValues(bothEndiansReadAndWrite<uint64_t>,99 uint64_t(0x5A5A5A5A5A5A5A5A));100}101