ProteoWizard
pwiz
utility
math
OrderedPairTest.cpp
Go to the documentation of this file.
1
//
2
// $Id$
3
//
4
//
5
// Original author: Darren Kessner <darren@proteowizard.org>
6
//
7
// Copyright 2009 Center for Applied Molecular Medicine
8
// University of Southern California, Los Angeles, CA
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 "
OrderedPair.hpp
"
25
#include "
pwiz/utility/misc/Std.hpp
"
26
#include "
pwiz/utility/misc/unit.hpp
"
27
#include "boost/static_assert.hpp"
28
29
30
using namespace
pwiz::util
;
31
using namespace
pwiz::math
;
32
33
34
ostream*
os_
= 0;
35
36
37
BOOST_STATIC_ASSERT
(
sizeof
(
OrderedPair
) == 2*
sizeof
(
double
));
38
39
40
void
testContainer
(
const
OrderedPairContainerRef
& pairs)
41
{
42
// verify that pairs == { (1,2), (3,4), (5,6) }
43
44
// test size
45
46
if
(
os_
)
47
{
48
copy(pairs.
begin
(), pairs.
end
(), ostream_iterator<OrderedPair>(*
os_
,
" "
));
49
*
os_
<< endl;
50
}
51
52
unit_assert
(pairs.
size
() == 3);
53
54
// test iteration
55
56
OrderedPairContainerRef::const_iterator
it = pairs.
begin
();
57
unit_assert
(it->
x
== 1);
58
unit_assert
(it->
y
== 2);
59
60
++it;
61
unit_assert
(it->
x
== 3);
62
unit_assert
(it->
y
== 4);
63
64
++it;
65
unit_assert
(it->
x
== 5);
66
unit_assert
(it->
y
== 6);
67
68
// test random access
69
70
unit_assert
(pairs[0].
x
== 1);
71
unit_assert
(pairs[0].
y
== 2);
72
unit_assert
(pairs[1].
x
== 3);
73
unit_assert
(pairs[1].
y
== 4);
74
unit_assert
(pairs[2].
x
== 5);
75
unit_assert
(pairs[2].
y
== 6);
76
77
// test algorithms
78
79
vector<OrderedPair> v;
80
copy(pairs.
begin
(), pairs.
end
(), back_inserter(v));
81
unit_assert
(v.size() == 3);
82
unit_assert
(v[0].
x
== 1);
83
unit_assert
(v[0].
y
== 2);
84
unit_assert
(v[1].
x
== 3);
85
unit_assert
(v[1].
y
== 4);
86
unit_assert
(v[2].
x
== 5);
87
unit_assert
(v[2].
y
== 6);
88
}
89
90
91
void
testArray
()
92
{
93
if
(
os_
) *
os_
<<
"testArray()\n"
;
94
double
a[] = {1, 2, 3, 4, 5, 6};
95
OrderedPairContainerRef
pairs(a, a+
sizeof
(a)/
sizeof
(
double
));
96
testContainer
(pairs);
97
}
98
99
100
void
testVectorDouble
()
101
{
102
if
(
os_
) *
os_
<<
"testVectorDouble()\n"
;
103
vector<double> v;
104
for
(
int
i=1; i<=6; i++) v.push_back(i);
105
testContainer
(v);
// note automatic conversion: vector<double> -> OrderedPairContainerRef
106
}
107
108
109
void
testVectorOrderedPair
()
110
{
111
if
(
os_
) *
os_
<<
"testVectorOrderedPair()\n"
;
112
vector<OrderedPair> v;
113
v.push_back(
OrderedPair
(1,2));
114
v.push_back(
OrderedPair
(3,4));
115
v.push_back(
OrderedPair
(5,6));
116
testContainer
(v);
// note automatic conversion: vector<OrderedPair> -> OrderedPairContainerRef
117
}
118
119
120
#pragma pack(push, 1)
121
struct
CustomPair
{
double
a;
double
b;
CustomPair
(
double
_a,
double
_b) : a(_a), b(_b) {} };
122
#pragma pack(pop)
123
124
125
void
testVectorCustomPair
()
126
{
127
if
(
os_
) *
os_
<<
"testVectorCustomPair()\n"
;
128
vector<CustomPair> v;
129
v.push_back(
CustomPair
(1,2));
130
v.push_back(
CustomPair
(3,4));
131
v.push_back(
CustomPair
(5,6));
132
testContainer
(v);
// note automatic conversion: vector<CustomPair> -> OrderedPairContainerRef
133
}
134
135
136
void
testEquality
()
137
{
138
if
(
os_
) *
os_
<<
"testEquality()\n"
;
139
vector<OrderedPair> v;
140
v.push_back(
OrderedPair
(1,2));
141
v.push_back(
OrderedPair
(3,4));
142
v.push_back(
OrderedPair
(5,6));
143
144
vector<OrderedPair> w = v;
145
146
unit_assert
(v == w);
147
w.push_back(
OrderedPair
(7,8));
148
unit_assert
(v != w);
149
v.push_back(
OrderedPair
(7,9));
150
unit_assert
(v != w);
151
v.back().y = w.back().y;
152
unit_assert
(v == w);
153
}
154
155
156
void
testExtraction
()
157
{
158
vector<OrderedPair> v;
159
istringstream iss(
"(420,666) (421,667)"
);
160
copy(istream_iterator<OrderedPair>(iss), istream_iterator<OrderedPair>(), back_inserter(v));
161
unit_assert
(v.size() == 2);
162
unit_assert
(v[0].
x
== 420);
163
unit_assert
(v[0].
y
== 666);
164
unit_assert
(v[1].
x
== 421);
165
unit_assert
(v[1].
y
== 667);
166
}
167
168
169
void
test
()
170
{
171
testArray
();
172
testVectorDouble
();
173
testVectorOrderedPair
();
174
testVectorCustomPair
();
175
testEquality
();
176
testExtraction
();
177
}
178
179
180
int
main
(
int
argc,
char
* argv[])
181
{
182
TEST_PROLOG
(argc, argv)
183
184
try
185
{
186
if
(argc>1 && !strcmp(argv[1],
"-v"
))
os_
= &cout;
187
test
();
188
}
189
catch
(exception& e)
190
{
191
TEST_FAILED
(e.what())
192
}
193
catch
(...)
194
{
195
TEST_FAILED
(
"Caught unknown exception."
)
196
}
197
198
TEST_EPILOG
199
}
200
test
void test()
Definition:
OrderedPairTest.cpp:169
pwiz::math::OrderedPairContainerRef
wrapper class for accessing contiguous data as a container of OrderedPairs; note that it does not own...
Definition:
OrderedPair.hpp:84
pwiz::math::OrderedPair::y
double y
Definition:
OrderedPair.hpp:42
pwiz::math::OrderedPairContainerRef::begin
const_iterator begin() const
Definition:
OrderedPair.hpp:103
testVectorCustomPair
void testVectorCustomPair()
Definition:
OrderedPairTest.cpp:125
y
KernelTraitsBase< Kernel >::space_type::ordinate_type y
Definition:
MatchedFilter.hpp:143
testEquality
void testEquality()
Definition:
OrderedPairTest.cpp:136
testVectorOrderedPair
void testVectorOrderedPair()
Definition:
OrderedPairTest.cpp:109
CustomPair::CustomPair
CustomPair(double _a, double _b)
Definition:
OrderedPairTest.cpp:121
OrderedPair.hpp
pwiz::util
Definition:
almost_equal.hpp:33
testExtraction
void testExtraction()
Definition:
OrderedPairTest.cpp:156
TEST_EPILOG
#define TEST_EPILOG
Definition:
unit.hpp:183
testContainer
void testContainer(const OrderedPairContainerRef &pairs)
Definition:
OrderedPairTest.cpp:40
testVectorDouble
void testVectorDouble()
Definition:
OrderedPairTest.cpp:100
Std.hpp
main
int main(int argc, char *argv[])
Definition:
OrderedPairTest.cpp:180
x
KernelTraitsBase< Kernel >::space_type::abscissa_type x
Definition:
MatchedFilter.hpp:142
pwiz::math::OrderedPairContainerRef::size
size_t size() const
Definition:
OrderedPair.hpp:105
os_
ostream * os_
Definition:
OrderedPairTest.cpp:34
TEST_FAILED
#define TEST_FAILED(x)
Definition:
unit.hpp:177
TEST_PROLOG
#define TEST_PROLOG(argc, argv)
Definition:
unit.hpp:175
pwiz::math
Definition:
erf.hpp:33
testArray
void testArray()
Definition:
OrderedPairTest.cpp:91
pwiz::math::OrderedPair::x
double x
Definition:
OrderedPair.hpp:41
pwiz::math::OrderedPair
Definition:
OrderedPair.hpp:40
pwiz::math::OrderedPairContainerRef::end
const_iterator end() const
Definition:
OrderedPair.hpp:104
unit.hpp
unit_assert
#define unit_assert(x)
Definition:
unit.hpp:85
pwiz::util::BOOST_STATIC_ASSERT
BOOST_STATIC_ASSERT(sizeof(unsigned int)==4)
CustomPair
Definition:
OrderedPairTest.cpp:121
Generated by
1.8.20