blob: e01b2a224ffd3f9e93553866d3854f8463bd860b [file] [log] [blame]
Howard Hinnantf619e232013-01-11 20:36:59 +00001//===----------------------------------------------------------------------===//
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_front(const value_type& x);
13
14#include <deque>
15#include <cassert>
Marshall Clowd07fcd62015-03-09 17:08:51 +000016#include "test_allocator.h"
Howard Hinnantf619e232013-01-11 20:36:59 +000017
18// Flag that makes the copy constructor for CMyClass throw an exception
19static bool gCopyConstructorShouldThow = false;
20
21
22class CMyClass {
Marshall Clowcbb93052014-03-04 20:31:21 +000023 public: CMyClass(int tag);
Howard Hinnant171771a2013-07-08 21:06:38 +000024 public: CMyClass(const CMyClass& iOther);
25 public: ~CMyClass();
Howard Hinnantf619e232013-01-11 20:36:59 +000026
Marshall Clowcbb93052014-03-04 20:31:21 +000027 bool equal(const CMyClass &rhs) const
28 { return fTag == rhs.fTag && fMagicValue == rhs.fMagicValue; }
29 private:
30 int fMagicValue;
31 int fTag;
32
Howard Hinnant171771a2013-07-08 21:06:38 +000033 private: static int kStartedConstructionMagicValue;
34 private: static int kFinishedConstructionMagicValue;
Howard Hinnantf619e232013-01-11 20:36:59 +000035};
36
37// Value for fMagicValue when the constructor has started running, but not yet finished
38int CMyClass::kStartedConstructionMagicValue = 0;
39// Value for fMagicValue when the constructor has finished running
40int CMyClass::kFinishedConstructionMagicValue = 12345;
41
Marshall Clowcbb93052014-03-04 20:31:21 +000042CMyClass::CMyClass(int tag) :
43 fMagicValue(kStartedConstructionMagicValue), fTag(tag)
Howard Hinnantf619e232013-01-11 20:36:59 +000044{
Howard Hinnant171771a2013-07-08 21:06:38 +000045 // Signal that the constructor has finished running
46 fMagicValue = kFinishedConstructionMagicValue;
Howard Hinnantf619e232013-01-11 20:36:59 +000047}
48
Marshall Clowcbb93052014-03-04 20:31:21 +000049CMyClass::CMyClass(const CMyClass& iOther) :
50 fMagicValue(kStartedConstructionMagicValue), fTag(iOther.fTag)
Howard Hinnantf619e232013-01-11 20:36:59 +000051{
Howard Hinnant171771a2013-07-08 21:06:38 +000052 // If requested, throw an exception _before_ setting fMagicValue to kFinishedConstructionMagicValue
53 if (gCopyConstructorShouldThow) {
54 throw std::exception();
55 }
56 // Signal that the constructor has finished running
57 fMagicValue = kFinishedConstructionMagicValue;
Howard Hinnantf619e232013-01-11 20:36:59 +000058}
59
60CMyClass::~CMyClass() {
Howard Hinnant171771a2013-07-08 21:06:38 +000061 // Only instances for which the constructor has finished running should be destructed
62 assert(fMagicValue == kFinishedConstructionMagicValue);
Howard Hinnantf619e232013-01-11 20:36:59 +000063}
64
Marshall Clowcbb93052014-03-04 20:31:21 +000065bool operator==(const CMyClass &lhs, const CMyClass &rhs) { return lhs.equal(rhs); }
66
Howard Hinnantf619e232013-01-11 20:36:59 +000067int main()
68{
Marshall Clowcbb93052014-03-04 20:31:21 +000069 CMyClass instance(42);
Marshall Clowd07fcd62015-03-09 17:08:51 +000070 {
Howard Hinnant171771a2013-07-08 21:06:38 +000071 std::deque<CMyClass> vec;
Howard Hinnantf619e232013-01-11 20:36:59 +000072
Howard Hinnant171771a2013-07-08 21:06:38 +000073 vec.push_front(instance);
Marshall Clowcbb93052014-03-04 20:31:21 +000074 std::deque<CMyClass> vec2(vec);
Howard Hinnantf619e232013-01-11 20:36:59 +000075
Howard Hinnant171771a2013-07-08 21:06:38 +000076 gCopyConstructorShouldThow = true;
77 try {
78 vec.push_front(instance);
Marshall Clowd07fcd62015-03-09 17:08:51 +000079 assert(false);
80 }
81 catch (...) {
82 gCopyConstructorShouldThow = false;
83 assert(vec==vec2);
84 }
85 }
86
87 {
88 typedef std::deque<CMyClass, test_allocator<CMyClass> > C;
89 C vec;
90 C vec2(vec);
91
92 C::allocator_type::throw_after = 1;
93 try {
94 vec.push_front(instance);
95 assert(false);
Howard Hinnant171771a2013-07-08 21:06:38 +000096 }
97 catch (...) {
Marshall Clowcbb93052014-03-04 20:31:21 +000098 assert(vec==vec2);
Howard Hinnant171771a2013-07-08 21:06:38 +000099 }
Marshall Clowd07fcd62015-03-09 17:08:51 +0000100 }
Howard Hinnantf619e232013-01-11 20:36:59 +0000101}