1 #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 
10 namespace attn
11 {
12 // these are in the attn_lib but not all exposed via headers
13 int handleSpecial(Attention* i_attention);
14 } // namespace attn
15 
16 using namespace attn;
17 using namespace util::pdbg;
18 
19 /** @brief global function to be called back. */
20 int 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
35 Attention::AttentionType gType = Attention::AttentionType::Special;
36 // pointer to handler callback function
37 int (*gHandler)(Attention*)   = &(handleSpecial);
38 const AttentionFlag gAttnFlag = AttentionFlag::enBreakpoints;
39 
40 // Start preparation for UT case #1.
41 
42 // Global variables for UT #1
43 const char* gPosPath = "/proc0/pib/perv12";
44 const uint32_t gPos  = 12;
45 
46 /** @brief Fixture class for TEST_F(). */
47 class 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;
72     Config* config;
73     pdbg_target* target = nullptr;
74 };
75 
76 TEST_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.
88     attr                    = std::numeric_limits<uint32_t>::max();
89     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
106 const uint32_t gChipId = 0; // ID for proc0.
107 
108 /** @brief Fixture class for TEST_F(). */
109 class 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 
121         attr = std::numeric_limits<uint32_t>::max();
122         attr = getTrgtType(target);
123 
124         EXPECT_EQ(TYPE_PROC, attr);
125 
126         attr = std::numeric_limits<uint32_t>::max();
127         pdbg_target_get_attribute(target, "ATTR_CHIP_ID", 4, 1, &attr);
128         EXPECT_EQ(attr, gChipId);
129 
130         config = new Config;
131         EXPECT_EQ(true, config->getFlag(gAttnFlag));
132 
133         pAttn = std::make_unique<Attention>(
134             Attention(gType, gHandler, target, config));
135     }
136 
137     void TearDown()
138     {
139         delete config;
140     }
141 
142     std::unique_ptr<Attention> pAttn;
143     Config* config;
144     pdbg_target* target = nullptr;
145     uint32_t attr;
146 };
147 
148 TEST_F(AttentionTestProc, TestAttentionProc)
149 {
150     EXPECT_EQ(0, pAttn->getPriority());
151     EXPECT_EQ(RC_SUCCESS, pAttn->handle());
152 
153     // Verify the target in Attention object.
154     attr                    = std::numeric_limits<uint32_t>::max();
155     pdbg_target* target_tmp = pAttn->getTarget();
156     EXPECT_NE(nullptr, target_tmp);
157     attr = getTrgtType(target_tmp);
158     EXPECT_EQ(TYPE_PROC, attr);
159 
160     attr = std::numeric_limits<uint32_t>::max();
161     pdbg_target_get_attribute(target_tmp, "ATTR_CHIP_ID", 4, 1, &attr);
162     EXPECT_EQ(attr, gChipId);
163 
164     // Verify the config in Attention object.
165     Config* config_tmp = pAttn->getConfig();
166     EXPECT_EQ(true, config_tmp->getFlag(gAttnFlag));
167     config_tmp->clearFlag(gAttnFlag);
168     EXPECT_EQ(false, config_tmp->getFlag(gAttnFlag));
169     config_tmp->setFlag(gAttnFlag);
170     EXPECT_EQ(true, config_tmp->getFlag(gAttnFlag));
171 }