xref: /aosp_15_r20/external/cronet/third_party/libc++/src/test/std/re/re.badexp/regex_error.pass.cpp (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 // <regex>
10 
11 // class regex_error
12 //     : public runtime_error
13 // {
14 // public:
15 //     explicit regex_error(regex_constants::error_type ecode);
16 //     regex_constants::error_type code() const;
17 // };
18 
19 #include <regex>
20 #include <cassert>
21 #include "test_macros.h"
22 
main(int,char **)23 int main(int, char**)
24 {
25     {
26         std::regex_error e(std::regex_constants::error_collate);
27         assert(e.code() == std::regex_constants::error_collate);
28         LIBCPP_ASSERT(e.what() == std::string("The expression contained an invalid collating element name."));
29     }
30     {
31         std::regex_error e(std::regex_constants::error_ctype);
32         assert(e.code() == std::regex_constants::error_ctype);
33         LIBCPP_ASSERT(e.what() == std::string("The expression contained an invalid character class name."));
34     }
35     {
36         std::regex_error e(std::regex_constants::error_escape);
37         assert(e.code() == std::regex_constants::error_escape);
38         LIBCPP_ASSERT(e.what() == std::string("The expression contained an invalid escaped character, or a "
39                "trailing escape."));
40     }
41     {
42         std::regex_error e(std::regex_constants::error_backref);
43         assert(e.code() == std::regex_constants::error_backref);
44         LIBCPP_ASSERT(e.what() == std::string("The expression contained an invalid back reference."));
45     }
46     {
47         std::regex_error e(std::regex_constants::error_brack);
48         assert(e.code() == std::regex_constants::error_brack);
49         LIBCPP_ASSERT(e.what() == std::string("The expression contained mismatched [ and ]."));
50     }
51     {
52         std::regex_error e(std::regex_constants::error_paren);
53         assert(e.code() == std::regex_constants::error_paren);
54         LIBCPP_ASSERT(e.what() == std::string("The expression contained mismatched ( and )."));
55     }
56     {
57         std::regex_error e(std::regex_constants::error_brace);
58         assert(e.code() == std::regex_constants::error_brace);
59         LIBCPP_ASSERT(e.what() == std::string("The expression contained mismatched { and }."));
60     }
61     {
62         std::regex_error e(std::regex_constants::error_badbrace);
63         assert(e.code() == std::regex_constants::error_badbrace);
64         LIBCPP_ASSERT(e.what() == std::string("The expression contained an invalid range in a {} expression."));
65     }
66     {
67         std::regex_error e(std::regex_constants::error_range);
68         assert(e.code() == std::regex_constants::error_range);
69         LIBCPP_ASSERT(e.what() == std::string("The expression contained an invalid character range, "
70                "such as [b-a] in most encodings."));
71     }
72     {
73         std::regex_error e(std::regex_constants::error_space);
74         assert(e.code() == std::regex_constants::error_space);
75         LIBCPP_ASSERT(e.what() == std::string("There was insufficient memory to convert the expression into "
76                "a finite state machine."));
77     }
78     {
79         std::regex_error e(std::regex_constants::error_badrepeat);
80         assert(e.code() == std::regex_constants::error_badrepeat);
81         LIBCPP_ASSERT(e.what() == std::string("One of *?+{ was not preceded by a valid regular expression."));
82     }
83     {
84         std::regex_error e(std::regex_constants::error_complexity);
85         assert(e.code() == std::regex_constants::error_complexity);
86         LIBCPP_ASSERT(e.what() == std::string("The complexity of an attempted match against a regular "
87                "expression exceeded a pre-set level."));
88     }
89     {
90         std::regex_error e(std::regex_constants::error_stack);
91         assert(e.code() == std::regex_constants::error_stack);
92         LIBCPP_ASSERT(e.what() == std::string("There was insufficient memory to determine whether the regular "
93                "expression could match the specified character sequence."));
94     }
95 
96   return 0;
97 }
98