ProteoWizard
OrderedPairTest.cpp
Go to the documentation of this file.
1 //
2 // $Id: OrderedPairTest.cpp 4129 2012-11-20 00:05:37Z 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"
26 #include "boost/static_assert.hpp"
27 #include <iostream>
28 #include <algorithm>
29 #include <iterator>
30 #include <cstring>
31 
32 
33 using namespace std;
34 using namespace pwiz::util;
35 using namespace pwiz::math;
36 
37 
38 ostream* os_ = 0;
39 
40 
41 BOOST_STATIC_ASSERT(sizeof(OrderedPair) == 2*sizeof(double));
42 
43 
45 {
46  // verify that pairs == { (1,2), (3,4), (5,6) }
47 
48  // test size
49 
50  if (os_)
51  {
52  copy(pairs.begin(), pairs.end(), ostream_iterator<OrderedPair>(*os_, " "));
53  *os_ << endl;
54  }
55 
56  unit_assert(pairs.size() == 3);
57 
58  // test iteration
59 
61  unit_assert(it->x == 1);
62  unit_assert(it->y == 2);
63 
64  ++it;
65  unit_assert(it->x == 3);
66  unit_assert(it->y == 4);
67 
68  ++it;
69  unit_assert(it->x == 5);
70  unit_assert(it->y == 6);
71 
72  // test random access
73 
74  unit_assert(pairs[0].x == 1);
75  unit_assert(pairs[0].y == 2);
76  unit_assert(pairs[1].x == 3);
77  unit_assert(pairs[1].y == 4);
78  unit_assert(pairs[2].x == 5);
79  unit_assert(pairs[2].y == 6);
80 
81  // test algorithms
82 
83  vector<OrderedPair> v;
84  copy(pairs.begin(), pairs.end(), back_inserter(v));
85  unit_assert(v.size() == 3);
86  unit_assert(v[0].x == 1);
87  unit_assert(v[0].y == 2);
88  unit_assert(v[1].x == 3);
89  unit_assert(v[1].y == 4);
90  unit_assert(v[2].x == 5);
91  unit_assert(v[2].y == 6);
92 }
93 
94 
95 void testArray()
96 {
97  if (os_) *os_ << "testArray()\n";
98  double a[] = {1, 2, 3, 4, 5, 6};
99  OrderedPairContainerRef pairs(a, a+sizeof(a)/sizeof(double));
100  testContainer(pairs);
101 }
102 
103 
105 {
106  if (os_) *os_ << "testVectorDouble()\n";
107  vector<double> v;
108  for (int i=1; i<=6; i++) v.push_back(i);
109  testContainer(v); // note automatic conversion: vector<double> -> OrderedPairContainerRef
110 }
111 
112 
114 {
115  if (os_) *os_ << "testVectorOrderedPair()\n";
116  vector<OrderedPair> v;
117  v.push_back(OrderedPair(1,2));
118  v.push_back(OrderedPair(3,4));
119  v.push_back(OrderedPair(5,6));
120  testContainer(v); // note automatic conversion: vector<OrderedPair> -> OrderedPairContainerRef
121 }
122 
123 
124 #pragma pack(push, 1)
125 struct CustomPair {double a; double b; CustomPair(double _a, double _b) : a(_a), b(_b) {} };
126 #pragma pack(pop)
127 
128 
130 {
131  if (os_) *os_ << "testVectorCustomPair()\n";
132  vector<CustomPair> v;
133  v.push_back(CustomPair(1,2));
134  v.push_back(CustomPair(3,4));
135  v.push_back(CustomPair(5,6));
136  testContainer(v); // note automatic conversion: vector<CustomPair> -> OrderedPairContainerRef
137 }
138 
139 
141 {
142  if (os_) *os_ << "testEquality()\n";
143  vector<OrderedPair> v;
144  v.push_back(OrderedPair(1,2));
145  v.push_back(OrderedPair(3,4));
146  v.push_back(OrderedPair(5,6));
147 
148  vector<OrderedPair> w = v;
149 
150  unit_assert(v == w);
151  w.push_back(OrderedPair(7,8));
152  unit_assert(v != w);
153  v.push_back(OrderedPair(7,9));
154  unit_assert(v != w);
155  v.back().y = w.back().y;
156  unit_assert(v == w);
157 }
158 
159 
161 {
162  vector<OrderedPair> v;
163  istringstream iss("(420,666) (421,667)");
164  copy(istream_iterator<OrderedPair>(iss), istream_iterator<OrderedPair>(), back_inserter(v));
165  unit_assert(v.size() == 2);
166  unit_assert(v[0].x == 420);
167  unit_assert(v[0].y == 666);
168  unit_assert(v[1].x == 421);
169  unit_assert(v[1].y == 667);
170 }
171 
172 
173 void test()
174 {
175  testArray();
179  testEquality();
180  testExtraction();
181 }
182 
183 
184 int main(int argc, char* argv[])
185 {
186  TEST_PROLOG(argc, argv)
187 
188  try
189  {
190  if (argc>1 && !strcmp(argv[1],"-v")) os_ = &cout;
191  test();
192  }
193  catch (exception& e)
194  {
195  TEST_FAILED(e.what())
196  }
197  catch (...)
198  {
199  TEST_FAILED("Caught unknown exception.")
200  }
201 
203 }
204