blob: 052b5bf05563cdc7c849eb0d96ba4bd16ad1c89f [file] [log] [blame]
Christopher Ferris27866082018-08-02 15:21:37 -07001/*
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 <unwindstack/Regs.h>
22
23#include "RegsFake.h"
24#include "RegsInfo.h"
25
26namespace unwindstack {
27
28TEST(RegsInfoTest, single_uint32_t) {
29 RegsImplFake<uint32_t> regs(10);
30 RegsInfo<uint32_t> info(&regs);
31
32 regs[1] = 0x100;
33 ASSERT_FALSE(info.IsSaved(1));
34 ASSERT_EQ(0x100U, info.Get(1));
35 ASSERT_EQ(10, info.Total());
36
37 uint32_t* value = info.Save(1);
38 ASSERT_EQ(value, &regs[1]);
39 regs[1] = 0x200;
40 ASSERT_TRUE(info.IsSaved(1));
41 ASSERT_EQ(0x100U, info.Get(1));
42 ASSERT_EQ(0x200U, regs[1]);
43}
44
45TEST(RegsInfoTest, single_uint64_t) {
46 RegsImplFake<uint64_t> regs(20);
47 RegsInfo<uint64_t> info(&regs);
48
49 regs[3] = 0x300;
50 ASSERT_FALSE(info.IsSaved(3));
51 ASSERT_EQ(0x300U, info.Get(3));
52 ASSERT_EQ(20, info.Total());
53
54 uint64_t* value = info.Save(3);
55 ASSERT_EQ(value, &regs[3]);
56 regs[3] = 0x400;
57 ASSERT_TRUE(info.IsSaved(3));
58 ASSERT_EQ(0x300U, info.Get(3));
59 ASSERT_EQ(0x400U, regs[3]);
60}
61
62TEST(RegsInfoTest, all) {
63 RegsImplFake<uint64_t> regs(64);
64 RegsInfo<uint64_t> info(&regs);
65
66 for (uint32_t i = 0; i < 64; i++) {
67 regs[i] = i * 0x100;
68 ASSERT_EQ(i * 0x100, info.Get(i)) << "Reg " + std::to_string(i) + " failed.";
69 }
70
71 for (uint32_t i = 0; i < 64; i++) {
72 ASSERT_FALSE(info.IsSaved(i)) << "Reg " + std::to_string(i) + " failed.";
73 uint64_t* reg = info.Save(i);
74 ASSERT_EQ(reg, &regs[i]) << "Reg " + std::to_string(i) + " failed.";
75 *reg = i * 0x1000 + 0x100;
76 ASSERT_EQ(i * 0x1000 + 0x100, regs[i]) << "Reg " + std::to_string(i) + " failed.";
77 }
78
79 for (uint32_t i = 0; i < 64; i++) {
80 ASSERT_TRUE(info.IsSaved(i)) << "Reg " + std::to_string(i) + " failed.";
81 ASSERT_EQ(i * 0x100, info.Get(i)) << "Reg " + std::to_string(i) + " failed.";
82 }
83}
84
85} // namespace unwindstack