| 1 |
// 1999-06-03 bkoz |
| 2 |
// 2003-07-22 Matt Austern |
| 3 |
|
| 4 |
// Copyright (C) 1999-2025 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 3, 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 COPYING3. If not see |
| 19 |
// <http://www.gnu.org/licenses/>. |
| 20 |
|
| 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. |
| 25 |
|
| 26 |
#include <string> |
| 27 |
#include <cstring> |
| 28 |
#include <testsuite_hooks.h> |
| 29 |
|
| 30 |
void test02(void) |
| 31 |
{ |
| 32 |
typedef short char_type; |
| 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(char_type c, char_type d) |
| 47 |
// assigns c = d; |
| 48 |
char_type c1 = 'z'; |
| 49 |
char_type c2 = 'u'; |
| 50 |
VERIFY( c1 != c2 ); |
| 51 |
std::char_traits<char_type>::assign(c1,c2); |
| 52 |
VERIFY( c1 == 'u' ); |
| 53 |
|
| 54 |
// bool X::eq(char_type c, char_type d) |
| 55 |
c1 = 'z'; |
| 56 |
c2 = 'u'; |
| 57 |
VERIFY ( !std::char_traits<char_type>::eq(c1, c2) ); |
| 58 |
VERIFY ( std::char_traits<char_type>::eq(c1, c1) ); |
| 59 |
VERIFY ( std::char_traits<char_type>::eq(c2, c2) ); |
| 60 |
|
| 61 |
// bool X::lt(char_type c, char_type d) |
| 62 |
c1 = 'z'; |
| 63 |
c2 = 'u'; |
| 64 |
VERIFY ( std::char_traits<char_type>::lt(c2, c1) ); |
| 65 |
VERIFY ( !std::char_traits<char_type>::lt(c1, c2) ); |
| 66 |
VERIFY ( !std::char_traits<char_type>::lt(c1, c1) ); |
| 67 |
VERIFY ( !std::char_traits<char_type>::lt(c2, c2) ); |
| 68 |
|
| 69 |
// char_type* X::move(char_type* s, const char_type* 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 |
char_type array1[] = {'z', 'u', 'm', 'a', ' ', 'b', 'e', 'a', 'c', 'h', 0}; |
| 73 |
const std::basic_string<char_type> str_01(array1 + 0, array1 + 10); |
| 74 |
|
| 75 |
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}; |
| 76 |
|
| 77 |
const int array2_len = sizeof(str_lit1)/sizeof(char_type) + sizeof(array1)/sizeof(char_type) - 1; |
| 78 |
// two terminating chars |
| 79 |
char_type array3[] = {'b', 'o', 'r', 'a', 'c', 'a', 'y', ',', ' ', 'p', 'h', 'i', 'l', 'i', 'p', 'p', 'i', 'n', 'e', 's', 0}; |
| 80 |
char_type array2[array2_len]; |
| 81 |
int len = std::min<int>(array2_len, sizeof(array3)/sizeof(char_type)); |
| 82 |
std::char_traits<char_type>::copy(array2, array3, len); |
| 83 |
|
| 84 |
VERIFY( str_lit1[0] == 'm' ); |
| 85 |
c1 = array2[0]; |
| 86 |
c2 = str_lit1[0]; |
| 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 ); |
| 102 |
|
| 103 |
char_type* pc1 = array1 + 1; |
| 104 |
c1 = pc1[0]; |
| 105 |
c2 = array1[0]; |
| 106 |
VERIFY( c1 != c2 ); |
| 107 |
char_type* pc2 = std::char_traits<char_type>::move(array1, pc1, 0); |
| 108 |
c3 = pc1[0]; |
| 109 |
c4 = array1[0]; |
| 110 |
VERIFY( c1 == c3 ); |
| 111 |
VERIFY( c2 == c4 ); |
| 112 |
VERIFY( pc2 == array1 ); |
| 113 |
|
| 114 |
c1 = pc1[0]; |
| 115 |
c2 = array1[0]; |
| 116 |
char_type* pc3 = pc1; |
| 117 |
pc2 = std::char_traits<char_type>::move(array1, pc1, 10); |
| 118 |
c3 = pc1[0]; |
| 119 |
c4 = array1[0]; |
| 120 |
VERIFY( c1 != c3 ); // underlying char_type array changed. |
| 121 |
VERIFY( c4 != c3 ); |
| 122 |
VERIFY( pc2 == array1 ); |
| 123 |
VERIFY( pc3 == pc1 ); // but pointers o-tay |
| 124 |
c1 = *(str_01.data()); |
| 125 |
c2 = array1[0]; |
| 126 |
VERIFY( c1 != c2 ); |
| 127 |
|
| 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 ); |
| 131 |
|
| 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'); |
| 135 |
VERIFY( pc4 != 0 ); |
| 136 |
VERIFY( *pc4 == 'a' ); |
| 137 |
|
| 138 |
pc4 = std::char_traits<char_type>::find(str_lit1, N4, 0x0a73); |
| 139 |
VERIFY( pc4 == 0 ); |
| 140 |
|
| 141 |
// char_type* X::assign(char_type* s, size_t n, char_type c) |
| 142 |
std::memset(array2, 0xaf, array2_len * sizeof(char_type)); |
| 143 |
VERIFY( array2[0] != 0x15a8 ); |
| 144 |
|
| 145 |
pc1 = std::char_traits<char_type>::assign (array2, array2_len, 0x15a8); |
| 146 |
VERIFY( pc1 == array2 ); |
| 147 |
for (int i = 0; i < array2_len; ++i) |
| 148 |
VERIFY( array2[i] == 0x15a8 ); |
| 149 |
|
| 150 |
// char_type* X::copy(char_type* s, const char_type* p, size_t n) |
| 151 |
int n1 = sizeof(str_lit1) / sizeof(char_type); |
| 152 |
pc1 = std::char_traits<char_type>::copy(array2, str_lit1, n1); |
| 153 |
len = std::char_traits<char_type>::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 char_type* p, const char_type* q, size_t n) |
| 159 |
const char_type* pconst1 = str_01.data(); |
| 160 |
const char_type* pconst2 = str_lit1; |
| 161 |
|
| 162 |
VERIFY( std::char_traits<char_type>::compare(pconst1, pconst2, 10) > 0 ); |
| 163 |
VERIFY( std::char_traits<char_type>::compare(pconst2, pconst1, 10) < 0 ); |
| 164 |
VERIFY( std::char_traits<char_type>::compare(pconst1, pconst1, 10) == 0 ); |
| 165 |
VERIFY( std::char_traits<char_type>::compare(pconst2, pconst2, 10) == 0 ); |
| 166 |
} |
| 167 |
|
| 168 |
int main() |
| 169 |
{ |
| 170 |
test02(); |
| 171 |
return 0; |
| 172 |
} |