Imported Upstream version 1.21.0
[platform/core/ml/nnfw.git] / runtime / onert / core / src / util / ObjectManager.test.cc
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "util/Index.h"
18 #include "util/ObjectManager.h"
19
20 #include <gtest/gtest.h>
21
22 using namespace onert;
23
24 struct TestTag;
25 using Index = typename util::Index<uint32_t, TestTag>;
26
27 TEST(ObjectManager, emplace)
28 {
29   util::ObjectManager<Index, int> man;
30
31   auto index = man.emplace(100);
32   ASSERT_EQ(man.at(index), 100);
33 }
34
35 TEST(ObjectManager, neg_remove_1)
36 {
37   util::ObjectManager<Index, int> man;
38
39   Index index = man.emplace(100);
40   ASSERT_TRUE(man.exist(index));
41   ASSERT_EQ(man.at(index), 100);
42
43   man.remove(index);
44   ASSERT_FALSE(man.exist(index));
45 }
46
47 TEST(ObjectManager, neg_remove_2)
48 {
49   util::ObjectManager<Index, int> man;
50
51   auto index0 = man.emplace(100);
52   auto index1 = man.emplace(200);
53   ASSERT_TRUE(man.exist(index0));
54   ASSERT_EQ(man.at(index0), 100);
55   ASSERT_TRUE(man.exist(index1));
56   ASSERT_EQ(man.at(index1), 200);
57
58   man.remove(index0);
59   ASSERT_FALSE(man.exist(index0));
60   ASSERT_TRUE(man.exist(index1));
61   ASSERT_EQ(man.at(index1), 200);
62 }
63
64 TEST(ObjectManager, push)
65 {
66   util::ObjectManager<Index, int> man;
67
68   // Not specify index
69   auto index = man.push(std::make_unique<int>(100));
70   ASSERT_EQ(man.at(index), 100);
71
72   // Specify index
73   auto index2 = man.push(std::make_unique<int>(200), Index{33});
74   ASSERT_EQ(index2.value(), 33);
75   ASSERT_EQ(man.at(index2), 200);
76
77   auto index3 = man.push(std::make_unique<int>(300));
78   // NOTE auto-generated index number is always (biggest index in the ObjectManager + 1)
79   ASSERT_EQ(index3.value(), 34);
80   ASSERT_EQ(man.at(index3), 300);
81
82   auto index4 = man.push(std::make_unique<int>(400), Index{22});
83   ASSERT_EQ(index4.value(), 22);
84   ASSERT_EQ(man.at(index4), 400);
85
86   auto index5 = man.push(std::make_unique<int>(500));
87   // NOTE auto-generated index number is always (biggest index in the ObjectManager + 1)
88   ASSERT_EQ(index5.value(), 35);
89   ASSERT_EQ(man.at(index5), 500);
90 }
91
92 TEST(ObjectManager, neg_push)
93 {
94   util::ObjectManager<Index, int> man;
95
96   // Specify index
97   auto index = man.push(std::make_unique<int>(100), Index{55});
98   ASSERT_EQ(index.value(), 55);
99   ASSERT_EQ(man.at(index), 100);
100
101   // Specify the same index
102   auto index2 = man.push(std::make_unique<int>(200), Index{55});
103   ASSERT_FALSE(index2.valid());
104 }
105
106 static const uint32_t kMaxUInt32 = std::numeric_limits<uint32_t>::max();
107
108 TEST(ObjectManager, neg_push_undefined_index)
109 {
110   util::ObjectManager<Index, int> man;
111
112   // Try inserting invalid(undefined) index
113   auto index = man.push(std::make_unique<int>(100), Index{kMaxUInt32});
114   ASSERT_FALSE(index.valid());
115   ASSERT_EQ(man.size(), 0);
116 }
117
118 TEST(ObjectManager, neg_push_max_index)
119 {
120   util::ObjectManager<Index, int> man;
121
122   // Insert an object with maximum valid index
123   auto index = man.push(std::make_unique<int>(100), Index{kMaxUInt32 - 1});
124   ASSERT_EQ(index.value(), kMaxUInt32 - 1);
125   ASSERT_EQ(man.at(index), 100);
126   ASSERT_EQ(man.size(), 1);
127
128   // Reached to the final index so next push/emplace must fail
129   auto index2 = man.push(std::make_unique<int>(200));
130   ASSERT_EQ(man.size(), 1);
131   ASSERT_FALSE(index2.valid());
132 }
133
134 TEST(ObjectManager, neg_emplace_max_index)
135 {
136   util::ObjectManager<Index, int> man;
137
138   // Insert an object with maximum valid index
139   auto index = man.push(std::make_unique<int>(100), Index{kMaxUInt32 - 1});
140   ASSERT_EQ(index.value(), kMaxUInt32 - 1);
141   ASSERT_EQ(man.at(index), 100);
142   ASSERT_EQ(man.size(), 1);
143
144   // Reached to the final index so next push/emplace must fail
145   auto index3 = man.emplace(200);
146   ASSERT_EQ(man.size(), 1);
147   ASSERT_FALSE(index3.valid());
148 }
149
150 TEST(ObjectManager, const_iterate)
151 {
152   util::ObjectManager<Index, int> man;
153
154   auto index0 = man.emplace(100);
155   auto index1 = man.emplace(200);
156   auto index2 = man.emplace(300);
157
158   int sum = 0;
159   man.iterate([&](const Index &index, const int &val) { sum += val; });
160   ASSERT_EQ(sum, 600);
161 }
162
163 TEST(ObjectManager, non_const_iterate)
164 {
165   util::ObjectManager<Index, int> man;
166
167   auto index0 = man.emplace(100);
168   auto index1 = man.emplace(200);
169   auto index2 = man.emplace(300);
170
171   man.iterate([&](const Index &index, int &val) { val += 1; });
172   ASSERT_EQ(man.at(index0), 101);
173   ASSERT_EQ(man.at(index1), 201);
174   ASSERT_EQ(man.at(index2), 301);
175 }
176
177 TEST(ObjectManager, set)
178 {
179   util::ObjectManager<Index, int> man;
180   auto index = man.set(Index{1}, std::make_unique<int>(100)); // Insert
181   ASSERT_EQ(index, Index{1});
182   auto index2 = man.set(index, std::make_unique<int>(200)); // Overwrite
183   ASSERT_EQ(index2, index);
184   ASSERT_EQ(man.at(index2), 200);
185 }
186
187 TEST(ObjectManager, neg_set)
188 {
189   auto v = std::make_unique<int>(100);
190   util::ObjectManager<Index, int> man;
191   auto index = man.set(Index{}, std::move(v)); // Try set with an invalid index
192   ASSERT_EQ(index, Index{});
193   ASSERT_FALSE(index.valid());
194   ASSERT_NE(v, nullptr); // v must be kept when failure
195 }
196
197 TEST(ObjectManager, getRawPtr)
198 {
199   auto v = std::make_unique<int>(100);
200   auto v_ptr = v.get();
201   util::ObjectManager<Index, int> man;
202   auto index = man.push(std::move(v));
203   ASSERT_EQ(v_ptr, man.getRawPtr(index));
204 }
205
206 TEST(ObjectManager, neg_getRawPtr)
207 {
208   util::ObjectManager<Index, int> man;
209   auto ptr = man.getRawPtr(Index{1});
210   ASSERT_EQ(ptr, nullptr);
211 }