| 1 |
// 1999-06-03 bkoz |
| 2 |
|
| 3 |
// Copyright (C) 1999-2025 Free Software Foundation, Inc. |
| 4 |
// |
| 5 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 6 |
// software; you can redistribute it and/or modify it under the |
| 7 |
// terms of the GNU General Public License as published by the |
| 8 |
// Free Software Foundation; either version 3, or (at your option) |
| 9 |
// any later version. |
| 10 |
|
| 11 |
// This library is distributed in the hope that it will be useful, |
| 12 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
// GNU General Public License for more details. |
| 15 |
|
| 16 |
// You should have received a copy of the GNU General Public License along |
| 17 |
// with this library; see the file COPYING3. If not see |
| 18 |
// <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
// 21.1.1 Characher traits requirements |
| 21 |
|
| 22 |
#include <string> |
| 23 |
#include <testsuite_hooks.h> |
| 24 |
|
| 25 |
void test01(void) |
| 26 |
{ |
| 27 |
const std::string str_01("zuma beach"); |
| 28 |
const std::string str_02("montara and ocean beach"); |
| 29 |
|
| 30 |
// 21.1.1 character traits requirements |
| 31 |
|
| 32 |
// Key for decoding what function signatures really mean: |
| 33 |
// X == char_traits<_CharT> |
| 34 |
// [c,d] == _CharT |
| 35 |
// [p,q] == const _CharT* |
| 36 |
// s == _CharT* |
| 37 |
// [n,i,j] == size_t |
| 38 |
// f == X::int_type |
| 39 |
// pos == X::pos_type |
| 40 |
// state == X::state_type |
| 41 |
|
| 42 |
// void X::assign(char c, char d) |
| 43 |
// assigns c = d; |
| 44 |
char c1 = 'z'; |
| 45 |
char c2 = 'u'; |
| 46 |
VERIFY( c1 != c2 ); |
| 47 |
std::char_traits<char>::assign(c1,c2); |
| 48 |
VERIFY( c1 == 'u' ); |
| 49 |
|
| 50 |
// char* X::move(char* s, const char* p, size_t n) |
| 51 |
// for each i in [0,n) performs X::assign(s[i], p[i]). Copies |
| 52 |
// correctly even where p is in [s, s + n), and yields s. |
| 53 |
char array1[] = {'z', 'u', 'm', 'a', ' ', 'b', 'e', 'a', 'c', 'h', 0}; |
| 54 |
const char str_lit1[] = "montara and ocean beach"; |
| 55 |
const char str_lit2[] = "boracay, philippines"; |
| 56 |
const int len1 = sizeof(str_lit1)/sizeof(char); |
| 57 |
const int len2 = sizeof(str_lit2)/sizeof(char); |
| 58 |
char array2[len1 + len2 - 1]; // two terminating chars |
| 59 |
std::char_traits<char>::copy(array2, str_lit2, len2); |
| 60 |
|
| 61 |
VERIFY( str_lit1[0] == 'm' ); |
| 62 |
c1 = array2[0]; |
| 63 |
c2 = str_lit1[0]; |
| 64 |
char c3 = array2[1]; |
| 65 |
char c4 = str_lit1[1]; |
| 66 |
std::char_traits<char>::move(array2, str_lit1, 0); |
| 67 |
VERIFY( array2[0] == c1 ); |
| 68 |
VERIFY( str_lit1[0] == c2 ); |
| 69 |
std::char_traits<char>::move(array2, str_lit1, 1); |
| 70 |
VERIFY( array2[0] == c2 ); |
| 71 |
VERIFY( str_lit1[0] == c2 ); |
| 72 |
VERIFY( array2[1] == c3 ); |
| 73 |
VERIFY( str_lit1[1] == c4 ); |
| 74 |
std::char_traits<char>::move(array2, str_lit1, 2); |
| 75 |
VERIFY( array2[0] == c2 ); |
| 76 |
VERIFY( str_lit1[0] == c2 ); |
| 77 |
VERIFY( array2[1] == c4 ); |
| 78 |
VERIFY( str_lit1[1] == c4 ); |
| 79 |
|
| 80 |
char* pc1 = array1 + 1; |
| 81 |
c1 = pc1[0]; |
| 82 |
c2 = array1[0]; |
| 83 |
VERIFY( c1 != c2 ); |
| 84 |
char* pc2 = std::char_traits<char>::move(array1, pc1, 0); |
| 85 |
c3 = pc1[0]; |
| 86 |
c4 = array1[0]; |
| 87 |
VERIFY( c1 == c3 ); |
| 88 |
VERIFY( c2 == c4 ); |
| 89 |
VERIFY( pc2 == array1 ); |
| 90 |
|
| 91 |
c1 = pc1[0]; |
| 92 |
c2 = array1[0]; |
| 93 |
char* pc3 = pc1; |
| 94 |
pc2 = std::char_traits<char>::move(array1, pc1, 10); |
| 95 |
c3 = pc1[0]; |
| 96 |
c4 = array1[0]; |
| 97 |
VERIFY( c1 != c3 ); // underlying char array changed. |
| 98 |
VERIFY( c4 != c3 ); |
| 99 |
VERIFY( pc2 == array1 ); |
| 100 |
VERIFY( pc3 == pc1 ); // but pointers o-tay |
| 101 |
c1 = *(str_01.data()); |
| 102 |
c2 = array1[0]; |
| 103 |
VERIFY( c1 != c2 ); |
| 104 |
} |
| 105 |
|
| 106 |
int main() |
| 107 |
{ |
| 108 |
test01(); |
| 109 |
return 0; |
| 110 |
} |