ProteoWizard
Functions | Variables
BinaryDataTest.cpp File Reference
#include "Std.hpp"
#include "BinaryData.hpp"
#include "pwiz/utility/misc/unit.hpp"

Go to the source code of this file.

Functions

template<typename T >
defaultValue ()
 
template<typename T >
bool equals (const T &lhs, const T &rhs)
 
template<typename T >
void test ()
 
int main (int argc, char *argv[])
 

Variables

ostream * os_ = 0
 

Function Documentation

◆ defaultValue()

template<typename T >
T defaultValue ( )

◆ equals()

template<typename T >
bool equals ( const T &  lhs,
const T &  rhs 
)

Definition at line 34 of file BinaryDataTest.cpp.

34{ return lhs == rhs; }

Referenced by test().

◆ test()

template<typename T >
void test ( )

Definition at line 38 of file BinaryDataTest.cpp.

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}
bool equals(const T &lhs, const T &rhs)
A custom vector class that can store its contents in either a std::vector or a cli::array (when compi...
void swap(pwiz::util::BinaryData< T > &lhs, std::vector< T > &rhs)
#define unit_assert(x)
Definition unit.hpp:85
#define unit_assert_throws(x, exception)
Definition unit.hpp:106
#define unit_assert_operator_equal(expected, actual)
Definition unit.hpp:92

References equals(), std::swap(), unit_assert, unit_assert_operator_equal, and unit_assert_throws.

◆ main()

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

Definition at line 196 of file BinaryDataTest.cpp.

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}
ostream * os_
#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_EPILOG, TEST_FAILED, and TEST_PROLOG.

Variable Documentation

◆ os_

ostream* os_ = 0

Definition at line 30 of file BinaryDataTest.cpp.

Referenced by main().