Skip to content

Commit bfe0968

Browse files
authored
[NFC][libc++][exceptions] Adds tests for LWG3112. (#100881)
The tests kept being based on std::string instead of std::string_view to allow testing with older C++ dialects. Adds tests for: - LWG3112 system_error and filesystem_error constructors taking a string may not be able to meet their postconditions
1 parent 5c48f6f commit bfe0968

File tree

4 files changed

+107
-42
lines changed

4 files changed

+107
-42
lines changed

libcxx/docs/Status/Cxx20Issues.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@
134134
"`LWG3077 <https://wg21.link/LWG3077>`__","``(push|emplace)_back``\ should invalidate the ``end``\ iterator","Kona","|Nothing To Do|","",""
135135
"`LWG3087 <https://wg21.link/LWG3087>`__","One final ``&x``\ in |sect|\ [list.ops]","Kona","|Nothing To Do|","",""
136136
"`LWG3101 <https://wg21.link/LWG3101>`__","``span``\ 's ``Container``\ constructors need another constraint","Kona","|Complete|","",""
137-
"`LWG3112 <https://wg21.link/LWG3112>`__","``system_error``\ and ``filesystem_error``\ constructors taking a ``string``\ may not be able to meet their postconditions","Kona","","",""
137+
"`LWG3112 <https://wg21.link/LWG3112>`__","``system_error``\ and ``filesystem_error``\ constructors taking a ``string``\ may not be able to meet their postconditions","Kona","|Nothing To Do|","",""
138138
"`LWG3119 <https://wg21.link/LWG3119>`__","Program-definedness of closure types","Kona","|Nothing To Do|","",""
139139
"`LWG3133 <https://wg21.link/LWG3133>`__","Modernizing numeric type requirements","Kona","","",""
140140
"`LWG3144 <https://wg21.link/LWG3144>`__","``span``\ does not have a ``const_pointer``\ typedef","Kona","|Complete|","",""

libcxx/test/std/diagnostics/syserr/syserr.syserr/syserr.syserr.members/ctor_error_code_string.pass.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,25 @@
2121
#include "test_macros.h"
2222

2323
int main(int, char**) {
24+
{
2425
std::string what_arg("test message");
2526
std::system_error se(make_error_code(std::errc::not_a_directory), what_arg);
2627
assert(se.code() == std::make_error_code(std::errc::not_a_directory));
2728
std::string what_message(se.what());
2829
assert(what_message.find(what_arg) != std::string::npos);
30+
assert(what_message.find(what_arg.c_str()) != std::string::npos);
2931
assert(what_message.find("Not a directory") != std::string::npos);
32+
}
3033

31-
return 0;
34+
{
35+
std::string what_arg("test LWG3112 with and With embedded \0 character");
36+
std::system_error se(make_error_code(std::errc::not_a_directory), what_arg);
37+
assert(se.code() == std::make_error_code(std::errc::not_a_directory));
38+
std::string what_message(se.what());
39+
assert(what_message.find(what_arg) != std::string::npos);
40+
assert(what_message.find(what_arg.c_str()) != std::string::npos);
41+
assert(what_message.find("Not a directory") != std::string::npos);
42+
}
43+
44+
return 0;
3245
}

libcxx/test/std/diagnostics/syserr/syserr.syserr/syserr.syserr.members/ctor_int_error_category_string.pass.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,23 @@
2121
#include "test_macros.h"
2222

2323
int main(int, char**) {
24+
{
2425
std::string what_arg("test message");
25-
std::system_error se(static_cast<int>(std::errc::not_a_directory),
26-
std::generic_category(), what_arg);
26+
std::system_error se(static_cast<int>(std::errc::not_a_directory), std::generic_category(), what_arg);
2727
assert(se.code() == std::make_error_code(std::errc::not_a_directory));
2828
std::string what_message(se.what());
2929
assert(what_message.find(what_arg) != std::string::npos);
30+
assert(what_message.find(what_arg.c_str()) != std::string::npos);
3031
assert(what_message.find("Not a directory") != std::string::npos);
31-
32-
return 0;
32+
}
33+
{
34+
std::string what_arg("test LWG3112 with and With embedded \0 character");
35+
std::system_error se(static_cast<int>(std::errc::not_a_directory), std::generic_category(), what_arg);
36+
assert(se.code() == std::make_error_code(std::errc::not_a_directory));
37+
std::string what_message(se.what());
38+
assert(what_message.find(what_arg) != std::string::npos);
39+
assert(what_message.find(what_arg.c_str()) != std::string::npos);
40+
assert(what_message.find("Not a directory") != std::string::npos);
41+
}
42+
return 0;
3343
}

libcxx/test/std/input.output/filesystems/class.filesystem_error/filesystem_error.members.pass.cpp

Lines changed: 78 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -33,49 +33,91 @@ namespace fs = std::filesystem;
3333
void test_constructors() {
3434
using namespace fs;
3535

36-
// The string returned by "filesystem_error::what() must contain runtime_error::what()
37-
const std::string what_arg = "Hello World";
38-
const std::string what_contains = std::runtime_error(what_arg).what();
39-
assert(what_contains.find(what_arg) != std::string::npos);
40-
auto CheckWhat = [what_contains](filesystem_error const& e) {
41-
std::string s = e.what();
42-
assert(s.find(what_contains) != std::string::npos);
43-
};
36+
{
37+
// The string returned by "filesystem_error::what() must contain runtime_error::what().c_str()
38+
const std::string what_arg = "Hello World";
39+
const std::string what_contains = std::runtime_error(what_arg).what();
40+
assert(what_contains.find(what_arg) != std::string::npos);
41+
auto CheckWhat = [what_contains](filesystem_error const& e) {
42+
std::string s = e.what();
43+
assert(s.find(what_contains.c_str()) != std::string::npos);
44+
};
4445

45-
std::error_code ec = std::make_error_code(std::errc::file_exists);
46-
const path p1("foo");
47-
const path p2("bar");
46+
std::error_code ec = std::make_error_code(std::errc::file_exists);
47+
const path p1("foo");
48+
const path p2("bar");
4849

49-
// filesystem_error(const string& what_arg, error_code ec);
50-
{
51-
ASSERT_NOT_NOEXCEPT(filesystem_error(what_arg, ec));
52-
filesystem_error e(what_arg, ec);
53-
CheckWhat(e);
54-
assert(e.code() == ec);
55-
assert(e.path1().empty() && e.path2().empty());
56-
}
57-
// filesystem_error(const string& what_arg, const path&, error_code ec);
58-
{
59-
ASSERT_NOT_NOEXCEPT(filesystem_error(what_arg, p1, ec));
60-
filesystem_error e(what_arg, p1, ec);
61-
CheckWhat(e);
62-
assert(e.code() == ec);
63-
assert(e.path1() == p1);
64-
assert(e.path2().empty());
50+
// filesystem_error(const string& what_arg, error_code ec);
51+
{
52+
ASSERT_NOT_NOEXCEPT(filesystem_error(what_arg, ec));
53+
filesystem_error e(what_arg, ec);
54+
CheckWhat(e);
55+
assert(e.code() == ec);
56+
assert(e.path1().empty() && e.path2().empty());
57+
}
58+
// filesystem_error(const string& what_arg, const path&, error_code ec);
59+
{
60+
ASSERT_NOT_NOEXCEPT(filesystem_error(what_arg, p1, ec));
61+
filesystem_error e(what_arg, p1, ec);
62+
CheckWhat(e);
63+
assert(e.code() == ec);
64+
assert(e.path1() == p1);
65+
assert(e.path2().empty());
66+
}
67+
// filesystem_error(const string& what_arg, const path&, const path&, error_code ec);
68+
{
69+
ASSERT_NOT_NOEXCEPT(filesystem_error(what_arg, p1, p2, ec));
70+
filesystem_error e(what_arg, p1, p2, ec);
71+
CheckWhat(e);
72+
assert(e.code() == ec);
73+
assert(e.path1() == p1);
74+
assert(e.path2() == p2);
75+
}
6576
}
66-
// filesystem_error(const string& what_arg, const path&, const path&, error_code ec);
6777
{
68-
ASSERT_NOT_NOEXCEPT(filesystem_error(what_arg, p1, p2, ec));
69-
filesystem_error e(what_arg, p1, p2, ec);
70-
CheckWhat(e);
71-
assert(e.code() == ec);
72-
assert(e.path1() == p1);
73-
assert(e.path2() == p2);
78+
// The string returned by "filesystem_error::what() must contain runtime_error::what().c_str()
79+
const std::string what_arg("test LWG3112 with and With embedded \0 character");
80+
const std::string what_contains = std::runtime_error(what_arg).what();
81+
assert(what_contains.find(what_arg) != std::string::npos);
82+
auto CheckWhat = [what_contains](filesystem_error const& e) {
83+
std::string s = e.what();
84+
assert(s.find(what_contains.c_str()) != std::string::npos);
85+
};
86+
87+
std::error_code ec = std::make_error_code(std::errc::file_exists);
88+
const path p1("foo");
89+
const path p2("bar");
90+
91+
// filesystem_error(const string& what_arg, error_code ec);
92+
{
93+
ASSERT_NOT_NOEXCEPT(filesystem_error(what_arg, ec));
94+
filesystem_error e(what_arg, ec);
95+
CheckWhat(e);
96+
assert(e.code() == ec);
97+
assert(e.path1().empty() && e.path2().empty());
98+
}
99+
// filesystem_error(const string& what_arg, const path&, error_code ec);
100+
{
101+
ASSERT_NOT_NOEXCEPT(filesystem_error(what_arg, p1, ec));
102+
filesystem_error e(what_arg, p1, ec);
103+
CheckWhat(e);
104+
assert(e.code() == ec);
105+
assert(e.path1() == p1);
106+
assert(e.path2().empty());
107+
}
108+
// filesystem_error(const string& what_arg, const path&, const path&, error_code ec);
109+
{
110+
ASSERT_NOT_NOEXCEPT(filesystem_error(what_arg, p1, p2, ec));
111+
filesystem_error e(what_arg, p1, p2, ec);
112+
CheckWhat(e);
113+
assert(e.code() == ec);
114+
assert(e.path1() == p1);
115+
assert(e.path2() == p2);
116+
}
74117
}
75118
}
76119

77-
void test_signatures()
78-
{
120+
void test_signatures() {
79121
using namespace fs;
80122
const path p;
81123
std::error_code ec;

0 commit comments

Comments
 (0)