]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/backward/hash_map/1.cc
re PR libstdc++/30085 (switch debug mode hash containers from ext to tr1)
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / backward / hash_map / 1.cc
1 // { dg-options "-Wno-deprecated" }
2
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.
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 COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 // USA.
20
21 // hash_map (SGI extension)
22
23 #include <cstdlib>
24 #include <string>
25 #include <hash_map>
26 #include <testsuite_hooks.h>
27
28 namespace __gnu_cxx
29 {
30 using std::string;
31
32 inline size_t hash_string(const char* s)
33 {
34 unsigned long h;
35 for (h=0; *s; ++s) {
36 h = 5*h + *s;
37 }
38 return size_t(h);
39 }
40
41 template<class T> struct hash<T *>
42 {
43 size_t operator()(const T *const & s) const
44 { return reinterpret_cast<size_t>(s); }
45 };
46
47 template<> struct hash<string>
48 {
49 size_t operator()(const string &s) const { return hash_string(s.c_str()); }
50 };
51
52 template<> struct hash<const string>
53 {
54 size_t operator()(const string &s) const { return hash_string(s.c_str()); }
55 };
56
57 template<class T1, class T2> struct hash<pair<T1,T2> >
58 {
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);
63 }
64 };
65 }
66
67 void test01()
68 {
69 const int Size = 5;
70 bool test __attribute__((unused)) = true;
71
72 using std::string;
73 using std::pair;
74 using std::vector;
75 using __gnu_cxx::hash_map;
76
77 for (int i = 0; i < 10; i++)
78 {
79 hash_map<string, int> a;
80 hash_map<string, int> b;
81
82 vector<pair<string, int> > contents (Size);
83 for (int j = 0; j < Size; j++)
84 {
85 string s;
86 for (int k = 0; k < 10; k++)
87 {
88 s += 'a' + (rand() % 26);
89 }
90 contents[j] = make_pair(s,j);
91 }
92 for (int j = 0; j < Size; j++)
93 {
94 a[contents[j].first] = contents[j].second;
95 int k = Size - 1 - j;
96 b[contents[k].first] = contents[k].second;
97 }
98 VERIFY( a == b );
99 }
100 }
101
102 int main()
103 {
104 test01();
105 return 0;
106 }