ProteoWizard
IntegerSetTest.cpp
Go to the documentation of this file.
1//
2// $Id$
3//
4//
5// Original author: Darren Kessner <darren@proteowizard.org>
6//
7// Copyright 2007 Spielberg Family Center for Applied Proteomics
8// Cedars Sinai Medical Center, Los Angeles, California 90048
9//
10// Licensed under the Apache License, Version 2.0 (the "License");
11// you may not use this file except in compliance with the License.
12// You may obtain a copy of the License at
13//
14// http://www.apache.org/licenses/LICENSE-2.0
15//
16// Unless required by applicable law or agreed to in writing, software
17// distributed under the License is distributed on an "AS IS" BASIS,
18// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19// See the License for the specific language governing permissions and
20// limitations under the License.
21//
22
23
24#include "Std.hpp"
25#include "IntegerSet.hpp"
27#include <cstring>
28#include <limits>
29
30
31using namespace pwiz::util;
32
33
34ostream* os_ = 0;
35
36
37void test()
38{
39 // instantiate IntegerSet
40
41 IntegerSet a;
42 unit_assert(a.empty());
43
44 a.insert(1);
45 unit_assert(!a.empty());
46
47 a.insert(2);
49 a.insert(0,2);
50 a.insert(4);
51
52 // verify virtual container contents: 0, 1, 2, 4
53
54 if (os_)
55 {
56 copy(a.begin(), a.end(), ostream_iterator<int>(*os_," "));
57 *os_ << endl;
58 }
59
60 vector<int> b;
61 copy(a.begin(), a.end(), back_inserter(b));
62
63 unit_assert(b.size() == 4);
64 unit_assert(b[0] == 0);
65 unit_assert(b[1] == 1);
66 unit_assert(b[2] == 2);
67 unit_assert(b[3] == 4);
68
69 // insert [2,4], and verify contents: 0, 1, 2, 3, 4
70
71 a.insert(2,4);
72
73 if (os_)
74 {
75 copy(a.begin(), a.end(), ostream_iterator<int>(*os_," "));
76 *os_ << endl;
77 }
78
79 b.clear();
80 copy(a.begin(), a.end(), back_inserter(b));
81
82 unit_assert(b.size() == 5);
83 for (int i=0; i<5; i++)
84 unit_assert(i == b[i]);
85}
86
87
89{
90 IntegerSet a(666);
91 vector<int> b;
92 copy(a.begin(), a.end(), back_inserter(b));
93 unit_assert(b.size() == 1);
94 unit_assert(b[0] == 666);
95
96 IntegerSet c(666,668);
97 vector<int> d;
98 copy(c.begin(), c.end(), back_inserter(d));
99 unit_assert(d.size() == 3);
100 unit_assert(d[0] == 666);
101 unit_assert(d[1] == 667);
102 unit_assert(d[2] == 668);
103}
104
105
107{
108 IntegerSet a(3,5);
109 a.insert(11);
110 a.insert(13,17);
111
112 for (int i=0; i<3; i++)
113 unit_assert(!a.contains(i));
114 for (int i=3; i<6; i++)
115 unit_assert(a.contains(i));
116 for (int i=6; i<11; i++)
117 unit_assert(!a.contains(i));
118 unit_assert(a.contains(11));
119 unit_assert(!a.contains(12));
120 for (int i=13; i<18; i++)
121 unit_assert(a.contains(i));
122 for (int i=18; i<100; i++)
123 unit_assert(!a.contains(i));
124}
125
126
128{
129 IntegerSet a(3,5);
130
131 for (int i=0; i<5; i++)
133 for (int i=5; i<10; i++)
135}
136
137
139{
141
142 istringstream iss(" \t [-2,5] "); // whitespace okay around encoded interval, but not within it
143 iss >> i;
144
145 unit_assert(i.begin == -2);
146 unit_assert(i.end == 5);
147}
148
149
151{
153
154 istringstream iss(" \t 420 "); // whitespace okay around encoded interval, but not within it
155 iss >> i;
156 unit_assert(i.begin == 420);
157 unit_assert(i.end == 420);
158
159 istringstream iss2(" \n 420- ");
160 iss2 >> i;
161 unit_assert(i.begin == 420);
162 unit_assert(i.end == numeric_limits<int>::max());
163
164 istringstream iss3(" \n 420-goober "); // goober is ignored, since it's not an int
165 iss3 >> i;
166 unit_assert(i.begin == 420);
167 unit_assert(i.end == numeric_limits<int>::max());
168
169 istringstream iss4(" \n 420-666");
170 iss4 >> i;
171 unit_assert(i.begin == 420);
172 unit_assert(i.end == 666);
173}
174
175
177{
178 istringstream iss("1,100");
179 iss.imbue(locale("C")); // hack for msvc
180
181 int i = 0;
182 iss >> i;
183
184 unit_assert(i == 1);
185}
186
187
189{
190 IntegerSet a;
191
192 a.parse(" [-3,2] [5,5] [8,9] booger "); // insert(-3,2); insert(5); insert(8,9);
193
194 unit_assert(a.intervalCount() == 3);
195 unit_assert(a.size() == 9);
196
197 vector<int> b;
198 copy(a.begin(), a.end(), back_inserter(b));
199 unit_assert(b.size() == 9);
200 unit_assert(b[0] == -3);
201 unit_assert(b[1] == -2);
202 unit_assert(b[2] == -1);
203 unit_assert(b[3] == 0);
204 unit_assert(b[4] == 1);
205 unit_assert(b[5] == 2);
206 unit_assert(b[6] == 5);
207 unit_assert(b[7] == 8);
208 unit_assert(b[8] == 9);
209}
210
211
213{
214 IntegerSet a;
215
216 a.parse(" [-3,2] 5 8-9 10- "); // insert(-3,2); insert(5); insert(8,9); insert(10,INT_MAX);
217
218 unit_assert(a.intervalCount() == 3);
219 unit_assert(a.size() == 9ul + numeric_limits<int>::max()-10+1);
220
221 vector<int> b;
223 for (int i=0; i<11; ++i, ++it) // don't copy to the end() unless you have lots of time and space ;)
224 b.push_back(*it);
225
226 unit_assert(b.size() == 11);
227 unit_assert(b[0] == -3);
228 unit_assert(b[1] == -2);
229 unit_assert(b[2] == -1);
230 unit_assert(b[3] == 0);
231 unit_assert(b[4] == 1);
232 unit_assert(b[5] == 2);
233 unit_assert(b[6] == 5);
234 unit_assert(b[7] == 8);
235 unit_assert(b[8] == 9);
236 unit_assert(b[9] == 10);
237 unit_assert(b[10] == 11);
238}
239
240
241int main(int argc, char* argv[])
242{
243 TEST_PROLOG(argc, argv)
244
245 try
246 {
247 if (argc>1 && !strcmp(argv[1],"-v")) os_ = &cout;
248 test();
250 testContains();
255 testParse();
256 testParse2();
257 }
258 catch (exception& e)
259 {
260 TEST_FAILED(e.what())
261 }
262
264}
265
266
void testContains()
int main(int argc, char *argv[])
void testParse()
void testIntervalExtraction()
void testIntExtraction()
void testUpperBound()
void testInstantiation()
ostream * os_
void test()
void testParse2()
void testIntervalExtraction2()
forward iterator providing readonly access to the virtual container
a virtual container of integers, accessible via an iterator interface, stored as union of intervals
void insert(Interval interval)
insert an interval of integers into the virtual container
const_iterator end() const
bool empty() const
true iff IntegerSet is empty
const_iterator begin() const
size_t size() const
returns the number of integers in the set
bool hasUpperBound(int n) const
true iff n is an upper bound for the IntegerSet
bool contains(int n) const
true iff n is in the IntegerSet
size_t intervalCount() const
returns the number of intervals in the set
void parse(const std::string &intervalList)
insert intervals by parsing a string representing a whitespace-delimited list of closed intervals: pa...
a single closed interval of integers
#define unit_assert(x)
Definition unit.hpp:85
#define TEST_EPILOG
Definition unit.hpp:183
#define TEST_FAILED(x)
Definition unit.hpp:177
#define TEST_PROLOG(argc, argv)
Definition unit.hpp:175