]>
Commit | Line | Data |
---|---|---|
99b4f3a2 | 1 | /* Unit tests for hash-map.h. |
fbd26352 | 2 | Copyright (C) 2015-2019 Free Software Foundation, Inc. |
99b4f3a2 | 3 | |
4 | This file is part of GCC. | |
5 | ||
6 | GCC is free software; you can redistribute it and/or modify it under | |
7 | the terms of the GNU General Public License as published by the Free | |
8 | Software Foundation; either version 3, or (at your option) any later | |
9 | version. | |
10 | ||
11 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
12 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 | for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GCC; see the file COPYING3. If not see | |
18 | <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | #include "config.h" | |
21 | #include "system.h" | |
22 | #include "coretypes.h" | |
23 | #include "tm.h" | |
24 | #include "opts.h" | |
99b4f3a2 | 25 | #include "hash-set.h" |
26 | #include "fixed-value.h" | |
27 | #include "alias.h" | |
28 | #include "flags.h" | |
29 | #include "symtab.h" | |
30 | #include "tree-core.h" | |
31 | #include "stor-layout.h" | |
32 | #include "tree.h" | |
33 | #include "stringpool.h" | |
34 | #include "selftest.h" | |
35 | ||
36 | #if CHECKING_P | |
37 | ||
38 | namespace selftest { | |
39 | ||
40 | /* Construct a hash_map <const char *, int> and verify that | |
41 | various operations work correctly. */ | |
42 | ||
43 | static void | |
44 | test_map_of_strings_to_int () | |
45 | { | |
46 | hash_map <const char *, int> m; | |
47 | ||
48 | const char *ostrich = "ostrich"; | |
49 | const char *elephant = "elephant"; | |
50 | const char *ant = "ant"; | |
51 | const char *spider = "spider"; | |
52 | const char *millipede = "Illacme plenipes"; | |
53 | const char *eric = "half a bee"; | |
54 | ||
55 | /* A fresh hash_map should be empty. */ | |
9a78b979 | 56 | ASSERT_TRUE (m.is_empty ()); |
99b4f3a2 | 57 | ASSERT_EQ (NULL, m.get (ostrich)); |
58 | ||
59 | /* Populate the hash_map. */ | |
60 | ASSERT_EQ (false, m.put (ostrich, 2)); | |
61 | ASSERT_EQ (false, m.put (elephant, 4)); | |
62 | ASSERT_EQ (false, m.put (ant, 6)); | |
63 | ASSERT_EQ (false, m.put (spider, 8)); | |
64 | ASSERT_EQ (false, m.put (millipede, 750)); | |
65 | ASSERT_EQ (false, m.put (eric, 3)); | |
66 | ||
67 | /* Verify that we can recover the stored values. */ | |
68 | ASSERT_EQ (6, m.elements ()); | |
69 | ASSERT_EQ (2, *m.get (ostrich)); | |
70 | ASSERT_EQ (4, *m.get (elephant)); | |
71 | ASSERT_EQ (6, *m.get (ant)); | |
72 | ASSERT_EQ (8, *m.get (spider)); | |
73 | ASSERT_EQ (750, *m.get (millipede)); | |
74 | ASSERT_EQ (3, *m.get (eric)); | |
75 | ||
76 | /* Verify removing an item. */ | |
77 | m.remove (eric); | |
78 | ASSERT_EQ (5, m.elements ()); | |
79 | ASSERT_EQ (NULL, m.get (eric)); | |
a8d12eb3 | 80 | |
41a2340f | 81 | m.remove (eric); |
82 | ASSERT_EQ (5, m.elements ()); | |
83 | ASSERT_EQ (NULL, m.get (eric)); | |
84 | ||
a8d12eb3 | 85 | /* A plain char * key is hashed based on its value (address), rather |
86 | than the string it points to. */ | |
87 | char *another_ant = static_cast <char *> (xcalloc (4, 1)); | |
88 | another_ant[0] = 'a'; | |
89 | another_ant[1] = 'n'; | |
90 | another_ant[2] = 't'; | |
91 | another_ant[3] = 0; | |
92 | ASSERT_NE (ant, another_ant); | |
93 | unsigned prev_size = m.elements (); | |
94 | ASSERT_EQ (false, m.put (another_ant, 7)); | |
95 | ASSERT_EQ (prev_size + 1, m.elements ()); | |
96 | ||
97 | /* Need to use string_hash or nofree_string_hash key types to hash | |
98 | based on the string contents. */ | |
99 | hash_map <nofree_string_hash, int> string_map; | |
100 | ASSERT_EQ (false, string_map.put (ant, 1)); | |
101 | ASSERT_EQ (1, string_map.elements ()); | |
102 | ASSERT_EQ (true, string_map.put (another_ant, 5)); | |
103 | ASSERT_EQ (1, string_map.elements ()); | |
99b4f3a2 | 104 | } |
105 | ||
251317e4 | 106 | typedef class hash_map_test_val_t |
857ca76e | 107 | { |
251317e4 | 108 | public: |
857ca76e | 109 | static int ndefault; |
110 | static int ncopy; | |
111 | static int nassign; | |
112 | static int ndtor; | |
113 | ||
114 | hash_map_test_val_t () | |
115 | : ptr (&ptr) | |
116 | { | |
117 | ++ndefault; | |
118 | } | |
119 | ||
120 | hash_map_test_val_t (const hash_map_test_val_t &) | |
121 | : ptr (&ptr) | |
122 | { | |
123 | ++ncopy; | |
124 | } | |
125 | ||
126 | hash_map_test_val_t& operator= (const hash_map_test_val_t &) | |
127 | { | |
128 | ++nassign; | |
129 | return *this; | |
130 | } | |
131 | ||
132 | ~hash_map_test_val_t () | |
133 | { | |
134 | gcc_assert (ptr == &ptr); | |
135 | ++ndtor; | |
136 | } | |
137 | ||
138 | void *ptr; | |
139 | } val_t; | |
140 | ||
141 | int val_t::ndefault; | |
142 | int val_t::ncopy; | |
143 | int val_t::nassign; | |
144 | int val_t::ndtor; | |
145 | ||
146 | static void | |
147 | test_map_of_type_with_ctor_and_dtor () | |
148 | { | |
149 | typedef hash_map <void *, val_t> Map; | |
150 | ||
151 | { | |
152 | /* Test default ctor. */ | |
153 | Map m; | |
154 | (void)&m; | |
155 | } | |
156 | ||
157 | ASSERT_TRUE (val_t::ndefault == 0); | |
158 | ASSERT_TRUE (val_t::ncopy == 0); | |
159 | ASSERT_TRUE (val_t::nassign == 0); | |
160 | ASSERT_TRUE (val_t::ndtor == 0); | |
161 | ||
162 | { | |
163 | /* Test single insertion. */ | |
164 | Map m; | |
165 | void *p = &p; | |
166 | m.get_or_insert (p); | |
167 | } | |
168 | ||
169 | ASSERT_TRUE (val_t::ndefault + val_t::ncopy == val_t::ndtor); | |
170 | ||
171 | { | |
172 | /* Test copy ctor. */ | |
173 | Map m1; | |
174 | void *p = &p; | |
175 | val_t &rv1 = m1.get_or_insert (p); | |
176 | ||
177 | int ncopy = val_t::ncopy; | |
178 | int nassign = val_t::nassign; | |
179 | ||
180 | Map m2 (m1); | |
181 | val_t *pv2 = m2.get (p); | |
182 | ||
183 | ASSERT_TRUE (ncopy + 1 == val_t::ncopy); | |
184 | ASSERT_TRUE (nassign == val_t::nassign); | |
185 | ||
186 | ASSERT_TRUE (&rv1 != pv2); | |
187 | ASSERT_TRUE (pv2->ptr == &pv2->ptr); | |
188 | } | |
189 | ||
190 | ASSERT_TRUE (val_t::ndefault + val_t::ncopy == val_t::ndtor); | |
191 | ||
192 | #if 0 /* Avoid testing until bug 90959 is fixed. */ | |
193 | { | |
194 | /* Test copy assignment into an empty map. */ | |
195 | Map m1; | |
196 | void *p = &p; | |
197 | val_t &rv1 = m1.get_or_insert (p); | |
198 | ||
199 | int ncopy = val_t::ncopy; | |
200 | int nassign = val_t::nassign; | |
201 | ||
202 | Map m2; | |
203 | m2 = m1; | |
204 | val_t *pv2 = m2.get (p); | |
205 | ||
206 | ASSERT_TRUE (ncopy == val_t::ncopy); | |
207 | ASSERT_TRUE (nassign + 1 == val_t::nassign); | |
208 | ||
209 | ASSERT_TRUE (&rv1 != pv2); | |
210 | ASSERT_TRUE (pv2->ptr == &pv2->ptr); | |
211 | } | |
212 | ||
213 | ASSERT_TRUE (val_t::ndefault + val_t::ncopy == val_t::ndtor); | |
214 | ||
215 | #endif | |
216 | ||
217 | { | |
218 | Map m; | |
219 | void *p = &p, *q = &q; | |
220 | val_t &v1 = m.get_or_insert (p); | |
221 | val_t &v2 = m.get_or_insert (q); | |
222 | ||
223 | ASSERT_TRUE (v1.ptr == &v1.ptr && &v2.ptr == v2.ptr); | |
224 | } | |
225 | ||
226 | ASSERT_TRUE (val_t::ndefault + val_t::ncopy == val_t::ndtor); | |
227 | ||
228 | { | |
229 | Map m; | |
230 | void *p = &p, *q = &q; | |
231 | m.get_or_insert (p); | |
232 | m.remove (p); | |
233 | m.get_or_insert (q); | |
234 | m.remove (q); | |
235 | ||
236 | ASSERT_TRUE (val_t::ndefault + val_t::ncopy == val_t::ndtor); | |
237 | } | |
238 | } | |
239 | ||
99b4f3a2 | 240 | /* Run all of the selftests within this file. */ |
241 | ||
242 | void | |
243 | hash_map_tests_c_tests () | |
244 | { | |
245 | test_map_of_strings_to_int (); | |
857ca76e | 246 | test_map_of_type_with_ctor_and_dtor (); |
99b4f3a2 | 247 | } |
248 | ||
249 | } // namespace selftest | |
250 | ||
251 | #endif /* CHECKING_P */ |