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 
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;
167  unit_assert(diff.a_b.cvid == MS_ionization_type);
168  unit_assert(diff.b_a.cvid == MS_ionization_type);
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;
176  unit_assert(diff.a_b.cvid == MS_ionization_type);
177  unit_assert(diff.b_a.cvid == MS_ionization_type);
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);
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 
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 
pwiz::data::UserParam
Uncontrolled user parameters (essentially allowing free text). Before using these,...
Definition: ParamTypes.hpp:186
pwiz::cv::CV::fullName
std::string fullName
the usual name for the resource (e.g. The PSI-MS Controlled Vocabulary).
Definition: cv.hpp:14924
pwiz::data::UserParam::type
std::string type
the datatype of the parameter, where appropriate (e.g.: xsd:float).
Definition: ParamTypes.hpp:194
MS_charge_state
MS_charge_state
charge state: The charge state of the ion, single or multiple and positive or negatively charged.
Definition: cv.hpp:396
pwiz::data::BaseDiffConfig
Definition: diff_std.hpp:42
testIntegral
void testIntegral()
Definition: diff_std_test.cpp:65
pwiz::data::BaseDiffConfig::precision
double precision
Definition: diff_std.hpp:44
pwiz::cv::CV
Information about an ontology or CV source and a short 'lookup' tag to refer to.
Definition: cv.hpp:14916
pwiz::cv::CV::URI
std::string URI
the URI for the resource.
Definition: cv.hpp:14921
pwiz::cv
Definition: cv.hpp:108
pwiz::data::ParamContainer::userParams
std::vector< UserParam > userParams
a collection of uncontrolled user terms
Definition: ParamTypes.hpp:253
pwiz::data
Definition: BinaryIndexStream.hpp:31
pwiz::cv::CV::id
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
pwiz::data::UserParam::units
CVID units
an optional CV parameter for the unit term associated with the value, if any (e.g....
Definition: ParamTypes.hpp:197
testParamGroup
void testParamGroup()
Definition: diff_std_test.cpp:250
pwiz::cv::CV::version
std::string version
the version of the CV from which the referred-to terms are drawn.
Definition: cv.hpp:14927
UO_second
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
testUserParam
void testUserParam()
Definition: diff_std_test.cpp:119
MS_ionization_type
MS_ionization_type
ionization type: The method by which gas phase ions are generated from the sample.
Definition: cv.hpp:285
pwiz::data::ParamGroup
A collection of CVParam and UserParam elements that can be referenced from elsewhere in this mzML doc...
Definition: ParamTypes.hpp:347
pwiz::data::Diff
Calculate diffs of objects in a ProteoWizard data model hierarchy.
Definition: diff_std.hpp:143
pwiz::data::ParamGroupPtr
boost::shared_ptr< ParamGroup > ParamGroupPtr
Definition: ParamTypes.hpp:239
pwiz::util
Definition: almost_equal.hpp:33
TEST_EPILOG
#define TEST_EPILOG
Definition: unit.hpp:183
testIntegralReally
void testIntegralReally(integral_type a, integral_type b)
Definition: diff_std_test.cpp:52
pwiz::data::ParamContainer
The base class for elements that may contain cvParams, userParams, or paramGroup references.
Definition: ParamTypes.hpp:245
pwiz::data::diff_string
std::string diff_string(const diff_type &diff)
Definition: diff_std.hpp:177
test
void test()
Definition: diff_std_test.cpp:275
main
int main(int argc, char *argv[])
Definition: diff_std_test.cpp:304
pwiz::data::CVParam::value
std::string value
Definition: ParamTypes.hpp:47
Std.hpp
diff
void diff(const string &filename1, const string &filename2)
Definition: FrequencyDataTest.cpp:40
pwiz::data::UserParam::value
std::string value
the value for the parameter, where appropriate.
Definition: ParamTypes.hpp:191
pwiz::data::diff_impl
Definition: diff_std.hpp:50
epsilon
const double epsilon
Definition: DiffTest.cpp:41
testCVParam
void testCVParam()
Definition: diff_std_test.cpp:149
testCV
void testCV()
Definition: diff_std_test.cpp:88
pwiz::data::diff_impl::diff_floating
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
TEST_FAILED
#define TEST_FAILED(x)
Definition: unit.hpp:177
TEST_PROLOG
#define TEST_PROLOG(argc, argv)
Definition: unit.hpp:175
MS_m_z
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
testString
void testString(const string &a, const string &b)
Definition: diff_std_test.cpp:37
MS_peak_intensity
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
pwiz::data::ParamContainer::paramGroupPtrs
std::vector< ParamGroupPtr > paramGroupPtrs
a collection of references to ParamGroups
Definition: ParamTypes.hpp:247
os_
ostream * os_
Definition: diff_std_test.cpp:35
pwiz::data::CVParam::cvid
CVID cvid
Definition: ParamTypes.hpp:46
unit.hpp
unit_assert
#define unit_assert(x)
Definition: unit.hpp:85
testParamContainer
void testParamContainer()
Definition: diff_std_test.cpp:203
UO_minute
UO_minute
minute: A time unit which is equal to 60 seconds.
Definition: cv.hpp:13896
diff_std.hpp
testFloating
void testFloating(floating_type a, floating_type b, floating_type precision)
Definition: diff_std_test.cpp:75
pwiz::data::CVParam
represents a tag-value pair, where the tag comes from the controlled vocabulary
Definition: ParamTypes.hpp:45
pwiz::data::UserParam::name
std::string name
the name for the parameter.
Definition: ParamTypes.hpp:188
pwiz::data::diff_impl::diff_integral
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
pwiz::data::ParamContainer::cvParams
std::vector< CVParam > cvParams
a collection of controlled vocabulary terms
Definition: ParamTypes.hpp:250