Debian Codegraph @

libstdc++-v3/testsuite/28_regex/traits/char/lookup_classname.cc

raw symbols SCIP
1 // { dg-do run { target c++11 } }
2 // { dg-timeout-factor 2 }
3
4 //
5 // 2010-06-23 Stephen M. Webb <[email protected]>
6 //
7 // Copyright (C) 2010-2025 Free Software Foundation, Inc.
8 //
9 // This file is part of the GNU ISO C++ Library. This library is free
10 // software; you can redistribute it and/or modify it under the
11 // terms of the GNU General Public License as published by the
12 // Free Software Foundation; either version 3, or (at your option)
13 // any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License along
21 // with this library; see the file COPYING3. If not see
22 // <http://www.gnu.org/licenses/>.
23
24 // 28.3 Requirements [re.req]
25 // 28.2(4) Table 127 - Regular expression traits class requirements
26 // 28.7(9) Class template regex_traits [re.traits]
27
28 #include <regex>
29 #include <forward_list>
30 #include <string.h>
31 #include <ctype.h>
32 #include <testsuite_hooks.h>
33
34 void
35 test01()
36 {
37 typedef char CharT;
38 typedef std::regex_traits<CharT> traits;
39
40 char n1[] = "lower";
41 char n2[] = "alpha";
42 traits t;
43
44 traits::char_class_type c1 = t.lookup_classname(n1, n1+sizeof(n1)-1);
45 VERIFY( c1 != 0 );
46
47 traits::char_class_type c2 = t.lookup_classname(n1, n1+sizeof(n1)-1, true);
48 traits::char_class_type c3 = t.lookup_classname(n2, n2+sizeof(n2)-1, true);
49 VERIFY( c2 == c3 );
50 }
51
52 // Test forward iterator
53 void
54 test02()
55 {
56 const char strlit[] = "upper";
57 std::forward_list<char> s(strlit, strlit + strlen(strlit));
58 std::regex_traits<char> traits;
59 VERIFY(traits.isctype('C', traits.lookup_classname(s.begin(), s.end(), false)));
60 }
61
62 // icase
63 void
64 test03()
65 {
66 std::string s("lower");
67 std::regex_traits<char> traits;
68 VERIFY(traits.isctype('C', traits.lookup_classname(s.begin(), s.end(), true)));
69 }
70
71 void
72 test04()
73 {
74 // Transform "string" into "StRiNg"
75 auto mix_case = [](std::string s) {
76 int i = 0;
77 for (auto& ch : s)
78 if (++i % 2)
79 ch = toupper(ch);
80 return s;
81 };
82
83 typedef char CharT;
84 typedef std::regex_traits<CharT> traits;
85 traits t;
86
87 auto lookup = [&t](const std::string& s, bool icase) {
88 return t.lookup_classname(s.begin(), s.end(), icase);
89 };
90
91 VERIFY( lookup("", false) == 0 );
92 VERIFY( lookup(":::not a valid classname:::", false) == 0 );
93 VERIFY( lookup(":::not a valid classname:::", true) == 0 );
94 VERIFY( lookup("alnu", false) == 0 );
95 VERIFY( lookup("alnumb", false) == 0 );
96 VERIFY( lookup("x", false) == 0 );
97 VERIFY( lookup("di", false) == 0 );
98 VERIFY( lookup("digi", false) == 0 );
99 VERIFY( lookup("digix", false) == 0 );
100 VERIFY( lookup(std::string{"d\0i", 3}, false) == 0 );
101 VERIFY( lookup(std::string{"digit\0", 6}, false) == 0 );
102 VERIFY( lookup(std::string{"digit\0bad", 9}, false) == 0 );
103
104 for (std::string cls : { "alnum", "alpha", "blank", "cntrl", "digit",
105 "graph", "lower", "print", "punct", "space",
106 "upper", "xdigit", "d", "s", "w" })
107 {
108 traits::char_class_type val = lookup(cls, false);
109 // val should be non-zero:
110 VERIFY( val != 0 );
111 // val is independent of the case of the name,
112 // i.e. "alpha" and "ALPHA" and "AlPhA" give same result:
113 VERIFY( lookup(mix_case(cls), false) == val );
114
115 // Repeat same checks for icase=true.
116 traits::char_class_type ival = lookup(cls, true);
117 VERIFY( ival != 0 );
118 VERIFY( lookup(mix_case(cls), true) == ival );
119
120 if (cls == "lower" || cls == "upper") // icase=true should affect value
121 {
122 VERIFY( ival != val );
123 VERIFY( ival == lookup("alpha", false) );
124 }
125 else
126 VERIFY( ival == val );
127
128 if (cls == "d")
129 VERIFY( val == lookup("digit", false) );
130 else if (cls == "s")
131 VERIFY( val == lookup("space", false) );
132 }
133 }
134
135 int main()
136 {
137 test01();
138 test02();
139 test03();
140 test04();
141 return 0;
142 }