ProteoWizard
Classes | Functions | Variables
SavitzkyGolaySmootherTest.cpp File Reference
#include "SavitzkyGolaySmoother.hpp"
#include "pwiz/utility/misc/unit.hpp"
#include "pwiz/utility/misc/Std.hpp"

Go to the source code of this file.

Classes

struct  TestData
 

Functions

vector< double > parseDoubleArray (const string &doubleArray)
 
void test ()
 
int main (int argc, char *argv[])
 

Variables

ostream * os_ = 0
 
const TestData testData []
 
const size_t testDataSize = sizeof(testData) / sizeof(TestData)
 

Function Documentation

§ parseDoubleArray()

vector<double> parseDoubleArray ( const string &  doubleArray)

Definition at line 162 of file SavitzkyGolaySmootherTest.cpp.

Referenced by test().

163 {
164  vector<double> doubleVector;
165  vector<string> tokens;
166  bal::split(tokens, doubleArray, bal::is_space(), bal::token_compress_on);
167  if (!tokens.empty() && !tokens[0].empty())
168  for (size_t i=0; i < tokens.size(); ++i)
169  doubleVector.push_back(max(0.0, lexical_cast<double>(tokens[i])));
170  return doubleVector;
171 }

§ test()

void test ( )

Definition at line 174 of file SavitzkyGolaySmootherTest.cpp.

References parseDoubleArray(), TestData::polynomialOrder, testDataSize, unit_assert, unit_assert_equal, TestData::windowSize, TestData::xRaw, TestData::xSmoothed, TestData::yRaw, and TestData::ySmoothed.

Referenced by main().

175 {
176  // test that invalid value exceptions are thrown
177 
178  // order too low
179  /*unit_assert_throws_what(SavitzkyGolaySmoother(1, 15), runtime_error, \
180  "[SavitzkyGolaySmoother::ctor()] Invalid value for polynomial order; valid range is [2, 20]");
181 
182  // order too high
183  unit_assert_throws_what(SavitzkyGolaySmoother(21, 15), runtime_error, \
184  "[SavitzkyGolaySmoother::ctor()] Invalid value for polynomial order; valid range is [2, 20]");
185 
186  // window size too small
187  unit_assert_throws_what(SavitzkyGolaySmoother(2, 3), runtime_error, \
188  "[SavitzkyGolaySmoother::ctor()] Invalid value for window size; value must be odd and in range [5, infinity)");
189 
190  // window size isn't odd
191  unit_assert_throws_what(SavitzkyGolaySmoother(2, 6), runtime_error, \
192  "[SavitzkyGolaySmoother::ctor()] Invalid value for window size; value must be odd and in range [5, infinity)");
193 */
194  SavitzkyGolaySmoother(2, 100001); // window size is valid up to numeric limits
195 
196  /*if (os_)
197  {
198  *os_ << "Unsmoothed data (" << testY.size() << "):\t";
199  copy(testY.begin(), testY.end(), ostream_iterator<double>(*os_, "\t"));
200  *os_ << endl;
201  }*/
202 
203  /*if (os_)
204  {
205  *os_ << "Smoothed data (" << smoothData.size() << "):\t";
206  copy(smoothData.begin(), smoothData.end(), ostream_iterator<double>(*os_, "\t"));
207  *os_ << endl;
208  }*/
209 
210  for (size_t i=0; i < testDataSize; ++i)
211  {
212  const TestData& data = testData[i];
213 
214  vector<double> xRaw = parseDoubleArray(data.xRaw);
215  vector<double> yRaw = parseDoubleArray(data.yRaw);
216  vector<double> targetSmoothedX = parseDoubleArray(data.xSmoothed);
217  vector<double> targetSmoothedY = parseDoubleArray(data.ySmoothed);
218 
219  // sanity checks
220  unit_assert(xRaw.size() == yRaw.size());
221  unit_assert(targetSmoothedX.size() == targetSmoothedY.size());
222 
223  SavitzkyGolaySmoother smoother(data.polynomialOrder, data.windowSize);
224  vector<double> smoothedX, smoothedY;
225  smoother.smooth(xRaw, yRaw, smoothedX, smoothedY);
226 
227  unit_assert(smoothedX.size() == targetSmoothedX.size());
228  unit_assert(smoothedY.size() == targetSmoothedY.size());
229 
230  for (size_t j=0; j < smoothedY.size(); ++j)
231  unit_assert_equal(smoothedY[j], targetSmoothedY[j], 1e-5);
232  }
233 }
const char * yRaw
const char * xSmoothed
vector< double > parseDoubleArray(const string &doubleArray)
#define unit_assert_equal(x, y, epsilon)
Definition: unit.hpp:99
const char * ySmoothed
const TestData testData[]
const char * xRaw
const size_t testDataSize
#define unit_assert(x)
Definition: unit.hpp:85

§ main()

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

Definition at line 236 of file SavitzkyGolaySmootherTest.cpp.

References os_, test(), TEST_EPILOG, TEST_FAILED, and TEST_PROLOG.

237 {
238  TEST_PROLOG(argc, argv)
239 
240  try
241  {
242  if (argc>1 && !strcmp(argv[1],"-v")) os_ = &cout;
243  test();
244  }
245  catch (exception& e)
246  {
247  TEST_FAILED(e.what())
248  }
249  catch (...)
250  {
251  TEST_FAILED("Caught unknown exception.")
252  }
253 
255 }
#define TEST_EPILOG
Definition: unit.hpp:182
ostream * os_
#define TEST_FAILED(x)
Definition: unit.hpp:176
#define TEST_PROLOG(argc, argv)
Definition: unit.hpp:174

Variable Documentation

§ os_

ostream* os_ = 0

Definition at line 32 of file SavitzkyGolaySmootherTest.cpp.

Referenced by main().

§ testData

const TestData testData[]

Definition at line 44 of file SavitzkyGolaySmootherTest.cpp.

§ testDataSize

const size_t testDataSize = sizeof(testData) / sizeof(TestData)

Definition at line 159 of file SavitzkyGolaySmootherTest.cpp.

Referenced by test().