]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/containers/sequences/deque/deque.modifiers/push_front_exception_safety.pass.cpp
Vendor import of libc++ trunk r290819:
[FreeBSD/FreeBSD.git] / test / std / containers / sequences / deque / deque.modifiers / push_front_exception_safety.pass.cpp
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // UNSUPPORTED: libcpp-no-exceptions
11 // <deque>
12
13 // void push_front(const value_type& x);
14
15 #include <deque>
16 #include <cassert>
17 #include "test_allocator.h"
18
19 // Flag that makes the copy constructor for CMyClass throw an exception
20 static bool gCopyConstructorShouldThow = false;
21
22
23 class CMyClass {
24     public: CMyClass(int tag);
25     public: CMyClass(const CMyClass& iOther);
26     public: ~CMyClass();
27
28     bool equal(const CMyClass &rhs) const
29         { return fTag == rhs.fTag && fMagicValue == rhs.fMagicValue; }
30     private:
31         int fMagicValue;
32         int fTag;
33
34     private: static int kStartedConstructionMagicValue;
35     private: static int kFinishedConstructionMagicValue;
36 };
37
38 // Value for fMagicValue when the constructor has started running, but not yet finished
39 int CMyClass::kStartedConstructionMagicValue = 0;
40 // Value for fMagicValue when the constructor has finished running
41 int CMyClass::kFinishedConstructionMagicValue = 12345;
42
43 CMyClass::CMyClass(int tag) :
44     fMagicValue(kStartedConstructionMagicValue), fTag(tag)
45 {
46     // Signal that the constructor has finished running
47     fMagicValue = kFinishedConstructionMagicValue;
48 }
49
50 CMyClass::CMyClass(const CMyClass& iOther) :
51     fMagicValue(kStartedConstructionMagicValue), fTag(iOther.fTag)
52 {
53     // If requested, throw an exception _before_ setting fMagicValue to kFinishedConstructionMagicValue
54     if (gCopyConstructorShouldThow) {
55         throw std::exception();
56     }
57     // Signal that the constructor has finished running
58     fMagicValue = kFinishedConstructionMagicValue;
59 }
60
61 CMyClass::~CMyClass() {
62     // Only instances for which the constructor has finished running should be destructed
63     assert(fMagicValue == kFinishedConstructionMagicValue);
64 }
65
66 bool operator==(const CMyClass &lhs, const CMyClass &rhs) { return lhs.equal(rhs); }
67
68 int main()
69 {
70     CMyClass instance(42);
71     {
72     std::deque<CMyClass> vec;
73
74     vec.push_front(instance);
75     std::deque<CMyClass> vec2(vec);
76
77     gCopyConstructorShouldThow = true;
78     try {
79         vec.push_front(instance);
80         assert(false);
81     }
82     catch (...) {
83             gCopyConstructorShouldThow = false;
84         assert(vec==vec2);
85     }
86         }
87
88         {
89         typedef std::deque<CMyClass, test_allocator<CMyClass> > C;
90     C vec;
91     C vec2(vec);
92
93         C::allocator_type::throw_after = 1;
94     try {
95         vec.push_front(instance);
96         assert(false);
97     }
98     catch (...) {
99         assert(vec==vec2);
100     }
101     }
102 }