Merge -r 127928:132243 from trunk
[official-gcc.git] / libstdc++-v3 / testsuite / backward / hash_map / 1.cc
blobfaa97d6f2657ea935c93689b0bb6f69d4be2803a
1 // { dg-options "-Wno-deprecated" }
3 // Copyright (C) 2002, 2005, 2007 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 2, or (at your option)
9 // any later version.
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.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 // USA.
21 // hash_map (SGI extension)
23 #include <cstdlib>
24 #include <string>
25 #include <hash_map>
26 #include <testsuite_hooks.h>
28 namespace __gnu_cxx
30 using std::string;
32 inline size_t hash_string(const char* s)
34 unsigned long h;
35 for (h=0; *s; ++s) {
36 h = 5*h + *s;
38 return size_t(h);
41 template<class T> struct hash<T *>
43 size_t operator()(const T *const & s) const
44 { return reinterpret_cast<size_t>(s); }
45 };
47 template<> struct hash<string>
49 size_t operator()(const string &s) const { return hash_string(s.c_str()); }
52 template<> struct hash<const string>
54 size_t operator()(const string &s) const { return hash_string(s.c_str()); }
57 template<class T1, class T2> struct hash<pair<T1,T2> >
59 hash<T1> __fh;
60 hash<T2> __sh;
61 size_t operator()(const pair<T1,T2> &p) const {
62 return __fh(p.first) ^ __sh(p.second);
67 void test01()
69 const int Size = 5;
70 bool test __attribute__((unused)) = true;
72 using std::string;
73 using std::pair;
74 using std::vector;
75 using __gnu_cxx::hash_map;
77 for (int i = 0; i < 10; i++)
79 hash_map<string, int> a;
80 hash_map<string, int> b;
82 vector<pair<string, int> > contents (Size);
83 for (int j = 0; j < Size; j++)
85 string s;
86 for (int k = 0; k < 10; k++)
88 s += 'a' + (rand() % 26);
90 contents[j] = make_pair(s,j);
92 for (int j = 0; j < Size; j++)
94 a[contents[j].first] = contents[j].second;
95 int k = Size - 1 - j;
96 b[contents[k].first] = contents[k].second;
98 VERIFY( a == b );
102 int main()
104 test01();
105 return 0;