2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "util/Index.h"
18 #include "util/ObjectManager.h"
20 #include <gtest/gtest.h>
22 using namespace onert;
25 using Index = typename util::Index<uint32_t, TestTag>;
27 TEST(ObjectManager, emplace)
29 util::ObjectManager<Index, int> man;
31 auto index = man.emplace(100);
32 ASSERT_EQ(man.at(index), 100);
35 TEST(ObjectManager, neg_remove_1)
37 util::ObjectManager<Index, int> man;
39 Index index = man.emplace(100);
40 ASSERT_TRUE(man.exist(index));
41 ASSERT_EQ(man.at(index), 100);
44 ASSERT_FALSE(man.exist(index));
47 TEST(ObjectManager, neg_remove_2)
49 util::ObjectManager<Index, int> man;
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);
59 ASSERT_FALSE(man.exist(index0));
60 ASSERT_TRUE(man.exist(index1));
61 ASSERT_EQ(man.at(index1), 200);
64 TEST(ObjectManager, push)
66 util::ObjectManager<Index, int> man;
69 auto index = man.push(std::make_unique<int>(100));
70 ASSERT_EQ(man.at(index), 100);
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);
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);
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);
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);
92 TEST(ObjectManager, neg_push)
94 util::ObjectManager<Index, int> man;
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);
101 // Specify the same index
102 auto index2 = man.push(std::make_unique<int>(200), Index{55});
103 ASSERT_FALSE(index2.valid());
106 static const uint32_t kMaxUInt32 = std::numeric_limits<uint32_t>::max();
108 TEST(ObjectManager, neg_push_undefined_index)
110 util::ObjectManager<Index, int> man;
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);
118 TEST(ObjectManager, neg_push_max_index)
120 util::ObjectManager<Index, int> man;
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);
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());
134 TEST(ObjectManager, neg_emplace_max_index)
136 util::ObjectManager<Index, int> man;
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);
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());
150 TEST(ObjectManager, const_iterate)
152 util::ObjectManager<Index, int> man;
154 auto index0 = man.emplace(100);
155 auto index1 = man.emplace(200);
156 auto index2 = man.emplace(300);
159 man.iterate([&](const Index &index, const int &val) { sum += val; });
163 TEST(ObjectManager, non_const_iterate)
165 util::ObjectManager<Index, int> man;
167 auto index0 = man.emplace(100);
168 auto index1 = man.emplace(200);
169 auto index2 = man.emplace(300);
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);
177 TEST(ObjectManager, set)
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);
187 TEST(ObjectManager, neg_set)
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
197 TEST(ObjectManager, getRawPtr)
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));
206 TEST(ObjectManager, neg_getRawPtr)
208 util::ObjectManager<Index, int> man;
209 auto ptr = man.getRawPtr(Index{1});
210 ASSERT_EQ(ptr, nullptr);