ProteoWizard
OrderedPairTest.cpp
Go to the documentation of this file.
1 //
2 // $Id: OrderedPairTest.cpp 6141 2014-05-05 21:03:47Z chambm $
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"
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 
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 
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 
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 
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 
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 
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 
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();
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 
199 }
200 
void testVectorOrderedPair()
ostream * os_
void testVectorCustomPair()
int main(int argc, char *argv[])
void testEquality()
#define TEST_EPILOG
Definition: unit.hpp:182
BOOST_STATIC_ASSERT(sizeof(unsigned int)==4)
void test()
void testExtraction()
void testVectorDouble()
void testArray()
#define TEST_FAILED(x)
Definition: unit.hpp:176
CustomPair(double _a, double _b)
#define TEST_PROLOG(argc, argv)
Definition: unit.hpp:174
KernelTraitsBase< Kernel >::space_type::abscissa_type x
KernelTraitsBase< Kernel >::space_type::ordinate_type y
wrapper class for accessing contiguous data as a container of OrderedPairs; note that it does not own...
Definition: OrderedPair.hpp:83
void testContainer(const OrderedPairContainerRef &pairs)
#define unit_assert(x)
Definition: unit.hpp:85