]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/21_strings/char_traits/requirements/short/1.cc
2 // 2003-07-22 Matt Austern
4 // Copyright (C) 1999-2013 Free Software Foundation, Inc.
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 3, or (at your option)
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 COPYING3. If not see
19 // <http://www.gnu.org/licenses/>.
21 // 21.1.1 Character traits requirements
22 // Make sure we can instantiate char_traits and basic_string for
23 // charT = 'short', and make sure the char_traits memeber functions
24 // satisfy the requirements of 21.1.1.
28 #include <testsuite_hooks.h>
32 typedef short char_type
;
33 bool test
__attribute__((unused
)) = true;
35 // 21.1.1 character traits requirements
37 // Key for decoding what function signatures really mean:
38 // X == char_traits<_CharT>
40 // [p,q] == const _CharT*
45 // state == X::state_type
47 // void X::assign(char_type c, char_type d)
52 std::char_traits
<char_type
>::assign(c1
,c2
);
55 // bool X::eq(char_type c, char_type d)
58 VERIFY ( !std::char_traits
<char_type
>::eq(c1
, c2
) );
59 VERIFY ( std::char_traits
<char_type
>::eq(c1
, c1
) );
60 VERIFY ( std::char_traits
<char_type
>::eq(c2
, c2
) );
62 // bool X::lt(char_type c, char_type d)
65 VERIFY ( std::char_traits
<char_type
>::lt(c2
, c1
) );
66 VERIFY ( !std::char_traits
<char_type
>::lt(c1
, c2
) );
67 VERIFY ( !std::char_traits
<char_type
>::lt(c1
, c1
) );
68 VERIFY ( !std::char_traits
<char_type
>::lt(c2
, c2
) );
70 // char_type* X::move(char_type* s, const char_type* p, size_t n)
71 // for each i in [0,n) performs X::assign(s[i], p[i]). Copies
72 // correctly even where p is in [s, s + n), and yields s.
73 char_type array1
[] = {'z', 'u', 'm', 'a', ' ', 'b', 'e', 'a', 'c', 'h', 0};
74 const std::basic_string
<char_type
> str_01(array1
+ 0, array1
+ 10);
76 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};
78 int len
= sizeof(str_lit1
)/sizeof(char_type
) + sizeof(array1
)/sizeof(char_type
) - 1;
79 // two terminating chars
80 char_type array3
[] = {'b', 'o', 'r', 'a', 'c', 'a', 'y', ',', ' ', 'p', 'h', 'i', 'l', 'i', 'p', 'p', 'i', 'n', 'e', 's', 0};
81 char_type array2
[len
];
82 std::char_traits
<char_type
>::copy(array2
, array3
, len
);
84 VERIFY( str_lit1
[0] == 'm' );
87 char_type c3
= array2
[1];
88 char_type c4
= str_lit1
[1];
89 std::char_traits
<char_type
>::move(array2
, str_lit1
, 0);
90 VERIFY( array2
[0] == c1
);
91 VERIFY( str_lit1
[0] == c2
);
92 std::char_traits
<char_type
>::move(array2
, str_lit1
, 1);
93 VERIFY( array2
[0] == c2
);
94 VERIFY( str_lit1
[0] == c2
);
95 VERIFY( array2
[1] == c3
);
96 VERIFY( str_lit1
[1] == c4
);
97 std::char_traits
<char_type
>::move(array2
, str_lit1
, 2);
98 VERIFY( array2
[0] == c2
);
99 VERIFY( str_lit1
[0] == c2
);
100 VERIFY( array2
[1] == c4
);
101 VERIFY( str_lit1
[1] == c4
);
103 char_type
* pc1
= array1
+ 1;
107 char_type
* pc2
= std::char_traits
<char_type
>::move(array1
, pc1
, 0);
112 VERIFY( pc2
== array1
);
116 char_type
* pc3
= pc1
;
117 pc2
= std::char_traits
<char_type
>::move(array1
, pc1
, 10);
120 VERIFY( c1
!= c3
); // underlying char_type array changed.
122 VERIFY( pc2
== array1
);
123 VERIFY( pc3
== pc1
); // but pointers o-tay
124 c1
= *(str_01
.data());
128 // size_t X::length(const char_type* p)
129 len
= std::char_traits
<char_type
>::length(str_lit1
);
130 VERIFY( len
== sizeof(str_lit1
) / sizeof(char_type
) - 1 );
132 // const char_type* X::find(const char_type* s, size_t n, char_type c)
133 const int N4
= sizeof(str_lit1
) / sizeof(char_type
);
134 const char_type
* pc4
= std::char_traits
<char_type
>::find(str_lit1
, N4
, 'a');
136 VERIFY( *pc4
== 'a' );
138 pc4
= std::char_traits
<char_type
>::find(str_lit1
, N4
, 0x0a73);
141 // char_type* X::assign(char_type* s, size_t n, char_type c)
142 len
= sizeof(array2
) / sizeof(char_type
);
143 std::memset(array2
, 0xaf, len
* sizeof(char_type
));
144 VERIFY( array2
[0] != 0x15a8 );
146 pc1
= std::char_traits
<char_type
>::assign (array2
, len
, 0x15a8);
147 VERIFY( pc1
== array2
);
148 for (int i
= 0; i
< len
; ++i
)
149 VERIFY( array2
[i
] == 0x15a8 );
151 // char_type* X::copy(char_type* s, const char_type* p, size_t n)
152 int n1
= sizeof(str_lit1
) / sizeof(char_type
);
153 pc1
= std::char_traits
<char_type
>::copy(array2
, str_lit1
, n1
);
154 len
= std::char_traits
<char_type
>::length(array2
);
155 VERIFY( len
== n1
- 1 );
156 for (int i
= 0; i
< len
; ++i
)
157 VERIFY( str_lit1
[i
] == array2
[i
] );
159 // int X::compare(const char_type* p, const char_type* q, size_t n)
160 const char_type
* pconst1
= str_01
.data();
161 const char_type
* pconst2
= str_lit1
;
163 VERIFY( std::char_traits
<char_type
>::compare(pconst1
, pconst2
, 10) > 0 );
164 VERIFY( std::char_traits
<char_type
>::compare(pconst2
, pconst1
, 10) < 0 );
165 VERIFY( std::char_traits
<char_type
>::compare(pconst1
, pconst1
, 10) == 0 );
166 VERIFY( std::char_traits
<char_type
>::compare(pconst2
, pconst2
, 10) == 0 );