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

Unified Diff: Source/wtf/OptionalTest.cpp

Issue 1129793005: Replace OwnPtr with WTF::Optional for optional recorders. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: merge with master Created 5 years, 7 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 side-by-side diff with in-line comments
Download patch
Index: Source/wtf/OptionalTest.cpp
diff --git a/Source/wtf/OptionalTest.cpp b/Source/wtf/OptionalTest.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..7974637fce57ece4ce8a8c4b62cbc0fb18e8ad88
--- /dev/null
+++ b/Source/wtf/OptionalTest.cpp
@@ -0,0 +1,63 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "config.h"
+#include "wtf/Optional.h"
+
+#include <gtest/gtest.h>
+
+namespace WTF {
+namespace {
+
+struct IntBox {
+ IntBox(int n) : number(n) { }
+ int number;
+};
+
+class DestructionNotifier {
+public:
+ DestructionNotifier(bool& flag) : m_flag(flag) { }
+ ~DestructionNotifier() { m_flag = true; }
+private:
+ bool& m_flag;
+};
+
+TEST(OptionalTest, BooleanTest)
+{
+ Optional<int> optional;
+ EXPECT_FALSE(optional);
+ optional.emplace(0);
+ EXPECT_TRUE(optional);
+}
+
+TEST(OptionalTest, Dereference)
+{
+ Optional<int> optional;
+ optional.emplace(1);
+ EXPECT_EQ(1, *optional);
+
+ Optional<IntBox> optionalIntbox;
+ optionalIntbox.emplace(42);
+ EXPECT_EQ(42, optionalIntbox->number);
+}
+
+TEST(OptionalTest, DestructorCalled)
+{
+ // Destroying a disengaged optional shouldn't do anything.
+ {
+ Optional<DestructionNotifier> optional;
+ }
+
+ // Destroying an engaged optional should call the destructor.
+ bool isDestroyed = false;
+ {
+ Optional<DestructionNotifier> optional;
+ optional.emplace(isDestroyed);
+ EXPECT_FALSE(isDestroyed);
+ }
+ EXPECT_TRUE(isDestroyed);
+}
+
+} // namespace
+} // namespace WTF
« Source/wtf/Optional.h ('K') | « Source/wtf/Optional.h ('k') | Source/wtf/TypeTraits.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698