Upstream version 8.36.169.0
[platform/framework/web/crosswalk.git] / src / third_party / libc++ / trunk / test / containers / sequences / deque / deque.modifiers / push_back_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 // <deque>
11
12 // void push_back(const value_type& x);
13
14 #include <deque>
15 #include <cassert>
16
17 // Flag that makes the copy constructor for CMyClass throw an exception
18 static bool gCopyConstructorShouldThow = false;
19
20
21 class CMyClass {
22     public: CMyClass(int tag);
23     public: CMyClass(const CMyClass& iOther);
24     public: ~CMyClass();
25
26     bool equal(const CMyClass &rhs) const
27         { return fTag == rhs.fTag && fMagicValue == rhs.fMagicValue; }
28     private:
29         int fMagicValue;
30         int fTag;
31         
32     private: static int kStartedConstructionMagicValue;
33     private: static int kFinishedConstructionMagicValue;
34 };
35
36 // Value for fMagicValue when the constructor has started running, but not yet finished
37 int CMyClass::kStartedConstructionMagicValue = 0;
38 // Value for fMagicValue when the constructor has finished running
39 int CMyClass::kFinishedConstructionMagicValue = 12345;
40
41 CMyClass::CMyClass(int tag) :
42     fMagicValue(kStartedConstructionMagicValue), fTag(tag)
43 {
44     // Signal that the constructor has finished running
45     fMagicValue = kFinishedConstructionMagicValue;
46 }
47
48 CMyClass::CMyClass(const CMyClass& iOther) :
49     fMagicValue(kStartedConstructionMagicValue), fTag(iOther.fTag)
50 {
51     // If requested, throw an exception _before_ setting fMagicValue to kFinishedConstructionMagicValue
52     if (gCopyConstructorShouldThow) {
53         throw std::exception();
54     }
55     // Signal that the constructor has finished running
56     fMagicValue = kFinishedConstructionMagicValue;
57 }
58
59 CMyClass::~CMyClass() {
60     // Only instances for which the constructor has finished running should be destructed
61     assert(fMagicValue == kFinishedConstructionMagicValue);
62 }
63
64 bool operator==(const CMyClass &lhs, const CMyClass &rhs) { return lhs.equal(rhs); }
65
66 int main()
67 {
68     CMyClass instance(42);
69     std::deque<CMyClass> vec;
70
71     vec.push_back(instance);
72     std::deque<CMyClass> vec2(vec);
73
74     gCopyConstructorShouldThow = true;
75     try {
76         vec.push_back(instance);
77     }
78     catch (...) {
79         assert(vec==vec2);
80     }
81 }