Exercise: Bag Of Items, By Unique Reference¶
Continuing from Exercise: Bag Of Items, By Copy, create a similar class BagUnique
that contains pointers of type std::unique_ptr<Item>
such that the
tests below pass.
Special features:
Find out: what would the return type of
find_by_int() const
be?Can it be the vector’s member type, a
std::unique_ptr<Item>
? Why not?A
const std::unique_ptr<Item>&
maybe? Why not?
Try to create a
std::unique_ptr<Item>
and assign it to variable which you then insert, like (naively)auto v = std::make_unique<BagUnique::Item>("something", 42); bag.insert(v);
Does it work? What do you notice?
#include "bag-unique.h"
#include <gtest/gtest.h>
TEST(bag_unique_suite, find_by_int_ok)
{
BagUnique bag;
bag.insert(std::make_unique<BagUnique::Item>("something", 42));
auto item2 = std::make_unique<BagUnique::Item>("anything", 666);
bag.insert(std::move(item2));
const BagUnique& constbag = bag;
auto item = constbag.find_by_int(42);
ASSERT_NE(item, nullptr);
ASSERT_EQ(item->first, "something");
ASSERT_EQ(item->second, 42);
}
TEST(bag_unique_suite, find_by_int_nok)
{
BagUnique bag;
bag.insert(std::make_unique<BagUnique::Item>("something", 42));
bag.insert(std::make_unique<BagUnique::Item>("anything", 666));
auto item = bag.find_by_int(7);
ASSERT_EQ(item, nullptr);
}
TEST(bag_unique_suite, remove_by_int_ok)
{
BagUnique bag;
bag.insert(std::make_unique<BagUnique::Item>("something", 42));
bag.insert(std::make_unique<BagUnique::Item>("anything", 666));
std::size_t nremoved = bag.remove_by_int(42);
ASSERT_EQ(nremoved, 1);
}