Debian Codegraph @

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

raw
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 #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;
25 ~StaticInit() {
26 std::string str = ec->name();
27 assert(str == "system");
28 }
29 };
30 static StaticInit foo;
31
32 int main(int, char**) {
33 {
34 const std::error_category& e_cat1 = std::system_category();
35 std::error_condition e_cond = e_cat1.default_error_condition(5);
36 LIBCPP_ASSERT(e_cond.value() == 5);
37 LIBCPP_ASSERT(e_cond.category() == std::generic_category());
38 assert(e_cat1.equivalent(5, e_cond));
39
40 e_cond = e_cat1.default_error_condition(5000);
41 LIBCPP_ASSERT(e_cond.value() == 5000);
42 LIBCPP_ASSERT(e_cond.category() == std::system_category());
43 assert(e_cat1.equivalent(5000, e_cond));
44 }
45
46 // Test the result of message(int cond) when given a bad error condition
47 {
48 errno = E2BIG; // something that message will never generate
49 const std::error_category& e_cat1 = std::system_category();
50 const std::string msg = e_cat1.message(-1);
51 // Exact message format varies by platform. We can't detect
52 // some of these (Musl in particular) using the preprocessor,
53 // so accept a few sensible messages. Newlib unfortunately
54 // responds with an empty message, which we probably want to
55 // treat as a failure code otherwise, but we can detect that
56 // with the preprocessor.
57 #if defined(_NEWLIB_VERSION)
58 const bool is_newlib = true;
59 #else
60 const bool is_newlib = false;
61 #endif
62 (void)is_newlib;
63 LIBCPP_ASSERT(msg.rfind("Error -1 occurred", 0) == 0 // AIX
64 || msg.rfind("No error information", 0) == 0 // Musl
65 || msg.rfind("Unknown error", 0) == 0 // Glibc
66 || (is_newlib && msg.empty()));
67 assert(errno == E2BIG);
68 }
69
70 {
71 foo.ec = &std::system_category();
72 std::string m = foo.ec->name();
73 assert(m == "system");
74 }
75
76 return 0;
77 }