Debian Codegraph @

libstdc++-v3/testsuite/21_strings/basic_string/operations/compare/wchar_t/123991.cc

raw
1 // { dg-do run { target c++17 } }
2
3 // Bug 123991 - std::string::compare crashes instead of throwing
4
5 #include <string>
6 #include <string_view>
7 #include <stdexcept>
8 #include <testsuite_hooks.h>
9
10 void
11 test_compare_3arg()
12 {
13 std::wstring_view sv;
14 std::wstring s;
15 static_assert( ! noexcept(s.compare(0, 0, sv)) );
16 #ifdef __cpp_exceptions
17 try
18 {
19 (void) s.compare(1, 0, sv);
20 VERIFY(false);
21 }
22 catch (const std::out_of_range&)
23 { }
24 #endif
25 }
26
27 void
28 test_compare_5arg()
29 {
30 std::wstring_view sv;
31 std::wstring s;
32 static_assert( ! noexcept(s.compare(0, 0, sv, 0, 0)) );
33 #ifdef __cpp_exceptions
34 try
35 {
36 (void) s.compare(1, 0, sv, 0, 0);
37 VERIFY(false);
38 }
39 catch (const std::out_of_range&)
40 { }
41
42 try
43 {
44 (void) s.compare(0, 0, sv, 1, 0);
45 VERIFY(false);
46 }
47 catch (const std::out_of_range&)
48 { }
49 #endif
50 }
51
52 int main()
53 {
54 test_compare_3arg();
55 test_compare_5arg();
56 }