blob: b773f8954639f94aaa7ba69f48ab252c1e7c2ad1 [file] [log] [blame]
austinfcuibb742802022-03-10 08:12:22 -06001#include <attn/attention.hpp>
2#include <attn/attn_common.hpp>
3#include <attn/attn_config.hpp>
4#include <attn/attn_handler.hpp>
5#include <util/pdbg.hpp>
6#include <util/trace.hpp>
7
8#include "gtest/gtest.h"
9
10namespace attn
11{
12// these are in the attn_lib but not all exposed via headers
13int handleSpecial(Attention* i_attention);
14} // namespace attn
15
16using namespace attn;
17using namespace util::pdbg;
18
19/** @brief global function to be called back. */
20int handleAttention(Attention* attention)
21{
22 int rc = RC_SUCCESS;
23 if (attention != nullptr)
24 {
25 return rc;
26 }
27 else
28 {
29 return RC_NOT_HANDLED;
30 }
31}
32
33// Global variables for UT #1 and UT#2.
34// Attention type
35Attention::AttentionType gType = Attention::AttentionType::Special;
36// pointer to handler callback function
Patrick Williams27dd6362023-05-10 07:51:20 -050037int (*gHandler)(Attention*) = &(handleSpecial);
austinfcuibb742802022-03-10 08:12:22 -060038const AttentionFlag gAttnFlag = AttentionFlag::enBreakpoints;
39
40// Start preparation for UT case #1.
41
42// Global variables for UT #1
43const char* gPosPath = "/proc0/pib/perv12";
Patrick Williams27dd6362023-05-10 07:51:20 -050044const uint32_t gPos = 12;
austinfcuibb742802022-03-10 08:12:22 -060045
46/** @brief Fixture class for TEST_F(). */
47class AttentionTestPos : public testing::Test
48{
49 public:
50 AttentionTestPos() {}
51
52 void SetUp()
53 {
54 pdbg_targets_init(nullptr);
55
56 target = pdbg_target_from_path(nullptr, gPosPath);
57 EXPECT_NE(nullptr, target);
58
59 config = new Config;
60 EXPECT_EQ(true, config->getFlag(gAttnFlag));
61
62 pAttn = std::make_unique<Attention>(
63 Attention(gType, gHandler, target, config));
64 }
65
66 void TearDown()
67 {
68 delete config;
69 }
70
71 std::unique_ptr<Attention> pAttn;
Patrick Williams27dd6362023-05-10 07:51:20 -050072 Config* config = nullptr;
austinfcuibb742802022-03-10 08:12:22 -060073 pdbg_target* target = nullptr;
74};
75
76TEST_F(AttentionTestPos, TestAttnTargetPos)
77{
78 EXPECT_EQ(0, pAttn->getPriority());
79 EXPECT_EQ(RC_SUCCESS, pAttn->handle());
80
81 // Verify the global target_tmp.
82 EXPECT_NE(nullptr, target);
83 uint32_t attr = std::numeric_limits<uint32_t>::max();
84 pdbg_target_get_attribute(target, "ATTR_FAPI_POS", 4, 1, &attr);
85 EXPECT_EQ(gPos, attr);
86
87 // Verify the target in Attention object.
Patrick Williams27dd6362023-05-10 07:51:20 -050088 attr = std::numeric_limits<uint32_t>::max();
austinfcuibb742802022-03-10 08:12:22 -060089 pdbg_target* target_tmp = pAttn->getTarget();
90 EXPECT_NE(nullptr, target_tmp);
91 pdbg_target_get_attribute(target_tmp, "ATTR_FAPI_POS", 4, 1, &attr);
92 EXPECT_EQ(gPos, attr);
93
94 // Verify the config in Attention object.
95 Config* config_tmp = pAttn->getConfig();
96 EXPECT_EQ(true, config_tmp->getFlag(gAttnFlag));
97 config_tmp->clearFlag(gAttnFlag);
98 EXPECT_EQ(false, config_tmp->getFlag(gAttnFlag));
99 config_tmp->setFlag(gAttnFlag);
100 EXPECT_EQ(true, config_tmp->getFlag(gAttnFlag));
101}
102
103// Start preparation for UT case #2.
104
105// Global variables for UT #2
austinfcuie81eb152022-04-19 10:56:32 -0500106const uint32_t gChipId = 0x20da; // Chip ID for proc0.
austinfcuibb742802022-03-10 08:12:22 -0600107
108/** @brief Fixture class for TEST_F(). */
109class AttentionTestProc : public testing::Test
110{
111 public:
112 AttentionTestProc() {}
113
114 void SetUp()
115 {
116 pdbg_targets_init(nullptr);
117 target = getPrimaryProcessor();
118
119 EXPECT_NE(nullptr, target);
120
austinfcuibb742802022-03-10 08:12:22 -0600121 attr = getTrgtType(target);
austinfcuibb742802022-03-10 08:12:22 -0600122 EXPECT_EQ(TYPE_PROC, attr);
123
124 attr = std::numeric_limits<uint32_t>::max();
125 pdbg_target_get_attribute(target, "ATTR_CHIP_ID", 4, 1, &attr);
126 EXPECT_EQ(attr, gChipId);
127
128 config = new Config;
129 EXPECT_EQ(true, config->getFlag(gAttnFlag));
130
131 pAttn = std::make_unique<Attention>(
132 Attention(gType, gHandler, target, config));
133 }
134
135 void TearDown()
136 {
137 delete config;
138 }
139
140 std::unique_ptr<Attention> pAttn;
Patrick Williams27dd6362023-05-10 07:51:20 -0500141 Config* config = nullptr;
austinfcuibb742802022-03-10 08:12:22 -0600142 pdbg_target* target = nullptr;
Patrick Williams27dd6362023-05-10 07:51:20 -0500143 uint32_t attr = std::numeric_limits<uint32_t>::max();
austinfcuibb742802022-03-10 08:12:22 -0600144};
145
146TEST_F(AttentionTestProc, TestAttentionProc)
147{
148 EXPECT_EQ(0, pAttn->getPriority());
149 EXPECT_EQ(RC_SUCCESS, pAttn->handle());
150
151 // Verify the target in Attention object.
Patrick Williams27dd6362023-05-10 07:51:20 -0500152 attr = std::numeric_limits<uint32_t>::max();
austinfcuibb742802022-03-10 08:12:22 -0600153 pdbg_target* target_tmp = pAttn->getTarget();
154 EXPECT_NE(nullptr, target_tmp);
155 attr = getTrgtType(target_tmp);
156 EXPECT_EQ(TYPE_PROC, attr);
157
158 attr = std::numeric_limits<uint32_t>::max();
159 pdbg_target_get_attribute(target_tmp, "ATTR_CHIP_ID", 4, 1, &attr);
160 EXPECT_EQ(attr, gChipId);
161
162 // Verify the config in Attention object.
163 Config* config_tmp = pAttn->getConfig();
164 EXPECT_EQ(true, config_tmp->getFlag(gAttnFlag));
165 config_tmp->clearFlag(gAttnFlag);
166 EXPECT_EQ(false, config_tmp->getFlag(gAttnFlag));
167 config_tmp->setFlag(gAttnFlag);
168 EXPECT_EQ(true, config_tmp->getFlag(gAttnFlag));
austinfcuid28d5f82022-04-28 16:20:39 -0500169}
170
171TEST(AttnConfig, TestAttnConfig)
172{
173 Config* config = new Config();
174
175 // Test clearFlagAll() function.
176 config->clearFlagAll();
177 EXPECT_EQ(false, config->getFlag(AttentionFlag::enVital));
178 EXPECT_EQ(false, config->getFlag(AttentionFlag::enCheckstop));
179 EXPECT_EQ(false, config->getFlag(AttentionFlag::enTerminate));
180 EXPECT_EQ(false, config->getFlag(AttentionFlag::enBreakpoints));
181 // The dfltTi flag is not impacted.
182 EXPECT_EQ(false, config->getFlag(AttentionFlag::dfltTi));
183 EXPECT_EQ(false, config->getFlag(AttentionFlag::enClrAttnIntr));
184
185 // Test setFlagAll() function.
186 config->setFlagAll();
187 EXPECT_EQ(true, config->getFlag(AttentionFlag::enVital));
188 EXPECT_EQ(true, config->getFlag(AttentionFlag::enCheckstop));
189 EXPECT_EQ(true, config->getFlag(AttentionFlag::enTerminate));
190 EXPECT_EQ(true, config->getFlag(AttentionFlag::enBreakpoints));
191 // The dfltTi flag is not impacted.
192 EXPECT_EQ(false, config->getFlag(AttentionFlag::dfltTi));
193 EXPECT_EQ(true, config->getFlag(AttentionFlag::enClrAttnIntr));
194
195 // Test setFlag() and getFlag() functions.
196 // Only test one flag.
197 config->clearFlagAll();
198 config->setFlag(enVital);
199 EXPECT_EQ(true, config->getFlag(enVital));
200
201 delete config;
Patrick Williams27dd6362023-05-10 07:51:20 -0500202}