| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "config.h" | 5 #include "config.h" |
| 6 #include "wtf/Optional.h" | 6 #include "wtf/Optional.h" |
| 7 | 7 |
| 8 #include <gtest/gtest.h> | 8 #include <gtest/gtest.h> |
| 9 | 9 |
| 10 namespace WTF { | 10 namespace WTF { |
| 11 namespace { | 11 namespace { |
| 12 | 12 |
| 13 struct IntBox { | 13 struct IntBox { |
| 14 IntBox(int n) : number(n) { } | 14 IntBox(int n) |
| 15 int number; | 15 : number(n) {} |
| 16 int number; |
| 16 }; | 17 }; |
| 17 | 18 |
| 18 class DestructionNotifier { | 19 class DestructionNotifier { |
| 19 public: | 20 public: |
| 20 DestructionNotifier(bool& flag) : m_flag(flag) { } | 21 DestructionNotifier(bool& flag) |
| 21 ~DestructionNotifier() { m_flag = true; } | 22 : m_flag(flag) {} |
| 22 private: | 23 ~DestructionNotifier() { m_flag = true; } |
| 23 bool& m_flag; | 24 |
| 25 private: |
| 26 bool& m_flag; |
| 24 }; | 27 }; |
| 25 | 28 |
| 26 TEST(OptionalTest, BooleanTest) | 29 TEST(OptionalTest, BooleanTest) { |
| 27 { | 30 Optional<int> optional; |
| 28 Optional<int> optional; | 31 EXPECT_FALSE(optional); |
| 29 EXPECT_FALSE(optional); | 32 optional.emplace(0); |
| 30 optional.emplace(0); | 33 EXPECT_TRUE(optional); |
| 31 EXPECT_TRUE(optional); | |
| 32 } | 34 } |
| 33 | 35 |
| 34 TEST(OptionalTest, Dereference) | 36 TEST(OptionalTest, Dereference) { |
| 35 { | 37 Optional<int> optional; |
| 36 Optional<int> optional; | 38 optional.emplace(1); |
| 37 optional.emplace(1); | 39 EXPECT_EQ(1, *optional); |
| 38 EXPECT_EQ(1, *optional); | |
| 39 | 40 |
| 40 Optional<IntBox> optionalIntbox; | 41 Optional<IntBox> optionalIntbox; |
| 41 optionalIntbox.emplace(42); | 42 optionalIntbox.emplace(42); |
| 42 EXPECT_EQ(42, optionalIntbox->number); | 43 EXPECT_EQ(42, optionalIntbox->number); |
| 43 } | 44 } |
| 44 | 45 |
| 45 TEST(OptionalTest, DestructorCalled) | 46 TEST(OptionalTest, DestructorCalled) { |
| 46 { | 47 // Destroying a disengaged optional shouldn't do anything. |
| 47 // Destroying a disengaged optional shouldn't do anything. | 48 { |
| 48 { | 49 Optional<DestructionNotifier> optional; |
| 49 Optional<DestructionNotifier> optional; | 50 } |
| 50 } | |
| 51 | 51 |
| 52 // Destroying an engaged optional should call the destructor. | 52 // Destroying an engaged optional should call the destructor. |
| 53 bool isDestroyed = false; | 53 bool isDestroyed = false; |
| 54 { | 54 { |
| 55 Optional<DestructionNotifier> optional; | 55 Optional<DestructionNotifier> optional; |
| 56 optional.emplace(isDestroyed); | 56 optional.emplace(isDestroyed); |
| 57 EXPECT_FALSE(isDestroyed); | 57 EXPECT_FALSE(isDestroyed); |
| 58 } | 58 } |
| 59 EXPECT_TRUE(isDestroyed); | 59 EXPECT_TRUE(isDestroyed); |
| 60 } | 60 } |
| 61 | 61 |
| 62 } // namespace | 62 } // namespace |
| 63 } // namespace WTF | 63 } // namespace WTF |
| OLD | NEW |