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& generic_category();
14 
15 #include <system_error>
16 #include <cassert>
17 #include <string>
18 #include <cerrno>
19 
20 #include "test_macros.h"
21 
22 // See https://llvm.org/D65667
23 struct StaticInit {
24     const std::error_category* ec;
~StaticInitStaticInit25     ~StaticInit() {
26         std::string str = ec->name();
27         assert(str == "generic") ;
28     }
29 };
30 static StaticInit foo;
31 
main(int,char **)32 int main(int, char**)
33 {
34     {
35         const std::error_category& e_cat1 = std::generic_category();
36         std::string m1 = e_cat1.name();
37         assert(m1 == "generic");
38     }
39 
40     // Test the result of message(int cond) when given a bad error condition
41     {
42         errno = E2BIG; // something that message will never generate
43         const std::error_category& e_cat1 = std::generic_category();
44         const std::string msg = e_cat1.message(-1);
45         // Exact message format varies by platform.  We can't detect
46         // some of these (Musl in particular) using the preprocessor,
47         // so accept a few sensible messages.  Newlib unfortunately
48         // responds with an empty message, which we probably want to
49         // treat as a failure code otherwise, but we can detect that
50         // with the preprocessor.
51 #if defined(_NEWLIB_VERSION)
52         const bool is_newlib = true;
53 #else
54         const bool is_newlib = false;
55 #endif
56         (void)is_newlib;
57         LIBCPP_ASSERT(msg.rfind("Error -1 occurred", 0) == 0       // AIX
58                       || msg.rfind("No error information", 0) == 0 // Musl
59                       || msg.rfind("Unknown error", 0) == 0        // Glibc
60                       || (is_newlib && msg.empty()));
61         assert(errno == E2BIG);
62     }
63 
64     {
65         foo.ec = &std::generic_category();
66         std::string m2 = foo.ec->name();
67         assert(m2 == "generic");
68     }
69 
70     return 0;
71 }
72