36 lines · cpp
1#include <cstdint>2#include <vector>3 4#include "profile/MemProfData.inc"5#include "gtest/gtest.h"6 7namespace llvm {8namespace memprof {9namespace {10TEST(MemProf, F16EncodeDecode) {11 const std::vector<uint64_t> TestCases = {12 0, 100, 4095, 4096, 5000, 8191, 65535, 1000000, 134213640, 200000000,13 };14 15 for (const uint64_t TestCase : TestCases) {16 const uint16_t Encoded = encodeHistogramCount(TestCase);17 const uint64_t Decoded = decodeHistogramCount(Encoded);18 19 const uint64_t MaxRepresentable = static_cast<uint64_t>(MaxMantissa)20 << MaxExponent;21 22 if (TestCase >= MaxRepresentable) {23 EXPECT_EQ(Decoded, MaxRepresentable);24 } else if (TestCase <= MaxMantissa) {25 EXPECT_EQ(Decoded, TestCase);26 } else {27 // The decoded value should be close to the original value.28 // The error should be less than 1/1024 for larger numbers.29 EXPECT_NEAR(Decoded, TestCase, static_cast<double>(TestCase) / 1024.0);30 }31 }32}33} // namespace34} // namespace memprof35} // namespace llvm36