Skip to content

Commit

Permalink
Protect exceptional paths under libcpp-no-exceptions
Browse files Browse the repository at this point in the history
These tests are of the form

try {
   action-that-may-throw
   assert(!exceptional-condition)
   assert(some-other-facts)
 } catch (relevant-exception) {
   assert(exceptional-condition)
 }

Under libcpp-no-exceptions there is still value in verifying
some-other-facts while avoiding the exceptional case. So for these tests
just conditionally check some-other-facts if exceptional-condition is
false. When exception are supported make sure that a true
exceptional-condition throws an exception

Differential Revision: https://reviews.llvm.org/D26136

llvm-svn: 285697
  • Loading branch information
Roger Ferrer Ibanez committed Nov 1, 2016
1 parent 6dd8fab commit 8a915ed
Show file tree
Hide file tree
Showing 28 changed files with 598 additions and 248 deletions.
31 changes: 25 additions & 6 deletions libcxx/test/std/strings/basic.string/string.access/at.pass.cpp
Expand Up @@ -7,7 +7,6 @@
//
//===----------------------------------------------------------------------===//

// XFAIL: libcpp-no-exceptions
// <string>

// const_reference at(size_type pos) const;
Expand All @@ -19,21 +18,41 @@

#include "min_allocator.h"

#include "test_macros.h"

template <class S>
void
test(S s, typename S::size_type pos)
{
try
const S& cs = s;
if (pos < s.size())
{
const S& cs = s;
assert(s.at(pos) == s[pos]);
assert(cs.at(pos) == cs[pos]);
assert(pos < cs.size());
}
catch (std::out_of_range&)
#ifndef TEST_HAS_NO_EXCEPTIONS
else
{
assert(pos >= s.size());
try
{
s.at(pos);
assert(false);
}
catch (std::out_of_range&)
{
assert(pos >= s.size());
}
try
{
cs.at(pos);
assert(false);
}
catch (std::out_of_range&)
{
assert(pos >= s.size());
}
}
#endif
}

int main()
Expand Down
Expand Up @@ -7,7 +7,6 @@
//
//===----------------------------------------------------------------------===//

// XFAIL: libcpp-no-exceptions
// <string>

// void reserve(size_type res_arg=0);
Expand Down Expand Up @@ -38,18 +37,27 @@ test(S s, typename S::size_type res_arg)
{
typename S::size_type old_cap = s.capacity();
S s0 = s;
try
if (res_arg <= s.max_size())
{
s.reserve(res_arg);
assert(res_arg <= s.max_size());
assert(s == s0);
assert(s.capacity() >= res_arg);
assert(s.capacity() >= s.size());
}
catch (std::length_error&)
#ifndef TEST_HAS_NO_EXCEPTIONS
else
{
assert(res_arg > s.max_size());
try
{
s.reserve(res_arg);
assert(false);
}
catch (std::length_error&)
{
assert(res_arg > s.max_size());
}
}
#endif
}

int main()
Expand Down
Expand Up @@ -7,7 +7,6 @@
//
//===----------------------------------------------------------------------===//

// XFAIL: libcpp-no-exceptions
// <string>

// void resize(size_type n);
Expand All @@ -23,17 +22,26 @@ template <class S>
void
test(S s, typename S::size_type n, S expected)
{
try
if (n <= s.max_size())
{
s.resize(n);
LIBCPP_ASSERT(s.__invariants());
assert(n <= s.max_size());
assert(s == expected);
}
catch (std::length_error&)
#ifndef TEST_HAS_NO_EXCEPTIONS
else
{
assert(n > s.max_size());
try
{
s.resize(n);
assert(false);
}
catch (std::length_error&)
{
assert(n > s.max_size());
}
}
#endif
}

int main()
Expand Down
Expand Up @@ -7,7 +7,6 @@
//
//===----------------------------------------------------------------------===//

// XFAIL: libcpp-no-exceptions
// <string>

// void resize(size_type n, charT c);
Expand All @@ -23,17 +22,26 @@ template <class S>
void
test(S s, typename S::size_type n, typename S::value_type c, S expected)
{
try
if (n <= s.max_size())
{
s.resize(n, c);
LIBCPP_ASSERT(s.__invariants());
assert(n <= s.max_size());
assert(s == expected);
}
catch (std::length_error&)
#ifndef TEST_HAS_NO_EXCEPTIONS
else
{
assert(n > s.max_size());
try
{
s.resize(n, c);
assert(false);
}
catch (std::length_error&)
{
assert(n > s.max_size());
}
}
#endif
}

int main()
Expand Down
58 changes: 45 additions & 13 deletions libcxx/test/std/strings/basic.string/string.cons/substr.pass.cpp
Expand Up @@ -7,7 +7,6 @@
//
//===----------------------------------------------------------------------===//

// XFAIL: libcpp-no-exceptions
// <string>

