| 1 | // { dg-options "-Wno-deprecated" } |
| 2 | |
| 3 | // Copyright (C) 2002-2025 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 | |
| 27 | namespace __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 | |
| 66 | void test01() |
| 67 | { |
| 68 | const int Size = 5; |
| 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 | } |