ProteoWizard
MZToleranceTest.cpp
Go to the documentation of this file.
1//
2// $Id$
3//
4//
5// Original author: Darren Kessner <darren@proteowizard.org>
6//
7// Copyright 2009 Center for Applied Molecular Medicine
8// University of Southern California, Los Angeles, CA
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
24#include "MZTolerance.hpp"
27#include <cstring>
28
29
30using namespace pwiz::util;
31using namespace pwiz::chemistry;
32
33
34ostream* os_ = 0;
35
36
37const double epsilon_ = numeric_limits<double>::epsilon();
38
39
40void testMZ()
41{
42 double x = 1000;
43 MZTolerance tolerance(.1);
44
45 x += tolerance;
47
48 x -= tolerance;
50
51 unit_assert_equal(x+tolerance, 1000.1, epsilon_);
52 unit_assert_equal(x-tolerance, 999.9, epsilon_);
53}
54
55
56void testPPM()
57{
58 double x = 1000;
59 MZTolerance tolerance(5, MZTolerance::PPM);
60
61 x += tolerance;
62 unit_assert_equal(x, 1000.005, epsilon_);
63
64 x -= tolerance;
65 const double delta = 1000.005 * 5e-6; // a little more than .005
66 unit_assert_equal(x, 1000.005 - delta, epsilon_);
67
68 unit_assert_equal(1000+tolerance, 1000.005, epsilon_);
69 unit_assert_equal(1000-tolerance, 999.995, epsilon_);
70
71 unit_assert_equal(-1000+tolerance, -999.995, epsilon_);
72 unit_assert_equal(-1000-tolerance, -1000.005, epsilon_);
73}
74
75
77{
78 MZTolerance fiveppm(5, MZTolerance::PPM);
79 unit_assert(isWithinTolerance(1000.001, 1000, fiveppm));
80 unit_assert(isWithinTolerance(999.997, 1000, fiveppm));
81 unit_assert(!isWithinTolerance(1000.01, 1000, fiveppm));
82 unit_assert(!isWithinTolerance(999.99, 1000, fiveppm));
83
84 MZTolerance delta(.01);
85 unit_assert(isWithinTolerance(1000.001, 1000, delta));
86 unit_assert(isWithinTolerance(999.999, 1000, delta));
87 unit_assert(!isWithinTolerance(1000.1, 1000, delta));
88 unit_assert(!isWithinTolerance(999.9, 1000, .01)); // automatic conversion
89}
90
91
92void testIO()
93{
94 if (os_) *os_ << "testIO()\n";
95
96 MZTolerance temp;
97 if (os_) *os_ << "temp: " << temp << endl;
98
99 MZTolerance fiveppm(5, MZTolerance::PPM);
100 MZTolerance blackbirds(4.20, MZTolerance::MZ);
101
102 {
103 ostringstream oss;
104 oss << fiveppm;
105 if (os_) *os_ << "fiveppm: " << oss.str() << endl;
106
107 {
108 istringstream iss(oss.str());
109 iss >> temp;
110 if (os_) *os_ << "temp: " << temp << endl;
111 unit_assert(temp == fiveppm);
112 unit_assert(temp != blackbirds);
113 }
114
115 {
116 istringstream iss("5.0 PPM");
117 iss >> temp;
118 if (os_) *os_ << "temp: " << temp << endl;
119 unit_assert(temp == fiveppm);
120 }
121
122 {
123 istringstream iss("5ppm");
124 iss >> temp;
125 if (os_) *os_ << "temp: " << temp << endl;
126 unit_assert(temp == fiveppm);
127 }
128 }
129
130 {
131 ostringstream oss;
132 oss << blackbirds;
133 if (os_) *os_ << "blackbirds: " << oss.str() << endl;
134
135 {
136 istringstream iss(oss.str());
137 iss >> temp;
138 if (os_) *os_ << "temp: " << temp << endl;
139 unit_assert(temp == blackbirds);
140 unit_assert(temp != fiveppm);
141 }
142
143 {
144 istringstream iss("4.2mz");
145 iss >> temp;
146 if (os_) *os_ << "temp: " << temp << endl;
147 unit_assert(temp == blackbirds);
148 }
149
150 {
151 istringstream iss("4.20 da");
152 iss >> temp;
153 if (os_) *os_ << "temp: " << temp << endl;
154 unit_assert(temp == blackbirds);
155 }
156
157 {
158 istringstream iss("4.2 DALTONS");
159 iss >> temp;
160 if (os_) *os_ << "temp: " << temp << endl;
161 unit_assert(temp == blackbirds);
162 }
163 }
164}
165
166
167void test()
168{
169 testMZ();
170 testPPM();
172 testIO();
173}
174
175
176int main(int argc, char* argv[])
177{
178 TEST_PROLOG(argc, argv)
179
180 try
181 {
182 if (argc>1 && !strcmp(argv[1],"-v")) os_ = &cout;
183 test();
184 }
185 catch (exception& e)
186 {
187 TEST_FAILED(e.what())
188 }
189 catch (...)
190 {
191 TEST_FAILED("Caught unknown exception.")
192 }
193
195}
196
int main(int argc, char *argv[])
void testPPM()
void testIsWithinTolerance()
void testIO()
const double epsilon_
void testMZ()
ostream * os_
void test()
KernelTraitsBase< Kernel >::space_type::abscissa_type x
PWIZ_API_DECL bool isWithinTolerance(double a, double b, const MZTolerance &tolerance)
returns true iff a is in (b-tolerance, b+tolerance)
struct for expressing m/z tolerance in either amu or ppm
#define unit_assert(x)
Definition unit.hpp:85
#define TEST_EPILOG
Definition unit.hpp:183
#define TEST_FAILED(x)
Definition unit.hpp:177
#define unit_assert_equal(x, y, epsilon)
Definition unit.hpp:99
#define TEST_PROLOG(argc, argv)
Definition unit.hpp:175