1// 1999-06-03 bkoz 2// 2003-07-22 Matt Austern 3 4// Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009 5// Free Software Foundation, Inc. 6// 7// This file is part of the GNU ISO C++ Library. This library is free 8// software; you can redistribute it and/or modify it under the 9// terms of the GNU General Public License as published by the 10// Free Software Foundation; either version 3, or (at your option) 11// any later version. 12 13// This library is distributed in the hope that it will be useful, 14// but WITHOUT ANY WARRANTY; without even the implied warranty of 15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16// GNU General Public License for more details. 17 18// You should have received a copy of the GNU General Public License along 19// with this library; see the file COPYING3. If not see 20// <http://www.gnu.org/licenses/>. 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 <cstring> 29#include <testsuite_hooks.h> 30 31void test02(void) 32{ 33 typedef short char_type; 34 bool test __attribute__((unused)) = true; 35 36 // 21.1.1 character traits requirements 37 38 // Key for decoding what function signatures really mean: 39 // X == char_traits<_CharT> 40 // [c,d] == _CharT 41 // [p,q] == const _CharT* 42 // s == _CharT* 43 // [n,i,j] == size_t 44 // f == X::int_type 45 // pos == X::pos_type 46 // state == X::state_type 47 48 // void X::assign(char_type c, char_type d) 49 // assigns c = d; 50 char_type c1 = 'z'; 51 char_type c2 = 'u'; 52 VERIFY( c1 != c2 ); 53 std::char_traits<char_type>::assign(c1,c2); 54 VERIFY( c1 == 'u' ); 55 56 // bool X::eq(char_type c, char_type d) 57 c1 = 'z'; 58 c2 = 'u'; 59 VERIFY ( !std::char_traits<char_type>::eq(c1, c2) ); 60 VERIFY ( std::char_traits<char_type>::eq(c1, c1) ); 61 VERIFY ( std::char_traits<char_type>::eq(c2, c2) ); 62 63 // bool X::lt(char_type c, char_type d) 64 c1 = 'z'; 65 c2 = 'u'; 66 VERIFY ( std::char_traits<char_type>::lt(c2, c1) ); 67 VERIFY ( !std::char_traits<char_type>::lt(c1, c2) ); 68 VERIFY ( !std::char_traits<char_type>::lt(c1, c1) ); 69 VERIFY ( !std::char_traits<char_type>::lt(c2, c2) ); 70 71 // char_type* X::move(char_type* s, const char_type* p, size_t n) 72 // for each i in [0,n) performs X::assign(s[i], p[i]). Copies 73 // correctly even where p is in [s, s + n), and yields s. 74 char_type array1[] = {'z', 'u', 'm', 'a', ' ', 'b', 'e', 'a', 'c', 'h', 0}; 75 const std::basic_string<char_type> str_01(array1 + 0, array1 + 10); 76 77 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 79 int len = sizeof(str_lit1)/sizeof(char_type) + sizeof(array1)/sizeof(char_type) - 1; 80 // two terminating chars 81 char_type array3[] = {'b', 'o', 'r', 'a', 'c', 'a', 'y', ',', ' ', 'p', 'h', 'i', 'l', 'i', 'p', 'p', 'i', 'n', 'e', 's', 0}; 82 char_type array2[len]; 83 std::char_traits<char_type>::copy(array2, array3, len); 84 85 VERIFY( str_lit1[0] == 'm' ); 86 c1 = array2[0]; 87 c2 = str_lit1[0]; 88 char_type c3 = array2[1]; 89 char_type c4 = str_lit1[1]; 90 std::char_traits<char_type>::move(array2, str_lit1, 0); 91 VERIFY( array2[0] == c1 ); 92 VERIFY( str_lit1[0] == c2 ); 93 std::char_traits<char_type>::move(array2, str_lit1, 1); 94 VERIFY( array2[0] == c2 ); 95 VERIFY( str_lit1[0] == c2 ); 96 VERIFY( array2[1] == c3 ); 97 VERIFY( str_lit1[1] == c4 ); 98 std::char_traits<char_type>::move(array2, str_lit1, 2); 99 VERIFY( array2[0] == c2 ); 100 VERIFY( str_lit1[0] == c2 ); 101 VERIFY( array2[1] == c4 ); 102 VERIFY( str_lit1[1] == c4 ); 103 104 char_type* pc1 = array1 + 1; 105 c1 = pc1[0]; 106 c2 = array1[0]; 107 VERIFY( c1 != c2 ); 108 char_type* pc2 = std::char_traits<char_type>::move(array1, pc1, 0); 109 c3 = pc1[0]; 110 c4 = array1[0]; 111 VERIFY( c1 == c3 ); 112 VERIFY( c2 == c4 ); 113 VERIFY( pc2 == array1 ); 114 115 c1 = pc1[0]; 116 c2 = array1[0]; 117 char_type* pc3 = pc1; 118 pc2 = std::char_traits<char_type>::move(array1, pc1, 10); 119 c3 = pc1[0]; 120 c4 = array1[0]; 121 VERIFY( c1 != c3 ); // underlying char_type array changed. 122 VERIFY( c4 != c3 ); 123 VERIFY( pc2 == array1 ); 124 VERIFY( pc3 == pc1 ); // but pointers o-tay 125 c1 = *(str_01.data()); 126 c2 = array1[0]; 127 VERIFY( c1 != c2 ); 128 129 // size_t X::length(const char_type* p) 130 len = std::char_traits<char_type>::length(str_lit1); 131 VERIFY( len == sizeof(str_lit1) / sizeof(char_type) - 1 ); 132 133 // const char_type* X::find(const char_type* s, size_t n, char_type c) 134 const int N4 = sizeof(str_lit1) / sizeof(char_type); 135 const char_type* pc4 = std::char_traits<char_type>::find(str_lit1, N4, 'a'); 136 VERIFY( pc4 != 0 ); 137 VERIFY( *pc4 == 'a' ); 138 139 pc4 = std::char_traits<char_type>::find(str_lit1, N4, 0x0a73); 140 VERIFY( pc4 == 0 ); 141 142 // char_type* X::assign(char_type* s, size_t n, char_type c) 143 len = sizeof(array2) / sizeof(char_type); 144 std::memset(array2, 0xaf, len * sizeof(char_type)); 145 VERIFY( array2[0] != 0x15a8 ); 146 147 pc1 = std::char_traits<char_type>::assign (array2, len, 0x15a8); 148 VERIFY( pc1 == array2 ); 149 for (int i = 0; i < len; ++i) 150 VERIFY( array2[i] == 0x15a8 ); 151 152 // char_type* X::copy(char_type* s, const char_type* p, size_t n) 153 int n1 = sizeof(str_lit1) / sizeof(char_type); 154 pc1 = std::char_traits<char_type>::copy(array2, str_lit1, n1); 155 len = std::char_traits<char_type>::length(array2); 156 VERIFY( len == n1 - 1 ); 157 for (int i = 0; i < len; ++i) 158 VERIFY( str_lit1[i] == array2[i] ); 159 160 // int X::compare(const char_type* p, const char_type* q, size_t n) 161 const char_type* pconst1 = str_01.data(); 162 const char_type* pconst2 = str_lit1; 163 164 VERIFY( std::char_traits<char_type>::compare(pconst1, pconst2, 10) > 0 ); 165 VERIFY( std::char_traits<char_type>::compare(pconst2, pconst1, 10) < 0 ); 166 VERIFY( std::char_traits<char_type>::compare(pconst1, pconst1, 10) == 0 ); 167 VERIFY( std::char_traits<char_type>::compare(pconst2, pconst2, 10) == 0 ); 168} 169 170int main() 171{ 172 test02(); 173 return 0; 174} 175