// basic_string(const basic_string<charT,traits,Allocator>& str,
Expand Down Expand Up @@ -35,21 +34,31 @@ test(S str, unsigned pos)
{
typedef typename S::traits_type T;
typedef typename S::allocator_type A;
try

if (pos <= str.size())
{
S s2(str, pos);
LIBCPP_ASSERT(s2.__invariants());
assert(pos <= str.size());
unsigned rlen = str.size() - pos;
assert(s2.size() == rlen);
assert(T::compare(s2.data(), str.data() + pos, rlen) == 0);
assert(s2.get_allocator() == A());
assert(s2.capacity() >= s2.size());
}
catch (std::out_of_range&)
#ifndef TEST_HAS_NO_EXCEPTIONS
else
{
assert(pos > str.size());
try
{
S s2(str, pos);
assert(false);
}
catch (std::out_of_range&)
{
assert(pos > str.size());
}
}
#endif
}

template <class S>
Expand All @@ -58,21 +67,30 @@ test(S str, unsigned pos, unsigned n)
{
typedef typename S::traits_type T;
typedef typename S::allocator_type A;
try
if (pos <= str.size())
{
S s2(str, pos, n);
LIBCPP_ASSERT(s2.__invariants());
assert(pos <= str.size());
unsigned rlen = std::min<unsigned>(str.size() - pos, n);
assert(s2.size() == rlen);
assert(T::compare(s2.data(), str.data() + pos, rlen) == 0);
assert(s2.get_allocator() == A());
assert(s2.capacity() >= s2.size());
}
catch (std::out_of_range&)
#ifndef TEST_HAS_NO_EXCEPTIONS
else
{
assert(pos > str.size());
try
{
S s2(str, pos, n);
assert(false);
}
catch (std::out_of_range&)
{
assert(pos > str.size());
}
}
#endif
}

template <class S>
Expand All @@ -81,24 +99,35 @@ test(S str, unsigned pos, unsigned n, const typename S::allocator_type& a)
{
typedef typename S::traits_type T;
typedef typename S::allocator_type A;
try

if (pos <= str.size())
{
S s2(str, pos, n, a);
LIBCPP_ASSERT(s2.__invariants());
assert(pos <= str.size());
unsigned rlen = std::min<unsigned>(str.size() - pos, n);
assert(s2.size() == rlen);
assert(T::compare(s2.data(), str.data() + pos, rlen) == 0);
assert(s2.get_allocator() == a);
assert(s2.capacity() >= s2.size());
}
catch (std::out_of_range&)
#ifndef TEST_HAS_NO_EXCEPTIONS
else
{
assert(pos > str.size());
try
{
S s2(str, pos, n, a);
assert(false);
}
catch (std::out_of_range&)
{
assert(pos > str.size());
}
}
#endif
}

#if TEST_STD_VER >= 11
#ifndef TEST_HAS_NO_EXCEPTIONS
void test2583()
{ // LWG #2583
typedef std::basic_string<char, std::char_traits<char>, test_allocator<char> > StringA;
Expand All @@ -111,6 +140,7 @@ void test2583()
assert(false);
}
#endif
#endif

int main()
{
Expand Down Expand Up @@ -192,6 +222,8 @@ int main()
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 100, A());
}

#ifndef TEST_HAS_NO_EXCEPTIONS
test2583();
#endif
#endif
}
Expand Up @@ -7,7 +7,6 @@
//
//===----------------------------------------------------------------------===//

// XFAIL: libcpp-no-exceptions
// <string>

// template <class T>
Expand All @@ -25,34 +24,52 @@ template <class S, class SV>
void
test(S s, SV sv, typename S::size_type pos, typename S::size_type n, S expected)
{
try
if (pos <= sv.size())
{
s.append(sv, pos, n);
LIBCPP_ASSERT(s.__invariants());
assert(pos <= sv.size());
assert(s == expected);
}
catch (std::out_of_range&)
#ifndef TEST_HAS_NO_EXCEPTIONS
else
{
assert(pos > sv.size());
try
{
s.append(sv, pos, n);
assert(false);
}
catch (std::out_of_range&)
{
assert(pos > sv.size());
}
}
#endif
}

template <class S, class SV>
void
test_npos(S s, SV sv, typename S::size_type pos, S expected)
{
try
if (pos <= sv.size())
{
s.append(sv, pos);
LIBCPP_ASSERT(s.__invariants());
assert(pos <= sv.size());
assert(s == expected);
}
catch (std::out_of_range&)
#ifndef TEST_HAS_NO_EXCEPTIONS
else
{
assert(pos > sv.size());
try
{
s.append(sv, pos);
assert(false);
}
catch (std::out_of_range&)
{
assert(pos > sv.size());
}
}
#endif
}

int main()
Expand Down

0 comments on commit 8a915ed

Please sign in to comment.