ProteoWizard
BinaryDataTest.cpp
Go to the documentation of this file.
1 //
2 // $Id$
3 //
4 //
5 // Original author: Matt Chambers <matt.chambers .@. vanderbilt.edu>
6 //
7 // Copyright 2009 Vanderbilt University - Nashville, TN 37232
8 //
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 //
13 // http://www.apache.org/licenses/LICENSE-2.0
14 //
15 // Unless required by applicable law or agreed to in writing, software
16 // distributed under the License is distributed on an "AS IS" BASIS,
17 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 // See the License for the specific language governing permissions and
19 // limitations under the License.
20 //
21 
22 
23 #include "Std.hpp"
24 #include "BinaryData.hpp"
26 
27 using namespace pwiz::util;
28 
29 
30 ostream* os_ = 0;
31 
32 
33 template<typename T> T defaultValue() { return numeric_limits<T>::max(); }
34 template<typename T> bool equals(const T& lhs, const T& rhs) { return lhs == rhs; }
35 
36 
37 template <typename T>
38 void test()
39 {
40  typedef BinaryData<T> TestVector;
41  T testValue(defaultValue<T>());
42 
43  // test std::vector-ish constructors and accessor methods
44  {
45  TestVector v;
46  unit_assert_operator_equal(INT_MAX / sizeof(T), v.max_size());
47 
48  unit_assert(v.empty());
49  unit_assert(v.size() == 0);
50 
51  v.resize(10);
52  unit_assert(!v.empty());
53  unit_assert(v.size() == 10);
54 
55  v.clear();
56  unit_assert(v.empty());
57  unit_assert(v.size() == 0);
58 
59  v.resize(1000000, testValue);
60  unit_assert(!v.empty());
61  unit_assert(v.size() == 1000000);
62  }
63 
64  // test values returned by reference
65  {
66  TestVector v(10);
67  unit_assert(!v.empty());
68  unit_assert(v.size() == 10);
69 
70  for (size_t i = 0; i < v.size(); ++i)
71  {
72  v[i] = testValue;
73  unit_assert(equals(v[i], testValue));
74  }
75 
76  // test out_of_range exception
77  unit_assert_throws(v.at(v.size()), std::out_of_range);
78  }
79 
80  // test reserve
81  {
82  TestVector v;
83  v.reserve(10);
84  unit_assert(v.empty());
85  unit_assert_operator_equal(0, v.size());
86  unit_assert_operator_equal(10, v.capacity());
87  }
88 
89  // test iterators on empty data
90  {
91  TestVector v;
92  unit_assert(v.empty());
93  unit_assert_operator_equal(0, v.size());
94  unit_assert(v.begin() == v.end());
95  }
96 
97  // test iterators
98  {
99  TestVector v(10, testValue);
100  unit_assert(!v.empty());
101  unit_assert_operator_equal(10, v.size());
102  unit_assert(equals(v.front(), testValue));
103  unit_assert(equals(v.back(), testValue));
104  unit_assert(equals(*v.begin(), v.front()));
105  unit_assert(equals(*v.rbegin(), v.back()));
106 
107  for (size_t i = 0; i < v.size(); ++i)
108  {
109  unit_assert(equals(v[i], testValue));
110  unit_assert(equals(v.at(i), testValue));
111  }
112 
113  for (typename TestVector::iterator itr = v.begin(); itr != v.end(); ++itr)
114  unit_assert(equals(*itr, testValue));
115 
116  for (typename TestVector::const_iterator itr = v.cbegin(); itr != v.cend(); ++itr)
117  unit_assert(equals(*itr, testValue));
118 
119  for (typename TestVector::reverse_iterator itr = v.rbegin(); itr != v.rend(); ++itr)
120  unit_assert(equals(*itr, testValue));
121 
122  for (typename TestVector::const_reverse_iterator itr = v.crbegin(); itr != v.crend(); ++itr)
123  unit_assert(equals(*itr, testValue));
124  }
125 
126  // test swap with std::vector<T>
127  {
128  std::vector<T> vStd(10, testValue);
129 
130  unit_assert(!vStd.empty());
131  unit_assert(equals(vStd.front(), testValue));
132 
133  TestVector v;
134  unit_assert(v.empty());
135 
136  std::swap(vStd, v);
137 
138  unit_assert(!v.empty());
139  unit_assert_operator_equal(10, v.size());
140  unit_assert(equals(v.front(), testValue));
141  unit_assert(equals(v.back(), testValue));
142  unit_assert(equals(*v.begin(), v.front()));
143  unit_assert(equals(*v.rbegin(), v.back()));
144 
145  std::swap(vStd, v);
146 
147  unit_assert(v.empty());
148 
149  unit_assert(!vStd.empty());
150  unit_assert_operator_equal(10, vStd.size());
151  unit_assert(equals(vStd.front(), testValue));
152  unit_assert(equals(vStd.back(), testValue));
153  }
154 
155  // test implicit cast to std::vector<T>
156  {
157  TestVector v(10, testValue);
158  std::vector<T> vStd = v;
159 
160  unit_assert(!vStd.empty());
161  unit_assert_operator_equal(10, vStd.size());
162  unit_assert(equals(vStd.front(), testValue));
163  unit_assert(equals(vStd.back(), testValue));
164  unit_assert(equals(*vStd.begin(), vStd.front()));
165  unit_assert(equals(*vStd.rbegin(), vStd.back()));
166  }
167 
168  // test implicit cast to std::vector<T>& (not supported with iterator caching)
169  /*{
170  TestVector v(10, testValue);
171  std::vector<T>& vStd = v;
172 
173  unit_assert(!vStd.empty());
174  unit_assert_operator_equal(10, vStd.size());
175  unit_assert(equals(vStd.front(), testValue));
176  unit_assert(equals(vStd.back(), testValue));
177  unit_assert(equals(*vStd.begin(), vStd.front()));
178  unit_assert(equals(*vStd.rbegin(), vStd.back()));
179  }*/
180 
181  // test implicit cast to const std::vector<T>&
182  {
183  const TestVector v(10, testValue);
184  const std::vector<T>& vStd = v;
185 
186  unit_assert(!vStd.empty());
187  unit_assert_operator_equal(10, vStd.size());
188  unit_assert(equals(vStd.front(), testValue));
189  unit_assert(equals(vStd.back(), testValue));
190  unit_assert(equals(*vStd.begin(), vStd.front()));
191  unit_assert(equals(*vStd.rbegin(), vStd.back()));
192  }
193 }
194 
195 
196 int main(int argc, char* argv[])
197 {
198  TEST_PROLOG(argc, argv)
199 
200  try
201  {
202  if (argc>1 && !strcmp(argv[1], "-v")) os_ = &cout;
203  if (os_) *os_ << "BinaryDataTest\n";
204  test<double>();
205  test<float>();
206  }
207  catch (exception& e)
208  {
209  TEST_FAILED(e.what())
210  }
211  catch (...)
212  {
213  TEST_FAILED("Caught unknown exception.")
214  }
215 
217 }
unit_assert_throws
#define unit_assert_throws(x, exception)
Definition: unit.hpp:106
unit_assert_operator_equal
#define unit_assert_operator_equal(expected, actual)
Definition: unit.hpp:92
pwiz::util
Definition: almost_equal.hpp:33
TEST_EPILOG
#define TEST_EPILOG
Definition: unit.hpp:183
Std.hpp
test
void test()
Definition: BinaryDataTest.cpp:38
defaultValue
T defaultValue()
Definition: BinaryDataTest.cpp:33
main
int main(int argc, char *argv[])
Definition: BinaryDataTest.cpp:196
pwiz::util::BinaryData
A custom vector class that can store its contents in either a std::vector or a cli::array (when compi...
Definition: BinaryData.hpp:45
TEST_FAILED
#define TEST_FAILED(x)
Definition: unit.hpp:177
TEST_PROLOG
#define TEST_PROLOG(argc, argv)
Definition: unit.hpp:175
equals
bool equals(const T &lhs, const T &rhs)
Definition: BinaryDataTest.cpp:34
os_
ostream * os_
Definition: BinaryDataTest.cpp:30
BinaryData.hpp
unit.hpp
unit_assert
#define unit_assert(x)
Definition: unit.hpp:85