summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/21_strings/char_traits/requirements/short/1.cc
blob: 3f47f9f48531ebb66f03c331e5000ac4fb71de2c (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
// 1999-06-03 bkoz
// 2003-07-22 Matt Austern

// Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009
// 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 3, 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 COPYING3.  If not see
// <http://www.gnu.org/licenses/>.

// 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)
{
  typedef short char_type;
  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(char_type c, char_type d)
  // assigns c = d;
  char_type c1 = 'z';
  char_type c2 = 'u';
  VERIFY( c1 != c2 );
  std::char_traits<char_type>::assign(c1,c2);
  VERIFY( c1 == 'u' );

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

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

  // char_type* X::move(char_type* s, const char_type* 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.   
  char_type array1[] = {'z', 'u', 'm', 'a', ' ', 'b', 'e', 'a', 'c', 'h',  0};
  const std::basic_string<char_type> str_01(array1 + 0, array1 + 10);

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

  VERIFY( str_lit1[0] == 'm' );
  c1 = array2[0];
  c2 = str_lit1[0];
  char_type c3 = array2[1];
  char_type c4 = str_lit1[1];
  std::char_traits<char_type>::move(array2, str_lit1, 0);
  VERIFY( array2[0] == c1 );
  VERIFY( str_lit1[0] == c2 );
  std::char_traits<char_type>::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<char_type>::move(array2, str_lit1, 2);
  VERIFY( array2[0] == c2 );
  VERIFY( str_lit1[0] == c2 );
  VERIFY( array2[1] == c4 );
  VERIFY( str_lit1[1] == c4 );
 
  char_type* pc1 = array1 + 1;
  c1 = pc1[0];
  c2 = array1[0];
  VERIFY( c1 != c2 );
  char_type* pc2 = std::char_traits<char_type>::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];
  char_type* pc3 = pc1;
  pc2 = std::char_traits<char_type>::move(array1, pc1, 10);
  c3 = pc1[0];
  c4 = array1[0];
  VERIFY( c1 != c3 ); // underlying char_type 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 char_type* p)
  len = std::char_traits<char_type>::length(str_lit1);
  VERIFY( len == sizeof(str_lit1) / sizeof(char_type) - 1 );

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

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

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

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

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

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

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

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