blob: c0dd986c8a0ef65d1e20b771e7938cbdf1ae7fa7 [file] [log] [blame]
Narumol Prangnawarat15effd82019-10-22 14:17:11 +01001//
2// Copyright © 2019 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
janeil01c4946c72019-11-07 09:32:28 +00006#include <armnn/Types.hpp>
Narumol Prangnawarat15effd82019-10-22 14:17:11 +01007
Narumol Prangnawaratdbdd1b42019-11-15 17:38:44 +00008#include "LabelsAndEventClasses.hpp"
9#include "ProfilingGuidGenerator.hpp"
10
11#include <set>
12
Narumol Prangnawarat15effd82019-10-22 14:17:11 +010013#include <boost/test/unit_test.hpp>
14
15using namespace armnn::profiling;
16
17BOOST_AUTO_TEST_SUITE(ProfilingGuidTests)
18
19BOOST_AUTO_TEST_CASE(GuidTest)
20{
21 ProfilingGuid guid0(0);
22 ProfilingGuid guid1(1);
23 ProfilingGuid guid2(1);
24
25 BOOST_TEST(guid0 != guid1);
26 BOOST_TEST(guid1 == guid2);
27 BOOST_TEST(guid0 < guid1);
28 BOOST_TEST(guid0 <= guid1);
29 BOOST_TEST(guid1 <= guid2);
30 BOOST_TEST(guid1 > guid0);
31 BOOST_TEST(guid1 >= guid0);
32 BOOST_TEST(guid1 >= guid2);
33}
34
35BOOST_AUTO_TEST_CASE(StaticGuidTest)
36{
37 ProfilingStaticGuid guid0(0);
38 ProfilingStaticGuid guid1(1);
39 ProfilingStaticGuid guid2(1);
40
41 BOOST_TEST(guid0 != guid1);
42 BOOST_TEST(guid1 == guid2);
43 BOOST_TEST(guid0 < guid1);
44 BOOST_TEST(guid0 <= guid1);
45 BOOST_TEST(guid1 <= guid2);
46 BOOST_TEST(guid1 > guid0);
47 BOOST_TEST(guid1 >= guid0);
48 BOOST_TEST(guid1 >= guid2);
49}
50
51BOOST_AUTO_TEST_CASE(DynamicGuidTest)
52{
53 ProfilingDynamicGuid guid0(0);
54 ProfilingDynamicGuid guid1(1);
55 ProfilingDynamicGuid guid2(1);
56
57 BOOST_TEST(guid0 != guid1);
58 BOOST_TEST(guid1 == guid2);
59 BOOST_TEST(guid0 < guid1);
60 BOOST_TEST(guid0 <= guid1);
61 BOOST_TEST(guid1 <= guid2);
62 BOOST_TEST(guid1 > guid0);
63 BOOST_TEST(guid1 >= guid0);
64 BOOST_TEST(guid1 >= guid2);
65}
66
Narumol Prangnawaratdbdd1b42019-11-15 17:38:44 +000067std::string GenerateRandomString()
68{
69 // Random a string lengh from 3 - 100
70 int minLength = 3;
71 int maxLength = 100;
72
73 // Random a character from lower case alphabets, upper case alphabets, numbers and special characters
74 int minAscii = 32; // space 32
75 int maxAscii = 126; // ~
76
77 int stringLen = rand() % (maxLength - minLength + 1) + minLength;
78 char str[stringLen + 1];
79 for (int i = 0; i < stringLen; ++i)
80 {
81 int randAscii = rand() % (maxAscii - minAscii + 1) + minAscii;
82 str[i] = char(randAscii);
83 }
84 str[stringLen] = '\0';
85 return std::string(str);
86}
87
88void CheckStaticGuid(uint64_t guid, uint64_t expectedGuid)
89{
90 BOOST_TEST(guid == expectedGuid);
91 BOOST_TEST(guid >= MIN_STATIC_GUID);
92}
93
94void CheckDynamicGuid(uint64_t guid, uint64_t expectedGuid)
95{
96 BOOST_TEST(guid == expectedGuid);
97 BOOST_TEST(guid < MIN_STATIC_GUID);
98}
99
100BOOST_AUTO_TEST_CASE(StaticGuidGeneratorCollisionTest)
101{
102 ProfilingGuidGenerator generator;
103 std::set<uint64_t> guids;
104 std::set<std::string> strs;
105 std::map<uint64_t,std::string> guidMap;
106 int collision = 0;
107 for (int i = 0; i < 1000000; ++i)
108 {
109 std::string str = GenerateRandomString();
110 if(strs.find(str) != strs.end())
111 {
112 continue;
113 }
114 strs.insert(str);
115 ProfilingStaticGuid guid = generator.GenerateStaticId(str.c_str());
116 if (guids.find(guid) != guids.end())
117 {
118 collision++;
119 }
120 guids.insert(guid);
121 }
122 BOOST_TEST(collision == 0);
123}
124
125BOOST_AUTO_TEST_CASE(StaticGuidGeneratorTest)
126{
127 ProfilingGuidGenerator generator;
128
129 ProfilingStaticGuid staticGuid0 = generator.GenerateStaticId("name");
130 CheckStaticGuid(staticGuid0, LabelsAndEventClasses::NAME_GUID);
131 BOOST_TEST(staticGuid0 != generator.GenerateStaticId("Name"));
132
133 ProfilingStaticGuid staticGuid1 = generator.GenerateStaticId("type");
134 CheckStaticGuid(staticGuid1, LabelsAndEventClasses::TYPE_GUID);
135 BOOST_TEST(staticGuid1 != generator.GenerateStaticId("Type"));
136
137 ProfilingStaticGuid staticGuid2 = generator.GenerateStaticId("index");
138 CheckStaticGuid(staticGuid2, LabelsAndEventClasses::INDEX_GUID);
139 BOOST_TEST(staticGuid2 != generator.GenerateStaticId("Index"));
140}
141
142BOOST_AUTO_TEST_CASE(DynamicGuidGeneratorTest)
143{
144 ProfilingGuidGenerator generator;
145
146 for (unsigned int i = 0; i < 100; ++i)
147 {
148 ProfilingDynamicGuid guid = generator.NextGuid();
149 CheckDynamicGuid(guid, i);
150 }
151}
152
Narumol Prangnawarat15effd82019-10-22 14:17:11 +0100153BOOST_AUTO_TEST_SUITE_END()