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

Side by Side Diff: base/containers/small_map_unittest.cc

Issue 1738883004: Support for move-only types in SmallMap. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "base/containers/small_map.h" 5 #include "base/containers/small_map.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <functional> 10 #include <functional>
(...skipping 461 matching lines...) Expand 10 before | Expand all | Expand 10 after
472 472
473 EXPECT_EQ(4u, m.size()); 473 EXPECT_EQ(4u, m.size());
474 EXPECT_EQ(0u, m.count(-1)); 474 EXPECT_EQ(0u, m.count(-1));
475 475
476 m[5] = 5; 476 m[5] = 5;
477 EXPECT_EQ(6u, m.size()); 477 EXPECT_EQ(6u, m.size());
478 // Our functor adds an extra item when we convert to a map. 478 // Our functor adds an extra item when we convert to a map.
479 EXPECT_EQ(1u, m.count(-1)); 479 EXPECT_EQ(1u, m.count(-1));
480 } 480 }
481 481
482 // This class acts as a basic implementation of a move-only type. The canonical
483 // example of such a type is scoped_ptr/unique_ptr.
484 class MoveOnlyType {
485 public:
486 MoveOnlyType() : value_(0) {}
487 explicit MoveOnlyType(int value) : value_(value) {}
488
489 MoveOnlyType(MoveOnlyType&& other) {
490 *this = std::move(other);
491 }
492
493 MoveOnlyType& operator=(MoveOnlyType&& other) {
494 value_ = other.value_;
495 other.value_ = 0;
496 return *this;
497 }
498
499 MoveOnlyType(const MoveOnlyType&) = delete;
500 MoveOnlyType& operator=(const MoveOnlyType&) = delete;
501
502 int value() const { return value_; }
503
504 private:
505 int value_;
506 };
507
508 TEST(SmallMap, MoveOnlyValueType) {
509 SmallMap<std::map<int, MoveOnlyType>, 2> m;
510
511 m[0] = MoveOnlyType(1);
512 m[1] = MoveOnlyType(2);
513 m.erase(m.begin());
514
515 // SmallMap will move m[1] to an earlier index in the internal array.
516 EXPECT_EQ(m.size(), 1u);
517 EXPECT_EQ(m[1].value(), 2);
518
519 m[0] = MoveOnlyType(1);
520 // SmallMap must move the values from the array into the internal std::map.
521 m[2] = MoveOnlyType(3);
522
523 EXPECT_EQ(m.size(), 3u);
524 EXPECT_EQ(m[0].value(), 1);
525 EXPECT_EQ(m[1].value(), 2);
526 EXPECT_EQ(m[2].value(), 3);
527
528 m.erase(m.begin());
529
530 // SmallMap should also let internal std::map erase with a move-only type.
531 EXPECT_EQ(m.size(), 2u);
532 EXPECT_EQ(m[1].value(), 2);
533 EXPECT_EQ(m[2].value(), 3);
534 }
535
482 } // namespace base 536 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698