IVGCVSW-3425 Create the Command Handler Functor base class
[platform/upstream/armnn.git] / src / profiling / test / ProfilingTests.cpp
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5
6 #include "../CommandHandlerKey.hpp"
7 #include "../CommandHandlerFunctor.hpp"
8 #include "../Packet.hpp"
9
10 #include <cstdint>
11 #include <cstring>
12 #include <boost/test/unit_test.hpp>
13 #include <map>
14
15 BOOST_AUTO_TEST_SUITE(ExternalProfiling)
16
17 BOOST_AUTO_TEST_CASE(CheckCommandHandlerKeyComparisons)
18 {
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);
25
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);
34
35     BOOST_CHECK(!(testKey1==testKey5));
36     BOOST_CHECK(!(testKey1!=testKey1));
37     BOOST_CHECK(testKey1!=testKey5);
38
39     BOOST_CHECK(testKey1==testKey2 && testKey2==testKey1);
40     BOOST_CHECK(testKey0==testKey1 && testKey1==testKey2 && testKey0==testKey2);
41
42     BOOST_CHECK(testKey1.GetPacketId()==1);
43     BOOST_CHECK(testKey1.GetVersion()==1);
44
45     std::vector<CommandHandlerKey> vect =
46         {
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)
50         };
51
52     std::sort(vect.begin(), vect.end());
53
54     std::vector<CommandHandlerKey> expectedVect =
55         {
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)
59         };
60
61     BOOST_CHECK(vect == expectedVect);
62 }
63
64 BOOST_AUTO_TEST_CASE(CheckPacketClass)
65 {
66     const char* data = "test";
67     unsigned int length = static_cast<unsigned int>(std::strlen(data));
68
69     Packet packetTest1(472580096,length,data);
70     BOOST_CHECK_THROW(Packet packetTest2(472580096,0,""), armnn::Exception);
71
72     Packet packetTest3(472580096,0, nullptr);
73
74     BOOST_CHECK(packetTest1.GetLength() == length);
75     BOOST_CHECK(packetTest1.GetData() == data);
76
77     BOOST_CHECK(packetTest1.GetPacketFamily() == 7);
78     BOOST_CHECK(packetTest1.GetPacketId() == 43);
79     BOOST_CHECK(packetTest1.GetPacketType() == 3);
80     BOOST_CHECK(packetTest1.GetPacketClass() == 5);
81 }
82
83 BOOST_AUTO_TEST_CASE(CheckCommandHandlerFunctor)
84 {
85     // Create Derived Classes
86     class TestFunctorA : public CommandHandlerFunctor
87     {
88     public:
89         using CommandHandlerFunctor::CommandHandlerFunctor;
90
91         int GetCount() { return m_Count; }
92
93         void operator()(const Packet& packet) override
94         {
95             m_Count++;
96         }
97
98     private:
99         int m_Count = 0;
100     };
101
102     class TestFunctorB : public TestFunctorA
103     {
104         using TestFunctorA::TestFunctorA;
105     };
106
107     class TestFunctorC : public TestFunctorA
108     {
109         using TestFunctorA::TestFunctorA;
110     };
111
112     // Hard code the version as it will be the same during a single profiling session
113     uint32_t version = 1;
114
115     TestFunctorA testFunctorA(461, version);
116     TestFunctorB testFunctorB(963, version);
117     TestFunctorC testFunctorC(983, version);
118
119     CommandHandlerKey keyA(testFunctorA.GetPacketId(), testFunctorA.GetVersion());
120     CommandHandlerKey keyB(testFunctorB.GetPacketId(), testFunctorB.GetVersion());
121     CommandHandlerKey keyC(testFunctorC.GetPacketId(), testFunctorC.GetVersion());
122
123     // Create the unwrapped map to simulate the Command Handler Registry
124     std::map<CommandHandlerKey, CommandHandlerFunctor*> registry;
125
126     registry.insert(std::make_pair(keyB, &testFunctorB));
127     registry.insert(std::make_pair(keyA, &testFunctorA));
128     registry.insert(std::make_pair(keyC, &testFunctorC));
129
130     // Check the order of the map is correct
131     auto it = registry.begin();
132     BOOST_CHECK(it->first==keyA);
133     it++;
134     BOOST_CHECK(it->first==keyB);
135     it++;
136     BOOST_CHECK(it->first==keyC);
137
138     Packet packetA(500000000, 0, nullptr);
139     Packet packetB(600000000, 0, nullptr);
140     Packet packetC(400000000, 0, nullptr);
141
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);
147
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);
152
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);
157 }
158
159 BOOST_AUTO_TEST_SUITE_END()