1.cc revision 1.1.1.1
1// { dg-options "-Wno-deprecated" } 2 3// Copyright (C) 2002, 2005, 2007, 2009 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// hash_map (SGI extension) 21 22#include <cstdlib> 23#include <string> 24#include <hash_map> 25#include <testsuite_hooks.h> 26 27namespace __gnu_cxx 28{ 29 using std::string; 30 31 inline size_t hash_string(const char* s) 32 { 33 unsigned long h; 34 for (h=0; *s; ++s) { 35 h = 5*h + *s; 36 } 37 return size_t(h); 38 } 39 40 template<class T> struct hash<T *> 41 { 42 size_t operator()(const T *const & s) const 43 { return reinterpret_cast<size_t>(s); } 44 }; 45 46 template<> struct hash<string> 47 { 48 size_t operator()(const string &s) const { return hash_string(s.c_str()); } 49 }; 50 51 template<> struct hash<const string> 52 { 53 size_t operator()(const string &s) const { return hash_string(s.c_str()); } 54 }; 55 56 template<class T1, class T2> struct hash<pair<T1,T2> > 57 { 58 hash<T1> __fh; 59 hash<T2> __sh; 60 size_t operator()(const pair<T1,T2> &p) const { 61 return __fh(p.first) ^ __sh(p.second); 62 } 63 }; 64} 65 66void test01() 67{ 68 const int Size = 5; 69 bool test __attribute__((unused)) = true; 70 71 using std::string; 72 using std::pair; 73 using std::vector; 74 using __gnu_cxx::hash_map; 75 76 for (int i = 0; i < 10; i++) 77 { 78 hash_map<string, int> a; 79 hash_map<string, int> b; 80 81 vector<pair<string, int> > contents (Size); 82 for (int j = 0; j < Size; j++) 83 { 84 string s; 85 for (int k = 0; k < 10; k++) 86 { 87 s += 'a' + (rand() % 26); 88 } 89 contents[j] = make_pair(s,j); 90 } 91 for (int j = 0; j < Size; j++) 92 { 93 a[contents[j].first] = contents[j].second; 94 int k = Size - 1 - j; 95 b[contents[k].first] = contents[k].second; 96 } 97 VERIFY( a == b ); 98 } 99} 100 101int main() 102{ 103 test01(); 104 return 0; 105} 106