OSDN Git Service

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