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
diff --git a/libcxx/test/std/strings/basic.string/string.cons/substr.pass.cpp b/libcxx/test/std/strings/basic.string/string.cons/substr.pass.cpp
index a10239b..381ed8b 100644
--- a/libcxx/test/std/strings/basic.string/string.cons/substr.pass.cpp
+++ b/libcxx/test/std/strings/basic.string/string.cons/substr.pass.cpp
@@ -7,7 +7,6 @@
 //
 //===----------------------------------------------------------------------===//
 
-// XFAIL: libcpp-no-exceptions
 // <string>
 
 // basic_string(const basic_string<charT,traits,Allocator>& str,
@@ -35,21 +34,31 @@
 {
     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>
@@ -58,21 +67,30 @@
 {
     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>
@@ -81,24 +99,35 @@
 {
     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;
@@ -111,6 +140,7 @@
     assert(false);
 }
 #endif
+#endif
 
 int main()
 {
@@ -192,6 +222,8 @@
     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 100, A());
     }
 
+#ifndef TEST_HAS_NO_EXCEPTIONS
     test2583();
 #endif
+#endif
 }