Skip to content

[libc++][test] Use LIBCPP_ASSERT in some system_category-related tests #78834

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,88 +22,87 @@

#include "test_macros.h"

int main(int, char**)
{
std::error_code e_code1(5, std::generic_category());
std::error_code e_code2(5, std::system_category());
std::error_code e_code3(6, std::generic_category());
std::error_code e_code4(6, std::system_category());
std::error_condition e_condition1(5, std::generic_category());
std::error_condition e_condition2(5, std::system_category());
std::error_condition e_condition3(6, std::generic_category());
std::error_condition e_condition4(6, std::system_category());
int main(int, char**) {
std::error_code e_code1(5, std::generic_category());
std::error_code e_code2(5, std::system_category());
std::error_code e_code3(6, std::generic_category());
std::error_code e_code4(6, std::system_category());
std::error_condition e_condition1(5, std::generic_category());
std::error_condition e_condition2(5, std::system_category());
std::error_condition e_condition3(6, std::generic_category());
std::error_condition e_condition4(6, std::system_category());

assert(e_code1 == e_code1);
assert(e_code1 != e_code2);
assert(e_code1 != e_code3);
assert(e_code1 != e_code4);
assert(e_code1 == e_condition1);
assert(e_code1 != e_condition2);
assert(e_code1 != e_condition3);
assert(e_code1 != e_condition4);
assert(e_code1 == e_code1);
assert(e_code1 != e_code2);
assert(e_code1 != e_code3);
assert(e_code1 != e_code4);
assert(e_code1 == e_condition1);
assert(e_code1 != e_condition2);
assert(e_code1 != e_condition3);
assert(e_code1 != e_condition4);

assert(e_code2 != e_code1);
assert(e_code2 == e_code2);
assert(e_code2 != e_code3);
assert(e_code2 != e_code4);
assert(e_code2 == e_condition1); // ?
assert(e_code2 == e_condition2);
assert(e_code2 != e_condition3);
assert(e_code2 != e_condition4);
assert(e_code2 != e_code1);
assert(e_code2 == e_code2);
assert(e_code2 != e_code3);
assert(e_code2 != e_code4);
LIBCPP_ASSERT(e_code2 == e_condition1);
assert(e_code2 == e_condition2);
LIBCPP_ASSERT(e_code2 != e_condition3);
assert(e_code2 != e_condition4);

assert(e_code3 != e_code1);
assert(e_code3 != e_code2);
assert(e_code3 == e_code3);
assert(e_code3 != e_code4);
assert(e_code3 != e_condition1);
assert(e_code3 != e_condition2);
assert(e_code3 == e_condition3);
assert(e_code3 != e_condition4);
assert(e_code3 != e_code1);
assert(e_code3 != e_code2);
assert(e_code3 == e_code3);
assert(e_code3 != e_code4);
assert(e_code3 != e_condition1);
assert(e_code3 != e_condition2);
assert(e_code3 == e_condition3);
assert(e_code3 != e_condition4);

assert(e_code4 != e_code1);
assert(e_code4 != e_code2);
assert(e_code4 != e_code3);
assert(e_code4 == e_code4);
assert(e_code4 != e_condition1);
assert(e_code4 != e_condition2);
assert(e_code4 == e_condition3); // ?
assert(e_code4 == e_condition4);
assert(e_code4 != e_code1);
assert(e_code4 != e_code2);
assert(e_code4 != e_code3);
assert(e_code4 == e_code4);
LIBCPP_ASSERT(e_code4 != e_condition1);
assert(e_code4 != e_condition2);
LIBCPP_ASSERT(e_code4 == e_condition3);
assert(e_code4 == e_condition4);

assert(e_condition1 == e_code1);
assert(e_condition1 == e_code2); // ?
assert(e_condition1 != e_code3);
assert(e_condition1 != e_code4);
assert(e_condition1 == e_condition1);
assert(e_condition1 != e_condition2);
assert(e_condition1 != e_condition3);
assert(e_condition1 != e_condition4);
assert(e_condition1 == e_code1);
LIBCPP_ASSERT(e_condition1 == e_code2);
assert(e_condition1 != e_code3);
LIBCPP_ASSERT(e_condition1 != e_code4);
assert(e_condition1 == e_condition1);
assert(e_condition1 != e_condition2);
assert(e_condition1 != e_condition3);
assert(e_condition1 != e_condition4);

assert(e_condition2 != e_code1);
assert(e_condition2 == e_code2);
assert(e_condition2 != e_code3);
assert(e_condition2 != e_code4);
assert(e_condition2 != e_condition1);
assert(e_condition2 == e_condition2);
assert(e_condition2 != e_condition3);
assert(e_condition2 != e_condition4);
assert(e_condition2 != e_code1);
assert(e_condition2 == e_code2);
assert(e_condition2 != e_code3);
assert(e_condition2 != e_code4);
assert(e_condition2 != e_condition1);
assert(e_condition2 == e_condition2);
assert(e_condition2 != e_condition3);
assert(e_condition2 != e_condition4);

assert(e_condition3 != e_code1);
assert(e_condition3 != e_code2);
assert(e_condition3 == e_code3);
assert(e_condition3 == e_code4); // ?
assert(e_condition3 != e_condition1);
assert(e_condition3 != e_condition2);
assert(e_condition3 == e_condition3);
assert(e_condition3 != e_condition4);
assert(e_condition3 != e_code1);
LIBCPP_ASSERT(e_condition3 != e_code2);
assert(e_condition3 == e_code3);
LIBCPP_ASSERT(e_condition3 == e_code4);
assert(e_condition3 != e_condition1);
assert(e_condition3 != e_condition2);
assert(e_condition3 == e_condition3);
assert(e_condition3 != e_condition4);

assert(e_condition4 != e_code1);
assert(e_condition4 != e_code2);
assert(e_condition4 != e_code3);
assert(e_condition4 == e_code4);
assert(e_condition4 != e_condition1);
assert(e_condition4 != e_condition2);
assert(e_condition4 != e_condition3);
assert(e_condition4 == e_condition4);
assert(e_condition4 != e_code1);
assert(e_condition4 != e_code2);
assert(e_condition4 != e_code3);
assert(e_condition4 == e_code4);
assert(e_condition4 != e_condition1);
assert(e_condition4 != e_condition2);
assert(e_condition4 != e_condition3);
assert(e_condition4 == e_condition4);

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,17 @@

#include "test_macros.h"

int main(int, char**)
{
const std::error_category& e_cat1 = std::generic_category();
const std::error_category& e_cat2 = std::system_category();
std::string m1 = e_cat1.message(5);
std::string m2 = e_cat2.message(5);
std::string m3 = e_cat2.message(6);
assert(!m1.empty());
assert(!m2.empty());
assert(!m3.empty());
assert(m1 == m2);
assert(m1 != m3);
int main(int, char**) {
const std::error_category& e_cat1 = std::generic_category();
const std::error_category& e_cat2 = std::system_category();
std::string m1 = e_cat1.message(5);
std::string m2 = e_cat2.message(5);
std::string m3 = e_cat2.message(6);
assert(!m1.empty());
assert(!m2.empty());
assert(!m3.empty());
LIBCPP_ASSERT(m1 == m2);
assert(m1 != m3);

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,47 +23,49 @@

// See https://llvm.org/D65667
struct StaticInit {
const std::error_category* ec;
~StaticInit() {
std::string str = ec->name();
assert(str == "system") ;
}
const std::error_category* ec;
~StaticInit() {
std::string str = ec->name();
assert(str == "system");
}
};
static StaticInit foo;

int main(int, char**)
{
{
const std::error_category& e_cat1 = std::system_category();
std::error_condition e_cond = e_cat1.default_error_condition(5);
assert(e_cond.value() == 5);
assert(e_cond.category() == std::generic_category());
e_cond = e_cat1.default_error_condition(5000);
assert(e_cond.value() == 5000);
assert(e_cond.category() == std::system_category());
}
int main(int, char**) {
{
const std::error_category& e_cat1 = std::system_category();
std::error_condition e_cond = e_cat1.default_error_condition(5);
LIBCPP_ASSERT(e_cond.value() == 5);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this fascinating. Thank you for doing these checks. Does it seem like we should rework this test so that we check whether the result of

e_cat1.default_error_condition(e)

,where e corresponds to a POSIX errno value), compares equal to

error_condition(e, generic_category())

as required by the standard?

LIBCPP_ASSERT(e_cond.category() == std::generic_category());
assert(e_cat1.equivalent(5, e_cond));

// Test the result of message(int cond) when given a bad error condition
{
errno = E2BIG; // something that message will never generate
const std::error_category& e_cat1 = std::system_category();
const std::string msg = e_cat1.message(-1);
// Exact message format varies by platform.
e_cond = e_cat1.default_error_condition(5000);
LIBCPP_ASSERT(e_cond.value() == 5000);
LIBCPP_ASSERT(e_cond.category() == std::system_category());
assert(e_cat1.equivalent(5000, e_cond));
}

// Test the result of message(int cond) when given a bad error condition
{
errno = E2BIG; // something that message will never generate
const std::error_category& e_cat1 = std::system_category();
const std::string msg = e_cat1.message(-1);
// Exact message format varies by platform.
#if defined(_AIX)
LIBCPP_ASSERT(msg.rfind("Error -1 occurred", 0) == 0);
LIBCPP_ASSERT(msg.rfind("Error -1 occurred", 0) == 0);
#elif defined(_NEWLIB_VERSION)
LIBCPP_ASSERT(msg.empty());
LIBCPP_ASSERT(msg.empty());
#else
LIBCPP_ASSERT(msg.rfind("Unknown error", 0) == 0);
LIBCPP_ASSERT(msg.rfind("Unknown error", 0) == 0);
#endif
assert(errno == E2BIG);
}
assert(errno == E2BIG);
}

{
foo.ec = &std::system_category();
std::string m = foo.ec->name();
assert(m == "system");
}
{
foo.ec = &std::system_category();
std::string m = foo.ec->name();
assert(m == "system");
}

return 0;
return 0;
}