]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/19_diagnostics/error_category/system_category.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 19_diagnostics / error_category / system_category.cc
1 // Copyright (C) 2018-2023 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17
18 // { dg-do run { target c++11 } }
19
20 #include <system_error>
21 #include <locale>
22 #include <testsuite_hooks.h>
23
24 void
25 test01()
26 {
27 const char* name = std::system_category().name();
28 VERIFY( name == std::string("system") );
29 }
30
31 void
32 test02()
33 {
34 const std::error_category& cat = std::system_category();
35 std::error_condition cond;
36
37 #if defined __MING32__ || defined __MINGW64__
38 cond = cat.default_error_condition(8); // ERROR_NOT_ENOUGH_MEMORY
39 VERIFY( cond.value() == ENOMEM );
40 VERIFY( cond.category() == std::generic_category() );
41 VERIFY( cond == std::errc::not_enough_memory );
42
43 cond = cat.default_error_condition(5); // ERROR_ACCESS_DENIED
44 VERIFY( cond.value() == EACCES );
45 VERIFY( cond.category() == std::generic_category() );
46 VERIFY( cond == std::errc::permission_denied );
47 return;
48 #endif
49
50 // As of 2011, ISO C only defines EDOM, EILSEQ and ERANGE:
51 cond = cat.default_error_condition(EDOM);
52 VERIFY( cond.value() == EDOM );
53 VERIFY( cond == std::errc::argument_out_of_domain );
54 VERIFY( cond.category() == std::generic_category() );
55 cond = cat.default_error_condition(EILSEQ);
56 VERIFY( cond.value() == EILSEQ );
57 VERIFY( cond == std::errc::illegal_byte_sequence );
58 VERIFY( cond.category() == std::generic_category() );
59 cond = cat.default_error_condition(ERANGE);
60 VERIFY( cond.value() == ERANGE );
61 VERIFY( cond == std::errc::result_out_of_range );
62 VERIFY( cond.category() == std::generic_category() );
63
64 // EBADF and EACCES are defined on all targets,
65 // according to config/os/*/error_constants.h
66 cond = cat.default_error_condition(EBADF);
67 VERIFY( cond.value() == EBADF );
68 VERIFY( cond == std::errc::bad_file_descriptor );
69 VERIFY( cond.category() == std::generic_category() );
70 cond = cat.default_error_condition(EACCES);
71 VERIFY( cond.value() == EACCES );
72 VERIFY( cond == std::errc::permission_denied );
73 VERIFY( cond.category() == std::generic_category() );
74
75 // All POSIX errno values are positive:
76 cond = cat.default_error_condition(-1);
77 VERIFY( cond.value() == -1 );
78 VERIFY( cond.category() == cat );
79 cond = cat.default_error_condition(-99);
80 VERIFY( cond.value() == -99 );
81 VERIFY( cond.category() == cat );
82
83 // PR libstdc++/60555
84 VERIFY( std::error_code(EDOM, cat) == std::errc::argument_out_of_domain );
85 VERIFY( std::error_code(EILSEQ, cat) == std::errc::illegal_byte_sequence );
86 VERIFY( std::error_code(ERANGE, cat) == std::errc::result_out_of_range );
87 VERIFY( std::error_code(EBADF, cat) == std::errc::bad_file_descriptor );
88 VERIFY( std::error_code(EACCES, cat) == std::errc::permission_denied );
89
90 // As shown at https://gcc.gnu.org/ml/libstdc++/2018-08/msg00018.html
91 // these two error codes might have the same value on AIX, but we still
92 // expect both to be matched by system_category and so use generic_category:
93 #ifdef EEXIST
94 cond = cat.default_error_condition(EEXIST);
95 VERIFY( cond.value() == EEXIST );
96 VERIFY( cond == std::errc::file_exists );
97 VERIFY( cond.category() == std::generic_category() );
98 VERIFY( std::error_code(EEXIST, cat) == std::errc::file_exists );
99 #endif
100 #ifdef ENOTEMPTY
101 cond = cat.default_error_condition(ENOTEMPTY);
102 VERIFY( cond.value() == ENOTEMPTY );
103 VERIFY( cond == std::errc::directory_not_empty );
104 VERIFY( cond.category() == std::generic_category() );
105 VERIFY( std::error_code(ENOTEMPTY, cat) == std::errc::directory_not_empty );
106 #endif
107 }
108
109 void
110 test03()
111 {
112 // set "C" locale to get expected message
113 auto loc = std::locale::global(std::locale::classic());
114
115 #if defined __MING32__ || defined __MINGW64__
116 std::string msg = std::system_category().message(5); // ERROR_ACCESS_DENIED
117 VERIFY(msg == "Access denied");
118 #else
119 std::string msg = std::system_category().message(EBADF);
120 VERIFY( msg.find("file") != std::string::npos );
121 #endif
122
123 std::locale::global(loc);
124 }
125
126 int
127 main()
128 {
129 test01();
130 test02();
131 test03();
132 }