1// Copyright (C) 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
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
6// Free Software Foundation; either version 3, or (at your option)
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
15// with this library; see the file COPYING3.  If not see
16// <http://www.gnu.org/licenses/>.
17
18
19// 22.2.1.3.2 ctype<char> members
20
21#include <locale>
22#include <cstring>
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,
31	    char, char* to) const
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;
60  bool test __attribute__((unused)) = true;
61
62  const char src[] = "abcd";
63
64  locale mylocale1(locale::classic(), new Ctype1);
65  const ctype<char>& mc1 = use_facet<ctype<char> >(mylocale1);
66
67  char dst1[sizeof(src)];
68  memset(dst1, 0, sizeof(src));
69  char dst2[sizeof(src)];
70  memset(dst2, 0, sizeof(src));
71
72  mc1.narrow(src, src + sizeof(src), '*', dst1);
73  mc1.narrow(src, src + sizeof(src), '*', dst2);
74
75  VERIFY( !memcmp(dst1, "aceg\004", 5) );
76  VERIFY( !memcmp(dst1, dst2, 5) );
77
78  locale mylocale2(locale::classic(), new Ctype2);
79  const ctype<char>& mc2 = use_facet<ctype<char> >(mylocale2);
80
81  char dst3[sizeof(src)];
82  memset(dst3, 0, sizeof(src));
83  char dst4[sizeof(src)];
84  memset(dst4, 0, sizeof(src));
85
86  mc2.narrow(src, src + sizeof(src), '*', dst3);
87  mc2.narrow(src, src + sizeof(src), '*', dst4);
88
89  VERIFY( !memcmp(dst3, "abcd*", 5) );
90  VERIFY( !memcmp(dst3, dst4, 5) );
91}
92
93int main()
94{
95  test01();
96  return 0;
97}
98