ProteoWizard
diff_std_test.cpp
Go to the documentation of this file.
1 //
2 // $Id: diff_std_test.cpp 4129 2012-11-20 00:05:37Z chambm $
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 
30 using namespace pwiz::util;
31 using namespace pwiz::cv;
32 using namespace pwiz::data;
33 using namespace pwiz::data::diff_impl;
34 
35 ostream* os_ = 0;
36 
37 void 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 
51 template <typename integral_type>
52 void 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 
64 template <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 
74 template <typename floating_type>
75 void 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 
88 void 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 
99  Diff<CV> diff;
100  diff(a,b);
101 
102  unit_assert(diff.a_b.empty());
103  unit_assert(diff.b_a.empty());
104  unit_assert(!diff);
105 
106  a.version = "version_changed";
107 
108  diff(a,b);
109  if (os_) *os_ << diff << endl;
110  unit_assert(diff);
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);
131  unit_assert(!diff);
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);
159  unit_assert(!diff);
160  unit_assert(diff.a_b.empty());
161  unit_assert(diff.b_a.empty());
162 
163  b.value = "value_changed";
164  diff(a,b);
165  unit_assert(diff);
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);
174  unit_assert(diff);
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);
184  unit_assert(diff);
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);
190  unit_assert(!diff);
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);
196  unit_assert(diff);
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 
220  unit_assert(!diff);
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;
231  unit_assert(diff);
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);
240  unit_assert(diff.b_a.cvParams.size() == 1);
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 
258  Diff<ParamGroup> diff(a, b);
259  unit_assert(!diff);
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;
266  unit_assert(diff);
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 
275 void 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();
298  testUserParam();
299  testCVParam();
301  testParamGroup();
302 }
303 
304 int 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 
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:13356
MS_charge_state
charge state: The charge state of the ion, single or multiple and positive or negatively charged...
Definition: cv.hpp:235
std::string value
Definition: ParamTypes.hpp:47
void testIntegral()
bool empty() const
returns ture iff id, URI, fullName, and version are all empty
boost::shared_ptr< ParamGroup > ParamGroupPtr
Definition: ParamTypes.hpp:239
std::string value
the value for the parameter, where appropriate.
Definition: ParamTypes.hpp:191
const double epsilon
Definition: DiffTest.cpp:41
void testIntegralReally(integral_type a, integral_type b)
Calculate diffs of objects in a ProteoWizard data model hierarchy.
Definition: diff_std.hpp:142
#define TEST_EPILOG
Definition: unit.hpp:182
ostream * os_
Information about an ontology or CV source and a short &#39;lookup&#39; tag to refer to.
Definition: cv.hpp:13353
void testUserParam()
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:12526
bool empty() const
returns true iff name, value, type, and units are all empty
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:241
void diff(const string &filename1, const string &filename2)
std::string fullName
the usual name for the resource (e.g. The PSI-MS Controlled Vocabulary).
Definition: cv.hpp:13362
Uncontrolled user parameters (essentially allowing free text). Before using these, one should verify whether there is an appropriate CV term available, and if so, use the CV term instead.
Definition: ParamTypes.hpp:185
std::vector< UserParam > userParams
a collection of uncontrolled user terms
Definition: ParamTypes.hpp:253
object_result_type b_a
Definition: diff_std.hpp:157
std::string type
the datatype of the parameter, where appropriate (e.g.: xsd:float).
Definition: ParamTypes.hpp:194
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:223
std::string diff_string(const diff_type &diff)
Definition: diff_std.hpp:177
MS_ionization_type
ionization type: The method by which gas phase ions are generated from the sample.
Definition: cv.hpp:124
The base class for elements that may contain cvParams, userParams, or paramGroup references.
Definition: ParamTypes.hpp:244
std::string URI
the URI for the resource.
Definition: cv.hpp:13359
std::vector< CVParam > cvParams
a collection of controlled vocabulary terms
Definition: ParamTypes.hpp:250
void test()
int main(int argc, char *argv[])
std::vector< ParamGroupPtr > paramGroupPtrs
a collection of references to ParamGroups
Definition: ParamTypes.hpp:247
void testFloating(floating_type a, floating_type b, floating_type precision)
void testParamGroup()
std::string name
the name for the parameter.
Definition: ParamTypes.hpp:188
A collection of CVParam and UserParam elements that can be referenced from elsewhere in this mzML doc...
Definition: ParamTypes.hpp:324
UO_minute
minute: A time unit which is equal to 60 seconds.
Definition: cv.hpp:12589
CVID units
an optional CV parameter for the unit term associated with the value, if any (e.g. MS_electron_volt).
Definition: ParamTypes.hpp:197
void testString(const string &a, const string &b)
#define TEST_FAILED(x)
Definition: unit.hpp:176
void testParamContainer()
object_result_type a_b
Definition: diff_std.hpp:156
void testCVParam()
#define TEST_PROLOG(argc, argv)
Definition: unit.hpp:174
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
void testCV()
std::string version
the version of the CV from which the referred-to terms are drawn.
Definition: cv.hpp:13365
#define unit_assert(x)
Definition: unit.hpp:85
bool empty() const
Definition: ParamTypes.hpp:142
Definition: cv.hpp:91
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
represents a tag-value pair, where the tag comes from the controlled vocabulary
Definition: ParamTypes.hpp:44