MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HashMap.cpp
1 /* Copyright 2009 Sun Microsystems, Inc.
2  All rights reserved. Use is subject to license terms.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
16 
17 #include <HashMap.hpp>
18 
19 #ifdef TEST_HASH
20 
21 #include <BaseString.hpp>
22 #include <NdbTap.hpp>
23 
24 struct NodePair { Uint32 node1; Uint32 node2; };
25 
26 TAPTEST(HashMap)
27 {
28 
29  OK(my_init() == 0); // Init mysys
30 
31  printf("int -> int\n");
32  {
33  HashMap<int, int> hash1;
34  for (int i= 0; i < 100; i++)
35  OK(hash1.insert(i, i*34));
36 
37  int int_val;
38  for (int i= 0; i < 100; i++)
39  {
40  OK(hash1.search(i, int_val));
41  OK(int_val == i*34);
42 
43  OK(!hash1.search(i+100, int_val));
44  }
45 
46  // Duplicate key insert disallowed
47  OK(!hash1.insert(32, 32));
48 
49  // Value should not have changed
50  OK(hash1.search(32, int_val));
51  OK(int_val == 32*34);
52 
53  // Duplicate key insert with replace flag
54  OK(hash1.insert(32, 37, true));
55 
56  // Value should now have changed
57  OK(hash1.search(32, int_val));
58  OK(int_val == 37);
59 
60  }
61 
62  printf("int -> BaseString\n");
63  {
65 
66  // Insert value with key 32
67  BaseString str1("hej");
68  OK(hash2.insert(32, str1));
69 
70  // Retrieve value with key 32 and check it's the same
71  BaseString str2;
72  OK(hash2.search(32, str2));
73  OK(str1 == str2);
74 
75  // no value with key 33 inserted
76  OK(!hash2.search(33, str2));
77 
78  for (int i= 100; i < 200; i++){
79  BaseString str;
80  str.assfmt("magnus%d", i);
81  OK(hash2.insert(i, str));
82  }
83 
84  for (int i= 100; i < 200; i++){
85  BaseString str;
86  OK(hash2.search(i, str));
87  }
88 
89  // Delete every second entry
90  for (int i= 100; i < 200; i+=2)
91  OK(hash2.remove(i));
92 
93  BaseString str3;
94  OK(!hash2.search(102, str3));
95  OK(hash2.search(103, str3));
96  }
97 
98  printf("struct NodePair -> Uint32\n");
99  {
101  NodePair pk;
102  pk.node1 = 1;
103  pk.node2 = 2;
104  OK(lookup.insert(pk, 37));
105 
106  // Duplicate insert
107  OK(!lookup.insert(pk, 38));
108 
109  Uint32 value;
110  OK(lookup.search(pk, value));
111  OK(value == 37);
112  }
113 
114  printf("BaseString -> int\n");
115  {
117  OK(string_hash.insert("magnus", 1));
118  OK(string_hash.insert("mas", 2));
119  int value;
120  OK(string_hash.search("mas", value));
121  OK(value == 2);
122 
123  OK(string_hash.entries() == 2);
124 
125  // Remove entry
126  OK(string_hash.remove("mas"));
127 
128  // Check it does not exist
129  OK(!string_hash.search("mas", value));
130 
131  OK(string_hash.entries() == 1);
132  }
133 
134  my_end(0); // Bye mysys
135 
136  return 1; // OK
137 }
138 
139 #endif