root/tests/test_dense_setset.cc

Revision 1:d03d67d05dcf, 6.2 kB (checked in by Daniel Burrows <dburrows@…>, 5 years ago)

[aptitude @ Import the Subversion repository into darcs.]

Line 
1// test_dense_setset.cc
2//
3//   Copyright (C) 2005 Daniel Burrows
4//
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the GNU General Public License as
7//   published by the Free Software Foundation; either version 2 of
8//   the License, or (at your option) any later version.
9//
10//   This program is distributed in the hope that it will be useful,
11//   but WITHOUT ANY WARRANTY; without even the implied warranty of
12//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13//   General Public License for more details.
14//
15//   You should have received a copy of the GNU General Public License
16//   along with this program; see the file COPYING.  If not, write to
17//   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18//   Boston, MA 02111-1307, USA.
19
20#include <cppunit/extensions/HelperMacros.h>
21
22#include <generic/util/dense_setset.h>
23
24#include <iostream>
25
26template<typename Key, typename Val, typename Compare>
27inline
28std::ostream &operator<<(std::ostream &out, const imm::map<Key, Val, Compare> &s)
29{
30  out << "{";
31
32  for(typename imm::map<Key, Val, Compare>::const_iterator i = s.begin(); i != s.end(); ++i)
33    {
34      if(i != s.begin())
35        out << ", ";
36      out << i->first << " => " << i->second;
37    }
38
39  out << "}";
40
41  return out;
42}
43
44template<typename T, typename Compare>
45inline
46std::ostream &operator<<(std::ostream &out, const imm::set<T, Compare> &s)
47{
48  out << "{";
49
50  for(typename imm::set<T, Compare>::const_iterator i = s.begin(); i != s.end(); ++i)
51    {
52      if(i != s.begin())
53        out << ", ";
54      out << *i;
55    }
56
57  out << "}";
58
59  return out;
60}
61
62class Dense_SetsetTest : public CppUnit::TestFixture
63{
64  CPPUNIT_TEST_SUITE(Dense_SetsetTest);
65
66  CPPUNIT_TEST(testSubsetSearch);
67  CPPUNIT_TEST(testSubsetPredicateSearch);
68
69  CPPUNIT_TEST(testSubmapSearch);
70
71  CPPUNIT_TEST_SUITE_END();
72
73  struct identity
74  {
75    int operator()(int a) const
76    {
77      return a;
78    }
79  };
80
81public:
82  // Test searching for subsets of a set.
83  void testSubsetSearch()
84  {
85    imm::set<int> s1, s2, s3;
86
87    s1.insert(1);
88    s1.insert(2);
89    s1.insert(5);
90
91    s2.insert(5);
92
93    s3.insert(4);
94    s3.insert(5);
95
96    dense_setset<int, identity> S(10);
97
98    S.insert(s1);
99    S.insert(s2);
100    S.insert(s3);
101
102    imm::set<int> t;
103
104    t.insert(1);
105
106    dense_setset<int, identity>::const_iterator found = S.find_subset(t);
107    CPPUNIT_ASSERT(found == S.end());
108
109    t.insert(5);
110
111    found = S.find_subset(t);
112    CPPUNIT_ASSERT(found != S.end());
113    CPPUNIT_ASSERT_EQUAL(s2, *found);
114
115    found = S.find_subset_containing(t, 1);
116    CPPUNIT_ASSERT(found == S.end());
117
118    t.insert(6);
119    t.insert(4);
120    t.insert(3);
121
122    CPPUNIT_ASSERT(S.find_subset(t) != S.end());
123
124    found = S.find_subset_containing(t, 1);
125    CPPUNIT_ASSERT(found == S.end());
126
127    found = S.find_subset_containing(t, 4);
128    CPPUNIT_ASSERT(found != S.end());
129    CPPUNIT_ASSERT_EQUAL(s3, *found);
130  }
131
132  struct HalfCmp
133  {
134  public:
135    bool operator()(int a, int b) const
136    {
137      return a/2 < b/2;
138    }
139  };
140
141  struct HalfExtractId
142  {
143  public:
144    size_t operator()(int a) const
145    {
146      return a/2;
147    }
148  };
149
150  // Test searching for subsets of a set using subsumption.
151  void testSubsetPredicateSearch()
152  {
153    imm::set<int, HalfCmp> s1, s2, s3;
154
155    dense_setset<int, HalfExtractId, HalfCmp> S(20);
156
157    s1.insert(5);
158    CPPUNIT_ASSERT(s1.contains(5));
159    CPPUNIT_ASSERT_EQUAL(1U, s1.size());
160    S.insert(s1);
161
162    s2.insert(6);
163    s2.insert(8);
164    CPPUNIT_ASSERT_EQUAL(2U, s2.size());
165    S.insert(s2);
166
167    s3.insert(9);
168    s3.insert(11);
169    CPPUNIT_ASSERT_EQUAL(2U, s3.size());
170    S.insert(s3);
171
172    imm::set<int, HalfCmp> t;
173
174    t.insert(5);
175
176    dense_setset<int, HalfExtractId, HalfCmp>::const_iterator found = S.find_subset(t);
177    CPPUNIT_ASSERT(found != S.end());
178    CPPUNIT_ASSERT_EQUAL(s1, *found);
179
180    found = S.find_subset_containing(t, 4);
181    CPPUNIT_ASSERT(found != S.end());
182    CPPUNIT_ASSERT_EQUAL(s1, *found);
183
184    found = S.find_subset_containing(t, 8);
185    CPPUNIT_ASSERT(found == S.end());
186
187    t.insert(10);
188    t.insert(11);
189    t.insert(15);
190    t.insert(17);
191
192    found = S.find_subset(t);
193    CPPUNIT_ASSERT(found != S.end());
194    CPPUNIT_ASSERT_EQUAL(s1, *found);
195   
196    t.erase(5);
197
198    t.insert(4);
199    found = S.find_subset(t);
200    CPPUNIT_ASSERT(found != S.end());
201    CPPUNIT_ASSERT_EQUAL(s1, *found);
202
203    t.erase(4);
204    t.insert(7);
205    t.insert(8);
206    t.insertUpdate(11);
207
208    found = S.find_subset(t);
209    CPPUNIT_ASSERT(found != S.end());
210    CPPUNIT_ASSERT_EQUAL(s2, *found);
211
212    found = S.find_subset(t, std::greater<int>());
213
214    // Shouldn't show up since t contains 6 and 7, and the set is 6
215    // and 8.
216    CPPUNIT_ASSERT(found == S.end());
217
218    t.insertUpdate(10);
219    t.insertUpdate(8);
220
221    found = S.find_subset(t, std::greater<int>());
222
223    CPPUNIT_ASSERT(found != S.end());
224    CPPUNIT_ASSERT_EQUAL(s3, *found);
225
226    found = S.find_subset_containing(t, 6);
227
228    CPPUNIT_ASSERT(found != S.end());
229    CPPUNIT_ASSERT_EQUAL(s2, *found);
230
231    found = S.find_subset_containing(t, 11);
232
233    CPPUNIT_ASSERT(found != S.end());
234    CPPUNIT_ASSERT_EQUAL(s3, *found);
235  }
236
237  void testSubmapSearch()
238  {
239    imm::map<int, int> m1, m2, m3;
240
241    m1.put(1, 2);
242    m1.put(5, 2);
243
244    m2.put(1, 5);
245
246    m3.put(5, 2);
247    m3.put(6, 2);
248
249    dense_mapset<int, int, identity> S(10);
250
251    S.insert(m1);
252    S.insert(m2);
253    S.insert(m3);
254
255    imm::map<int, int> t;
256
257    t.put(1, 2);
258    t.put(3, 2);
259
260    dense_mapset<int, int, identity>::const_iterator found = S.find_submap(t);
261
262    CPPUNIT_ASSERT(found != S.end());
263    CPPUNIT_ASSERT_EQUAL(m2, *found);
264
265    found = S.find_submap(t, std::equal_to<std::pair<int, int> >());
266    CPPUNIT_ASSERT(found == S.end());
267
268    t.put(5, 2);
269    found = S.find_submap(t, std::equal_to<std::pair<int, int> >());
270    CPPUNIT_ASSERT(found != S.end());
271    CPPUNIT_ASSERT_EQUAL(m1, *found);
272
273    t.put(1, 5);
274    found = S.find_submap(t, std::equal_to<std::pair<int, int> >());
275    CPPUNIT_ASSERT(found != S.end());
276    CPPUNIT_ASSERT_EQUAL(m2, *found);
277
278    t.erase(1);
279    t.put(6, 2);
280    found = S.find_submap(t);
281    CPPUNIT_ASSERT(found != S.end());
282    CPPUNIT_ASSERT_EQUAL(m3, *found);
283  }
284};
285
286CPPUNIT_TEST_SUITE_REGISTRATION(Dense_SetsetTest);
Note: See TracBrowser for help on using the browser.