]>
Commit | Line | Data |
---|---|---|
d5ff4e3f MA |
1 | // 1999-06-03 bkoz |
2 | // 2003-07-22 Matt Austern | |
3 | ||
3dd50c74 | 4 | // Copyright (C) 1999, 2000, 2001, 2003, 2004 Free Software Foundation, Inc. |
d5ff4e3f MA |
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 2, 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 COPYING. If not, write to the Free | |
83f51799 | 19 | // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
d5ff4e3f MA |
20 | // USA. |
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> | |
28 | #include <testsuite_hooks.h> | |
29 | ||
30 | void test02(void) | |
31 | { | |
11f10e6b | 32 | bool test __attribute__((unused)) = true; |
d5ff4e3f MA |
33 | |
34 | // 21.1.1 character traits requirements | |
35 | ||
36 | // Key for decoding what function signatures really mean: | |
37 | // X == char_traits<_CharT> | |
38 | // [c,d] == _CharT | |
39 | // [p,q] == const _CharT* | |
40 | // s == _CharT* | |
41 | // [n,i,j] == size_t | |
42 | // f == X::int_type | |
43 | // pos == X::pos_type | |
44 | // state == X::state_type | |
45 | ||
46 | // void X::assign(short c, short d) | |
47 | // assigns c = d; | |
48 | short c1 = 'z'; | |
49 | short c2 = 'u'; | |
50 | VERIFY( c1 != c2 ); | |
51 | std::char_traits<short>::assign(c1,c2); | |
52 | VERIFY( c1 == 'u' ); | |
53 | ||
54 | // bool X::eq(short c, short d) | |
55 | c1 = 'z'; | |
56 | c2 = 'u'; | |
57 | VERIFY ( !std::char_traits<short>::eq(c1, c2) ); | |
58 | VERIFY ( std::char_traits<short>::eq(c1, c1) ); | |
59 | VERIFY ( std::char_traits<short>::eq(c2, c2) ); | |
60 | ||
61 | // bool X::lt(short c, short d) | |
62 | c1 = 'z'; | |
63 | c2 = 'u'; | |
64 | VERIFY ( std::char_traits<short>::lt(c2, c1) ); | |
65 | VERIFY ( !std::char_traits<short>::lt(c1, c2) ); | |
66 | VERIFY ( !std::char_traits<short>::lt(c1, c1) ); | |
67 | VERIFY ( !std::char_traits<short>::lt(c2, c2) ); | |
68 | ||
69 | // short* X::move(short* s, const short* p, size_t n) | |
70 | // for each i in [0,n) performs X::assign(s[i], p[i]). Copies | |
71 | // correctly even where p is in [s, s + n), and yields s. | |
72 | short array1[] = {'z', 'u', 'm', 'a', ' ', 'b', 'e', 'a', 'c', 'h', 0}; | |
73 | const std::basic_string<short> str_01(array1 + 0, array1 + 10); | |
74 | ||
75 | 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}; | |
76 | ||
77 | int len = sizeof(str_lit1)/sizeof(short) + sizeof(array1)/sizeof(short) - 1; | |
78 | // two terminating chars | |
3dd50c74 | 79 | short array3[] = {'b', 'o', 'r', 'a', 'c', 'a', 'y', ',', ' ', 'p', 'h', 'i', 'l', 'i', 'p', 'p', 'i', 'n', 'e', 's', 0}; |
d5ff4e3f | 80 | short array2[len]; |
3dd50c74 | 81 | std::char_traits<short>::copy(array2, array3, len); |
d5ff4e3f MA |
82 | |
83 | VERIFY( str_lit1[0] == 'm' ); | |
84 | c1 = array2[0]; | |
85 | c2 = str_lit1[0]; | |
86 | short c3 = array2[1]; | |
87 | short c4 = str_lit1[1]; | |
88 | std::char_traits<short>::move(array2, str_lit1, 0); | |
89 | VERIFY( array2[0] == c1 ); | |
90 | VERIFY( str_lit1[0] == c2 ); | |
91 | std::char_traits<short>::move(array2, str_lit1, 1); | |
92 | VERIFY( array2[0] == c2 ); | |
93 | VERIFY( str_lit1[0] == c2 ); | |
94 | VERIFY( array2[1] == c3 ); | |
95 | VERIFY( str_lit1[1] == c4 ); | |
96 | std::char_traits<short>::move(array2, str_lit1, 2); | |
97 | VERIFY( array2[0] == c2 ); | |
98 | VERIFY( str_lit1[0] == c2 ); | |
99 | VERIFY( array2[1] == c4 ); | |
100 | VERIFY( str_lit1[1] == c4 ); | |
101 | ||
102 | short* pc1 = array1 + 1; | |
103 | c1 = pc1[0]; | |
104 | c2 = array1[0]; | |
105 | VERIFY( c1 != c2 ); | |
106 | short* pc2 = std::char_traits<short>::move(array1, pc1, 0); | |
107 | c3 = pc1[0]; | |
108 | c4 = array1[0]; | |
109 | VERIFY( c1 == c3 ); | |
110 | VERIFY( c2 == c4 ); | |
111 | VERIFY( pc2 == array1 ); | |
112 | ||
113 | c1 = pc1[0]; | |
114 | c2 = array1[0]; | |
115 | short* pc3 = pc1; | |
116 | pc2 = std::char_traits<short>::move(array1, pc1, 10); | |
117 | c3 = pc1[0]; | |
118 | c4 = array1[0]; | |
119 | VERIFY( c1 != c3 ); // underlying short array changed. | |
120 | VERIFY( c4 != c3 ); | |
121 | VERIFY( pc2 == array1 ); | |
122 | VERIFY( pc3 == pc1 ); // but pointers o-tay | |
123 | c1 = *(str_01.data()); | |
124 | c2 = array1[0]; | |
125 | VERIFY( c1 != c2 ); | |
126 | ||
127 | // size_t X::length(const short* p) | |
128 | len = std::char_traits<short>::length(str_lit1); | |
129 | VERIFY( len == sizeof(str_lit1) / sizeof(short) - 1 ); | |
130 | ||
131 | // const short* X::find(const short* s, size_t n, short c) | |
132 | const int N4 = sizeof(str_lit1) / sizeof(short); | |
133 | const short* pc4 = std::char_traits<short>::find(str_lit1, N4, 'a'); | |
134 | VERIFY( pc4 != 0 ); | |
135 | VERIFY( *pc4 == 'a' ); | |
136 | ||
137 | pc4 = std::char_traits<short>::find(str_lit1, N4, 0x0a73); | |
138 | VERIFY( pc4 == 0 ); | |
139 | ||
140 | // short* X::assign(short* s, size_t n, short c) | |
141 | len = sizeof(array2) / sizeof(short); | |
142 | memset(array2, 0xaf, len * sizeof(short)); | |
143 | VERIFY( array2[0] != 0x15a8 ); | |
144 | ||
145 | pc1 = std::char_traits<short>::assign (array2, len, 0x15a8); | |
146 | VERIFY( pc1 == array2 ); | |
147 | for (int i = 0; i < len; ++i) | |
148 | VERIFY( array2[i] == 0x15a8 ); | |
149 | ||
150 | // short* X::copy(short* s, const short* p, size_t n) | |
151 | int n1 = sizeof(str_lit1) / sizeof(short); | |
152 | pc1 = std::char_traits<short>::copy(array2, str_lit1, n1); | |
153 | len = std::char_traits<short>::length(array2); | |
154 | VERIFY( len == n1 - 1 ); | |
155 | for (int i = 0; i < len; ++i) | |
156 | VERIFY( str_lit1[i] == array2[i] ); | |
157 | ||
158 | // int X::compare(const short* p, const short* q, size_t n) | |
159 | const short* pconst1 = str_01.data(); | |
160 | const short* pconst2 = str_lit1; | |
161 | ||
162 | VERIFY( std::char_traits<short>::compare(pconst1, pconst2, 10) > 0 ); | |
163 | VERIFY( std::char_traits<short>::compare(pconst2, pconst1, 10) < 0 ); | |
164 | VERIFY( std::char_traits<short>::compare(pconst1, pconst1, 10) == 0 ); | |
165 | VERIFY( std::char_traits<short>::compare(pconst2, pconst2, 10) == 0 ); | |
166 | } | |
167 | ||
168 | ||
169 | ||
170 | int main() | |
171 | { | |
172 | test02(); | |
173 | return 0; | |
174 | } |