Skip to content

Commit

Permalink
[libcxx][test] Attempt to make debug mode tests more bulletproof
Browse files Browse the repository at this point in the history
The problem with debug mode tests is that it isn't known which particular
_LIBCPP_ASSERT causes the test to exit, and as shown by
https://reviews.llvm.org/D100029 and 2908eb2 it might be not the
expected one.

The patch adds TEST_LIBCPP_ASSERT_FAILURE macro that allows checking
_LIBCPP_ASSERT message to ensure we caught an expected failure.

Reviewed By: Quuxplusone, ldionne

Differential Revision: https://reviews.llvm.org/D100595
  • Loading branch information
chbessonova committed May 18, 2021
1 parent 38e2359 commit 9f4f012
Show file tree
Hide file tree
Showing 177 changed files with 516 additions and 962 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@

// pop_back() more than the number of elements in a deque

#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0))
// UNSUPPORTED: libcxx-no-debug-mode

// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DEBUG=1

#include <cstdlib>
#include <deque>

#include "test_macros.h"

#include "debug_macros.h"

int main(int, char**) {
std::deque<int> q;
q.push_back(0);
q.pop_back();
q.pop_back();
std::exit(1);
TEST_LIBCPP_ASSERT_FAILURE(q.pop_back(), "deque::pop_back called on an empty deque");

return 0;
return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,19 @@
// UNSUPPORTED: libcxx-no-debug-mode

// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DEBUG=1
#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0))

#include <list>
#include <cstdlib>
#include <cassert>

#include "test_macros.h"
#include "debug_macros.h"

int main(int, char**)
{
std::list<int> l1;
l1.push_back(1); l1.push_back(2); l1.push_back(3);
std::list<int>::iterator i = l1.begin();
std::list<int> l2 = l1;
l2.erase(i);
assert(false);
TEST_LIBCPP_ASSERT_FAILURE(l2.erase(i), "list::erase(iterator) called with an iterator not referring to this list");

return 0;
return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,25 @@
// UNSUPPORTED: libcxx-no-debug-mode

// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DEBUG=1
#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0))

#include <list>
#include <cstdlib>
#include <cassert>

#include "test_macros.h"
#include "debug_macros.h"

class A
{
int i_;
double d_;

A(const A&);
A& operator=(const A&);
public:
A(int i, double d)
: i_(i), d_(d) {}

int geti() const {return i_;}
double getd() const {return d_;}
struct A {
explicit A(int i, double d) {
(void)i;
(void)d;
}
};

int main(int, char**)
{
std::list<A> c1;
std::list<A> c2;
std::list<A>::iterator i = c1.emplace(c2.cbegin(), 2, 3.5);
assert(false);
TEST_LIBCPP_ASSERT_FAILURE(c1.emplace(c2.cbegin(), 2, 3.5),
"list::emplace(iterator, args...) called with an iterator not referring to this list");

return 0;
return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,18 @@
// UNSUPPORTED: libcxx-no-debug-mode

// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DEBUG=1
#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0))

#include <list>
#include <cassert>
#include <cstdlib>

#include "test_macros.h"
#include "debug_macros.h"

int main(int, char**)
{
int a1[] = {1, 2, 3};
std::list<int> l1(a1, a1+3);
std::list<int>::const_iterator i = l1.end();
l1.erase(i);
assert(false);
TEST_LIBCPP_ASSERT_FAILURE(l1.erase(i), "list::erase(iterator) called with a non-dereferenceable iterator");

return 0;
return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,19 @@
// UNSUPPORTED: libcxx-no-debug-mode

// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DEBUG=1
#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0))

#include <list>
#include <cassert>
#include <cstdlib>

#include "test_macros.h"
#include "debug_macros.h"

int main(int, char**)
{
int a1[] = {1, 2, 3};
std::list<int> l1(a1, a1+3);
std::list<int> l2(a1, a1+3);
std::list<int>::const_iterator i = l2.begin();
l1.erase(i);
assert(false);
TEST_LIBCPP_ASSERT_FAILURE(l1.erase(i), "list::erase(iterator) called with an iterator not referring to this list");

return 0;
return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,19 @@
// UNSUPPORTED: libcxx-no-debug-mode

// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DEBUG=1
#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0))

#include <list>
#include <cassert>
#include <cstdlib>

#include "test_macros.h"
#include "debug_macros.h"

int main(int, char**)
{
int a1[] = {1, 2, 3};
std::list<int> l1(a1, a1+3);
std::list<int> l2(a1, a1+3);
std::list<int>::iterator i = l1.erase(l2.cbegin(), next(l1.cbegin()));
assert(false);
TEST_LIBCPP_ASSERT_FAILURE(l1.erase(l2.cbegin(), std::next(l1.cbegin())),
"list::erase(iterator, iterator) called with an iterator not referring to this list");

return 0;
return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,19 @@
// UNSUPPORTED: libcxx-no-debug-mode

// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DEBUG=1
#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0))

#include <list>
#include <cassert>
#include <cstdlib>

#include "test_macros.h"
#include "debug_macros.h"

int main(int, char**)
{
int a1[] = {1, 2, 3};
std::list<int> l1(a1, a1+3);
std::list<int> l2(a1, a1+3);
std::list<int>::iterator i = l1.erase(l1.cbegin(), next(l2.cbegin()));
assert(false);
TEST_LIBCPP_ASSERT_FAILURE(l1.erase(l1.cbegin(), std::next(l2.cbegin())),
"list::erase(iterator, iterator) called with an iterator not referring to this list");

return 0;
return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,19 @@
// UNSUPPORTED: libcxx-no-debug-mode

// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DEBUG=1
#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0))

#include <list>
#include <cassert>
#include <cstdlib>

#include "test_macros.h"
#include "debug_macros.h"

int main(int, char**)
{
int a1[] = {1, 2, 3};
std::list<int> l1(a1, a1+3);
std::list<int> l2(a1, a1+3);
std::list<int>::iterator i = l1.erase(l2.cbegin(), next(l2.cbegin()));
assert(false);
TEST_LIBCPP_ASSERT_FAILURE(l1.erase(l2.cbegin(), std::next(l2.cbegin())),
"list::erase(iterator, iterator) called with an iterator not referring to this list");

return 0;
return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,18 @@
// UNSUPPORTED: libcxx-no-debug-mode

// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DEBUG=1
#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0))

#include <list>
#include <cassert>
#include <cstdlib>

#include "test_macros.h"
#include "debug_macros.h"

int main(int, char**)
{
int a1[] = {1, 2, 3};
std::list<int> l1(a1, a1+3);
std::list<int>::iterator i = l1.erase(next(l1.cbegin()), l1.cbegin());
assert(false);
TEST_LIBCPP_ASSERT_FAILURE(l1.erase(std::next(l1.cbegin()), l1.cbegin()),
"Attempted to increment a non-incrementable list::const_iterator");

return 0;
return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,20 @@
// UNSUPPORTED: libcxx-no-debug-mode

// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DEBUG=1
#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0))

#include <list>
#include <cstdlib>
#include <cassert>

#include "test_macros.h"
#include "test_iterators.h"
#include "debug_macros.h"

int main(int, char**)
{
{
std::list<int> v(100);
std::list<int> v2(100);
int a[] = {1, 2, 3, 4, 5};
const int N = sizeof(a)/sizeof(a[0]);
std::list<int>::iterator i = v.insert(next(v2.cbegin(), 10),
cpp17_input_iterator<const int*>(a),
cpp17_input_iterator<const int*>(a+N));
assert(false);
TEST_LIBCPP_ASSERT_FAILURE(v.insert(v2.cbegin(), a, a + 5),
"list::insert(iterator, range) called with an iterator not referring to this list");
}

return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,18 @@
// UNSUPPORTED: libcxx-no-debug-mode

// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DEBUG=1
#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0))

#include <list>
#include <cstdlib>
#include <cassert>

#include "test_macros.h"
#include "debug_macros.h"

int main(int, char**)
{
std::list<int> v1(3);
std::list<int> v2(3);
v1.insert(v2.begin(), 4);
assert(false);
TEST_LIBCPP_ASSERT_FAILURE(v1.insert(v2.begin(), 4),
"list::insert(iterator, x) called with an iterator not referring to this list");

return 0;
return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,18 @@
// UNSUPPORTED: libcxx-no-debug-mode

// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DEBUG=1
#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0))

#include <list>
#include <cstdlib>
#include <cassert>

#include "test_macros.h"
#include "debug_macros.h"

int main(int, char**)
{
std::list<int> c1(100);
std::list<int> c2;
std::list<int>::iterator i = c1.insert(next(c2.cbegin(), 10), 5, 1);
assert(false);
TEST_LIBCPP_ASSERT_FAILURE(c1.insert(c2.cbegin(), 5, 1),
"list::insert(iterator, n, x) called with an iterator not referring to this list");

return 0;
return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,19 @@
// UNSUPPORTED: libcxx-no-debug-mode

// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DEBUG=1
#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0))

#include <list>
#include <cstdlib>
#include <cassert>

#include "test_macros.h"

#include "debug_macros.h"

int main(int, char**)
{
std::list<int> v1(3);
std::list<int> v2(3);
int i = 4;
v1.insert(v2.begin(), i);
assert(false);
TEST_LIBCPP_ASSERT_FAILURE(v1.insert(v2.begin(), i),
"list::insert(iterator, x) called with an iterator not referring to this list");

return 0;
return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@
// UNSUPPORTED: libcxx-no-debug-mode

// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DEBUG=1
#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0))

#include <list>
#include <cstdlib>
#include <cassert>

#include "test_macros.h"
#include "debug_macros.h"

int main(int, char**)
{
Expand All @@ -31,8 +30,7 @@ int main(int, char**)
assert(c == std::list<int>(a, a+1));
c.pop_back();
assert(c.empty());
c.pop_back(); // operation under test
assert(false);
TEST_LIBCPP_ASSERT_FAILURE(c.pop_back(), "list::pop_back() called on an empty list");

return 0;
return 0;
}
Loading

0 comments on commit 9f4f012

Please sign in to comment.