aboutsummaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/21_strings/char_traits/requirements/short/1.cc
blob: 3a0394daf1ff71c749cd5a02984fae0c669e3f63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// 1999-06-03 bkoz
// 2003-07-22 Matt Austern

// Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
// Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING.  If not, write to the Free
// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA.

// 21.1.1 Character traits requirements
// Make sure we can instantiate char_traits and basic_string for
// charT = 'short', and make sure the char_traits memeber functions
// satisfy the requirements of 21.1.1.

#include <string>
#include <cstring>
#include <testsuite_hooks.h>

void test02(void)
{
  bool test __attribute__((unused)) = true;
 
  // 21.1.1 character traits requirements

  // Key for decoding what function signatures really mean:
  // X                == char_traits<_CharT>
  // [c,d]    == _CharT
  // [p,q]    == const _CharT*
  // s                == _CharT*
  // [n,i,j]  == size_t
  // f                == X::int_type
  // pos      == X::pos_type
  // state    == X::state_type

  // void X::assign(short c, short d)
  // assigns c = d;
  short c1 = 'z';
  short c2 = 'u';
  VERIFY( c1 != c2 );
  std::char_traits<short>::assign(c1,c2);
  VERIFY( c1 == 'u' );

  // bool X::eq(short c, short d)
  c1 = 'z';
  c2 = 'u';
  VERIFY ( !std::char_traits<short>::eq(c1, c2) );
  VERIFY ( std::char_traits<short>::eq(c1, c1) );
  VERIFY ( std::char_traits<short>::eq(c2, c2) );

  // bool X::lt(short c, short d)
  c1 = 'z';
  c2 = 'u';
  VERIFY ( std::char_traits<short>::lt(c2, c1) );
  VERIFY ( !std::char_traits<short>::lt(c1, c2) );
  VERIFY ( !std::char_traits<short>::lt(c1, c1) );
  VERIFY ( !std::char_traits<short>::lt(c2, c2) );

  // short* X::move(short* s, const short* p, size_t n)
  // for each i in [0,n) performs X::assign(s[i], p[i]). Copies
  // correctly even where p is in [s, s + n), and yields s.   
  short array1[] = {'z', 'u', 'm', 'a', ' ', 'b', 'e', 'a', 'c', 'h',  0};
  const std::basic_string<short> str_01(array1 + 0, array1 + 10);

  const short str_lit1[] = {'m', 'o', 'n', 't', 'a', 'r', 'a', ' ', 'a', 'n', 'd', ' ', 'o', 'c', 'e', 'a', 'n', ' ', 'b', 'e', 'a', 'c', 'h', 0};

  int len = sizeof(str_lit1)/sizeof(short) + sizeof(array1)/sizeof(short) - 1;
  // two terminating chars
  short array3[] = {'b', 'o', 'r', 'a', 'c', 'a', 'y', ',', ' ', 'p', 'h', 'i', 'l', 'i', 'p', 'p', 'i', 'n', 'e', 's', 0};
  short array2[len];
  std::char_traits<short>::copy(array2, array3, len);

  VERIFY( str_lit1[0] == 'm' );
  c1 = array2[0];
  c2 = str_lit1[0];
  short c3 = array2[1];
  short c4 = str_lit1[1];
  std::char_traits<short>::move(array2, str_lit1, 0);
  VERIFY( array2[0] == c1 );
  VERIFY( str_lit1[0] == c2 );
  std::char_traits<short>::move(array2, str_lit1, 1);
  VERIFY( array2[0] == c2 );
  VERIFY( str_lit1[0] == c2 );
  VERIFY( array2[1] == c3 );
  VERIFY( str_lit1[1] == c4 );
  std::char_traits<short>::move(array2, str_lit1, 2);
  VERIFY( array2[0] == c2 );
  VERIFY( str_lit1[0] == c2 );
  VERIFY( array2[1] == c4 );
  VERIFY( str_lit1[1] == c4 );
 
  short* pc1 = array1 + 1;
  c1 = pc1[0];
  c2 = array1[0];
  VERIFY( c1 != c2 );
  short* pc2 = std::char_traits<short>::move(array1, pc1, 0);
  c3 = pc1[0];
  c4 = array1[0];
  VERIFY( c1 == c3 );
  VERIFY( c2 == c4 );
  VERIFY( pc2 == array1 );

  c1 = pc1[0];
  c2 = array1[0];
  short* pc3 = pc1;
  pc2 = std::char_traits<short>::move(array1, pc1, 10);
  c3 = pc1[0];
  c4 = array1[0];
  VERIFY( c1 != c3 ); // underlying short array changed.
  VERIFY( c4 != c3 );
  VERIFY( pc2 == array1 );
  VERIFY( pc3 == pc1 ); // but pointers o-tay
  c1 = *(str_01.data());
  c2 = array1[0];
  VERIFY( c1 != c2 );

  // size_t X::length(const short* p)
  len = std::char_traits<short>::length(str_lit1);
  VERIFY( len == sizeof(str_lit1) / sizeof(short) - 1 );

  // const short* X::find(const short* s, size_t n, short c)
  const int N4 = sizeof(str_lit1) / sizeof(short);
  const short* pc4 = std::char_traits<short>::find(str_lit1, N4, 'a');
  VERIFY( pc4 != 0 );
  VERIFY( *pc4 == 'a' );

  pc4 = std::char_traits<short>::find(str_lit1, N4, 0x0a73);
  VERIFY( pc4 == 0 );

  // short* X::assign(short* s, size_t n, short c)
  len = sizeof(array2) / sizeof(short);
  std::memset(array2, 0xaf, len * sizeof(short));
  VERIFY( array2[0] != 0x15a8 );

  pc1 = std::char_traits<short>::assign (array2, len, 0x15a8);
  VERIFY( pc1 == array2 );
  for (int i = 0; i < len; ++i)
    VERIFY( array2[i] == 0x15a8 );

  // short* X::copy(short* s, const short* p, size_t n)
  int n1 = sizeof(str_lit1) / sizeof(short);
  pc1 = std::char_traits<short>::copy(array2, str_lit1, n1);
  len = std::char_traits<short>::length(array2);
  VERIFY( len == n1 - 1 );
  for (int i = 0; i < len; ++i)
    VERIFY( str_lit1[i] == array2[i] );

  // int X::compare(const short* p, const short* q, size_t n)
  const short* pconst1 = str_01.data();
  const short* pconst2 = str_lit1;

  VERIFY( std::char_traits<short>::compare(pconst1, pconst2, 10) > 0 );
  VERIFY( std::char_traits<short>::compare(pconst2, pconst1, 10) < 0 );
  VERIFY( std::char_traits<short>::compare(pconst1, pconst1, 10) == 0 );
  VERIFY( std::char_traits<short>::compare(pconst2, pconst2, 10) == 0 );
}



int main()
{ 
  test02();
  return 0;
}