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.
6 #include "wtf/Optional.h"
8 #include <gtest/gtest.h>
14 IntBox(int n
) : number(n
) { }
18 class DestructionNotifier
{
20 DestructionNotifier(bool& flag
) : m_flag(flag
) { }
21 ~DestructionNotifier() { m_flag
= true; }
26 TEST(OptionalTest
, BooleanTest
)
28 Optional
<int> optional
;
29 EXPECT_FALSE(optional
);
31 EXPECT_TRUE(optional
);
34 TEST(OptionalTest
, Dereference
)
36 Optional
<int> optional
;
38 EXPECT_EQ(1, *optional
);
40 Optional
<IntBox
> optionalIntbox
;
41 optionalIntbox
.emplace(42);
42 EXPECT_EQ(42, optionalIntbox
->number
);
45 TEST(OptionalTest
, DestructorCalled
)
47 // Destroying a disengaged optional shouldn't do anything.
49 Optional
<DestructionNotifier
> optional
;
52 // Destroying an engaged optional should call the destructor.
53 bool isDestroyed
= false;
55 Optional
<DestructionNotifier
> optional
;
56 optional
.emplace(isDestroyed
);
57 EXPECT_FALSE(isDestroyed
);
59 EXPECT_TRUE(isDestroyed
);