blob: a8ec0277d2e4388341742bfa4cef478b7b1fb6a3 [file] [log] [blame]
Francis Murtagh1f7db452019-08-14 09:49:34 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include "../CommandHandlerKey.hpp"
Francis Murtagh11f99b42019-08-16 11:28:52 +01007#include "../CommandHandlerFunctor.hpp"
Francis Murtagh94d79152019-08-16 17:45:07 +01008#include "../CommandHandlerRegistry.hpp"
Nikhil Rajbc626052019-08-15 15:49:45 +01009#include "../Packet.hpp"
Francis Murtagh1f7db452019-08-14 09:49:34 +010010
Nikhil Rajbc626052019-08-15 15:49:45 +010011#include <cstdint>
12#include <cstring>
Francis Murtagh1f7db452019-08-14 09:49:34 +010013#include <boost/test/unit_test.hpp>
Francis Murtagh11f99b42019-08-16 11:28:52 +010014#include <map>
Francis Murtagh1f7db452019-08-14 09:49:34 +010015
16BOOST_AUTO_TEST_SUITE(ExternalProfiling)
17
18BOOST_AUTO_TEST_CASE(CheckCommandHandlerKeyComparisons)
19{
20 CommandHandlerKey testKey0(1, 1);
21 CommandHandlerKey testKey1(1, 1);
22 CommandHandlerKey testKey2(1, 1);
23 CommandHandlerKey testKey3(0, 0);
24 CommandHandlerKey testKey4(2, 2);
25 CommandHandlerKey testKey5(0, 2);
26
27 BOOST_CHECK(testKey1<testKey4);
28 BOOST_CHECK(testKey1>testKey3);
29 BOOST_CHECK(testKey1<=testKey4);
30 BOOST_CHECK(testKey1>=testKey3);
31 BOOST_CHECK(testKey1<=testKey2);
32 BOOST_CHECK(testKey1>=testKey2);
33 BOOST_CHECK(testKey1==testKey2);
34 BOOST_CHECK(testKey1==testKey1);
35
36 BOOST_CHECK(!(testKey1==testKey5));
37 BOOST_CHECK(!(testKey1!=testKey1));
38 BOOST_CHECK(testKey1!=testKey5);
39
40 BOOST_CHECK(testKey1==testKey2 && testKey2==testKey1);
41 BOOST_CHECK(testKey0==testKey1 && testKey1==testKey2 && testKey0==testKey2);
42
43 BOOST_CHECK(testKey1.GetPacketId()==1);
44 BOOST_CHECK(testKey1.GetVersion()==1);
45
46 std::vector<CommandHandlerKey> vect =
47 {
48 CommandHandlerKey(0,1), CommandHandlerKey(2,0), CommandHandlerKey(1,0),
49 CommandHandlerKey(2,1), CommandHandlerKey(1,1), CommandHandlerKey(0,1),
50 CommandHandlerKey(2,0), CommandHandlerKey(0,0)
51 };
52
53 std::sort(vect.begin(), vect.end());
54
55 std::vector<CommandHandlerKey> expectedVect =
56 {
57 CommandHandlerKey(0,0), CommandHandlerKey(0,1), CommandHandlerKey(0,1),
58 CommandHandlerKey(1,0), CommandHandlerKey(1,1), CommandHandlerKey(2,0),
59 CommandHandlerKey(2,0), CommandHandlerKey(2,1)
60 };
61
62 BOOST_CHECK(vect == expectedVect);
63}
64
Nikhil Rajbc626052019-08-15 15:49:45 +010065BOOST_AUTO_TEST_CASE(CheckPacketClass)
66{
67 const char* data = "test";
68 unsigned int length = static_cast<unsigned int>(std::strlen(data));
69
70 Packet packetTest1(472580096,length,data);
71 BOOST_CHECK_THROW(Packet packetTest2(472580096,0,""), armnn::Exception);
72
73 Packet packetTest3(472580096,0, nullptr);
74
75 BOOST_CHECK(packetTest1.GetLength() == length);
76 BOOST_CHECK(packetTest1.GetData() == data);
77
78 BOOST_CHECK(packetTest1.GetPacketFamily() == 7);
79 BOOST_CHECK(packetTest1.GetPacketId() == 43);
80 BOOST_CHECK(packetTest1.GetPacketType() == 3);
81 BOOST_CHECK(packetTest1.GetPacketClass() == 5);
82}
83
Francis Murtagh94d79152019-08-16 17:45:07 +010084// Create Derived Classes
85class TestFunctorA : public CommandHandlerFunctor
86{
87public:
88 using CommandHandlerFunctor::CommandHandlerFunctor;
89
90 int GetCount() { return m_Count; }
91
92 void operator()(const Packet& packet) override
93 {
94 m_Count++;
95 }
96
97private:
98 int m_Count = 0;
99};
100
101class TestFunctorB : public TestFunctorA
102{
103 using TestFunctorA::TestFunctorA;
104};
105
106class TestFunctorC : public TestFunctorA
107{
108 using TestFunctorA::TestFunctorA;
109};
110
Francis Murtagh11f99b42019-08-16 11:28:52 +0100111BOOST_AUTO_TEST_CASE(CheckCommandHandlerFunctor)
112{
Francis Murtagh11f99b42019-08-16 11:28:52 +0100113 // Hard code the version as it will be the same during a single profiling session
114 uint32_t version = 1;
115
116 TestFunctorA testFunctorA(461, version);
117 TestFunctorB testFunctorB(963, version);
118 TestFunctorC testFunctorC(983, version);
119
120 CommandHandlerKey keyA(testFunctorA.GetPacketId(), testFunctorA.GetVersion());
121 CommandHandlerKey keyB(testFunctorB.GetPacketId(), testFunctorB.GetVersion());
122 CommandHandlerKey keyC(testFunctorC.GetPacketId(), testFunctorC.GetVersion());
123
124 // Create the unwrapped map to simulate the Command Handler Registry
125 std::map<CommandHandlerKey, CommandHandlerFunctor*> registry;
126
127 registry.insert(std::make_pair(keyB, &testFunctorB));
128 registry.insert(std::make_pair(keyA, &testFunctorA));
129 registry.insert(std::make_pair(keyC, &testFunctorC));
130
131 // Check the order of the map is correct
132 auto it = registry.begin();
133 BOOST_CHECK(it->first==keyA);
134 it++;
135 BOOST_CHECK(it->first==keyB);
136 it++;
137 BOOST_CHECK(it->first==keyC);
138
139 Packet packetA(500000000, 0, nullptr);
140 Packet packetB(600000000, 0, nullptr);
141 Packet packetC(400000000, 0, nullptr);
142
143 // Check the correct operator of derived class is called
144 registry.at(CommandHandlerKey(packetA.GetPacketId(), version))->operator()(packetA);
145 BOOST_CHECK(testFunctorA.GetCount() == 1);
146 BOOST_CHECK(testFunctorB.GetCount() == 0);
147 BOOST_CHECK(testFunctorC.GetCount() == 0);
148
149 registry.at(CommandHandlerKey(packetB.GetPacketId(), version))->operator()(packetB);
150 BOOST_CHECK(testFunctorA.GetCount() == 1);
151 BOOST_CHECK(testFunctorB.GetCount() == 1);
152 BOOST_CHECK(testFunctorC.GetCount() == 0);
153
154 registry.at(CommandHandlerKey(packetC.GetPacketId(), version))->operator()(packetC);
155 BOOST_CHECK(testFunctorA.GetCount() == 1);
156 BOOST_CHECK(testFunctorB.GetCount() == 1);
157 BOOST_CHECK(testFunctorC.GetCount() == 1);
158}
159
Francis Murtagh94d79152019-08-16 17:45:07 +0100160BOOST_AUTO_TEST_CASE(CheckCommandHandlerRegistry)
161{
162 // Hard code the version as it will be the same during a single profiling session
163 uint32_t version = 1;
164
165 TestFunctorA testFunctorA(461, version);
166 TestFunctorB testFunctorB(963, version);
167 TestFunctorC testFunctorC(983, version);
168
169 // Create the Command Handler Registry
170 CommandHandlerRegistry registry;
171
172 // Register multiple different derived classes
173 registry.RegisterFunctor(&testFunctorA, testFunctorA.GetPacketId(), testFunctorA.GetVersion());
174 registry.RegisterFunctor(&testFunctorB, testFunctorB.GetPacketId(), testFunctorB.GetVersion());
175 registry.RegisterFunctor(&testFunctorC, testFunctorC.GetPacketId(), testFunctorC.GetVersion());
176
177 Packet packetA(500000000, 0, nullptr);
178 Packet packetB(600000000, 0, nullptr);
179 Packet packetC(400000000, 0, nullptr);
180
181 // Check the correct operator of derived class is called
182 registry.GetFunctor(packetA.GetPacketId(), version)->operator()(packetA);
183 BOOST_CHECK(testFunctorA.GetCount() == 1);
184 BOOST_CHECK(testFunctorB.GetCount() == 0);
185 BOOST_CHECK(testFunctorC.GetCount() == 0);
186
187 registry.GetFunctor(packetB.GetPacketId(), version)->operator()(packetB);
188 BOOST_CHECK(testFunctorA.GetCount() == 1);
189 BOOST_CHECK(testFunctorB.GetCount() == 1);
190 BOOST_CHECK(testFunctorC.GetCount() == 0);
191
192 registry.GetFunctor(packetC.GetPacketId(), version)->operator()(packetC);
193 BOOST_CHECK(testFunctorA.GetCount() == 1);
194 BOOST_CHECK(testFunctorB.GetCount() == 1);
195 BOOST_CHECK(testFunctorC.GetCount() == 1);
196
197 // Re-register an existing key with a new function
198 registry.RegisterFunctor(&testFunctorC, testFunctorA.GetPacketId(), version);
199 registry.GetFunctor(packetA.GetPacketId(), version)->operator()(packetC);
200 BOOST_CHECK(testFunctorA.GetCount() == 1);
201 BOOST_CHECK(testFunctorB.GetCount() == 1);
202 BOOST_CHECK(testFunctorC.GetCount() == 2);
203
204 // Check that non-existent key returns nullptr for its functor
205 BOOST_CHECK_THROW(registry.GetFunctor(0, 0), armnn::Exception);
206}
207
Francis Murtagh1f7db452019-08-14 09:49:34 +0100208BOOST_AUTO_TEST_SUITE_END()