summaryrefslogtreecommitdiff
path: root/gcc/hash-set-tests.c
blob: bb32094be20b1c908248e0060c79d39f6f3bcbc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/* Unit tests for hash-set.h.
   Copyright (C) 2015-2020 Free Software Foundation, Inc.

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "opts.h"
#include "hash-set.h"
#include "selftest.h"

#if CHECKING_P

namespace selftest {

/* Construct a hash_set <const char *> and verify that various operations
   work correctly.  */

static void
test_set_of_strings ()
{
  hash_set <const char *> s;
  ASSERT_EQ (0, s.elements ());

  const char *red = "red";
  const char *green = "green";
  const char *blue = "blue";

  ASSERT_EQ (false, s.contains (red));

  for (hash_set<const char *>::iterator it = s.begin (); it != s.end (); ++it)
    ASSERT_EQ (true, false);

  /* Populate the hash_set.  */
  ASSERT_EQ (false, s.add (red));
  ASSERT_EQ (false, s.add (green));
  ASSERT_EQ (false, s.add (blue));
  ASSERT_EQ (true, s.add (green));

  /* Verify that the values are now within the set.  */
  ASSERT_EQ (true, s.contains (red));
  ASSERT_EQ (true, s.contains (green));
  ASSERT_EQ (true, s.contains (blue));
  ASSERT_EQ (3, s.elements ());

  /* Test removal.  */
  s.remove (red);
  ASSERT_EQ (false, s.contains (red));
  ASSERT_EQ (true, s.contains (green));
  ASSERT_EQ (true, s.contains (blue));
  ASSERT_EQ (2, s.elements ());

  s.remove (red);
  ASSERT_EQ (false, s.contains (red));
  ASSERT_EQ (true, s.contains (green));
  ASSERT_EQ (true, s.contains (blue));
  ASSERT_EQ (2, s.elements ());

  int seen = 0;
  for (hash_set<const char *>::iterator it = s.begin (); it != s.end (); ++it)
    {
      int n = *it == green;
      if (n == 0)
	ASSERT_EQ (*it, blue);
      ASSERT_EQ (seen & (1 << n), 0);
      seen |= 1 << n;
    }
  ASSERT_EQ (seen, 3);

  hash_set <const char *, true> t;
  ASSERT_EQ (0, t.elements ());

  ASSERT_EQ (false, t.contains (red));

  for (hash_set<const char *, true>::iterator it = t.begin ();
       it != t.end (); ++it)
    ASSERT_EQ (true, false);

  /* Populate the hash_set.  */
  ASSERT_EQ (false, t.add (red));
  ASSERT_EQ (false, t.add (green));
  ASSERT_EQ (false, t.add (blue));
  ASSERT_EQ (true, t.add (green));

  /* Verify that the values are now within the set.  */
  ASSERT_EQ (true, t.contains (red));
  ASSERT_EQ (true, t.contains (green));
  ASSERT_EQ (true, t.contains (blue));
  ASSERT_EQ (3, t.elements ());

  seen = 0;
  for (hash_set<const char *, true>::iterator it = t.begin ();
       it != t.end (); ++it)
    {
      int n = 2;
      if (*it == green)
	n = 0;
      else if (*it == blue)
	n = 1;
      else
	ASSERT_EQ (*it, red);
      ASSERT_EQ (seen & (1 << n), 0);
      seen |= 1 << n;
    }
  ASSERT_EQ (seen, 7);

  /* Test removal.  */
  t.remove (red);
  ASSERT_EQ (false, t.contains (red));
  ASSERT_EQ (true, t.contains (green));
  ASSERT_EQ (true, t.contains (blue));
  ASSERT_EQ (2, t.elements ());

  t.remove (red);
  ASSERT_EQ (false, t.contains (red));
  ASSERT_EQ (true, t.contains (green));
  ASSERT_EQ (true, t.contains (blue));
  ASSERT_EQ (2, t.elements ());
}

