ProteoWizard
diff_std_test.cpp
Go to the documentation of this file.
1//
2// $Id$
3//
4//
5// Original author: Robert Burke <robetr.burke@proteowizard.org>
6//
7// Copyright 2009 Spielberg Family Center for Applied Proteomics
8// University of Southern California, Los Angeles, California 90033
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#define PWIZ_SOURCE
24
25#include "diff_std.hpp"
28#include <cstring>
29
30using namespace pwiz::util;
31using namespace pwiz::cv;
32using namespace pwiz::data;
33using namespace pwiz::data::diff_impl;
34
35ostream* os_ = 0;
36
37void testString(const string& a, const string& b)
38{
39 if (os_) *os_ << "diff_string(\"" << a << "\", \"" << b << "\")" << endl;
40
41 string a_b, b_a;
42 diff_string(a, b, a_b, b_a);
43 if (os_) *os_ << "a-b: " << a_b << "\nb-a: " << b_a << endl;
44
45 if (a == b)
46 unit_assert(a_b.empty() && b_a.empty());
47 else
48 unit_assert(!a_b.empty() && !b_a.empty());
49}
50
51template <typename integral_type>
52void testIntegralReally(integral_type a, integral_type b)
53{
54 if (os_) *os_ << "diff_integral(\"" << a << "\", \"" << b << "\")" << endl;
55
56 integral_type a_b, b_a;
57 diff_integral(a, b, a_b, b_a, BaseDiffConfig());
58 if (a == b)
59 unit_assert(a_b == integral_type() && b_a == integral_type());
60 else
61 unit_assert(a_b != integral_type() || b_a != integral_type());
62}
63
64template <typename integral_type>
66{
67 testIntegralReally<int>(1, 1);
68 testIntegralReally<int>(-1, 1);
69 testIntegralReally<int>(-1, -1);
70 testIntegralReally<int>(1, 0);
71 testIntegralReally<int>(-1, 0);
72}
73
74template <typename floating_type>
75void testFloating(floating_type a, floating_type b, floating_type precision)
76{
77 floating_type a_b, b_a;
78 BaseDiffConfig config((double) precision);
79
80 diff_floating(a, b, a_b, b_a, config);
81 if (fabs(a - b) <= config.precision + std::numeric_limits<floating_type>::epsilon())
82 unit_assert(a_b == floating_type() && b_a == floating_type());
83 else
84 unit_assert(a_b == fabs(a - b) && b_a == fabs(a - b));
85}
86
87
88void testCV()
89{
90 if (os_) *os_ << "testCV()\n";
91
92 CV a, b;
93 a.URI = "uri";
94 a.id = "cvLabel";
95 a.fullName = "fullName";
96 a.version = "version";
97 b = a;
98
100 diff(a,b);
101
102 unit_assert(diff.a_b.empty());
103 unit_assert(diff.b_a.empty());
105
106 a.version = "version_changed";
107
108 diff(a,b);
109 if (os_) *os_ << diff << endl;
111 unit_assert(diff.a_b.URI.empty() && diff.b_a.URI.empty());
112 unit_assert(diff.a_b.id.empty() && diff.b_a.id.empty());
113 unit_assert(diff.a_b.fullName.empty() && diff.b_a.fullName.empty());
114 unit_assert(diff.a_b.version == "version_changed");
115 unit_assert(diff.b_a.version == "version");
116}
117
118
120{
121 if (os_) *os_ << "testUserParam()\n";
122
123 UserParam a, b;
124 a.name = "name";
125 a.value = "value";
126 a.type = "type";
127 a.units = UO_minute;
128 b = a;
129
130 Diff<UserParam> diff(a, b);
132 unit_assert(diff.a_b.empty());
133 unit_assert(diff.b_a.empty());
134
135 b.value = "value_changed";
136 a.units = UO_second;
137 unit_assert(diff(a,b));
138 if (os_) *os_ << diff << endl;
139 unit_assert(diff.a_b.name == "name");
140 unit_assert(diff.b_a.name == "name");
141 unit_assert(diff.a_b.value == "value");
142 unit_assert(diff.b_a.value == "value_changed");
143 unit_assert(diff.a_b.type.empty() && diff.b_a.type.empty());
144 unit_assert(diff.a_b.units == UO_second);
145 unit_assert(diff.b_a.units == UO_minute);
146}
147
148
150{
151 if (os_) *os_ << "testCVParam()\n";
152
153 CVParam a, b, c;
155 a.value = "420";
156 c = b = a;
157
158 Diff<CVParam> diff(a, b);
160 unit_assert(diff.a_b.empty());
161 unit_assert(diff.b_a.empty());
162
163 b.value = "value_changed";
164 diff(a,b);
166 if (os_) *os_ << diff << endl;
169 unit_assert(diff.a_b.value == "420");
170 unit_assert(diff.b_a.value == "value_changed");
171
172 c.value = "421"; // prove fix for bug that wouldn't catch diff in int values
173 diff(a,c);
175 if (os_) *os_ << diff << endl;
178 unit_assert(diff.a_b.value == "420");
179 unit_assert(diff.b_a.value == "421");
180
181 a.value = "4.1e5"; // make sure we handle scientific notation properly
182 c.value = "4.1";
183 diff(a,c);
185 if (os_) *os_ << diff << endl;
186
187 a.value = "4.1e5"; // make sure we handle scientific notation properly
188 c.value = "410000.0";
189 diff(a,c);
191 if (os_) *os_ << diff << endl;
192
193 a.value = "1a"; // make sure we aren't naive about things that start out as ints
194 c.value = "1b";
195 diff(a,c);
197 if (os_) *os_ << diff << endl;
198
199
200}
201
202
204{
205 if (os_) *os_ << "testParamContainer()\n";
206
207 ParamGroupPtr pgp1(new ParamGroup("pg1"));
208 ParamGroupPtr pgp2(new ParamGroup("pg2"));
209 ParamGroupPtr pgp3(new ParamGroup("pg3"));
210
211 ParamContainer a, b;
212 a.userParams.push_back(UserParam("common"));
213 b.userParams.push_back(UserParam("common"));
214 a.cvParams.push_back(MS_m_z);
215 b.cvParams.push_back(MS_m_z);
216 a.paramGroupPtrs.push_back(pgp1);
217 b.paramGroupPtrs.push_back(pgp1);
218
221
222 a.userParams.push_back(UserParam("different", "1"));
223 b.userParams.push_back(UserParam("different", "2"));
224 a.cvParams.push_back(MS_charge_state);
225 b.cvParams.push_back(MS_peak_intensity);
226 a.paramGroupPtrs.push_back(pgp2);
227 b.paramGroupPtrs.push_back(pgp3);
228
229 diff(a, b);
230 if (os_) *os_ << diff << endl;
232
233 unit_assert(diff.a_b.userParams.size() == 1);
234 unit_assert(diff.a_b.userParams[0] == UserParam("different","1"));
235 unit_assert(diff.b_a.userParams.size() == 1);
236 unit_assert(diff.b_a.userParams[0] == UserParam("different","2"));
237
238 unit_assert(diff.a_b.cvParams.size() == 1);
239 unit_assert(diff.a_b.cvParams[0] == MS_charge_state);
240 unit_assert(diff.b_a.cvParams.size() == 1);
241 unit_assert(diff.b_a.cvParams[0] == MS_peak_intensity);
242
243 unit_assert(diff.a_b.paramGroupPtrs.size() == 1);
244 unit_assert(diff.a_b.paramGroupPtrs[0]->id == "pg2");
245 unit_assert(diff.b_a.paramGroupPtrs.size() == 1);
246 unit_assert(diff.b_a.paramGroupPtrs[0]->id == "pg3");
247}
248
249
251{
252 if (os_) *os_ << "testParamGroup()\n";
253
254 ParamGroup a("pg"), b("pg");
255 a.userParams.push_back(UserParam("common"));
256 b.userParams.push_back(UserParam("common"));
257
260
261 a.userParams.push_back(UserParam("different", "1"));
262 b.userParams.push_back(UserParam("different", "2"));
263
264 diff(a, b);
265 if (os_) *os_ << diff << endl;
267
268 unit_assert(diff.a_b.userParams.size() == 1);
269 unit_assert(diff.a_b.userParams[0] == UserParam("different","1"));
270 unit_assert(diff.b_a.userParams.size() == 1);
271 unit_assert(diff.b_a.userParams[0] == UserParam("different","2"));
272}
273
274
275void test()
276{
277 testString("goober", "goober");
278 testString("goober", "goo");
279
280 testIntegral<int>();
281 testIntegral<short>();
282 testIntegral<long>();
283 testIntegral<unsigned int>();
284 testIntegral<unsigned short>();
285 testIntegral<unsigned long>();
286
287 testFloating<float>(1.f, 1.f, 1.e-6f);
288 testFloating<float>(1.f, 1.0000000001f, 1.e-6f);
289 testFloating<float>(1.f, 1.00001f, 1.e-6f);
290 testFloating<float>(4.f, 4.2f, 1.f);
291
292 testFloating<double>(1, 1, 1e-6);
293 testFloating<double>(1, 1.0000000001, 1e-6);
294 testFloating<double>(1, 1.00001, 1e-6);
295 testFloating<double>(4, 4.2, 1);
296
297 testCV();
299 testCVParam();
302}
303
304int main(int argc, char* argv[])
305{
306 TEST_PROLOG(argc, argv)
307
308 try
309 {
310 if (argc>1 && !strcmp(argv[1],"-v")) os_ = &cout;
311 test();
312 }
313 catch (exception& e)
314 {
315 TEST_FAILED(e.what())
316 }
317 catch (...)
318 {
319 TEST_FAILED("Caught unknown exception.")
320 }
321
323}
324
UO_minute
minute: A time unit which is equal to 60 seconds.
Definition cv.hpp:13896
MS_m_z
m/z: Three-character symbol m/z is used to denote the quantity formed by dividing the mass of an ion ...
Definition cv.hpp:384
MS_charge_state
charge state: The charge state of the ion, single or multiple and positive or negatively charged.
Definition cv.hpp:396
MS_ionization_type
ionization type: The method by which gas phase ions are generated from the sample.
Definition cv.hpp:285
UO_second
second: A time unit which is equal to the duration of 9 192 631 770 periods of the radiation correspo...
Definition cv.hpp:13833
MS_peak_intensity
peak intensity: Intensity of ions as measured by the height or area of a peak in a mass spectrum.
Definition cv.hpp:402
int main(int argc, char *argv[])
void testParamContainer()
void testUserParam()
void testCVParam()
void testFloating(floating_type a, floating_type b, floating_type precision)
void testIntegral()
void testCV()
void testString(const string &a, const string &b)
void testIntegralReally(integral_type a, integral_type b)
ostream * os_
void test()
void testParamGroup()
void diff_floating(const floating_type &a, const floating_type &b, floating_type &a_b, floating_type &b_a, const BaseDiffConfig &config)
Definition diff_std.hpp:276
PWIZ_API_DECL void diff(const std::string &a, const std::string &b, std::string &a_b, std::string &b_a, const BaseDiffConfig &config)
void diff_integral(const integral_type &a, const integral_type &b, integral_type &a_b, integral_type &b_a, const BaseDiffConfig &config)
Definition diff_std.hpp:258
std::string diff_string(const diff_type &diff)
Definition diff_std.hpp:177
boost::shared_ptr< ParamGroup > ParamGroupPtr
Information about an ontology or CV source and a short 'lookup' tag to refer to.
Definition cv.hpp:14916
std::string id
the short label to be used as a reference tag with which to refer to this particular Controlled Vocab...
Definition cv.hpp:14918
std::string fullName
the usual name for the resource (e.g. The PSI-MS Controlled Vocabulary).
Definition cv.hpp:14924
std::string URI
the URI for the resource.
Definition cv.hpp:14921
std::string version
the version of the CV from which the referred-to terms are drawn.
Definition cv.hpp:14927
represents a tag-value pair, where the tag comes from the controlled vocabulary
Calculate diffs of objects in a ProteoWizard data model hierarchy.
Definition diff_std.hpp:143
The base class for elements that may contain cvParams, userParams, or paramGroup references.
std::vector< ParamGroupPtr > paramGroupPtrs
a collection of references to ParamGroups
std::vector< CVParam > cvParams
a collection of controlled vocabulary terms
std::vector< UserParam > userParams
a collection of uncontrolled user terms
A collection of CVParam and UserParam elements that can be referenced from elsewhere in this mzML doc...
Uncontrolled user parameters (essentially allowing free text). Before using these,...
CVID units
an optional CV parameter for the unit term associated with the value, if any (e.g....
std::string value
the value for the parameter, where appropriate.
std::string name
the name for the parameter.
std::string type
the datatype of the parameter, where appropriate (e.g.: xsd:float).
#define unit_assert(x)
Definition unit.hpp:85
#define TEST_EPILOG
Definition unit.hpp:183
#define TEST_FAILED(x)
Definition unit.hpp:177
#define TEST_PROLOG(argc, argv)
Definition unit.hpp:175