1// 1999-06-03 bkoz
2
3// Copyright (C) 1999, 2000, 2001, 2003, 2004, 2005, 2009
4// Free Software Foundation, Inc.
5//
6// This file is part of the GNU ISO C++ Library.  This library is free
7// software; you can redistribute it and/or modify it under the
8// terms of the GNU General Public License as published by the
9// Free Software Foundation; either version 3, or (at your option)
10// any later version.
11
12// This library is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15// GNU General Public License for more details.
16
17// You should have received a copy of the GNU General Public License along
18// with this library; see the file COPYING3.  If not see
19// <http://www.gnu.org/licenses/>.
20
21// 21.1.1 Characher traits requirements
22
23#include <string>
24#include <testsuite_hooks.h>
25
26void test01(void)
27{
28  bool test __attribute__((unused)) = true;
29  const std::string str_01("zuma beach");
30  const std::string str_02("montara and ocean beach");
31
32  // 21.1.1 character traits requirements
33
34  // Key for decoding what function signatures really mean:
35  // X        == char_traits<_CharT>
36  // [c,d]    == _CharT
37  // [p,q]    == const _CharT*
38  // s        == _CharT*
39  // [n,i,j]  == size_t
40  // f        == X::int_type
41  // pos      == X::pos_type
42  // state    == X::state_type
43
44  // void X::assign(char c, char d)
45  // assigns c = d;
46  char c1 = 'z';
47  char c2 = 'u';
48  VERIFY( c1 != c2 );
49  std::char_traits<char>::assign(c1,c2);
50  VERIFY( c1 == 'u' );
51
52  // char* X::move(char* s, const char* p, size_t n)
53  // for each i in [0,n) performs X::assign(s[i], p[i]). Copies
54  // correctly even where p is in [s, s + n), and yields s.
55  char array1[] = {'z', 'u', 'm', 'a', ' ', 'b', 'e', 'a', 'c', 'h',  0};
56  const char str_lit1[] = "montara and ocean beach";
57  const char str_lit2[] = "boracay, philippines";
58  const int len1 = sizeof(str_lit1)/sizeof(char);
59  const int len2 = sizeof(str_lit2)/sizeof(char);
60  char array2[len1 + len2 - 1]; // two terminating chars
61  std::char_traits<char>::copy(array2, str_lit2, len2);
62
63  VERIFY( str_lit1[0] == 'm' );
64  c1 = array2[0];
65  c2 = str_lit1[0];
66  char c3 = array2[1];
67  char c4 = str_lit1[1];
68  std::char_traits<char>::move(array2, str_lit1, 0);
69  VERIFY( array2[0] == c1 );
70  VERIFY( str_lit1[0] == c2 );
71  std::char_traits<char>::move(array2, str_lit1, 1);
72  VERIFY( array2[0] == c2 );
73  VERIFY( str_lit1[0] == c2 );
74  VERIFY( array2[1] == c3 );
75  VERIFY( str_lit1[1] == c4 );
76  std::char_traits<char>::move(array2, str_lit1, 2);
77  VERIFY( array2[0] == c2 );
78  VERIFY( str_lit1[0] == c2 );
79  VERIFY( array2[1] == c4 );
80  VERIFY( str_lit1[1] == c4 );
81
82  char* pc1 = array1 + 1;
83  c1 = pc1[0];
84  c2 = array1[0];
85  VERIFY( c1 != c2 );
86  char* pc2 = std::char_traits<char>::move(array1, pc1, 0);
87  c3 = pc1[0];
88  c4 = array1[0];
89  VERIFY( c1 == c3 );
90  VERIFY( c2 == c4 );
91  VERIFY( pc2 == array1 );
92
93  c1 = pc1[0];
94  c2 = array1[0];
95  char* pc3 = pc1;
96  pc2 = std::char_traits<char>::move(array1, pc1, 10);
97  c3 = pc1[0];
98  c4 = array1[0];
99  VERIFY( c1 != c3 ); // underlying char array changed.
100  VERIFY( c4 != c3 );
101  VERIFY( pc2 == array1 );
102  VERIFY( pc3 == pc1 ); // but pointers o-tay
103  c1 = *(str_01.data());
104  c2 = array1[0];
105  VERIFY( c1 != c2 );
106}
107
108int main()
109{
110  test01();
111  return 0;
112}
113