[lld][WebAssembly] Introduce Ctx::arg
[llvm-project.git] / libcxx / test / std / containers / sequences / deque / deque.modifiers / push_front_exception_safety.pass.cpp
blobc1d7723bbbf25d21d0ba813d1441b379734d7197
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: no-exceptions
10 // <deque>
12 // void push_front(const value_type& x);
14 #include <deque>
15 #include <cassert>
16 #include "test_macros.h"
17 #include "test_allocator.h"
19 // Flag that makes the copy constructor for CMyClass throw an exception
20 static bool gCopyConstructorShouldThrow = false;
22 class CMyClass {
23 public: CMyClass(int tag);
24 public: CMyClass(const CMyClass& iOther);
25 public: ~CMyClass();
27 bool equal(const CMyClass &rhs) const
28 { return fTag == rhs.fTag && fMagicValue == rhs.fMagicValue; }
29 private:
30 int fMagicValue;
31 int fTag;
33 private: static int kStartedConstructionMagicValue;
34 private: static int kFinishedConstructionMagicValue;
37 // Value for fMagicValue when the constructor has started running, but not yet finished
38 int CMyClass::kStartedConstructionMagicValue = 0;
39 // Value for fMagicValue when the constructor has finished running
40 int CMyClass::kFinishedConstructionMagicValue = 12345;
42 CMyClass::CMyClass(int tag) :
43 fMagicValue(kStartedConstructionMagicValue), fTag(tag)
45 // Signal that the constructor has finished running
46 fMagicValue = kFinishedConstructionMagicValue;
49 CMyClass::CMyClass(const CMyClass& iOther) :
50 fMagicValue(kStartedConstructionMagicValue), fTag(iOther.fTag)
52 // If requested, throw an exception _before_ setting fMagicValue to kFinishedConstructionMagicValue
53 if (gCopyConstructorShouldThrow) {
54 throw std::exception();
56 // Signal that the constructor has finished running
57 fMagicValue = kFinishedConstructionMagicValue;
60 CMyClass::~CMyClass() {
61 // Only instances for which the constructor has finished running should be destructed
62 assert(fMagicValue == kFinishedConstructionMagicValue);
65 bool operator==(const CMyClass &lhs, const CMyClass &rhs) { return lhs.equal(rhs); }
67 int main(int, char**)
69 CMyClass instance(42);
71 std::deque<CMyClass> vec;
73 vec.push_front(instance);
74 std::deque<CMyClass> vec2(vec);
76 gCopyConstructorShouldThrow = true;
77 try {
78 vec.push_front(instance);
79 assert(false);
81 catch (...) {
82 gCopyConstructorShouldThrow = false;
83 assert(vec == vec2);
88 test_allocator_statistics alloc_stats;
89 typedef std::deque<CMyClass, test_allocator<CMyClass> > C;
90 C vec((test_allocator<CMyClass>(&alloc_stats)));
91 C vec2(vec, test_allocator<CMyClass>(&alloc_stats));
93 alloc_stats.throw_after = 1;
94 try {
95 vec.push_front(instance);
96 assert(false);
98 catch (...) {
99 assert(vec==vec2);
103 return 0;