From 8a915ed644b910ae9d7d2aef7ad491791fbd4944 Mon Sep 17 00:00:00 2001 From: Roger Ferrer Ibanez Date: Tue, 1 Nov 2016 15:46:16 +0000 Subject: Protect exceptional paths under libcpp-no-exceptions 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 --- .../string_insert/size_T_size_size.pass.cpp | 43 +++++++++++++++------- .../string_insert/size_pointer.pass.cpp | 22 +++++++---- .../string_insert/size_pointer_size.pass.cpp | 22 +++++++---- .../string_insert/size_size_char.pass.cpp | 22 +++++++---- .../string_insert/size_string.pass.cpp | 22 +++++++---- .../string_insert/size_string_size_size.pass.cpp | 43 +++++++++++++++------- 6 files changed, 120 insertions(+), 54 deletions(-) (limited to 'libcxx/test/std/strings/basic.string/string.modifiers/string_insert') diff --git a/libcxx/test/std/strings/basic.string/string.modifiers/string_insert/size_T_size_size.pass.cpp b/libcxx/test/std/strings/basic.string/string.modifiers/string_insert/size_T_size_size.pass.cpp index 6c49ea447f8..9a25bc9c729 100644 --- a/libcxx/test/std/strings/basic.string/string.modifiers/string_insert/size_T_size_size.pass.cpp +++ b/libcxx/test/std/strings/basic.string/string.modifiers/string_insert/size_T_size_size.pass.cpp @@ -7,7 +7,6 @@ // //===----------------------------------------------------------------------===// -// XFAIL: libcpp-no-exceptions // // template @@ -28,20 +27,29 @@ test(S s, typename S::size_type pos1, SV sv, typename S::size_type pos2, typename S::size_type n, S expected) { static_assert((!std::is_same::value), ""); - typename S::size_type old_size = s.size(); + const typename S::size_type old_size = s.size(); S s0 = s; - try + if (pos1 <= old_size && pos2 <= sv.size()) { s.insert(pos1, sv, pos2, n); LIBCPP_ASSERT(s.__invariants()); - assert(pos1 <= old_size && pos2 <= sv.size()); assert(s == expected); } - catch (std::out_of_range&) +#ifndef TEST_HAS_NO_EXCEPTIONS + else { - assert(pos1 > old_size || pos2 > sv.size()); - assert(s == s0); + try + { + s.insert(pos1, sv, pos2, n); + assert(false); + } + catch (std::out_of_range&) + { + assert(pos1 > old_size || pos2 > sv.size()); + assert(s == s0); + } } +#endif } template @@ -49,20 +57,29 @@ void test_npos(S s, typename S::size_type pos1, SV sv, typename S::size_type pos2, S expected) { static_assert((!std::is_same::value), ""); - typename S::size_type old_size = s.size(); + const typename S::size_type old_size = s.size(); S s0 = s; - try + if (pos1 <= old_size && pos2 <= sv.size()) { s.insert(pos1, sv, pos2); LIBCPP_ASSERT(s.__invariants()); - assert(pos1 <= old_size && pos2 <= sv.size()); assert(s == expected); } - catch (std::out_of_range&) +#ifndef TEST_HAS_NO_EXCEPTIONS + else { - assert(pos1 > old_size || pos2 > sv.size()); - assert(s == s0); + try + { + s.insert(pos1, sv, pos2); + assert(false); + } + catch (std::out_of_range&) + { + assert(pos1 > old_size || pos2 > sv.size()); + assert(s == s0); + } } +#endif } diff --git a/libcxx/test/std/strings/basic.string/string.modifiers/string_insert/size_pointer.pass.cpp b/libcxx/test/std/strings/basic.string/string.modifiers/string_insert/size_pointer.pass.cpp index 3b1c2cbd727..e734ffafa84 100644 --- a/libcxx/test/std/strings/basic.string/string.modifiers/string_insert/size_pointer.pass.cpp +++ b/libcxx/test/std/strings/basic.string/string.modifiers/string_insert/size_pointer.pass.cpp @@ -7,7 +7,6 @@ // //===----------------------------------------------------------------------===// -// XFAIL: libcpp-no-exceptions // // basic_string& @@ -24,20 +23,29 @@ template void test(S s, typename S::size_type pos, const typename S::value_type* str, S expected) { - typename S::size_type old_size = s.size(); + const typename S::size_type old_size = s.size(); S s0 = s; - try + if (pos <= old_size) { s.insert(pos, str); LIBCPP_ASSERT(s.__invariants()); - assert(pos <= old_size); assert(s == expected); } - catch (std::out_of_range&) +#ifndef TEST_HAS_NO_EXCEPTIONS + else { - assert(pos > old_size); - assert(s == s0); + try + { + s.insert(pos, str); + assert(false); + } + catch (std::out_of_range&) + { + assert(pos > old_size); + assert(s == s0); + } } +#endif } int main() diff --git a/libcxx/test/std/strings/basic.string/string.modifiers/string_insert/size_pointer_size.pass.cpp b/libcxx/test/std/strings/basic.string/string.modifiers/string_insert/size_pointer_size.pass.cpp index 554b93cdce1..b5998437425 100644 --- a/libcxx/test/std/strings/basic.string/string.modifiers/string_insert/size_pointer_size.pass.cpp +++ b/libcxx/test/std/strings/basic.string/string.modifiers/string_insert/size_pointer_size.pass.cpp @@ -7,7 +7,6 @@ // //===----------------------------------------------------------------------===// -// XFAIL: libcpp-no-exceptions // // basic_string& @@ -25,20 +24,29 @@ void test(S s, typename S::size_type pos, const typename S::value_type* str, typename S::size_type n, S expected) { - typename S::size_type old_size = s.size(); + const typename S::size_type old_size = s.size(); S s0 = s; - try + if (pos <= old_size) { s.insert(pos, str, n); LIBCPP_ASSERT(s.__invariants()); - assert(pos <= old_size); assert(s == expected); } - catch (std::out_of_range&) +#ifndef TEST_HAS_NO_EXCEPTIONS + else { - assert(pos > old_size); - assert(s == s0); + try + { + s.insert(pos, str, n); + assert(false); + } + catch (std::out_of_range&) + { + assert(pos > old_size); + assert(s == s0); + } } +#endif } int main() diff --git a/libcxx/test/std/strings/basic.string/string.modifiers/string_insert/size_size_char.pass.cpp b/libcxx/test/std/strings/basic.string/string.modifiers/string_insert/size_size_char.pass.cpp index 04ea1d37694..a769604c6ff 100644 --- a/libcxx/test/std/strings/basic.string/string.modifiers/string_insert/size_size_char.pass.cpp +++ b/libcxx/test/std/strings/basic.string/string.modifiers/string_insert/size_size_char.pass.cpp @@ -7,7 +7,6 @@ // //===----------------------------------------------------------------------===// -// XFAIL: libcpp-no-exceptions // // basic_string& @@ -25,20 +24,29 @@ void test(S s, typename S::size_type pos, typename S::size_type n, typename S::value_type str, S expected) { - typename S::size_type old_size = s.size(); + const typename S::size_type old_size = s.size(); S s0 = s; - try + if (pos <= old_size) { s.insert(pos, n, str); LIBCPP_ASSERT(s.__invariants()); - assert(pos <= old_size); assert(s == expected); } - catch (std::out_of_range&) +#ifndef TEST_HAS_NO_EXCEPTIONS + else { - assert(pos > old_size); - assert(s == s0); + try + { + s.insert(pos, n, str); + assert(false); + } + catch (std::out_of_range&) + { + assert(pos > old_size); + assert(s == s0); + } } +#endif } int main() diff --git a/libcxx/test/std/strings/basic.string/string.modifiers/string_insert/size_string.pass.cpp b/libcxx/test/std/strings/basic.string/string.modifiers/string_insert/size_string.pass.cpp index 1945871b99a..e7c5ecdc9c1 100644 --- a/libcxx/test/std/strings/basic.string/string.modifiers/string_insert/size_string.pass.cpp +++ b/libcxx/test/std/strings/basic.string/string.modifiers/string_insert/size_string.pass.cpp @@ -7,7 +7,6 @@ // //===----------------------------------------------------------------------===// -// XFAIL: libcpp-no-exceptions // // basic_string& @@ -24,20 +23,29 @@ template void test(S s, typename S::size_type pos, S str, S expected) { - typename S::size_type old_size = s.size(); + const typename S::size_type old_size = s.size(); S s0 = s; - try + if (pos <= old_size) { s.insert(pos, str); LIBCPP_ASSERT(s.__invariants()); - assert(pos <= old_size); assert(s == expected); } - catch (std::out_of_range&) +#ifndef TEST_HAS_NO_EXCEPTIONS + else { - assert(pos > old_size); - assert(s == s0); + try + { + s.insert(pos, str); + assert(false); + } + catch (std::out_of_range&) + { + assert(pos > old_size); + assert(s == s0); + } } +#endif } int main() diff --git a/libcxx/test/std/strings/basic.string/string.modifiers/string_insert/size_string_size_size.pass.cpp b/libcxx/test/std/strings/basic.string/string.modifiers/string_insert/size_string_size_size.pass.cpp index 32485db478d..67ba7ec108f 100644 --- a/libcxx/test/std/strings/basic.string/string.modifiers/string_insert/size_string_size_size.pass.cpp +++ b/libcxx/test/std/strings/basic.string/string.modifiers/string_insert/size_string_size_size.pass.cpp @@ -7,7 +7,6 @@ // //===----------------------------------------------------------------------===// -// XFAIL: libcpp-no-exceptions // // basic_string& @@ -27,40 +26,58 @@ void test(S s, typename S::size_type pos1, S str, typename S::size_type pos2, typename S::size_type n, S expected) { - typename S::size_type old_size = s.size(); + const typename S::size_type old_size = s.size(); S s0 = s; - try + if (pos1 <= old_size && pos2 <= str.size()) { s.insert(pos1, str, pos2, n); LIBCPP_ASSERT(s.__invariants()); - assert(pos1 <= old_size && pos2 <= str.size()); assert(s == expected); } - catch (std::out_of_range&) +#ifndef TEST_HAS_NO_EXCEPTIONS + else { - assert(pos1 > old_size || pos2 > str.size()); - assert(s == s0); + try + { + s.insert(pos1, str, pos2, n); + assert(false); + } + catch (std::out_of_range&) + { + assert(pos1 > old_size || pos2 > str.size()); + assert(s == s0); + } } +#endif } template void test_npos(S s, typename S::size_type pos1, S str, typename S::size_type pos2, S expected) { - typename S::size_type old_size = s.size(); + const typename S::size_type old_size = s.size(); S s0 = s; - try + if (pos1 <= old_size && pos2 <= str.size()) { s.insert(pos1, str, pos2); LIBCPP_ASSERT(s.__invariants()); - assert(pos1 <= old_size && pos2 <= str.size()); assert(s == expected); } - catch (std::out_of_range&) +#ifndef TEST_HAS_NO_EXCEPTIONS + else { - assert(pos1 > old_size || pos2 > str.size()); - assert(s == s0); + try + { + s.insert(pos1, str, pos2); + assert(false); + } + catch (std::out_of_range&) + { + assert(pos1 > old_size || pos2 > str.size()); + assert(s == s0); + } } +#endif } -- cgit v1.2.3