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