1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// <system_error>
10
11// class error_category
12
13// const error_category& system_category();
14
15// XFAIL: stdlib=apple-libc++ && target={{.+}}-apple-macosx10.{{9|10|11|12}}
16
17#include <system_error>
18#include <cassert>
19#include <string>
20#include <cerrno>
21
22#include "test_macros.h"
23
24// See https://llvm.org/D65667
25struct StaticInit {
26 const std::error_category* ec;
27 ~StaticInit() {
28 std::string str = ec->name();
29 assert(str == "system");
30 }
31};
32static StaticInit foo;
33
34int main(int, char**) {
35 {
36 const std::error_category& e_cat1 = std::system_category();
37 std::error_condition e_cond = e_cat1.default_error_condition(i: 5);
38 LIBCPP_ASSERT(e_cond.value() == 5);
39 LIBCPP_ASSERT(e_cond.category() == std::generic_category());
40 assert(e_cat1.equivalent(5, e_cond));
41
42 e_cond = e_cat1.default_error_condition(i: 5000);
43 LIBCPP_ASSERT(e_cond.value() == 5000);
44 LIBCPP_ASSERT(e_cond.category() == std::system_category());
45 assert(e_cat1.equivalent(5000, e_cond));
46 }
47
48 // Test the result of message(int cond) when given a bad error condition
49 {
50 errno = E2BIG; // something that message will never generate
51 const std::error_category& e_cat1 = std::system_category();
52 const std::string msg = e_cat1.message(-1);
53 // Exact message format varies by platform. We can't detect
54 // some of these (Musl in particular) using the preprocessor,
55 // so accept a few sensible messages. Newlib unfortunately
56 // responds with an empty message, which we probably want to
57 // treat as a failure code otherwise, but we can detect that
58 // with the preprocessor.
59 LIBCPP_ASSERT(msg.rfind(s: "Error -1 occurred", pos: 0) == 0 // AIX
60 || msg.rfind(s: "No error information", pos: 0) == 0 // Musl
61 || msg.rfind(s: "Unknown error", pos: 0) == 0 // Glibc
62#if defined(_NEWLIB_VERSION)
63 || msg.empty()
64#endif
65 );
66 assert(errno == E2BIG);
67 }
68
69 {
70 foo.ec = &std::system_category();
71 std::string m = foo.ec->name();
72 assert(m == "system");
73 }
74
75 return 0;
76}
77

source code of libcxx/test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.objects/system_category.pass.cpp