Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(611)

Side by Side Diff: mojo/public/cpp/bindings/tests/map_unittest.cc

Issue 814543006: Move //mojo/{public, edk} underneath //third_party (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "mojo/public/cpp/bindings/array.h"
6 #include "mojo/public/cpp/bindings/lib/array_serialization.h"
7 #include "mojo/public/cpp/bindings/lib/bindings_internal.h"
8 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h"
9 #include "mojo/public/cpp/bindings/lib/validate_params.h"
10 #include "mojo/public/cpp/bindings/map.h"
11 #include "mojo/public/cpp/bindings/string.h"
12 #include "mojo/public/cpp/bindings/tests/container_test_util.h"
13 #include "mojo/public/cpp/environment/environment.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace mojo {
17 namespace test {
18
19 namespace {
20
21 using mojo::internal::Array_Data;
22 using mojo::internal::ArrayValidateParams;
23 using mojo::internal::FixedBuffer;
24 using mojo::internal::Map_Data;
25 using mojo::internal::NoValidateParams;
26 using mojo::internal::String_Data;
27
28 struct StringIntData {
29 const char* string_data;
30 int int_data;
31 } kStringIntData[] = {
32 {"one", 1},
33 {"two", 2},
34 {"three", 3},
35 {"four", 4},
36 };
37
38 const size_t kStringIntDataSize = 4;
39
40 class MapTest : public testing::Test {
41 public:
42 ~MapTest() override {}
43
44 private:
45 Environment env_;
46 };
47
48 // Tests that basic Map operations work.
49 TEST_F(MapTest, InsertWorks) {
50 Map<String, int> map;
51 for (size_t i = 0; i < kStringIntDataSize; ++i)
52 map.insert(kStringIntData[i].string_data, kStringIntData[i].int_data);
53
54 for (size_t i = 0; i < kStringIntDataSize; ++i) {
55 EXPECT_EQ(kStringIntData[i].int_data,
56 map.at(kStringIntData[i].string_data));
57 }
58 }
59
60 TEST_F(MapTest, TestIndexOperator) {
61 Map<String, int> map;
62 for (size_t i = 0; i < kStringIntDataSize; ++i)
63 map[kStringIntData[i].string_data] = kStringIntData[i].int_data;
64
65 for (size_t i = 0; i < kStringIntDataSize; ++i) {
66 EXPECT_EQ(kStringIntData[i].int_data,
67 map.at(kStringIntData[i].string_data));
68 }
69 }
70
71 TEST_F(MapTest, TestIndexOperatorAsRValue) {
72 Map<String, int> map;
73 for (size_t i = 0; i < kStringIntDataSize; ++i)
74 map.insert(kStringIntData[i].string_data, kStringIntData[i].int_data);
75
76 for (size_t i = 0; i < kStringIntDataSize; ++i) {
77 EXPECT_EQ(kStringIntData[i].int_data, map[kStringIntData[i].string_data]);
78 }
79 }
80
81 TEST_F(MapTest, TestIndexOperatorMoveOnly) {
82 ASSERT_EQ(0u, MoveOnlyType::num_instances());
83 mojo::Map<mojo::String, mojo::Array<int32_t>> map;
84 std::vector<MoveOnlyType*> value_ptrs;
85
86 for (size_t i = 0; i < kStringIntDataSize; ++i) {
87 const char* key = kStringIntData[i].string_data;
88 Array<int32_t> array(1);
89 array[0] = kStringIntData[i].int_data;
90 map[key] = array.Pass();
91 EXPECT_TRUE(map);
92 }
93
94 // We now read back that data, to test the behavior of operator[].
95 for (size_t i = 0; i < kStringIntDataSize; ++i) {
96 auto it = map.find(kStringIntData[i].string_data);
97 ASSERT_TRUE(it != map.end());
98 ASSERT_EQ(1u, it.GetValue().size());
99 EXPECT_EQ(kStringIntData[i].int_data, it.GetValue()[0]);
100 }
101 }
102
103 TEST_F(MapTest, ConstructedFromArray) {
104 Array<String> keys(kStringIntDataSize);
105 Array<int> values(kStringIntDataSize);
106 for (size_t i = 0; i < kStringIntDataSize; ++i) {
107 keys[i] = kStringIntData[i].string_data;
108 values[i] = kStringIntData[i].int_data;
109 }
110
111 Map<String, int> map(keys.Pass(), values.Pass());
112
113 for (size_t i = 0; i < kStringIntDataSize; ++i) {
114 EXPECT_EQ(kStringIntData[i].int_data,
115 map.at(mojo::String(kStringIntData[i].string_data)));
116 }
117 }
118
119 TEST_F(MapTest, DecomposeMapTo) {
120 Array<String> keys(kStringIntDataSize);
121 Array<int> values(kStringIntDataSize);
122 for (size_t i = 0; i < kStringIntDataSize; ++i) {
123 keys[i] = kStringIntData[i].string_data;
124 values[i] = kStringIntData[i].int_data;
125 }
126
127 Map<String, int> map(keys.Pass(), values.Pass());
128 EXPECT_EQ(kStringIntDataSize, map.size());
129
130 Array<String> keys2;
131 Array<int> values2;
132 map.DecomposeMapTo(&keys2, &values2);
133 EXPECT_EQ(0u, map.size());
134
135 EXPECT_EQ(kStringIntDataSize, keys2.size());
136 EXPECT_EQ(kStringIntDataSize, values2.size());
137
138 for (size_t i = 0; i < kStringIntDataSize; ++i) {
139 // We are not guaranteed that the copies have the same sorting as the
140 // originals.
141 String key = kStringIntData[i].string_data;
142 int value = kStringIntData[i].int_data;
143
144 bool found = false;
145 for (size_t j = 0; j < keys2.size(); ++j) {
146 if (keys2[j] == key) {
147 EXPECT_EQ(value, values2[j]);
148 found = true;
149 break;
150 }
151 }
152
153 EXPECT_TRUE(found);
154 }
155 }
156
157 TEST_F(MapTest, Insert_Copyable) {
158 ASSERT_EQ(0u, CopyableType::num_instances());
159 mojo::Map<mojo::String, CopyableType> map;
160 std::vector<CopyableType*> value_ptrs;
161
162 for (size_t i = 0; i < kStringIntDataSize; ++i) {
163 const char* key = kStringIntData[i].string_data;
164 CopyableType value;
165 value_ptrs.push_back(value.ptr());
166 map.insert(key, value);
167 ASSERT_EQ(i + 1, map.size());
168 ASSERT_EQ(i + 1, value_ptrs.size());
169 EXPECT_EQ(map.size() + 1, CopyableType::num_instances());
170 EXPECT_TRUE(map.at(key).copied());
171 EXPECT_EQ(value_ptrs[i], map.at(key).ptr());
172 map.at(key).ResetCopied();
173 EXPECT_TRUE(map);
174 }
175
176 // std::map doesn't have a capacity() method like std::vector so this test is
177 // a lot more boring.
178
179 map.reset();
180 EXPECT_EQ(0u, CopyableType::num_instances());
181 }
182
183 TEST_F(MapTest, Insert_MoveOnly) {
184 ASSERT_EQ(0u, MoveOnlyType::num_instances());
185 mojo::Map<mojo::String, MoveOnlyType> map;
186 std::vector<MoveOnlyType*> value_ptrs;
187
188 for (size_t i = 0; i < kStringIntDataSize; ++i) {
189 const char* key = kStringIntData[i].string_data;
190 MoveOnlyType value;
191 value_ptrs.push_back(value.ptr());
192 map.insert(key, value.Pass());
193 ASSERT_EQ(i + 1, map.size());
194 ASSERT_EQ(i + 1, value_ptrs.size());
195 EXPECT_EQ(map.size() + 1, MoveOnlyType::num_instances());
196 EXPECT_TRUE(map.at(key).moved());
197 EXPECT_EQ(value_ptrs[i], map.at(key).ptr());
198 map.at(key).ResetMoved();
199 EXPECT_TRUE(map);
200 }
201
202 // std::map doesn't have a capacity() method like std::vector so this test is
203 // a lot more boring.
204
205 map.reset();
206 EXPECT_EQ(0u, MoveOnlyType::num_instances());
207 }
208
209 TEST_F(MapTest, IndexOperator_MoveOnly) {
210 ASSERT_EQ(0u, MoveOnlyType::num_instances());
211 mojo::Map<mojo::String, MoveOnlyType> map;
212 std::vector<MoveOnlyType*> value_ptrs;
213
214 for (size_t i = 0; i < kStringIntDataSize; ++i) {
215 const char* key = kStringIntData[i].string_data;
216 MoveOnlyType value;
217 value_ptrs.push_back(value.ptr());
218 map[key] = value.Pass();
219 ASSERT_EQ(i + 1, map.size());
220 ASSERT_EQ(i + 1, value_ptrs.size());
221 EXPECT_EQ(map.size() + 1, MoveOnlyType::num_instances());
222 EXPECT_TRUE(map.at(key).moved());
223 EXPECT_EQ(value_ptrs[i], map.at(key).ptr());
224 map.at(key).ResetMoved();
225 EXPECT_TRUE(map);
226 }
227
228 // std::map doesn't have a capacity() method like std::vector so this test is
229 // a lot more boring.
230
231 map.reset();
232 EXPECT_EQ(0u, MoveOnlyType::num_instances());
233 }
234
235 TEST_F(MapTest, STLToMojo) {
236 std::map<std::string, int> stl_data;
237 for (size_t i = 0; i < kStringIntDataSize; ++i)
238 stl_data[kStringIntData[i].string_data] = kStringIntData[i].int_data;
239
240 Map<String, int32_t> mojo_data = Map<String, int32_t>::From(stl_data);
241
242 for (size_t i = 0; i < kStringIntDataSize; ++i) {
243 EXPECT_EQ(kStringIntData[i].int_data,
244 mojo_data.at(kStringIntData[i].string_data));
245 }
246 }
247
248 TEST_F(MapTest, MojoToSTL) {
249 Map<String, int32_t> mojo_map;
250 for (size_t i = 0; i < kStringIntDataSize; ++i)
251 mojo_map.insert(kStringIntData[i].string_data, kStringIntData[i].int_data);
252
253 std::map<std::string, int> stl_map =
254 mojo_map.To<std::map<std::string, int>>();
255 for (size_t i = 0; i < kStringIntDataSize; ++i) {
256 auto it = stl_map.find(kStringIntData[i].string_data);
257 ASSERT_TRUE(it != stl_map.end());
258 EXPECT_EQ(kStringIntData[i].int_data, it->second);
259 }
260 }
261
262 TEST_F(MapTest, MapArrayClone) {
263 Map<String, Array<String>> m;
264 for (size_t i = 0; i < kStringIntDataSize; ++i) {
265 Array<String> s;
266 s.push_back(kStringIntData[i].string_data);
267 m.insert(kStringIntData[i].string_data, s.Pass());
268 }
269
270 Map<String, Array<String>> m2 = m.Clone();
271
272 for (auto it = m2.begin(); it != m2.end(); ++it) {
273 ASSERT_EQ(1u, it.GetValue().size());
274 EXPECT_EQ(it.GetKey(), it.GetValue().at(0));
275 }
276 }
277
278 TEST_F(MapTest, ArrayOfMap) {
279 {
280 Array<Map<int32_t, int8_t>> array(1);
281 array[0].insert(1, 42);
282
283 size_t size = GetSerializedSize_(array);
284 FixedBuffer buf(size);
285 Array_Data<Map_Data<int32_t, int8_t>*>* data;
286 SerializeArray_<ArrayValidateParams<
287 0, false, ArrayValidateParams<0, false, NoValidateParams>>>(
288 array.Pass(), &buf, &data);
289
290 Array<Map<int32_t, int8_t>> deserialized_array;
291 Deserialize_(data, &deserialized_array);
292
293 ASSERT_EQ(1u, deserialized_array.size());
294 ASSERT_EQ(1u, deserialized_array[0].size());
295 ASSERT_EQ(42, deserialized_array[0].at(1));
296 }
297
298 {
299 Array<Map<String, Array<bool>>> array(1);
300 Array<bool> map_value(2);
301 map_value[0] = false;
302 map_value[1] = true;
303 array[0].insert("hello world", map_value.Pass());
304
305 size_t size = GetSerializedSize_(array);
306 FixedBuffer buf(size);
307 Array_Data<Map_Data<String_Data*, Array_Data<bool>*>*>* data;
308 SerializeArray_<ArrayValidateParams<
309 0, false,
310 ArrayValidateParams<0, false,
311 ArrayValidateParams<0, false, NoValidateParams>>>>(
312 array.Pass(), &buf, &data);
313
314 Array<Map<String, Array<bool>>> deserialized_array;
315 Deserialize_(data, &deserialized_array);
316
317 ASSERT_EQ(1u, deserialized_array.size());
318 ASSERT_EQ(1u, deserialized_array[0].size());
319 ASSERT_FALSE(deserialized_array[0].at("hello world")[0]);
320 ASSERT_TRUE(deserialized_array[0].at("hello world")[1]);
321 }
322 }
323
324 } // namespace
325 } // namespace test
326 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/tests/interface_ptr_unittest.cc ('k') | mojo/public/cpp/bindings/tests/request_response_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698