]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/22_locale/ctype/narrow/char/19955.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 22_locale / ctype / narrow / char / 19955.cc
CommitLineData
8d9254fc 1// Copyright (C) 2005-2020 Free Software Foundation, Inc.
82ce2a94
PC
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
748086b7 6// Free Software Foundation; either version 3, or (at your option)
82ce2a94
PC
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
748086b7
JJ
15// with this library; see the file COPYING3. If not see
16// <http://www.gnu.org/licenses/>.
82ce2a94 17
82ce2a94
PC
18
19// 22.2.1.3.2 ctype<char> members
20
21#include <locale>
1b716e90 22#include <cstring>
82ce2a94
PC
23#include <testsuite_hooks.h>
24
25class Ctype1
26: public std::ctype<char>
27{
28protected:
29 const char*
30 do_narrow(const char* lo, const char* hi,
8b5bc374 31 char, char* to) const
82ce2a94
PC
32 {
33 for (int i = 0; lo != hi; ++lo, ++to, ++i)
34 *to = *lo + i;
35 return hi;
36 }
37};
38
39class Ctype2
40: public std::ctype<char>
41{
42protected:
43 const char*
44 do_narrow(const char* lo, const char* hi,
45 char dflt, char* to) const
46 {
47 for (int i = 0; lo != hi; ++lo, ++to, ++i)
48 if (*lo == '\000')
49 *to = dflt;
50 else
51 *to = *lo;
52 return hi;
53 }
54};
55
56// libstdc++/19955
57void test01()
58{
59 using namespace std;
82ce2a94
PC
60
61 const char src[] = "abcd";
62
63 locale mylocale1(locale::classic(), new Ctype1);
64 const ctype<char>& mc1 = use_facet<ctype<char> >(mylocale1);
65
66 char dst1[sizeof(src)];
67 memset(dst1, 0, sizeof(src));
68 char dst2[sizeof(src)];
69 memset(dst2, 0, sizeof(src));
70
71 mc1.narrow(src, src + sizeof(src), '*', dst1);
72 mc1.narrow(src, src + sizeof(src), '*', dst2);
73
74 VERIFY( !memcmp(dst1, "aceg\004", 5) );
75 VERIFY( !memcmp(dst1, dst2, 5) );
76
77 locale mylocale2(locale::classic(), new Ctype2);
78 const ctype<char>& mc2 = use_facet<ctype<char> >(mylocale2);
79
80 char dst3[sizeof(src)];
81 memset(dst3, 0, sizeof(src));
82 char dst4[sizeof(src)];
83 memset(dst4, 0, sizeof(src));
84
85 mc2.narrow(src, src + sizeof(src), '*', dst3);
86 mc2.narrow(src, src + sizeof(src), '*', dst4);
87
88 VERIFY( !memcmp(dst3, "abcd*", 5) );
89 VERIFY( !memcmp(dst3, dst4, 5) );
90}
91
92int main()
93{
94 test01();
95 return 0;
96}