]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/21_strings/char_traits/requirements/short/1.cc
Licensing changes to GPLv3 resp. GPLv3 with GCC Runtime Exception.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 21_strings / char_traits / requirements / short / 1.cc
CommitLineData
d5ff4e3f
MA
1// 1999-06-03 bkoz
2// 2003-07-22 Matt Austern
3
748086b7 4// Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009
1b716e90 5// Free Software Foundation, Inc.
d5ff4e3f
MA
6//
7// This file is part of the GNU ISO C++ Library. This library is free
8// software; you can redistribute it and/or modify it under the
9// terms of the GNU General Public License as published by the
748086b7 10// Free Software Foundation; either version 3, or (at your option)
d5ff4e3f
MA
11// any later version.
12
13// This library is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17
18// You should have received a copy of the GNU General Public License along
748086b7
JJ
19// with this library; see the file COPYING3. If not see
20// <http://www.gnu.org/licenses/>.
d5ff4e3f
MA
21
22// 21.1.1 Character traits requirements
23// Make sure we can instantiate char_traits and basic_string for
24// charT = 'short', and make sure the char_traits memeber functions
25// satisfy the requirements of 21.1.1.
26
27#include <string>
1b716e90 28#include <cstring>
d5ff4e3f
MA
29#include <testsuite_hooks.h>
30
31void test02(void)
32{
bd1a56a0 33 typedef short char_type;
11f10e6b 34 bool test __attribute__((unused)) = true;
d5ff4e3f
MA
35
36 // 21.1.1 character traits requirements
37
38 // Key for decoding what function signatures really mean:
39 // X == char_traits<_CharT>
40 // [c,d] == _CharT
41 // [p,q] == const _CharT*
42 // s == _CharT*
43 // [n,i,j] == size_t
44 // f == X::int_type
45 // pos == X::pos_type
46 // state == X::state_type
47
bd1a56a0 48 // void X::assign(char_type c, char_type d)
d5ff4e3f 49 // assigns c = d;
bd1a56a0
BK
50 char_type c1 = 'z';
51 char_type c2 = 'u';
d5ff4e3f 52 VERIFY( c1 != c2 );
bd1a56a0 53 std::char_traits<char_type>::assign(c1,c2);
d5ff4e3f
MA
54 VERIFY( c1 == 'u' );
55
bd1a56a0 56 // bool X::eq(char_type c, char_type d)
d5ff4e3f
MA
57 c1 = 'z';
58 c2 = 'u';
bd1a56a0
BK
59 VERIFY ( !std::char_traits<char_type>::eq(c1, c2) );
60 VERIFY ( std::char_traits<char_type>::eq(c1, c1) );
61 VERIFY ( std::char_traits<char_type>::eq(c2, c2) );
d5ff4e3f 62
bd1a56a0 63 // bool X::lt(char_type c, char_type d)
d5ff4e3f
MA
64 c1 = 'z';
65 c2 = 'u';
bd1a56a0
BK
66 VERIFY ( std::char_traits<char_type>::lt(c2, c1) );
67 VERIFY ( !std::char_traits<char_type>::lt(c1, c2) );
68 VERIFY ( !std::char_traits<char_type>::lt(c1, c1) );
69 VERIFY ( !std::char_traits<char_type>::lt(c2, c2) );
d5ff4e3f 70
bd1a56a0 71 // char_type* X::move(char_type* s, const char_type* p, size_t n)
d5ff4e3f
MA
72 // for each i in [0,n) performs X::assign(s[i], p[i]). Copies
73 // correctly even where p is in [s, s + n), and yields s.
bd1a56a0
BK
74 char_type array1[] = {'z', 'u', 'm', 'a', ' ', 'b', 'e', 'a', 'c', 'h', 0};
75 const std::basic_string<char_type> str_01(array1 + 0, array1 + 10);
d5ff4e3f 76
bd1a56a0 77 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};
d5ff4e3f 78
bd1a56a0 79 int len = sizeof(str_lit1)/sizeof(char_type) + sizeof(array1)/sizeof(char_type) - 1;
d5ff4e3f 80 // two terminating chars
bd1a56a0
BK
81 char_type array3[] = {'b', 'o', 'r', 'a', 'c', 'a', 'y', ',', ' ', 'p', 'h', 'i', 'l', 'i', 'p', 'p', 'i', 'n', 'e', 's', 0};
82 char_type array2[len];
83 std::char_traits<char_type>::copy(array2, array3, len);
d5ff4e3f
MA
84
85 VERIFY( str_lit1[0] == 'm' );
86 c1 = array2[0];
87 c2 = str_lit1[0];
bd1a56a0
BK
88 char_type c3 = array2[1];
89 char_type c4 = str_lit1[1];
90 std::char_traits<char_type>::move(array2, str_lit1, 0);
d5ff4e3f
MA
91 VERIFY( array2[0] == c1 );
92 VERIFY( str_lit1[0] == c2 );
bd1a56a0 93 std::char_traits<char_type>::move(array2, str_lit1, 1);
d5ff4e3f
MA
94 VERIFY( array2[0] == c2 );
95 VERIFY( str_lit1[0] == c2 );
96 VERIFY( array2[1] == c3 );
97 VERIFY( str_lit1[1] == c4 );
bd1a56a0 98 std::char_traits<char_type>::move(array2, str_lit1, 2);
d5ff4e3f
MA
99 VERIFY( array2[0] == c2 );
100 VERIFY( str_lit1[0] == c2 );
101 VERIFY( array2[1] == c4 );
102 VERIFY( str_lit1[1] == c4 );
103
bd1a56a0 104 char_type* pc1 = array1 + 1;
d5ff4e3f
MA
105 c1 = pc1[0];
106 c2 = array1[0];
107 VERIFY( c1 != c2 );
bd1a56a0 108 char_type* pc2 = std::char_traits<char_type>::move(array1, pc1, 0);
d5ff4e3f
MA
109 c3 = pc1[0];
110 c4 = array1[0];
111 VERIFY( c1 == c3 );
112 VERIFY( c2 == c4 );
113 VERIFY( pc2 == array1 );
114
115 c1 = pc1[0];
116 c2 = array1[0];
bd1a56a0
BK
117 char_type* pc3 = pc1;
118 pc2 = std::char_traits<char_type>::move(array1, pc1, 10);
d5ff4e3f
MA
119 c3 = pc1[0];
120 c4 = array1[0];
bd1a56a0 121 VERIFY( c1 != c3 ); // underlying char_type array changed.
d5ff4e3f
MA
122 VERIFY( c4 != c3 );
123 VERIFY( pc2 == array1 );
124 VERIFY( pc3 == pc1 ); // but pointers o-tay
125 c1 = *(str_01.data());
126 c2 = array1[0];
127 VERIFY( c1 != c2 );
128
bd1a56a0
BK
129 // size_t X::length(const char_type* p)
130 len = std::char_traits<char_type>::length(str_lit1);
131 VERIFY( len == sizeof(str_lit1) / sizeof(char_type) - 1 );
d5ff4e3f 132
bd1a56a0
BK
133 // const char_type* X::find(const char_type* s, size_t n, char_type c)
134 const int N4 = sizeof(str_lit1) / sizeof(char_type);
135 const char_type* pc4 = std::char_traits<char_type>::find(str_lit1, N4, 'a');
d5ff4e3f
MA
136 VERIFY( pc4 != 0 );
137 VERIFY( *pc4 == 'a' );
138
bd1a56a0 139 pc4 = std::char_traits<char_type>::find(str_lit1, N4, 0x0a73);
d5ff4e3f
MA
140 VERIFY( pc4 == 0 );
141
bd1a56a0
BK
142 // char_type* X::assign(char_type* s, size_t n, char_type c)
143 len = sizeof(array2) / sizeof(char_type);
144 std::memset(array2, 0xaf, len * sizeof(char_type));
d5ff4e3f
MA
145 VERIFY( array2[0] != 0x15a8 );
146
bd1a56a0 147 pc1 = std::char_traits<char_type>::assign (array2, len, 0x15a8);
d5ff4e3f
MA
148 VERIFY( pc1 == array2 );
149 for (int i = 0; i < len; ++i)
150 VERIFY( array2[i] == 0x15a8 );
151
bd1a56a0
BK
152 // char_type* X::copy(char_type* s, const char_type* p, size_t n)
153 int n1 = sizeof(str_lit1) / sizeof(char_type);
154 pc1 = std::char_traits<char_type>::copy(array2, str_lit1, n1);
155 len = std::char_traits<char_type>::length(array2);
d5ff4e3f
MA
156 VERIFY( len == n1 - 1 );
157 for (int i = 0; i < len; ++i)
158 VERIFY( str_lit1[i] == array2[i] );
159
bd1a56a0
BK
160 // int X::compare(const char_type* p, const char_type* q, size_t n)
161 const char_type* pconst1 = str_01.data();
162 const char_type* pconst2 = str_lit1;
d5ff4e3f 163
bd1a56a0
BK
164 VERIFY( std::char_traits<char_type>::compare(pconst1, pconst2, 10) > 0 );
165 VERIFY( std::char_traits<char_type>::compare(pconst2, pconst1, 10) < 0 );
166 VERIFY( std::char_traits<char_type>::compare(pconst1, pconst1, 10) == 0 );
167 VERIFY( std::char_traits<char_type>::compare(pconst2, pconst2, 10) == 0 );
d5ff4e3f
MA
168}
169
d5ff4e3f
MA
170int main()
171{
172 test02();
173 return 0;
174}