2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
6 #include "../CommandHandlerKey.hpp"
7 #include "../CommandHandlerFunctor.hpp"
8 #include "../Packet.hpp"
12 #include <boost/test/unit_test.hpp>
15 BOOST_AUTO_TEST_SUITE(ExternalProfiling)
17 BOOST_AUTO_TEST_CASE(CheckCommandHandlerKeyComparisons)
19 CommandHandlerKey testKey0(1, 1);
20 CommandHandlerKey testKey1(1, 1);
21 CommandHandlerKey testKey2(1, 1);
22 CommandHandlerKey testKey3(0, 0);
23 CommandHandlerKey testKey4(2, 2);
24 CommandHandlerKey testKey5(0, 2);
26 BOOST_CHECK(testKey1<testKey4);
27 BOOST_CHECK(testKey1>testKey3);
28 BOOST_CHECK(testKey1<=testKey4);
29 BOOST_CHECK(testKey1>=testKey3);
30 BOOST_CHECK(testKey1<=testKey2);
31 BOOST_CHECK(testKey1>=testKey2);
32 BOOST_CHECK(testKey1==testKey2);
33 BOOST_CHECK(testKey1==testKey1);
35 BOOST_CHECK(!(testKey1==testKey5));
36 BOOST_CHECK(!(testKey1!=testKey1));
37 BOOST_CHECK(testKey1!=testKey5);
39 BOOST_CHECK(testKey1==testKey2 && testKey2==testKey1);
40 BOOST_CHECK(testKey0==testKey1 && testKey1==testKey2 && testKey0==testKey2);
42 BOOST_CHECK(testKey1.GetPacketId()==1);
43 BOOST_CHECK(testKey1.GetVersion()==1);
45 std::vector<CommandHandlerKey> vect =
47 CommandHandlerKey(0,1), CommandHandlerKey(2,0), CommandHandlerKey(1,0),
48 CommandHandlerKey(2,1), CommandHandlerKey(1,1), CommandHandlerKey(0,1),
49 CommandHandlerKey(2,0), CommandHandlerKey(0,0)
52 std::sort(vect.begin(), vect.end());
54 std::vector<CommandHandlerKey> expectedVect =
56 CommandHandlerKey(0,0), CommandHandlerKey(0,1), CommandHandlerKey(0,1),
57 CommandHandlerKey(1,0), CommandHandlerKey(1,1), CommandHandlerKey(2,0),
58 CommandHandlerKey(2,0), CommandHandlerKey(2,1)
61 BOOST_CHECK(vect == expectedVect);
64 BOOST_AUTO_TEST_CASE(CheckPacketClass)
66 const char* data = "test";
67 unsigned int length = static_cast<unsigned int>(std::strlen(data));
69 Packet packetTest1(472580096,length,data);
70 BOOST_CHECK_THROW(Packet packetTest2(472580096,0,""), armnn::Exception);
72 Packet packetTest3(472580096,0, nullptr);
74 BOOST_CHECK(packetTest1.GetLength() == length);
75 BOOST_CHECK(packetTest1.GetData() == data);
77 BOOST_CHECK(packetTest1.GetPacketFamily() == 7);
78 BOOST_CHECK(packetTest1.GetPacketId() == 43);
79 BOOST_CHECK(packetTest1.GetPacketType() == 3);
80 BOOST_CHECK(packetTest1.GetPacketClass() == 5);
83 BOOST_AUTO_TEST_CASE(CheckCommandHandlerFunctor)
85 // Create Derived Classes
86 class TestFunctorA : public CommandHandlerFunctor
89 using CommandHandlerFunctor::CommandHandlerFunctor;
91 int GetCount() { return m_Count; }
93 void operator()(const Packet& packet) override
102 class TestFunctorB : public TestFunctorA
104 using TestFunctorA::TestFunctorA;
107 class TestFunctorC : public TestFunctorA
109 using TestFunctorA::TestFunctorA;
112 // Hard code the version as it will be the same during a single profiling session
113 uint32_t version = 1;
115 TestFunctorA testFunctorA(461, version);
116 TestFunctorB testFunctorB(963, version);
117 TestFunctorC testFunctorC(983, version);
119 CommandHandlerKey keyA(testFunctorA.GetPacketId(), testFunctorA.GetVersion());
120 CommandHandlerKey keyB(testFunctorB.GetPacketId(), testFunctorB.GetVersion());
121 CommandHandlerKey keyC(testFunctorC.GetPacketId(), testFunctorC.GetVersion());
123 // Create the unwrapped map to simulate the Command Handler Registry
124 std::map<CommandHandlerKey, CommandHandlerFunctor*> registry;
126 registry.insert(std::make_pair(keyB, &testFunctorB));
127 registry.insert(std::make_pair(keyA, &testFunctorA));
128 registry.insert(std::make_pair(keyC, &testFunctorC));
130 // Check the order of the map is correct
131 auto it = registry.begin();
132 BOOST_CHECK(it->first==keyA);
134 BOOST_CHECK(it->first==keyB);
136 BOOST_CHECK(it->first==keyC);
138 Packet packetA(500000000, 0, nullptr);
139 Packet packetB(600000000, 0, nullptr);
140 Packet packetC(400000000, 0, nullptr);
142 // Check the correct operator of derived class is called
143 registry.at(CommandHandlerKey(packetA.GetPacketId(), version))->operator()(packetA);
144 BOOST_CHECK(testFunctorA.GetCount() == 1);
145 BOOST_CHECK(testFunctorB.GetCount() == 0);
146 BOOST_CHECK(testFunctorC.GetCount() == 0);
148 registry.at(CommandHandlerKey(packetB.GetPacketId(), version))->operator()(packetB);
149 BOOST_CHECK(testFunctorA.GetCount() == 1);
150 BOOST_CHECK(testFunctorB.GetCount() == 1);
151 BOOST_CHECK(testFunctorC.GetCount() == 0);
153 registry.at(CommandHandlerKey(packetC.GetPacketId(), version))->operator()(packetC);
154 BOOST_CHECK(testFunctorA.GetCount() == 1);
155 BOOST_CHECK(testFunctorB.GetCount() == 1);
156 BOOST_CHECK(testFunctorC.GetCount() == 1);
159 BOOST_AUTO_TEST_SUITE_END()