1 //===-- RegisterValueTest.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-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/Utility/RegisterValue.h"
10 #include "gtest/gtest.h"
11 
12 using namespace lldb_private;
13 using llvm::APInt;
14 using llvm::ArrayRef;
15 
TEST(RegisterValueTest,GetSet8)16 TEST(RegisterValueTest, GetSet8) {
17   RegisterValue R8(uint8_t(47));
18   EXPECT_EQ(47u, R8.GetAsUInt8());
19   R8 = uint8_t(42);
20   EXPECT_EQ(42u, R8.GetAsUInt8());
21   EXPECT_EQ(42u, R8.GetAsUInt16());
22   EXPECT_EQ(42u, R8.GetAsUInt32());
23   EXPECT_EQ(42u, R8.GetAsUInt64());
24 }
25 
TEST(RegisterValueTest,GetScalarValue)26 TEST(RegisterValueTest, GetScalarValue) {
27   using RV = RegisterValue;
28   const auto &Get = [](const RV &V) -> llvm::Optional<Scalar> {
29     Scalar S;
30     if (V.GetScalarValue(S))
31       return S;
32     return llvm::None;
33   };
34   EXPECT_EQ(Get(RV(uint8_t(47))), Scalar(47));
35   EXPECT_EQ(Get(RV(uint16_t(4747))), Scalar(4747));
36   EXPECT_EQ(Get(RV(uint32_t(47474242))), Scalar(47474242));
37   EXPECT_EQ(Get(RV(uint64_t(4747424247474242))), Scalar(4747424247474242));
38   EXPECT_EQ(Get(RV(APInt::getMaxValue(128))), Scalar(APInt::getMaxValue(128)));
39   EXPECT_EQ(Get(RV(47.5f)), Scalar(47.5f));
40   EXPECT_EQ(Get(RV(47.5)), Scalar(47.5));
41   EXPECT_EQ(Get(RV(47.5L)), Scalar(47.5L));
42   EXPECT_EQ(Get(RV({0xff, 0xee, 0xdd, 0xcc}, lldb::eByteOrderLittle)),
43             Scalar(0xccddeeff));
44   EXPECT_EQ(Get(RV({0xff, 0xee, 0xdd, 0xcc}, lldb::eByteOrderBig)),
45             Scalar(0xffeeddcc));
46   EXPECT_EQ(Get(RV({0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88, 0x77, 0x66,
47                     0x55, 0x44, 0x33, 0x22, 0x11, 0x00},
48                    lldb::eByteOrderLittle)),
49             Scalar((APInt(128, 0x0011223344556677ull) << 64) |
50                    APInt(128, 0x8899aabbccddeeff)));
51   EXPECT_EQ(Get(RV({0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88, 0x77, 0x66,
52                     0x55, 0x44, 0x33, 0x22, 0x11, 0x00},
53                    lldb::eByteOrderBig)),
54             Scalar((APInt(128, 0xffeeddccbbaa9988ull) << 64) |
55                    APInt(128, 0x7766554433221100)));
56 }
57