1// 980930 bkoz work with libstdc++v3 2 3// Copyright (C) 1998-2015 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.3.6.8 basic_string::compare 21// int compare(const basic_string& str) const; 22// int compare(size_type pos1, size_type n1, const basic_string& str) const; 23// int compare(size_type pos1, size_type n1, const basic_string& str, 24// size_type pos2, size_type n2) const; 25// int compare(const charT* s) const; 26// int compare(size_type pos1, size_type n1, 27// const charT* s, size_type n2 = npos) const; 28 29// NB compare should be thought of as a lexographical compare, ie how 30// things would be sorted in a dictionary. 31 32#include <string> 33#include <testsuite_hooks.h> 34 35enum want_value {lt=0, z=1, gt=2}; 36 37int 38test_value(int result, want_value expected); 39 40int 41test_value(int result, want_value expected) 42{ 43 bool test __attribute__((unused)) = true; 44 bool pass = false; 45 46 switch (expected) { 47 case lt: 48 if (result < 0) 49 pass = true; 50 break; 51 case z: 52 if (!result) 53 pass = true; 54 break; 55 case gt: 56 if (result > 0) 57 pass = true; 58 break; 59 default: 60 pass = false; //should not get here 61 } 62 63 VERIFY(pass); 64 return 0; 65} 66 67 68int 69test01() 70{ 71 using namespace std; 72 73 wstring str_0(L"costa rica"); 74 wstring str_1(L"costa marbella"); 75 wstring str_2; 76 77 //sanity check 78 test_value(wcscmp(L"costa marbella", L"costa rica"), lt); 79 test_value(wcscmp(L"costa rica", L"costa rica"), z); 80 test_value(wcscmp(str_1.data(), str_0.data()), lt); 81 test_value(wcscmp(str_0.data(), str_1.data()), gt); 82 test_value(wcsncmp(str_1.data(), str_0.data(), 6), z); 83 test_value(wcsncmp(str_1.data(), str_0.data(), 14), lt); 84 test_value(wmemcmp(str_1.data(), str_0.data(), 6), z); 85 test_value(wmemcmp(str_1.data(), str_0.data(), 14), lt); 86 test_value(wmemcmp(L"costa marbella", L"costa rica", 14), lt); 87 88 // int compare(const basic_string& str) const; 89 test_value(str_0.compare(str_1), gt); //because r>m 90 test_value(str_1.compare(str_0), lt); //because m<r 91 str_2 = str_0; 92 test_value(str_2.compare(str_0), z); 93 str_2 = L"cost"; 94 test_value(str_2.compare(str_0), lt); 95 str_2 = L"costa ricans"; 96 test_value(str_2.compare(str_0), gt); 97 98 // int compare(size_type pos1, size_type n1, const basic_string& str) const; 99 test_value(str_1.compare(0, 6, str_0), lt); 100 str_2 = L"cost"; 101 test_value(str_1.compare(0, 4, str_2), z); 102 test_value(str_1.compare(0, 5, str_2), gt); 103 104 // int compare(size_type pos1, size_type n1, const basic_string& str, 105 // size_type pos2, size_type n2) const; 106 test_value(str_1.compare(0, 6, str_0, 0, 6), z); 107 test_value(str_1.compare(0, 7, str_0, 0, 7), lt); 108 test_value(str_0.compare(0, 7, str_1, 0, 7), gt); 109 110 // int compare(const charT* s) const; 111 test_value(str_0.compare(L"costa marbella"), gt); 112 test_value(str_1.compare(L"costa rica"), lt); 113 str_2 = str_0; 114 test_value(str_2.compare(L"costa rica"), z); 115 test_value(str_2.compare(L"cost"), gt); 116 test_value(str_2.compare(L"costa ricans"), lt); 117 118 // int compare(size_type pos, size_type n1, const charT* str, 119 // size_type n2 = npos) const; 120 test_value(str_1.compare(0, 6, L"costa rica", 0, 6), z); 121 test_value(str_1.compare(0, 7, L"costa rica", 0, 7), lt); 122 test_value(str_0.compare(0, 7, L"costa marbella", 0, 7), gt); 123 124 return 0; 125} 126 127 128int 129main() 130{ 131 test01(); 132 return 0; 133} 134