2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libstdc++-v3 / testsuite / 21_strings / char_traits / requirements / short / 1.cc
blobb38b3cff177c8fe3bc3ca117361ecaca8f862e8a
1 // 1999-06-03 bkoz
2 // 2003-07-22 Matt Austern
4 // Copyright (C) 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
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.
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.
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
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 // USA.
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.
27 #include <string>
28 #include <testsuite_hooks.h>
30 void test02(void)
32 bool test __attribute__((unused)) = true;
34 // 21.1.1 character traits requirements
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
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' );
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) );
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) );
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);
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};
77 int len = sizeof(str_lit1)/sizeof(short) + sizeof(array1)/sizeof(short) - 1;
78 // two terminating chars
79 short array2[len];
81 VERIFY( str_lit1[0] == 'm' );
82 c1 = array2[0];
83 c2 = str_lit1[0];
84 short c3 = array2[1];
85 short c4 = str_lit1[1];
86 std::char_traits<short>::move(array2, str_lit1, 0);
87 VERIFY( array2[0] == c1 );
88 VERIFY( str_lit1[0] == c2 );
89 std::char_traits<short>::move(array2, str_lit1, 1);
90 VERIFY( array2[0] == c2 );
91 VERIFY( str_lit1[0] == c2 );
92 VERIFY( array2[1] == c3 );
93 VERIFY( str_lit1[1] == c4 );
94 std::char_traits<short>::move(array2, str_lit1, 2);
95 VERIFY( array2[0] == c2 );
96 VERIFY( str_lit1[0] == c2 );
97 VERIFY( array2[1] == c4 );
98 VERIFY( str_lit1[1] == c4 );
100 short* pc1 = array1 + 1;
101 c1 = pc1[0];
102 c2 = array1[0];
103 VERIFY( c1 != c2 );
104 short* pc2 = std::char_traits<short>::move(array1, pc1, 0);
105 c3 = pc1[0];
106 c4 = array1[0];
107 VERIFY( c1 == c3 );
108 VERIFY( c2 == c4 );
109 VERIFY( pc2 == array1 );
111 c1 = pc1[0];
112 c2 = array1[0];
113 short* pc3 = pc1;
114 pc2 = std::char_traits<short>::move(array1, pc1, 10);
115 c3 = pc1[0];
116 c4 = array1[0];
117 VERIFY( c1 != c3 ); // underlying short array changed.
118 VERIFY( c4 != c3 );
119 VERIFY( pc2 == array1 );
120 VERIFY( pc3 == pc1 ); // but pointers o-tay
121 c1 = *(str_01.data());
122 c2 = array1[0];
123 VERIFY( c1 != c2 );
125 // size_t X::length(const short* p)
126 len = std::char_traits<short>::length(str_lit1);
127 VERIFY( len == sizeof(str_lit1) / sizeof(short) - 1 );
129 // const short* X::find(const short* s, size_t n, short c)
130 const int N4 = sizeof(str_lit1) / sizeof(short);
131 const short* pc4 = std::char_traits<short>::find(str_lit1, N4, 'a');
132 VERIFY( pc4 != 0 );
133 VERIFY( *pc4 == 'a' );
135 pc4 = std::char_traits<short>::find(str_lit1, N4, 0x0a73);
136 VERIFY( pc4 == 0 );
138 // short* X::assign(short* s, size_t n, short c)
139 len = sizeof(array2) / sizeof(short);
140 memset(array2, 0xaf, len * sizeof(short));
141 VERIFY( array2[0] != 0x15a8 );
143 pc1 = std::char_traits<short>::assign (array2, len, 0x15a8);
144 VERIFY( pc1 == array2 );
145 for (int i = 0; i < len; ++i)
146 VERIFY( array2[i] == 0x15a8 );
148 // short* X::copy(short* s, const short* p, size_t n)
149 int n1 = sizeof(str_lit1) / sizeof(short);
150 pc1 = std::char_traits<short>::copy(array2, str_lit1, n1);
151 len = std::char_traits<short>::length(array2);
152 VERIFY( len == n1 - 1 );
153 for (int i = 0; i < len; ++i)
154 VERIFY( str_lit1[i] == array2[i] );
156 // int X::compare(const short* p, const short* q, size_t n)
157 const short* pconst1 = str_01.data();
158 const short* pconst2 = str_lit1;
160 VERIFY( std::char_traits<short>::compare(pconst1, pconst2, 10) > 0 );
161 VERIFY( std::char_traits<short>::compare(pconst2, pconst1, 10) < 0 );
162 VERIFY( std::char_traits<short>::compare(pconst1, pconst1, 10) == 0 );
163 VERIFY( std::char_traits<short>::compare(pconst2, pconst2, 10) == 0 );
168 int main()
170 test02();
171 return 0;