typedef class hash_set_test_value_t
{
public:
  static int ndefault;
  static int ncopy;
  static int nassign;
  static int ndtor;

  hash_set_test_value_t (int v = 1): pval (&val), val (v)
  {
    ++ndefault;
  }

  hash_set_test_value_t (const hash_set_test_value_t &rhs)
    : pval (&val), val (rhs.val)
  {
    ++ncopy;
  }

  hash_set_test_value_t& operator= (const hash_set_test_value_t &rhs)
    {
     ++nassign;
     val = rhs.val;
     return *this;
    }

  ~hash_set_test_value_t ()
    {
     /* Verify that the value hasn't been corrupted.  */
     gcc_assert (*pval > 0);
     gcc_assert (pval == &val);
     *pval = -3;
     ++ndtor;
    }

  int *pval;
  int val;
} val_t;

int val_t::ndefault;
int val_t::ncopy;
int val_t::nassign;
int val_t::ndtor;

struct value_hash_traits: int_hash<int, -1, -2>
{
  typedef int_hash<int, -1, -2> base_type;
  typedef val_t                 value_type;
  typedef value_type            compare_type;

  static hashval_t hash (const value_type &v)
  {
    return base_type::hash (v.val);
  }

  static bool equal (const value_type &a, const compare_type &b)
  {
    return base_type::equal (a.val, b.val);
  }

  static void mark_deleted (value_type &v)
  {
    base_type::mark_deleted (v.val);
  }

  static const bool empty_zero_p = false;

  static void mark_empty (value_type &v)
  {
    base_type::mark_empty (v.val);
  }

  static bool is_deleted (const value_type &v)
  {
    return base_type::is_deleted (v.val);
  }

  static bool is_empty (const value_type &v)
  {
    return base_type::is_empty (v.val);
  }

  static void remove (value_type &v)
  {
    v.~value_type ();
  }
};

static void
test_set_of_type_with_ctor_and_dtor ()
{
  typedef hash_set <val_t, false, value_hash_traits> Set;

  {
    Set s;
    (void)&s;
  }

  ASSERT_TRUE (val_t::ndefault == 0);
  ASSERT_TRUE (val_t::ncopy == 0);
  ASSERT_TRUE (val_t::nassign == 0);
  ASSERT_TRUE (val_t::ndtor == 0);

  {
    Set s;
    ASSERT_EQ (false, s.add (val_t ()));
    ASSERT_EQ (true, 1 == s.elements ());
  }

  ASSERT_TRUE (val_t::ndefault + val_t::ncopy == val_t::ndtor);

  {
    Set s;
    ASSERT_EQ (false, s.add (val_t ()));
    ASSERT_EQ (true, s.add (val_t ()));
    ASSERT_EQ (true, 1 == s.elements ());
  }

  ASSERT_TRUE (val_t::ndefault + val_t::ncopy == val_t::ndtor);

  {
    Set s;
    val_t v1 (1), v2 (2), v3 (3);
    int ndefault = val_t::ndefault;
    int nassign = val_t::nassign;

    ASSERT_EQ (false, s.add (v1));
    ASSERT_EQ (true, s.contains (v1));
    ASSERT_EQ (true, 1 == s.elements ());

    ASSERT_EQ (false, s.add (v2));
    ASSERT_EQ (true, s.contains (v2));
    ASSERT_EQ (true, 2 == s.elements ());

    ASSERT_EQ (false, s.add (v3));
    ASSERT_EQ (true, s.contains (v3));
    ASSERT_EQ (true, 3 == s.elements ());

    ASSERT_EQ (true, s.add (v2));
    ASSERT_EQ (true, s.contains (v2));
    ASSERT_EQ (true, 3 == s.elements ());

    s.remove (v2);
    ASSERT_EQ (true, 2 == s.elements ());
    s.remove (v3);
    ASSERT_EQ (true, 1 == s.elements ());

    /* Verify that no default ctors or assignment operators have
       been called.  */
    ASSERT_EQ (true, ndefault == val_t::ndefault);
    ASSERT_EQ (true, nassign == val_t::nassign);
  }

  ASSERT_TRUE (val_t::ndefault + val_t::ncopy == val_t::ndtor);
}

/* Run all of the selftests within this file.  */

void
hash_set_tests_c_tests ()
{
  test_set_of_strings ();
  test_set_of_type_with_ctor_and_dtor ();
}

} // namespace selftest

#endif /* #if CHECKING_P */