1 //===- llvm/unittest/Support/ScaledNumberTest.cpp - ScaledPair tests -----==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/ScaledNumber.h"
12 #include "llvm/Support/DataTypes.h"
13 #include "gtest/gtest.h"
16 using namespace llvm::ScaledNumbers;
20 template <class UIntT> struct ScaledPair {
23 ScaledPair(const std::pair<UIntT, int16_t> &F) : D(F.first), S(F.second) {}
24 ScaledPair(UIntT D, int S) : D(D), S(S) {}
26 bool operator==(const ScaledPair<UIntT> &X) const {
27 return D == X.D && S == X.S;
30 template <class UIntT>
31 bool operator==(const std::pair<UIntT, int16_t> &L,
32 const ScaledPair<UIntT> &R) {
33 return ScaledPair<UIntT>(L) == R;
35 template <class UIntT>
36 void PrintTo(const ScaledPair<UIntT> &F, ::std::ostream *os) {
37 *os << F.D << "*2^" << F.S;
40 typedef ScaledPair<uint32_t> SP32;
41 typedef ScaledPair<uint64_t> SP64;
43 TEST(ScaledNumberHelpersTest, getRounded) {
44 EXPECT_EQ(getRounded<uint32_t>(0, 0, false), SP32(0, 0));
45 EXPECT_EQ(getRounded<uint32_t>(0, 0, true), SP32(1, 0));
46 EXPECT_EQ(getRounded<uint32_t>(20, 21, true), SP32(21, 21));
47 EXPECT_EQ(getRounded<uint32_t>(UINT32_MAX, 0, false), SP32(UINT32_MAX, 0));
48 EXPECT_EQ(getRounded<uint32_t>(UINT32_MAX, 0, true), SP32(1 << 31, 1));
50 EXPECT_EQ(getRounded<uint64_t>(0, 0, false), SP64(0, 0));
51 EXPECT_EQ(getRounded<uint64_t>(0, 0, true), SP64(1, 0));
52 EXPECT_EQ(getRounded<uint64_t>(20, 21, true), SP64(21, 21));
53 EXPECT_EQ(getRounded<uint64_t>(UINT32_MAX, 0, false), SP64(UINT32_MAX, 0));
54 EXPECT_EQ(getRounded<uint64_t>(UINT32_MAX, 0, true),
55 SP64(UINT64_C(1) << 32, 0));
56 EXPECT_EQ(getRounded<uint64_t>(UINT64_MAX, 0, false), SP64(UINT64_MAX, 0));
57 EXPECT_EQ(getRounded<uint64_t>(UINT64_MAX, 0, true),
58 SP64(UINT64_C(1) << 63, 1));