LWG issue #2252: Add more tests for exception safety. No changes needed in the library

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@202885 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Marshall Clow 2014-03-04 20:31:21 +00:00
parent a6057a525a
commit cbb9305c26
3 changed files with 48 additions and 24 deletions

View File

@ -19,12 +19,16 @@ static bool gCopyConstructorShouldThow = false;
class CMyClass { class CMyClass {
public: CMyClass(); public: CMyClass(int tag);
public: CMyClass(const CMyClass& iOther); public: CMyClass(const CMyClass& iOther);
public: ~CMyClass(); public: ~CMyClass();
private: int fMagicValue; bool equal(const CMyClass &rhs) const
{ return fTag == rhs.fTag && fMagicValue == rhs.fMagicValue; }
private:
int fMagicValue;
int fTag;
private: static int kStartedConstructionMagicValue; private: static int kStartedConstructionMagicValue;
private: static int kFinishedConstructionMagicValue; private: static int kFinishedConstructionMagicValue;
}; };
@ -34,15 +38,15 @@ int CMyClass::kStartedConstructionMagicValue = 0;
// Value for fMagicValue when the constructor has finished running // Value for fMagicValue when the constructor has finished running
int CMyClass::kFinishedConstructionMagicValue = 12345; int CMyClass::kFinishedConstructionMagicValue = 12345;
CMyClass::CMyClass() : CMyClass::CMyClass(int tag) :
fMagicValue(kStartedConstructionMagicValue) fMagicValue(kStartedConstructionMagicValue), fTag(tag)
{ {
// Signal that the constructor has finished running // Signal that the constructor has finished running
fMagicValue = kFinishedConstructionMagicValue; fMagicValue = kFinishedConstructionMagicValue;
} }
CMyClass::CMyClass(const CMyClass& /*iOther*/) : CMyClass::CMyClass(const CMyClass& iOther) :
fMagicValue(kStartedConstructionMagicValue) fMagicValue(kStartedConstructionMagicValue), fTag(iOther.fTag)
{ {
// If requested, throw an exception _before_ setting fMagicValue to kFinishedConstructionMagicValue // If requested, throw an exception _before_ setting fMagicValue to kFinishedConstructionMagicValue
if (gCopyConstructorShouldThow) { if (gCopyConstructorShouldThow) {
@ -57,17 +61,21 @@ CMyClass::~CMyClass() {
assert(fMagicValue == kFinishedConstructionMagicValue); assert(fMagicValue == kFinishedConstructionMagicValue);
} }
bool operator==(const CMyClass &lhs, const CMyClass &rhs) { return lhs.equal(rhs); }
int main() int main()
{ {
CMyClass instance; CMyClass instance(42);
std::deque<CMyClass> vec; std::deque<CMyClass> vec;
vec.push_back(instance); vec.push_back(instance);
std::deque<CMyClass> vec2(vec);
gCopyConstructorShouldThow = true; gCopyConstructorShouldThow = true;
try { try {
vec.push_back(instance); vec.push_back(instance);
} }
catch (...) { catch (...) {
assert(vec==vec2);
} }
} }

View File

@ -19,12 +19,16 @@ static bool gCopyConstructorShouldThow = false;
class CMyClass { class CMyClass {
public: CMyClass(); public: CMyClass(int tag);
public: CMyClass(const CMyClass& iOther); public: CMyClass(const CMyClass& iOther);
public: ~CMyClass(); public: ~CMyClass();
private: int fMagicValue; bool equal(const CMyClass &rhs) const
{ return fTag == rhs.fTag && fMagicValue == rhs.fMagicValue; }
private:
int fMagicValue;
int fTag;
private: static int kStartedConstructionMagicValue; private: static int kStartedConstructionMagicValue;
private: static int kFinishedConstructionMagicValue; private: static int kFinishedConstructionMagicValue;
}; };
@ -34,15 +38,15 @@ int CMyClass::kStartedConstructionMagicValue = 0;
// Value for fMagicValue when the constructor has finished running // Value for fMagicValue when the constructor has finished running
int CMyClass::kFinishedConstructionMagicValue = 12345; int CMyClass::kFinishedConstructionMagicValue = 12345;
CMyClass::CMyClass() : CMyClass::CMyClass(int tag) :
fMagicValue(kStartedConstructionMagicValue) fMagicValue(kStartedConstructionMagicValue), fTag(tag)
{ {
// Signal that the constructor has finished running // Signal that the constructor has finished running
fMagicValue = kFinishedConstructionMagicValue; fMagicValue = kFinishedConstructionMagicValue;
} }
CMyClass::CMyClass(const CMyClass& /*iOther*/) : CMyClass::CMyClass(const CMyClass& iOther) :
fMagicValue(kStartedConstructionMagicValue) fMagicValue(kStartedConstructionMagicValue), fTag(iOther.fTag)
{ {
// If requested, throw an exception _before_ setting fMagicValue to kFinishedConstructionMagicValue // If requested, throw an exception _before_ setting fMagicValue to kFinishedConstructionMagicValue
if (gCopyConstructorShouldThow) { if (gCopyConstructorShouldThow) {
@ -57,17 +61,21 @@ CMyClass::~CMyClass() {
assert(fMagicValue == kFinishedConstructionMagicValue); assert(fMagicValue == kFinishedConstructionMagicValue);
} }
bool operator==(const CMyClass &lhs, const CMyClass &rhs) { return lhs.equal(rhs); }
int main() int main()
{ {
CMyClass instance; CMyClass instance(42);
std::deque<CMyClass> vec; std::deque<CMyClass> vec;
vec.push_front(instance); vec.push_front(instance);
std::deque<CMyClass> vec2(vec);
gCopyConstructorShouldThow = true; gCopyConstructorShouldThow = true;
try { try {
vec.push_front(instance); vec.push_front(instance);
} }
catch (...) { catch (...) {
assert(vec==vec2);
} }
} }

View File

@ -19,12 +19,16 @@ static bool gCopyConstructorShouldThow = false;
class CMyClass { class CMyClass {
public: CMyClass(); public: CMyClass(int tag);
public: CMyClass(const CMyClass& iOther); public: CMyClass(const CMyClass& iOther);
public: ~CMyClass(); public: ~CMyClass();
private: int fMagicValue; bool equal(const CMyClass &rhs) const
{ return fTag == rhs.fTag && fMagicValue == rhs.fMagicValue; }
private:
int fMagicValue;
int fTag;
private: static int kStartedConstructionMagicValue; private: static int kStartedConstructionMagicValue;
private: static int kFinishedConstructionMagicValue; private: static int kFinishedConstructionMagicValue;
}; };
@ -34,15 +38,15 @@ int CMyClass::kStartedConstructionMagicValue = 0;
// Value for fMagicValue when the constructor has finished running // Value for fMagicValue when the constructor has finished running
int CMyClass::kFinishedConstructionMagicValue = 12345; int CMyClass::kFinishedConstructionMagicValue = 12345;
CMyClass::CMyClass() : CMyClass::CMyClass(int tag) :
fMagicValue(kStartedConstructionMagicValue) fMagicValue(kStartedConstructionMagicValue), fTag(tag)
{ {
// Signal that the constructor has finished running // Signal that the constructor has finished running
fMagicValue = kFinishedConstructionMagicValue; fMagicValue = kFinishedConstructionMagicValue;
} }
CMyClass::CMyClass(const CMyClass& /*iOther*/) : CMyClass::CMyClass(const CMyClass& iOther) :
fMagicValue(kStartedConstructionMagicValue) fMagicValue(kStartedConstructionMagicValue), fTag(iOther.fTag)
{ {
// If requested, throw an exception _before_ setting fMagicValue to kFinishedConstructionMagicValue // If requested, throw an exception _before_ setting fMagicValue to kFinishedConstructionMagicValue
if (gCopyConstructorShouldThow) { if (gCopyConstructorShouldThow) {
@ -57,17 +61,21 @@ CMyClass::~CMyClass() {
assert(fMagicValue == kFinishedConstructionMagicValue); assert(fMagicValue == kFinishedConstructionMagicValue);
} }
bool operator==(const CMyClass &lhs, const CMyClass &rhs) { return lhs.equal(rhs); }
int main() int main()
{ {
CMyClass instance; CMyClass instance(42);
std::vector<CMyClass> vec; std::vector<CMyClass> vec;
vec.push_back(instance); vec.push_back(instance);
std::vector<CMyClass> vec2(vec);
gCopyConstructorShouldThow = true; gCopyConstructorShouldThow = true;
try { try {
vec.push_back(instance); vec.push_back(instance);
} }
catch (...) { catch (...) {
assert(vec==vec2);
} }
} }