Merge -r 127928:132243 from trunk
[official-gcc.git] / libstdc++-v3 / testsuite / 21_strings / char_traits / requirements / short / 1.cc
blob2e1b631f75c84a6daf437b5a59871c0855e77914
1 // 1999-06-03 bkoz
2 // 2003-07-22 Matt Austern
4 // Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
5 // Free Software Foundation, Inc.
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
10 // Free Software Foundation; either version 2, or (at your option)
11 // any later version.
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.
18 // You should have received a copy of the GNU General Public License along
19 // with this library; see the file COPYING. If not, write to the Free
20 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // USA.
23 // 21.1.1 Character traits requirements
24 // Make sure we can instantiate char_traits and basic_string for
25 // charT = 'short', and make sure the char_traits memeber functions
26 // satisfy the requirements of 21.1.1.
28 #include <string>
29 #include <cstring>
30 #include <testsuite_hooks.h>
32 void test02(void)
34 typedef short char_type;
35 bool test __attribute__((unused)) = true;
37 // 21.1.1 character traits requirements
39 // Key for decoding what function signatures really mean:
40 // X == char_traits<_CharT>
41 // [c,d] == _CharT
42 // [p,q] == const _CharT*
43 // s == _CharT*
44 // [n,i,j] == size_t
45 // f == X::int_type
46 // pos == X::pos_type
47 // state == X::state_type
49 // void X::assign(char_type c, char_type d)
50 // assigns c = d;
51 char_type c1 = 'z';
52 char_type c2 = 'u';
53 VERIFY( c1 != c2 );
54 std::char_traits<char_type>::assign(c1,c2);
55 VERIFY( c1 == 'u' );
57 // bool X::eq(char_type c, char_type d)
58 c1 = 'z';
59 c2 = 'u';
60 VERIFY ( !std::char_traits<char_type>::eq(c1, c2) );
61 VERIFY ( std::char_traits<char_type>::eq(c1, c1) );
62 VERIFY ( std::char_traits<char_type>::eq(c2, c2) );
64 // bool X::lt(char_type c, char_type d)
65 c1 = 'z';
66 c2 = 'u';
67 VERIFY ( std::char_traits<char_type>::lt(c2, c1) );
68 VERIFY ( !std::char_traits<char_type>::lt(c1, c2) );
69 VERIFY ( !std::char_traits<char_type>::lt(c1, c1) );
70 VERIFY ( !std::char_traits<char_type>::lt(c2, c2) );
72 // char_type* X::move(char_type* s, const char_type* p, size_t n)
73 // for each i in [0,n) performs X::assign(s[i], p[i]). Copies
74 // correctly even where p is in [s, s + n), and yields s.
75 char_type array1[] = {'z', 'u', 'm', 'a', ' ', 'b', 'e', 'a', 'c', 'h', 0};
76 const std::basic_string<char_type> str_01(array1 + 0, array1 + 10);
78 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};
80 int len = sizeof(str_lit1)/sizeof(char_type) + sizeof(array1)/sizeof(char_type) - 1;
81 // two terminating chars
82 char_type array3[] = {'b', 'o', 'r', 'a', 'c', 'a', 'y', ',', ' ', 'p', 'h', 'i', 'l', 'i', 'p', 'p', 'i', 'n', 'e', 's', 0};
83 char_type array2[len];
84 std::char_traits<char_type>::copy(array2, array3, len);
86 VERIFY( str_lit1[0] == 'm' );
87 c1 = array2[0];
88 c2 = str_lit1[0];
89 char_type c3 = array2[1];
90 char_type c4 = str_lit1[1];
91 std::char_traits<char_type>::move(array2, str_lit1, 0);
92 VERIFY( array2[0] == c1 );
93 VERIFY( str_lit1[0] == c2 );
94 std::char_traits<char_type>::move(array2, str_lit1, 1);
95 VERIFY( array2[0] == c2 );
96 VERIFY( str_lit1[0] == c2 );
97 VERIFY( array2[1] == c3 );
98 VERIFY( str_lit1[1] == c4 );
99 std::char_traits<char_type>::move(array2, str_lit1, 2);
100 VERIFY( array2[0] == c2 );
101 VERIFY( str_lit1[0] == c2 );
102 VERIFY( array2[1] == c4 );
103 VERIFY( str_lit1[1] == c4 );
105 char_type* pc1 = array1 + 1;
106 c1 = pc1[0];
107 c2 = array1[0];
108 VERIFY( c1 != c2 );
109 char_type* pc2 = std::char_traits<char_type>::move(array1, pc1, 0);
110 c3 = pc1[0];
111 c4 = array1[0];
112 VERIFY( c1 == c3 );
113 VERIFY( c2 == c4 );
114 VERIFY( pc2 == array1 );
116 c1 = pc1[0];
117 c2 = array1[0];
118 char_type* pc3 = pc1;
119 pc2 = std::char_traits<char_type>::move(array1, pc1, 10);
120 c3 = pc1[0];
121 c4 = array1[0];
122 VERIFY( c1 != c3 ); // underlying char_type array changed.
123 VERIFY( c4 != c3 );
124 VERIFY( pc2 == array1 );
125 VERIFY( pc3 == pc1 ); // but pointers o-tay
126 c1 = *(str_01.data());
127 c2 = array1[0];
128 VERIFY( c1 != c2 );
130 // size_t X::length(const char_type* p)
131 len = std::char_traits<char_type>::length(str_lit1);
132 VERIFY( len == sizeof(str_lit1) / sizeof(char_type) - 1 );
134 // const char_type* X::find(const char_type* s, size_t n, char_type c)
135 const int N4 = sizeof(str_lit1) / sizeof(char_type);
136 const char_type* pc4 = std::char_traits<char_type>::find(str_lit1, N4, 'a');
137 VERIFY( pc4 != 0 );
138 VERIFY( *pc4 == 'a' );
140 pc4 = std::char_traits<char_type>::find(str_lit1, N4, 0x0a73);
141 VERIFY( pc4 == 0 );
143 // char_type* X::assign(char_type* s, size_t n, char_type c)
144 len = sizeof(array2) / sizeof(char_type);
145 std::memset(array2, 0xaf, len * sizeof(char_type));
146 VERIFY( array2[0] != 0x15a8 );
148 pc1 = std::char_traits<char_type>::assign (array2, len, 0x15a8);
149 VERIFY( pc1 == array2 );
150 for (int i = 0; i < len; ++i)
151 VERIFY( array2[i] == 0x15a8 );
153 // char_type* X::copy(char_type* s, const char_type* p, size_t n)
154 int n1 = sizeof(str_lit1) / sizeof(char_type);
155 pc1 = std::char_traits<char_type>::copy(array2, str_lit1, n1);
156 len = std::char_traits<char_type>::length(array2);
157 VERIFY( len == n1 - 1 );
158 for (int i = 0; i < len; ++i)
159 VERIFY( str_lit1[i] == array2[i] );
161 // int X::compare(const char_type* p, const char_type* q, size_t n)
162 const char_type* pconst1 = str_01.data();
163 const char_type* pconst2 = str_lit1;
165 VERIFY( std::char_traits<char_type>::compare(pconst1, pconst2, 10) > 0 );
166 VERIFY( std::char_traits<char_type>::compare(pconst2, pconst1, 10) < 0 );
167 VERIFY( std::char_traits<char_type>::compare(pconst1, pconst1, 10) == 0 );
168 VERIFY( std::char_traits<char_type>::compare(pconst2, pconst2, 10) == 0 );
171 int main()
173 test02();
174 return 0;