1 /* Copyright (c) 2009, 2021, Oracle and/or its affiliates.
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, version 2.0,
6    as published by the Free Software Foundation.
7 
8    This program is also distributed with certain software (including
9    but not limited to OpenSSL) that is licensed under separate terms,
10    as designated in a particular file or component or in included license
11    documentation.  The authors of MySQL hereby grant you an additional
12    permission to link the program and your derivative works with the
13    separately licensed software that they have included with MySQL.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License, version 2.0, for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
23 
24 #include <HashMap.hpp>
25 
26 #ifdef TEST_HASH
27 
28 #include <BaseString.hpp>
29 #include <NdbTap.hpp>
30 
31 struct NodePair { Uint32 node1; Uint32 node2; };
32 
TAPTEST(HashMap)33 TAPTEST(HashMap)
34 {
35 
36   OK(my_init() == 0); // Init mysys
37 
38   printf("int -> int\n");
39   {
40     HashMap<int, int> hash1;
41     for (int i= 0; i < 100; i++)
42       OK(hash1.insert(i, i*34));
43 
44     int int_val;
45     for (int i= 0; i < 100; i++)
46     {
47       OK(hash1.search(i, int_val));
48       OK(int_val == i*34);
49 
50       OK(!hash1.search(i+100, int_val));
51     }
52 
53     // Duplicate key insert disallowed
54     OK(!hash1.insert(32, 32));
55 
56     // Value should not have changed
57     OK(hash1.search(32, int_val));
58     OK(int_val == 32*34);
59 
60     // Duplicate key insert with replace flag
61     OK(hash1.insert(32, 37, true));
62 
63     // Value should now have changed
64     OK(hash1.search(32, int_val));
65     OK(int_val == 37);
66 
67   }
68 
69   printf("int -> BaseString\n");
70   {
71     HashMap<int, BaseString> hash2;
72 
73     // Insert value with key 32
74     BaseString str1("hej");
75     OK(hash2.insert(32, str1));
76 
77     // Retrieve value with key 32 and check it's the same
78     BaseString str2;
79     OK(hash2.search(32, str2));
80     OK(str1 == str2);
81 
82     // no value with key 33 inserted
83     OK(!hash2.search(33, str2));
84 
85     for (int i= 100; i < 200; i++){
86       BaseString str;
87       str.assfmt("magnus%d", i);
88       OK(hash2.insert(i, str));
89     }
90 
91     for (int i= 100; i < 200; i++){
92       BaseString str;
93       OK(hash2.search(i, str));
94     }
95 
96     // Delete every second entry
97     for (int i= 100; i < 200; i+=2)
98       OK(hash2.remove(i));
99 
100     BaseString str3;
101     OK(!hash2.search(102, str3));
102     OK(hash2.search(103, str3));
103   }
104 
105   printf("struct NodePair -> Uint32\n");
106   {
107     HashMap<NodePair, Uint32> lookup;
108     NodePair pk;
109     pk.node1 = 1;
110     pk.node2 = 2;
111     OK(lookup.insert(pk, 37));
112 
113     // Duplicate insert
114     OK(!lookup.insert(pk, 38));
115 
116     Uint32 value;
117     OK(lookup.search(pk, value));
118     OK(value == 37);
119   }
120 
121   printf("BaseString -> int\n");
122   {
123     HashMap<BaseString, int, BaseString_get_key > string_hash;
124     OK(string_hash.insert("magnus", 1));
125     OK(string_hash.insert("mas", 2));
126     int value;
127     OK(string_hash.search("mas", value));
128     OK(value == 2);
129 
130     OK(string_hash.entries() == 2);
131 
132     // Remove entry
133     OK(string_hash.remove("mas"));
134 
135     // Check it does not exist
136     OK(!string_hash.search("mas", value));
137 
138     OK(string_hash.entries() == 1);
139   }
140 
141   my_end(0); // Bye mysys
142 
143   return 1; // OK
144 }
145 
146 #endif
147