1//===-- DumpRegisterInfoTest.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/Core/DumpRegisterInfo.h"
10#include "lldb/Target/RegisterFlags.h"
11#include "lldb/Utility/StreamString.h"
12#include "gtest/gtest.h"
13
14using namespace lldb_private;
15
16TEST(DoDumpRegisterInfoTest, MinimumInfo) {
17 StreamString strm;
18 DoDumpRegisterInfo(strm, name: "foo", alt_name: nullptr, byte_size: 4, invalidates: {}, read_from: {}, in_sets: {}, flags_type: nullptr, terminal_width: 0);
19 ASSERT_EQ(strm.GetString(), " Name: foo\n"
20 " Size: 4 bytes (32 bits)");
21}
22
23TEST(DoDumpRegisterInfoTest, AltName) {
24 StreamString strm;
25 DoDumpRegisterInfo(strm, name: "foo", alt_name: "bar", byte_size: 4, invalidates: {}, read_from: {}, in_sets: {}, flags_type: nullptr, terminal_width: 0);
26 ASSERT_EQ(strm.GetString(), " Name: foo (bar)\n"
27 " Size: 4 bytes (32 bits)");
28}
29
30TEST(DoDumpRegisterInfoTest, Invalidates) {
31 StreamString strm;
32 DoDumpRegisterInfo(strm, name: "foo", alt_name: nullptr, byte_size: 4, invalidates: {"foo2"}, read_from: {}, in_sets: {}, flags_type: nullptr, terminal_width: 0);
33 ASSERT_EQ(strm.GetString(), " Name: foo\n"
34 " Size: 4 bytes (32 bits)\n"
35 "Invalidates: foo2");
36
37 strm.Clear();
38 DoDumpRegisterInfo(strm, name: "foo", alt_name: nullptr, byte_size: 4, invalidates: {"foo2", "foo3", "foo4"}, read_from: {}, in_sets: {},
39 flags_type: nullptr, terminal_width: 0);
40 ASSERT_EQ(strm.GetString(), " Name: foo\n"
41 " Size: 4 bytes (32 bits)\n"
42 "Invalidates: foo2, foo3, foo4");
43}
44
45TEST(DoDumpRegisterInfoTest, ReadFrom) {
46 StreamString strm;
47 DoDumpRegisterInfo(strm, name: "foo", alt_name: nullptr, byte_size: 4, invalidates: {}, read_from: {"foo1"}, in_sets: {}, flags_type: nullptr, terminal_width: 0);
48 ASSERT_EQ(strm.GetString(), " Name: foo\n"
49 " Size: 4 bytes (32 bits)\n"
50 " Read from: foo1");
51
52 strm.Clear();
53 DoDumpRegisterInfo(strm, name: "foo", alt_name: nullptr, byte_size: 4, invalidates: {}, read_from: {"foo1", "foo2", "foo3"}, in_sets: {},
54 flags_type: nullptr, terminal_width: 0);
55 ASSERT_EQ(strm.GetString(), " Name: foo\n"
56 " Size: 4 bytes (32 bits)\n"
57 " Read from: foo1, foo2, foo3");
58}
59
60TEST(DoDumpRegisterInfoTest, InSets) {
61 StreamString strm;
62 DoDumpRegisterInfo(strm, name: "foo", alt_name: nullptr, byte_size: 4, invalidates: {}, read_from: {}, in_sets: {{"set1", 101}}, flags_type: nullptr,
63 terminal_width: 0);
64 ASSERT_EQ(strm.GetString(), " Name: foo\n"
65 " Size: 4 bytes (32 bits)\n"
66 " In sets: set1 (index 101)");
67
68 strm.Clear();
69 DoDumpRegisterInfo(strm, name: "foo", alt_name: nullptr, byte_size: 4, invalidates: {}, read_from: {},
70 in_sets: {{"set1", 0}, {"set2", 1}, {"set3", 2}}, flags_type: nullptr, terminal_width: 0);
71 ASSERT_EQ(strm.GetString(),
72 " Name: foo\n"
73 " Size: 4 bytes (32 bits)\n"
74 " In sets: set1 (index 0), set2 (index 1), set3 (index 2)");
75}
76
77TEST(DoDumpRegisterInfoTest, MaxInfo) {
78 StreamString strm;
79 DoDumpRegisterInfo(strm, name: "foo", alt_name: nullptr, byte_size: 4, invalidates: {"foo2", "foo3"},
80 read_from: {"foo3", "foo4"}, in_sets: {{"set1", 1}, {"set2", 2}}, flags_type: nullptr, terminal_width: 0);
81 ASSERT_EQ(strm.GetString(), " Name: foo\n"
82 " Size: 4 bytes (32 bits)\n"
83 "Invalidates: foo2, foo3\n"
84 " Read from: foo3, foo4\n"
85 " In sets: set1 (index 1), set2 (index 2)");
86}
87
88TEST(DoDumpRegisterInfoTest, FieldsTable) {
89 // This is thoroughly tested in RegisterFlags itself, only checking the
90 // integration here.
91 StreamString strm;
92 RegisterFlags flags(
93 "", 4,
94 {RegisterFlags::Field("A", 24, 31), RegisterFlags::Field("B", 16, 23),
95 RegisterFlags::Field("C", 8, 15), RegisterFlags::Field("D", 0, 7)});
96
97 DoDumpRegisterInfo(strm, name: "foo", alt_name: nullptr, byte_size: 4, invalidates: {}, read_from: {}, in_sets: {}, flags_type: &flags, terminal_width: 100);
98 ASSERT_EQ(strm.GetString(), " Name: foo\n"
99 " Size: 4 bytes (32 bits)\n"
100 "\n"
101 "| 31-24 | 23-16 | 15-8 | 7-0 |\n"
102 "|-------|-------|------|-----|\n"
103 "| A | B | C | D |");
104}
105
106TEST(DoDumpRegisterInfoTest, Enumerators) {
107 StreamString strm;
108
109 FieldEnum enum_one("enum_one", {{0, "an_enumerator"}});
110 FieldEnum enum_two("enum_two",
111 {{1, "another_enumerator"}, {2, "another_enumerator_2"}});
112
113 RegisterFlags flags("", 4,
114 {RegisterFlags::Field("A", 24, 31, &enum_one),
115 RegisterFlags::Field("B", 16, 23),
116 RegisterFlags::Field("C", 8, 15, &enum_two)});
117
118 DoDumpRegisterInfo(strm, name: "abc", alt_name: nullptr, byte_size: 4, invalidates: {}, read_from: {}, in_sets: {}, flags_type: &flags, terminal_width: 100);
119 ASSERT_EQ(strm.GetString(),
120 " Name: abc\n"
121 " Size: 4 bytes (32 bits)\n"
122 "\n"
123 "| 31-24 | 23-16 | 15-8 | 7-0 |\n"
124 "|-------|-------|------|-----|\n"
125 "| A | B | C | |\n"
126 "\n"
127 "A: 0 = an_enumerator\n"
128 "\n"
129 "C: 1 = another_enumerator, 2 = another_enumerator_2");
130}
131

source code of lldb/unittests/Core/DumpRegisterInfoTest.cpp