TEST(Optional, Shared) { shared_ptr<int> ptr; Optional<shared_ptr<int>> opt; EXPECT_FALSE(bool(opt)); // empty->emplaced opt.emplace(new int(5)); EXPECT_TRUE(bool(opt)); ptr = opt.value(); EXPECT_EQ(ptr.get(), opt->get()); EXPECT_EQ(2, ptr.use_count()); opt.clear(); EXPECT_EQ(1, ptr.use_count()); // full->copied opt = ptr; EXPECT_EQ(2, ptr.use_count()); EXPECT_EQ(ptr.get(), opt->get()); opt.clear(); EXPECT_EQ(1, ptr.use_count()); // full->moved opt = std::move(ptr); EXPECT_EQ(1, opt->use_count()); EXPECT_EQ(nullptr, ptr.get()); { Optional<shared_ptr<int>> copied(opt); EXPECT_EQ(2, opt->use_count()); Optional<shared_ptr<int>> moved(std::move(opt)); EXPECT_EQ(2, moved->use_count()); moved.emplace(new int(6)); EXPECT_EQ(1, moved->use_count()); copied = moved; EXPECT_EQ(2, moved->use_count()); } }
TEST(Optional, Unique) { Optional<unique_ptr<int>> opt; opt.clear(); EXPECT_FALSE(bool(opt)); // empty->emplaced opt.emplace(new int(5)); EXPECT_TRUE(bool(opt)); EXPECT_EQ(5, **opt); opt.clear(); // empty->moved opt = unique_ptr<int>(new int(6)); EXPECT_EQ(6, **opt); // full->moved opt = unique_ptr<int>(new int(7)); EXPECT_EQ(7, **opt); // move it out by move construct Optional<unique_ptr<int>> moved(std::move(opt)); EXPECT_TRUE(bool(moved)); EXPECT_FALSE(bool(opt)); EXPECT_EQ(7, **moved); EXPECT_TRUE(bool(moved)); opt = std::move(moved); // move it back by move assign EXPECT_FALSE(bool(moved)); EXPECT_TRUE(bool(opt)); EXPECT_EQ(7, **opt); }
TEST(Optional, NoDefault) { Optional<NoDefault> x; EXPECT_FALSE(x); x.emplace(4, 5); EXPECT_TRUE(bool(x)); x.clear(); EXPECT_FALSE(x); }
TEST(Optional, Simple) { Optional<int> opt; EXPECT_FALSE(bool(opt)); opt = 4; EXPECT_TRUE(bool(opt)); EXPECT_EQ(4, *opt); opt = 5; EXPECT_EQ(5, *opt); opt.clear(); EXPECT_FALSE(bool(opt)); }
TEST_F(ScopedEventBaseThreadTest, eb_dtor_in_io_thread) { Optional<ScopedEventBaseThread> sebt; sebt.emplace(); auto const io_thread_id = sebt->getThreadId(); EXPECT_NE(this_thread::get_id(), io_thread_id) << "sanity"; auto const eb = sebt->getEventBase(); thread::id eb_dtor_thread_id; eb->runOnDestruction(new EventBase::FunctionLoopCallback( [&] { eb_dtor_thread_id = this_thread::get_id(); })); sebt.clear(); EXPECT_EQ(io_thread_id, eb_dtor_thread_id); }
template<typename T> void _readPropertyOfElement( xercesc::DOMElement *parent, const char *namespaceURI, const char *localName, Optional<T>& field, void(*xmladapter)(const char *, Optional<T>&) = &xmlAdapter) { const XMLCh *text = DOMHelper::getElementTextContentByTagNameNS( parent, DOMHelper::fromUTF8(XML_NS), DOMHelper::fromUTF8(localName)); if (text) { xmladapter(DOMHelper::toUTF8(text), field); } else { field.clear(); } }
TEST(Optional, Const) { { // default construct Optional<const int> opt; EXPECT_FALSE(bool(opt)); opt.emplace(4); EXPECT_EQ(*opt, 4); opt.emplace(5); EXPECT_EQ(*opt, 5); opt.clear(); EXPECT_FALSE(bool(opt)); } { // copy-constructed const int x = 6; Optional<const int> opt(x); EXPECT_EQ(*opt, 6); } { // move-constructed const int x = 7; Optional<const int> opt(std::move(x)); EXPECT_EQ(*opt, 7); } // no assignment allowed }