xref: /llvm-project/libcxx/test/std/re/re.badexp/regex_error.pass.cpp (revision ef94609d6ebe981767788e6877b0b3b731d425af)
15a83710eSEric Fiselier //===----------------------------------------------------------------------===//
25a83710eSEric Fiselier //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65a83710eSEric Fiselier //
75a83710eSEric Fiselier //===----------------------------------------------------------------------===//
85a83710eSEric Fiselier 
95a83710eSEric Fiselier // <regex>
105a83710eSEric Fiselier 
115a83710eSEric Fiselier // class regex_error
125a83710eSEric Fiselier //     : public runtime_error
135a83710eSEric Fiselier // {
145a83710eSEric Fiselier // public:
155a83710eSEric Fiselier //     explicit regex_error(regex_constants::error_type ecode);
165a83710eSEric Fiselier //     regex_constants::error_type code() const;
175a83710eSEric Fiselier // };
185a83710eSEric Fiselier 
195a83710eSEric Fiselier #include <regex>
205a83710eSEric Fiselier #include <cassert>
21fd5ceb22SMarshall Clow #include "test_macros.h"
225a83710eSEric Fiselier 
main(int,char **)232df59c50SJF Bastien int main(int, char**)
245a83710eSEric Fiselier {
255a83710eSEric Fiselier     {
265a83710eSEric Fiselier         std::regex_error e(std::regex_constants::error_collate);
275a83710eSEric Fiselier         assert(e.code() == std::regex_constants::error_collate);
28*ef94609dSJonathan Wakely         LIBCPP_ASSERT(e.what() == std::string("The expression contained an invalid collating element name."));
295a83710eSEric Fiselier     }
305a83710eSEric Fiselier     {
315a83710eSEric Fiselier         std::regex_error e(std::regex_constants::error_ctype);
325a83710eSEric Fiselier         assert(e.code() == std::regex_constants::error_ctype);
33*ef94609dSJonathan Wakely         LIBCPP_ASSERT(e.what() == std::string("The expression contained an invalid character class name."));
345a83710eSEric Fiselier     }
355a83710eSEric Fiselier     {
365a83710eSEric Fiselier         std::regex_error e(std::regex_constants::error_escape);
375a83710eSEric Fiselier         assert(e.code() == std::regex_constants::error_escape);
38*ef94609dSJonathan Wakely         LIBCPP_ASSERT(e.what() == std::string("The expression contained an invalid escaped character, or a "
395a83710eSEric Fiselier                "trailing escape."));
405a83710eSEric Fiselier     }
415a83710eSEric Fiselier     {
425a83710eSEric Fiselier         std::regex_error e(std::regex_constants::error_backref);
435a83710eSEric Fiselier         assert(e.code() == std::regex_constants::error_backref);
44*ef94609dSJonathan Wakely         LIBCPP_ASSERT(e.what() == std::string("The expression contained an invalid back reference."));
455a83710eSEric Fiselier     }
465a83710eSEric Fiselier     {
475a83710eSEric Fiselier         std::regex_error e(std::regex_constants::error_brack);
485a83710eSEric Fiselier         assert(e.code() == std::regex_constants::error_brack);
49*ef94609dSJonathan Wakely         LIBCPP_ASSERT(e.what() == std::string("The expression contained mismatched [ and ]."));
505a83710eSEric Fiselier     }
515a83710eSEric Fiselier     {
525a83710eSEric Fiselier         std::regex_error e(std::regex_constants::error_paren);
535a83710eSEric Fiselier         assert(e.code() == std::regex_constants::error_paren);
54*ef94609dSJonathan Wakely         LIBCPP_ASSERT(e.what() == std::string("The expression contained mismatched ( and )."));
555a83710eSEric Fiselier     }
565a83710eSEric Fiselier     {
575a83710eSEric Fiselier         std::regex_error e(std::regex_constants::error_brace);
585a83710eSEric Fiselier         assert(e.code() == std::regex_constants::error_brace);
59*ef94609dSJonathan Wakely         LIBCPP_ASSERT(e.what() == std::string("The expression contained mismatched { and }."));
605a83710eSEric Fiselier     }
615a83710eSEric Fiselier     {
625a83710eSEric Fiselier         std::regex_error e(std::regex_constants::error_badbrace);
635a83710eSEric Fiselier         assert(e.code() == std::regex_constants::error_badbrace);
64*ef94609dSJonathan Wakely         LIBCPP_ASSERT(e.what() == std::string("The expression contained an invalid range in a {} expression."));
655a83710eSEric Fiselier     }
665a83710eSEric Fiselier     {
675a83710eSEric Fiselier         std::regex_error e(std::regex_constants::error_range);
685a83710eSEric Fiselier         assert(e.code() == std::regex_constants::error_range);
69*ef94609dSJonathan Wakely         LIBCPP_ASSERT(e.what() == std::string("The expression contained an invalid character range, "
705a83710eSEric Fiselier                "such as [b-a] in most encodings."));
715a83710eSEric Fiselier     }
725a83710eSEric Fiselier     {
735a83710eSEric Fiselier         std::regex_error e(std::regex_constants::error_space);
745a83710eSEric Fiselier         assert(e.code() == std::regex_constants::error_space);
75*ef94609dSJonathan Wakely         LIBCPP_ASSERT(e.what() == std::string("There was insufficient memory to convert the expression into "
765a83710eSEric Fiselier                "a finite state machine."));
775a83710eSEric Fiselier     }
785a83710eSEric Fiselier     {
795a83710eSEric Fiselier         std::regex_error e(std::regex_constants::error_badrepeat);
805a83710eSEric Fiselier         assert(e.code() == std::regex_constants::error_badrepeat);
81*ef94609dSJonathan Wakely         LIBCPP_ASSERT(e.what() == std::string("One of *?+{ was not preceded by a valid regular expression."));
825a83710eSEric Fiselier     }
835a83710eSEric Fiselier     {
845a83710eSEric Fiselier         std::regex_error e(std::regex_constants::error_complexity);
855a83710eSEric Fiselier         assert(e.code() == std::regex_constants::error_complexity);
86*ef94609dSJonathan Wakely         LIBCPP_ASSERT(e.what() == std::string("The complexity of an attempted match against a regular "
875a83710eSEric Fiselier                "expression exceeded a pre-set level."));
885a83710eSEric Fiselier     }
895a83710eSEric Fiselier     {
905a83710eSEric Fiselier         std::regex_error e(std::regex_constants::error_stack);
915a83710eSEric Fiselier         assert(e.code() == std::regex_constants::error_stack);
92*ef94609dSJonathan Wakely         LIBCPP_ASSERT(e.what() == std::string("There was insufficient memory to determine whether the regular "
935a83710eSEric Fiselier                "expression could match the specified character sequence."));
945a83710eSEric Fiselier     }
952df59c50SJF Bastien 
962df59c50SJF Bastien   return 0;
975a83710eSEric Fiselier }
98