brintos

brintos / llvm-project-archived public Read only

0
0
Text · 9.7 KiB · caa04c7 Raw
251 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// <regex>10 11// template <class charT> struct regex_traits;12 13// template <class ForwardIterator>14//   char_class_type15//   lookup_classname(ForwardIterator first, ForwardIterator last,16//                    bool icase = false) const;17 18#include <regex>19#include <cassert>20#include "test_macros.h"21#include "test_iterators.h"22 23template <class char_type>24void25test(const char_type* A,26     typename std::regex_traits<char_type>::char_class_type expected,27     bool icase = false)28{29    typedef typename std::regex_traits<char_type>::char_class_type char_class_type;30    std::regex_traits<char_type> t;31    typedef forward_iterator<const char_type*> F;32    char_class_type result = t.lookup_classname(F(A), F(A + t.length(A)), icase);33    assert(result == expected);34}35 36template <class char_type>37void38test_w(const char_type* A,39       typename std::regex_traits<char_type>::char_class_type expected,40        bool icase = false)41{42    typedef typename std::regex_traits<char_type>::char_class_type char_class_type;43    std::regex_traits<char_type> t;44    typedef forward_iterator<const char_type*> F;45    char_class_type result = t.lookup_classname(F(A), F(A + t.length(A)), icase);46    assert((result & expected) == expected);47    LIBCPP_ASSERT((expected | std::regex_traits<char_type>::__regex_word) == result);48 49    const bool matches_underscore = t.isctype('_', result);50    if (result != expected)51      assert(matches_underscore && "expected to match underscore");52    else53      assert(!matches_underscore && "should not match underscore");54}55 56int main(int, char**)57{58//  if __regex_word is not distinct from all the classes, bad things happen59//  See https://llvm.org/PR26476 for an example.60    LIBCPP_ASSERT((std::ctype_base::space  & std::regex_traits<char>::__regex_word) == 0);61    LIBCPP_ASSERT((std::ctype_base::print  & std::regex_traits<char>::__regex_word) == 0);62    LIBCPP_ASSERT((std::ctype_base::cntrl  & std::regex_traits<char>::__regex_word) == 0);63    LIBCPP_ASSERT((std::ctype_base::upper  & std::regex_traits<char>::__regex_word) == 0);64    LIBCPP_ASSERT((std::ctype_base::lower  & std::regex_traits<char>::__regex_word) == 0);65    LIBCPP_ASSERT((std::ctype_base::alpha  & std::regex_traits<char>::__regex_word) == 0);66    LIBCPP_ASSERT((std::ctype_base::digit  & std::regex_traits<char>::__regex_word) == 0);67    LIBCPP_ASSERT((std::ctype_base::punct  & std::regex_traits<char>::__regex_word) == 0);68    LIBCPP_ASSERT((std::ctype_base::xdigit & std::regex_traits<char>::__regex_word) == 0);69    LIBCPP_ASSERT((std::ctype_base::blank  & std::regex_traits<char>::__regex_word) == 0);70 71    test("d", std::ctype_base::digit);72    test("D", std::ctype_base::digit);73    test("d", std::ctype_base::digit, true);74    test("D", std::ctype_base::digit, true);75 76    test_w("w", std::ctype_base::alnum77              | std::ctype_base::upper | std::ctype_base::lower);78    test_w("W", std::ctype_base::alnum79              | std::ctype_base::upper | std::ctype_base::lower);80    test_w("w", std::ctype_base::alnum81              | std::ctype_base::upper | std::ctype_base::lower, true);82    test_w("W", std::ctype_base::alnum83              | std::ctype_base::upper | std::ctype_base::lower, true);84 85    test("s", std::ctype_base::space);86    test("S", std::ctype_base::space);87    test("s", std::ctype_base::space, true);88    test("S", std::ctype_base::space, true);89 90    test("alnum", std::ctype_base::alnum);91    test("AlNum", std::ctype_base::alnum);92    test("alnum", std::ctype_base::alnum, true);93    test("AlNum", std::ctype_base::alnum, true);94 95    test("alpha", std::ctype_base::alpha);96    test("Alpha", std::ctype_base::alpha);97    test("alpha", std::ctype_base::alpha, true);98    test("Alpha", std::ctype_base::alpha, true);99 100    test("blank", std::ctype_base::blank);101    test("Blank", std::ctype_base::blank);102    test("blank", std::ctype_base::blank, true);103    test("Blank", std::ctype_base::blank, true);104 105    test("cntrl", std::ctype_base::cntrl);106    test("Cntrl", std::ctype_base::cntrl);107    test("cntrl", std::ctype_base::cntrl, true);108    test("Cntrl", std::ctype_base::cntrl, true);109 110    test("digit", std::ctype_base::digit);111    test("Digit", std::ctype_base::digit);112    test("digit", std::ctype_base::digit, true);113    test("Digit", std::ctype_base::digit, true);114 115    test("digit", std::ctype_base::digit);116    test("DIGIT", std::ctype_base::digit);117    test("digit", std::ctype_base::digit, true);118    test("Digit", std::ctype_base::digit, true);119 120    test("graph", std::ctype_base::graph);121    test("GRAPH", std::ctype_base::graph);122    test("graph", std::ctype_base::graph, true);123    test("Graph", std::ctype_base::graph, true);124 125    test("lower", std::ctype_base::lower);126    test("LOWER", std::ctype_base::lower);127    test("lower", std::ctype_base::lower | std::ctype_base::alpha, true);128    test("Lower", std::ctype_base::lower | std::ctype_base::alpha, true);129 130    test("print", std::ctype_base::print);131    test("PRINT", std::ctype_base::print);132    test("print", std::ctype_base::print, true);133    test("Print", std::ctype_base::print, true);134 135    test("punct", std::ctype_base::punct);136    test("PUNCT", std::ctype_base::punct);137    test("punct", std::ctype_base::punct, true);138    test("Punct", std::ctype_base::punct, true);139 140    test("space", std::ctype_base::space);141    test("SPACE", std::ctype_base::space);142    test("space", std::ctype_base::space, true);143    test("Space", std::ctype_base::space, true);144 145    test("upper", std::ctype_base::upper);146    test("UPPER", std::ctype_base::upper);147    test("upper", std::ctype_base::upper | std::ctype_base::alpha, true);148    test("Upper", std::ctype_base::upper | std::ctype_base::alpha, true);149 150    test("xdigit", std::ctype_base::xdigit);151    test("XDIGIT", std::ctype_base::xdigit);152    test("xdigit", std::ctype_base::xdigit, true);153    test("Xdigit", std::ctype_base::xdigit, true);154 155    test("dig", std::ctype_base::mask());156    test("", std::ctype_base::mask());157    test("digits", std::ctype_base::mask());158 159#ifndef TEST_HAS_NO_WIDE_CHARACTERS160    test(L"d", std::ctype_base::digit);161    test(L"D", std::ctype_base::digit);162    test(L"d", std::ctype_base::digit, true);163    test(L"D", std::ctype_base::digit, true);164 165    test_w(L"w", std::ctype_base::alnum166                      | std::ctype_base::upper | std::ctype_base::lower);167    test_w(L"W", std::ctype_base::alnum168                      | std::ctype_base::upper | std::ctype_base::lower);169    test_w(L"w", std::ctype_base::alnum170                      | std::ctype_base::upper | std::ctype_base::lower, true);171    test_w(L"W", std::ctype_base::alnum172                      | std::ctype_base::upper | std::ctype_base::lower, true);173 174    test(L"s", std::ctype_base::space);175    test(L"S", std::ctype_base::space);176    test(L"s", std::ctype_base::space, true);177    test(L"S", std::ctype_base::space, true);178 179    test(L"alnum", std::ctype_base::alnum);180    test(L"AlNum", std::ctype_base::alnum);181    test(L"alnum", std::ctype_base::alnum, true);182    test(L"AlNum", std::ctype_base::alnum, true);183 184    test(L"alpha", std::ctype_base::alpha);185    test(L"Alpha", std::ctype_base::alpha);186    test(L"alpha", std::ctype_base::alpha, true);187    test(L"Alpha", std::ctype_base::alpha, true);188 189    test(L"blank", std::ctype_base::blank);190    test(L"Blank", std::ctype_base::blank);191    test(L"blank", std::ctype_base::blank, true);192    test(L"Blank", std::ctype_base::blank, true);193 194    test(L"cntrl", std::ctype_base::cntrl);195    test(L"Cntrl", std::ctype_base::cntrl);196    test(L"cntrl", std::ctype_base::cntrl, true);197    test(L"Cntrl", std::ctype_base::cntrl, true);198 199    test(L"digit", std::ctype_base::digit);200    test(L"Digit", std::ctype_base::digit);201    test(L"digit", std::ctype_base::digit, true);202    test(L"Digit", std::ctype_base::digit, true);203 204    test(L"digit", std::ctype_base::digit);205    test(L"DIGIT", std::ctype_base::digit);206    test(L"digit", std::ctype_base::digit, true);207    test(L"Digit", std::ctype_base::digit, true);208 209    test(L"graph", std::ctype_base::graph);210    test(L"GRAPH", std::ctype_base::graph);211    test(L"graph", std::ctype_base::graph, true);212    test(L"Graph", std::ctype_base::graph, true);213 214    test(L"lower", std::ctype_base::lower);215    test(L"LOWER", std::ctype_base::lower);216    test(L"lower", std::ctype_base::lower | std::ctype_base::alpha, true);217    test(L"Lower", std::ctype_base::lower | std::ctype_base::alpha, true);218 219    test(L"print", std::ctype_base::print);220    test(L"PRINT", std::ctype_base::print);221    test(L"print", std::ctype_base::print, true);222    test(L"Print", std::ctype_base::print, true);223 224    test(L"punct", std::ctype_base::punct);225    test(L"PUNCT", std::ctype_base::punct);226    test(L"punct", std::ctype_base::punct, true);227    test(L"Punct", std::ctype_base::punct, true);228 229    test(L"space", std::ctype_base::space);230    test(L"SPACE", std::ctype_base::space);231    test(L"space", std::ctype_base::space, true);232    test(L"Space", std::ctype_base::space, true);233 234    test(L"upper", std::ctype_base::upper);235    test(L"UPPER", std::ctype_base::upper);236    test(L"upper", std::ctype_base::upper | std::ctype_base::alpha, true);237    test(L"Upper", std::ctype_base::upper | std::ctype_base::alpha, true);238 239    test(L"xdigit", std::ctype_base::xdigit);240    test(L"XDIGIT", std::ctype_base::xdigit);241    test(L"xdigit", std::ctype_base::xdigit, true);242    test(L"Xdigit", std::ctype_base::xdigit, true);243 244    test(L"dig", std::ctype_base::mask());245    test(L"", std::ctype_base::mask());246    test(L"digits", std::ctype_base::mask());247#endif248 249  return 0;250}251