]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/hash-set-tests.c
* config/mips/mips.c (mips_final_postscan_insn): Modify call
[thirdparty/gcc.git] / gcc / hash-set-tests.c
CommitLineData
99b4f3a2 1/* Unit tests for hash-set.h.
fbd26352 2 Copyright (C) 2015-2019 Free Software Foundation, Inc.
99b4f3a2 3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along 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 "selftest.h"
27
28#if CHECKING_P
29
30namespace selftest {
31
32/* Construct a hash_set <const char *> and verify that various operations
33 work correctly. */
34
35static void
36test_set_of_strings ()
37{
38 hash_set <const char *> s;
39 ASSERT_EQ (0, s.elements ());
40
41 const char *red = "red";
42 const char *green = "green";
43 const char *blue = "blue";
44
45 ASSERT_EQ (false, s.contains (red));
46
067e9a50 47 for (hash_set<const char *>::iterator it = s.begin (); it != s.end (); ++it)
48 ASSERT_EQ (true, false);
49
99b4f3a2 50 /* Populate the hash_set. */
51 ASSERT_EQ (false, s.add (red));
52 ASSERT_EQ (false, s.add (green));
53 ASSERT_EQ (false, s.add (blue));
41a2340f 54 ASSERT_EQ (true, s.add (green));
99b4f3a2 55
56 /* Verify that the values are now within the set. */
57 ASSERT_EQ (true, s.contains (red));
58 ASSERT_EQ (true, s.contains (green));
59 ASSERT_EQ (true, s.contains (blue));
41a2340f 60 ASSERT_EQ (3, s.elements ());
61
62 /* Test removal. */
63 s.remove (red);
64 ASSERT_EQ (false, s.contains (red));
65 ASSERT_EQ (true, s.contains (green));
66 ASSERT_EQ (true, s.contains (blue));
67 ASSERT_EQ (2, s.elements ());
68
69 s.remove (red);
70 ASSERT_EQ (false, s.contains (red));
71 ASSERT_EQ (true, s.contains (green));
72 ASSERT_EQ (true, s.contains (blue));
73 ASSERT_EQ (2, s.elements ());
067e9a50 74
75 int seen = 0;
76 for (hash_set<const char *>::iterator it = s.begin (); it != s.end (); ++it)
77 {
78 int n = *it == green;
79 if (n == 0)
80 ASSERT_EQ (*it, blue);
81 ASSERT_EQ (seen & (1 << n), 0);
82 seen |= 1 << n;
83 }
84 ASSERT_EQ (seen, 3);
85
86 hash_set <const char *, true> t;
87 ASSERT_EQ (0, t.elements ());
88
89 ASSERT_EQ (false, t.contains (red));
90
91 for (hash_set<const char *, true>::iterator it = t.begin ();
92 it != t.end (); ++it)
93 ASSERT_EQ (true, false);
94
95 /* Populate the hash_set. */
96 ASSERT_EQ (false, t.add (red));
97 ASSERT_EQ (false, t.add (green));
98 ASSERT_EQ (false, t.add (blue));
99 ASSERT_EQ (true, t.add (green));
100
101 /* Verify that the values are now within the set. */
102 ASSERT_EQ (true, t.contains (red));
103 ASSERT_EQ (true, t.contains (green));
104 ASSERT_EQ (true, t.contains (blue));
105 ASSERT_EQ (3, t.elements ());
106
107 seen = 0;
108 for (hash_set<const char *, true>::iterator it = t.begin ();
109 it != t.end (); ++it)
110 {
111 int n = 2;
112 if (*it == green)
113 n = 0;
114 else if (*it == blue)
115 n = 1;
116 else
117 ASSERT_EQ (*it, red);
118 ASSERT_EQ (seen & (1 << n), 0);
119 seen |= 1 << n;
120 }
121 ASSERT_EQ (seen, 7);
122
123 /* Test removal. */
124 t.remove (red);
125 ASSERT_EQ (false, t.contains (red));
126 ASSERT_EQ (true, t.contains (green));
127 ASSERT_EQ (true, t.contains (blue));
128 ASSERT_EQ (2, t.elements ());
129
130 t.remove (red);
131 ASSERT_EQ (false, t.contains (red));
132 ASSERT_EQ (true, t.contains (green));
133 ASSERT_EQ (true, t.contains (blue));
134 ASSERT_EQ (2, t.elements ());
99b4f3a2 135}
136
137/* Run all of the selftests within this file. */
138
139void
140hash_set_tests_c_tests ()
141{
142 test_set_of_strings ();
143}
144
145} // namespace selftest
146
147#endif /* #if CHECKING_P */