This commit was manufactured by cvs2svn to create branch
[official-gcc.git] / libstdc++-v3 / testsuite / ext / hash_map.cc
blob2a0bc0bce6d8dac7f6fc6d0a965de795a5a06cee
1 // Copyright (C) 2002 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 2, or (at your option)
7 // any later version.
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING. If not, write to the Free
16 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 // USA.
19 // hash_map (SGI extension)
21 #include <cstdlib>
22 #include <string>
23 #include <ext/hash_map>
24 #include <testsuite_hooks.h>
26 using namespace std;
27 using namespace __gnu_cxx;
29 namespace __gnu_cxx
31 inline size_t hash_string(const char* s)
33 unsigned long h;
34 for (h=0; *s; ++s) {
35 h = 5*h + *s;
37 return size_t(h);
40 template<class T> struct hash<T *>
42 size_t operator()(const T *const & s) const
43 { return reinterpret_cast<size_t>(s); }
44 };
46 template<> struct hash<string>
48 size_t operator()(const string &s) const { return hash_string(s.c_str()); }
51 template<> struct hash<const string>
53 size_t operator()(const string &s) const { return hash_string(s.c_str()); }
56 template<class T1, class T2> struct hash<pair<T1,T2> >
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);
67 const int Size = 5;
69 void test01()
71 bool test __attribute__((unused)) = true;
73 for (int i = 0; i < 10; i++)
75 hash_map<string,int> a;
76 hash_map<string,int> b;
78 vector<pair<string,int> > contents (Size);
79 for (int j = 0; j < Size; j++)
81 string s;
82 for (int k = 0; k < 10; k++)
84 s += 'a' + (rand() % 26);
86 contents[j] = make_pair(s,j);
88 for (int j = 0; j < Size; j++)
90 a[contents[j].first] = contents[j].second;
91 int k = Size - 1 - j;
92 b[contents[k].first] = contents[k].second;
94 VERIFY( a == b );
98 #if !__GXX_WEAK__ && _MT_ALLOCATOR_H
99 // Explicitly instantiate for systems with no COMDAT or weak support.
100 template class __gnu_cxx::__mt_alloc<__gnu_cxx::_Hashtable_node<std::pair<const std::string, int> > >;
101 template class __gnu_cxx::__mt_alloc<__gnu_cxx::_Hashtable_node<std::pair<const std::string, int> >* >;
102 template class __gnu_cxx::__mt_alloc<std::pair<std::string, int> >;
103 #endif
105 int main()
107 test01();
108 return 0;