ProteoWizard
Functions | Variables
IntegerSetTest.cpp File Reference
#include "Std.hpp"
#include "IntegerSet.hpp"
#include "pwiz/utility/misc/unit.hpp"
#include <cstring>
#include <limits>

Go to the source code of this file.

Functions

void test ()
 
void testInstantiation ()
 
void testContains ()
 
void testUpperBound ()
 
void testIntervalExtraction ()
 
void testIntervalExtraction2 ()
 
void testIntExtraction ()
 
void testParse ()
 
void testParse2 ()
 
int main (int argc, char *argv[])
 

Variables

ostream * os_ = 0
 

Function Documentation

◆ test()

void test ( )

Definition at line 37 of file IntegerSetTest.cpp.

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}
ostream * os_
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
a single closed interval of integers
#define unit_assert(x)
Definition unit.hpp:85

References pwiz::util::IntegerSet::begin(), pwiz::util::IntegerSet::empty(), pwiz::util::IntegerSet::end(), pwiz::util::IntegerSet::insert(), os_, and unit_assert.

Referenced by main().

◆ testInstantiation()

void testInstantiation ( )

Definition at line 88 of file IntegerSetTest.cpp.

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}

References pwiz::util::IntegerSet::begin(), pwiz::util::IntegerSet::end(), and unit_assert.

Referenced by main().

◆ testContains()

void testContains ( )

Definition at line 106 of file IntegerSetTest.cpp.

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}

References pwiz::util::IntegerSet::contains(), pwiz::util::IntegerSet::insert(), and unit_assert.

Referenced by main().

◆ testUpperBound()

void testUpperBound ( )

Definition at line 127 of file IntegerSetTest.cpp.

128{
129 IntegerSet a(3,5);
130
131 for (int i=0; i<5; i++)
132 unit_assert(!a.hasUpperBound(i));
133 for (int i=5; i<10; i++)
134 unit_assert(a.hasUpperBound(i));
135}

References pwiz::util::IntegerSet::hasUpperBound(), and unit_assert.

Referenced by main().

◆ testIntervalExtraction()

void testIntervalExtraction ( )

Definition at line 138 of file IntegerSetTest.cpp.

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}

References pwiz::util::IntegerSet::Interval::begin, pwiz::util::IntegerSet::Interval::end, and unit_assert.

Referenced by main().

◆ testIntervalExtraction2()

void testIntervalExtraction2 ( )

Definition at line 150 of file IntegerSetTest.cpp.

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}

References pwiz::util::IntegerSet::Interval::begin, pwiz::util::IntegerSet::Interval::end, and unit_assert.

Referenced by main().

◆ testIntExtraction()

void testIntExtraction ( )

Definition at line 176 of file IntegerSetTest.cpp.

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}

References unit_assert.

Referenced by main().

◆ testParse()

void testParse ( )

Definition at line 188 of file IntegerSetTest.cpp.

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}
size_t size() const
returns the number of integers in the set
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...

References pwiz::util::IntegerSet::begin(), pwiz::util::IntegerSet::end(), pwiz::util::IntegerSet::intervalCount(), pwiz::util::IntegerSet::parse(), pwiz::util::IntegerSet::size(), and unit_assert.

Referenced by main().

◆ testParse2()

void testParse2 ( )

Definition at line 212 of file IntegerSetTest.cpp.

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}
forward iterator providing readonly access to the virtual container

References pwiz::util::IntegerSet::begin(), pwiz::util::IntegerSet::intervalCount(), pwiz::util::IntegerSet::parse(), pwiz::util::IntegerSet::size(), and unit_assert.

Referenced by main().

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 241 of file IntegerSetTest.cpp.

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}
void testContains()
void testParse()
void testIntervalExtraction()
void testIntExtraction()
void testUpperBound()
void testInstantiation()
void test()
void testParse2()
void testIntervalExtraction2()
#define TEST_EPILOG
Definition unit.hpp:183
#define TEST_FAILED(x)
Definition unit.hpp:177
#define TEST_PROLOG(argc, argv)
Definition unit.hpp:175

References os_, test(), TEST_EPILOG, TEST_FAILED, TEST_PROLOG, testContains(), testInstantiation(), testIntervalExtraction(), testIntervalExtraction2(), testIntExtraction(), testParse(), testParse2(), and testUpperBound().

Variable Documentation

◆ os_

ostream* os_ = 0

Definition at line 34 of file IntegerSetTest.cpp.

Referenced by main(), and test().