[nfc][mlir][scf]: Define scf.for lower/upper bounds can be also negative or zero...
[llvm-project.git] / libcxx / test / std / containers / sequences / deque / deque.modifiers / push_back_exception_safety.pass.cpp
blob3e65f993f6506d2727ffcb70d59833020980b03a
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_back(const value_type& x);
14 #include <deque>
15 #include "test_macros.h"
16 #include "test_allocator.h"
17 #include <cassert>
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; }
30 private:
31 int fMagicValue;
32 int fTag;
34 private: static int kStartedConstructionMagicValue;
35 private: static int kFinishedConstructionMagicValue;
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;
43 CMyClass::CMyClass(int tag) :
44 fMagicValue(kStartedConstructionMagicValue), fTag(tag)
46 // Signal that the constructor has finished running
47 fMagicValue = kFinishedConstructionMagicValue;
50 CMyClass::CMyClass(const CMyClass& iOther) :
51 fMagicValue(kStartedConstructionMagicValue), fTag(iOther.fTag)
53 // If requested, throw an exception _before_ setting fMagicValue to kFinishedConstructionMagicValue
54 if (gCopyConstructorShouldThrow) {
55 throw std::exception();
57 // Signal that the constructor has finished running
58 fMagicValue = kFinishedConstructionMagicValue;
61 CMyClass::~CMyClass() {
62 // Only instances for which the constructor has finished running should be destructed
63 assert(fMagicValue == kFinishedConstructionMagicValue);
66 bool operator==(const CMyClass &lhs, const CMyClass &rhs) { return lhs.equal(rhs); }
68 int main(int, char**)
70 CMyClass instance(42);
72 std::deque<CMyClass> vec;
74 vec.push_back(instance);
75 std::deque<CMyClass> vec2(vec);
77 gCopyConstructorShouldThrow = true;
78 try {
79 vec.push_back(instance);
80 assert(false);
82 catch (...) {
83 gCopyConstructorShouldThrow = false;
84 assert(vec == vec2);
89 test_allocator_statistics alloc_stats;
90 typedef std::deque<CMyClass, test_allocator<CMyClass> > C;
91 C vec((test_allocator<CMyClass>(&alloc_stats)));
92 C vec2(vec, test_allocator<CMyClass>(&alloc_stats));
94 alloc_stats.throw_after = 1;
95 try {
96 vec.push_back(instance);
97 assert(false);
99 catch (...) {
100 assert(vec==vec2);
104 return 0;