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 | // XFAIL: stdlib=apple-libc++ && target={{.+}}-apple-macosx10.{{9|10|11|12}} |
10 | |
11 | // <system_error> |
12 | |
13 | // class error_category |
14 | |
15 | // const error_category& generic_category(); |
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 |
25 | struct StaticInit { |
26 | const std::error_category* ec; |
27 | ~StaticInit() { |
28 | std::string str = ec->name(); |
29 | assert(str == "generic" ) ; |
30 | } |
31 | }; |
32 | static StaticInit foo; |
33 | |
34 | int main(int, char**) |
35 | { |
36 | { |
37 | const std::error_category& e_cat1 = std::generic_category(); |
38 | std::string m1 = e_cat1.name(); |
39 | assert(m1 == "generic" ); |
40 | } |
41 | |
42 | // Test the result of message(int cond) when given a bad error condition |
43 | { |
44 | errno = E2BIG; // something that message will never generate |
45 | const std::error_category& e_cat1 = std::generic_category(); |
46 | const std::string msg = e_cat1.message(-1); |
47 | // Exact message format varies by platform. We can't detect |
48 | // some of these (Musl in particular) using the preprocessor, |
49 | // so accept a few sensible messages. Newlib unfortunately |
50 | // responds with an empty message, which we probably want to |
51 | // treat as a failure code otherwise, but we can detect that |
52 | // with the preprocessor. |
53 | LIBCPP_ASSERT(msg.rfind(s: "Error -1 occurred" , pos: 0) == 0 // AIX |
54 | || msg.rfind(s: "No error information" , pos: 0) == 0 // Musl |
55 | || msg.rfind(s: "Unknown error" , pos: 0) == 0 // Glibc |
56 | #if defined(_NEWLIB_VERSION) |
57 | || msg.empty() |
58 | #endif |
59 | ); |
60 | assert(errno == E2BIG); |
61 | } |
62 | |
63 | { |
64 | foo.ec = &std::generic_category(); |
65 | std::string m2 = foo.ec->name(); |
66 | assert(m2 == "generic" ); |
67 | } |
68 | |
69 | return 0; |
70 | } |
71 | |