2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
6 #include "../CommandHandlerKey.hpp"
7 #include "../CommandHandlerFunctor.hpp"
8 #include "../CommandHandlerRegistry.hpp"
9 #include "../Packet.hpp"
13 #include <boost/test/unit_test.hpp>
16 BOOST_AUTO_TEST_SUITE(ExternalProfiling)
18 BOOST_AUTO_TEST_CASE(CheckCommandHandlerKeyComparisons)
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);
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);
36 BOOST_CHECK(!(testKey1==testKey5));
37 BOOST_CHECK(!(testKey1!=testKey1));
38 BOOST_CHECK(testKey1!=testKey5);
40 BOOST_CHECK(testKey1==testKey2 && testKey2==testKey1);
41 BOOST_CHECK(testKey0==testKey1 && testKey1==testKey2 && testKey0==testKey2);
43 BOOST_CHECK(testKey1.GetPacketId()==1);
44 BOOST_CHECK(testKey1.GetVersion()==1);
46 std::vector<CommandHandlerKey> vect =
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)
53 std::sort(vect.begin(), vect.end());
55 std::vector<CommandHandlerKey> expectedVect =
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)
62 BOOST_CHECK(vect == expectedVect);
65 BOOST_AUTO_TEST_CASE(CheckPacketClass)
67 const char* data = "test";
68 unsigned int length = static_cast<unsigned int>(std::strlen(data));
70 Packet packetTest1(472580096,length,data);
71 BOOST_CHECK_THROW(Packet packetTest2(472580096,0,""), armnn::Exception);
73 Packet packetTest3(472580096,0, nullptr);
75 BOOST_CHECK(packetTest1.GetLength() == length);
76 BOOST_CHECK(packetTest1.GetData() == data);
78 BOOST_CHECK(packetTest1.GetPacketFamily() == 7);
79 BOOST_CHECK(packetTest1.GetPacketId() == 43);
80 BOOST_CHECK(packetTest1.GetPacketType() == 3);
81 BOOST_CHECK(packetTest1.GetPacketClass() == 5);
84 // Create Derived Classes
85 class TestFunctorA : public CommandHandlerFunctor
88 using CommandHandlerFunctor::CommandHandlerFunctor;
90 int GetCount() { return m_Count; }
92 void operator()(const Packet& packet) override
101 class TestFunctorB : public TestFunctorA
103 using TestFunctorA::TestFunctorA;
106 class TestFunctorC : public TestFunctorA
108 using TestFunctorA::TestFunctorA;
111 BOOST_AUTO_TEST_CASE(CheckCommandHandlerFunctor)
113 // Hard code the version as it will be the same during a single profiling session
114 uint32_t version = 1;
116 TestFunctorA testFunctorA(461, version);
117 TestFunctorB testFunctorB(963, version);
118 TestFunctorC testFunctorC(983, version);
120 CommandHandlerKey keyA(testFunctorA.GetPacketId(), testFunctorA.GetVersion());
121 CommandHandlerKey keyB(testFunctorB.GetPacketId(), testFunctorB.GetVersion());
122 CommandHandlerKey keyC(testFunctorC.GetPacketId(), testFunctorC.GetVersion());
124 // Create the unwrapped map to simulate the Command Handler Registry
125 std::map<CommandHandlerKey, CommandHandlerFunctor*> registry;
127 registry.insert(std::make_pair(keyB, &testFunctorB));
128 registry.insert(std::make_pair(keyA, &testFunctorA));
129 registry.insert(std::make_pair(keyC, &testFunctorC));
131 // Check the order of the map is correct
132 auto it = registry.begin();
133 BOOST_CHECK(it->first==keyA);
135 BOOST_CHECK(it->first==keyB);
137 BOOST_CHECK(it->first==keyC);
139 Packet packetA(500000000, 0, nullptr);
140 Packet packetB(600000000, 0, nullptr);
141 Packet packetC(400000000, 0, nullptr);
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);
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);
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);
160 BOOST_AUTO_TEST_CASE(CheckCommandHandlerRegistry)
162 // Hard code the version as it will be the same during a single profiling session
163 uint32_t version = 1;
165 TestFunctorA testFunctorA(461, version);
166 TestFunctorB testFunctorB(963, version);
167 TestFunctorC testFunctorC(983, version);
169 // Create the Command Handler Registry
170 CommandHandlerRegistry registry;
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());
177 Packet packetA(500000000, 0, nullptr);
178 Packet packetB(600000000, 0, nullptr);
179 Packet packetC(400000000, 0, nullptr);
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);
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);
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);
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);
204 // Check that non-existent key returns nullptr for its functor
205 BOOST_CHECK_THROW(registry.GetFunctor(0, 0), armnn::Exception);
208 BOOST_AUTO_TEST_SUITE_END()