OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mojo/public/cpp/bindings/array.h" | 5 #include "mojo/public/cpp/bindings/array.h" |
6 #include "mojo/public/cpp/bindings/lib/array_serialization.h" | 6 #include "mojo/public/cpp/bindings/lib/array_serialization.h" |
7 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" | 7 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" |
8 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" | 8 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" |
9 #include "mojo/public/cpp/bindings/map.h" | 9 #include "mojo/public/cpp/bindings/map.h" |
10 #include "mojo/public/cpp/bindings/string.h" | 10 #include "mojo/public/cpp/bindings/string.h" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 Map<String, int> map; | 43 Map<String, int> map; |
44 for (size_t i = 0; i < kStringIntDataSize; ++i) | 44 for (size_t i = 0; i < kStringIntDataSize; ++i) |
45 map.insert(kStringIntData[i].string_data, kStringIntData[i].int_data); | 45 map.insert(kStringIntData[i].string_data, kStringIntData[i].int_data); |
46 | 46 |
47 for (size_t i = 0; i < kStringIntDataSize; ++i) { | 47 for (size_t i = 0; i < kStringIntDataSize; ++i) { |
48 EXPECT_EQ(kStringIntData[i].int_data, | 48 EXPECT_EQ(kStringIntData[i].int_data, |
49 map.at(kStringIntData[i].string_data)); | 49 map.at(kStringIntData[i].string_data)); |
50 } | 50 } |
51 } | 51 } |
52 | 52 |
| 53 TEST_F(MapTest, TestIndexOperator) { |
| 54 Map<String, int> map; |
| 55 for (size_t i = 0; i < kStringIntDataSize; ++i) |
| 56 map[kStringIntData[i].string_data] = kStringIntData[i].int_data; |
| 57 |
| 58 for (size_t i = 0; i < kStringIntDataSize; ++i) { |
| 59 EXPECT_EQ(kStringIntData[i].int_data, |
| 60 map.at(kStringIntData[i].string_data)); |
| 61 } |
| 62 } |
| 63 |
| 64 TEST_F(MapTest, TestIndexOperatorAsRValue) { |
| 65 Map<String, int> map; |
| 66 for (size_t i = 0; i < kStringIntDataSize; ++i) |
| 67 map.insert(kStringIntData[i].string_data, kStringIntData[i].int_data); |
| 68 |
| 69 for (size_t i = 0; i < kStringIntDataSize; ++i) { |
| 70 EXPECT_EQ(kStringIntData[i].int_data, map[kStringIntData[i].string_data]); |
| 71 } |
| 72 } |
| 73 |
| 74 TEST_F(MapTest, TestIndexOperatorMoveOnly) { |
| 75 ASSERT_EQ(0u, MoveOnlyType::num_instances()); |
| 76 mojo::Map<mojo::String, mojo::Array<int32_t>> map; |
| 77 std::vector<MoveOnlyType*> value_ptrs; |
| 78 |
| 79 for (size_t i = 0; i < kStringIntDataSize; ++i) { |
| 80 const char* key = kStringIntData[i].string_data; |
| 81 Array<int32_t> array(1); |
| 82 array[0] = kStringIntData[i].int_data; |
| 83 map[key] = array.Pass(); |
| 84 EXPECT_TRUE(map); |
| 85 } |
| 86 |
| 87 // We now read back that data, to test the behavior of operator[]. |
| 88 for (size_t i = 0; i < kStringIntDataSize; ++i) { |
| 89 auto it = map.find(kStringIntData[i].string_data); |
| 90 ASSERT_TRUE(it != map.end()); |
| 91 ASSERT_EQ(1u, it.GetValue().size()); |
| 92 EXPECT_EQ(kStringIntData[i].int_data, it.GetValue()[0]); |
| 93 } |
| 94 } |
| 95 |
53 TEST_F(MapTest, ConstructedFromArray) { | 96 TEST_F(MapTest, ConstructedFromArray) { |
54 Array<String> keys(kStringIntDataSize); | 97 Array<String> keys(kStringIntDataSize); |
55 Array<int> values(kStringIntDataSize); | 98 Array<int> values(kStringIntDataSize); |
56 for (size_t i = 0; i < kStringIntDataSize; ++i) { | 99 for (size_t i = 0; i < kStringIntDataSize; ++i) { |
57 keys[i] = kStringIntData[i].string_data; | 100 keys[i] = kStringIntData[i].string_data; |
58 values[i] = kStringIntData[i].int_data; | 101 values[i] = kStringIntData[i].int_data; |
59 } | 102 } |
60 | 103 |
61 Map<String, int> map(keys.Pass(), values.Pass()); | 104 Map<String, int> map(keys.Pass(), values.Pass()); |
62 | 105 |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 EXPECT_TRUE(map.at(key).moved()); | 189 EXPECT_TRUE(map.at(key).moved()); |
147 EXPECT_EQ(value_ptrs[i], map.at(key).ptr()); | 190 EXPECT_EQ(value_ptrs[i], map.at(key).ptr()); |
148 map.at(key).ResetMoved(); | 191 map.at(key).ResetMoved(); |
149 EXPECT_TRUE(map); | 192 EXPECT_TRUE(map); |
150 } | 193 } |
151 | 194 |
152 // std::map doesn't have a capacity() method like std::vector so this test is | 195 // std::map doesn't have a capacity() method like std::vector so this test is |
153 // a lot more boring. | 196 // a lot more boring. |
154 | 197 |
155 map.reset(); | 198 map.reset(); |
156 EXPECT_EQ(0u, CopyableType::num_instances()); | 199 EXPECT_EQ(0u, MoveOnlyType::num_instances()); |
| 200 } |
| 201 |
| 202 TEST_F(MapTest, IndexOperator_MoveOnly) { |
| 203 ASSERT_EQ(0u, MoveOnlyType::num_instances()); |
| 204 mojo::Map<mojo::String, MoveOnlyType> map; |
| 205 std::vector<MoveOnlyType*> value_ptrs; |
| 206 |
| 207 for (size_t i = 0; i < kStringIntDataSize; ++i) { |
| 208 const char* key = kStringIntData[i].string_data; |
| 209 MoveOnlyType value; |
| 210 value_ptrs.push_back(value.ptr()); |
| 211 map[key] = value.Pass(); |
| 212 ASSERT_EQ(i + 1, map.size()); |
| 213 ASSERT_EQ(i + 1, value_ptrs.size()); |
| 214 EXPECT_EQ(map.size() + 1, MoveOnlyType::num_instances()); |
| 215 EXPECT_TRUE(map.at(key).moved()); |
| 216 EXPECT_EQ(value_ptrs[i], map.at(key).ptr()); |
| 217 map.at(key).ResetMoved(); |
| 218 EXPECT_TRUE(map); |
| 219 } |
| 220 |
| 221 // std::map doesn't have a capacity() method like std::vector so this test is |
| 222 // a lot more boring. |
| 223 |
| 224 map.reset(); |
| 225 EXPECT_EQ(0u, MoveOnlyType::num_instances()); |
157 } | 226 } |
158 | 227 |
159 TEST_F(MapTest, STLToMojo) { | 228 TEST_F(MapTest, STLToMojo) { |
160 std::map<std::string, int> stl_data; | 229 std::map<std::string, int> stl_data; |
161 for (size_t i = 0; i < kStringIntDataSize; ++i) | 230 for (size_t i = 0; i < kStringIntDataSize; ++i) |
162 stl_data[kStringIntData[i].string_data] = kStringIntData[i].int_data; | 231 stl_data[kStringIntData[i].string_data] = kStringIntData[i].int_data; |
163 | 232 |
164 Map<String, int32_t> mojo_data = Map<String, int32_t>::From(stl_data); | 233 Map<String, int32_t> mojo_data = Map<String, int32_t>::From(stl_data); |
165 | 234 |
166 for (size_t i = 0; i < kStringIntDataSize; ++i) { | 235 for (size_t i = 0; i < kStringIntDataSize; ++i) { |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
329 | 398 |
330 ASSERT_EQ(1u, to_receive->second.size()); | 399 ASSERT_EQ(1u, to_receive->second.size()); |
331 ASSERT_EQ(1u, to_receive->second[0].size()); | 400 ASSERT_EQ(1u, to_receive->second[0].size()); |
332 ASSERT_FALSE(to_receive->second[0].at("hello world")[0]); | 401 ASSERT_FALSE(to_receive->second[0].at("hello world")[0]); |
333 ASSERT_TRUE(to_receive->second[0].at("hello world")[1]); | 402 ASSERT_TRUE(to_receive->second[0].at("hello world")[1]); |
334 } | 403 } |
335 | 404 |
336 } // namespace | 405 } // namespace |
337 } // namespace test | 406 } // namespace test |
338 } // namespace mojo | 407 } // namespace mojo |
OLD | NEW |