1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <stdint.h>
18
19 #include <gtest/gtest.h>
20
21 #include <android-base/silent_death_test.h>
22 #include <unwindstack/Regs.h>
23
24 #include "RegsFake.h"
25 #include "RegsInfo.h"
26
27 namespace unwindstack {
28
TEST(RegsInfoTest,single_uint32_t)29 TEST(RegsInfoTest, single_uint32_t) {
30 RegsImplFake<uint32_t> regs(10);
31 RegsInfo<uint32_t> info(®s);
32
33 regs[1] = 0x100;
34 ASSERT_FALSE(info.IsSaved(1));
35 ASSERT_EQ(0x100U, info.Get(1));
36 ASSERT_EQ(10, info.Total());
37
38 uint32_t* value = info.Save(1);
39 ASSERT_EQ(value, ®s[1]);
40 regs[1] = 0x200;
41 ASSERT_TRUE(info.IsSaved(1));
42 ASSERT_EQ(0x100U, info.Get(1));
43 ASSERT_EQ(0x200U, regs[1]);
44 }
45
TEST(RegsInfoTest,single_uint64_t)46 TEST(RegsInfoTest, single_uint64_t) {
47 RegsImplFake<uint64_t> regs(20);
48 RegsInfo<uint64_t> info(®s);
49
50 regs[3] = 0x300;
51 ASSERT_FALSE(info.IsSaved(3));
52 ASSERT_EQ(0x300U, info.Get(3));
53 ASSERT_EQ(20, info.Total());
54
55 uint64_t* value = info.Save(3);
56 ASSERT_EQ(value, ®s[3]);
57 regs[3] = 0x400;
58 ASSERT_TRUE(info.IsSaved(3));
59 ASSERT_EQ(0x300U, info.Get(3));
60 ASSERT_EQ(0x400U, regs[3]);
61 }
62
TEST(RegsInfoTest,all)63 TEST(RegsInfoTest, all) {
64 RegsImplFake<uint64_t> regs(64);
65 RegsInfo<uint64_t> info(®s);
66
67 for (uint32_t i = 0; i < 64; i++) {
68 regs[i] = i * 0x100;
69 ASSERT_EQ(i * 0x100, info.Get(i)) << "Reg " + std::to_string(i) + " failed.";
70 }
71
72 for (uint32_t i = 0; i < 64; i++) {
73 ASSERT_FALSE(info.IsSaved(i)) << "Reg " + std::to_string(i) + " failed.";
74 uint64_t* reg = info.Save(i);
75 ASSERT_EQ(reg, ®s[i]) << "Reg " + std::to_string(i) + " failed.";
76 *reg = i * 0x1000 + 0x100;
77 ASSERT_EQ(i * 0x1000 + 0x100, regs[i]) << "Reg " + std::to_string(i) + " failed.";
78 }
79
80 for (uint32_t i = 0; i < 64; i++) {
81 ASSERT_TRUE(info.IsSaved(i)) << "Reg " + std::to_string(i) + " failed.";
82 ASSERT_EQ(i * 0x100, info.Get(i)) << "Reg " + std::to_string(i) + " failed.";
83 }
84 }
85
86 using RegsInfoDeathTest = SilentDeathTest;
87
TEST_F(RegsInfoDeathTest,invalid_register)88 TEST_F(RegsInfoDeathTest, invalid_register) {
89 RegsImplFake<uint64_t> regs(64);
90 RegsInfo<uint64_t> info(®s);
91
92 ASSERT_DEATH(info.Save(RegsInfo<uint64_t>::MAX_REGISTERS), "");
93 }
94
95 } // namespace unwindstack
96