ProteoWizard
SpectrumListWrapperTest.cpp
Go to the documentation of this file.
1 //
2 // $Id: SpectrumListWrapperTest.cpp 4129 2012-11-20 00:05:37Z chambm $
3 //
4 //
5 // Original author: Darren Kessner <darren@proteowizard.org>
6 //
7 // Copyright 2008 Spielberg Family Center for Applied Proteomics
8 // Cedars Sinai Medical Center, Los Angeles, California 90048
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 "SpectrumListWrapper.hpp"
27 
28 using namespace pwiz::cv;
29 using namespace pwiz::msdata;
30 using namespace pwiz::util;
31 
32 
33 class MyWrapper : public SpectrumListWrapper
34 {
35  public:
36 
37  MyWrapper(const SpectrumListPtr& inner)
38  : SpectrumListWrapper(inner)
39  {}
40 
41  void verifySize(size_t size)
42  {
43  // verify that we can see inner_
44  unit_assert(size == inner_->size());
45  }
46 
47  virtual SpectrumPtr spectrum(size_t index, bool getBinaryData = false) const {return inner_->spectrum(index, getBinaryData);}
48 };
49 
50 
52 {
53  // a simple filter that returns only even indices
54 
55  public:
56 
58  : SpectrumListWrapper(inner)
59  {}
60 
61  virtual size_t size() const {return inner_->size()/2;}
62  virtual const SpectrumIdentity& spectrumIdentity(size_t index) const {return inner_->spectrumIdentity(index*2);}
63  virtual SpectrumPtr spectrum(size_t index, bool getBinaryData = false) const {return inner_->spectrum(index*2, getBinaryData);}
64 };
65 
66 
67 void test()
68 {
70 
71  const size_t spectrumCount = 10;
72  for (size_t i=0; i<spectrumCount; i++)
73  {
74  simple->spectra.push_back(SpectrumPtr(new Spectrum));
75  Spectrum& s = *simple->spectra.back();
76  s.index = i;
77  s.id = "scan=" + lexical_cast<string>(i);
78  }
79 
80  // check MyWrapper
81 
82  shared_ptr<MyWrapper> wrapper(new MyWrapper(simple));
83 
84  wrapper->verifySize(10);
85  unit_assert(wrapper->size() == 10);
86  for (size_t i=0; i<spectrumCount; i++)
87  {
88  string id = "scan=" + lexical_cast<string>(i);
89 
90  unit_assert(wrapper->find(id) == i);
91  IndexList indexList = wrapper->findNameValue("scan", lexical_cast<string>(i));
92  unit_assert(indexList.size()==1 && indexList[0]==i);
93 
94  const SpectrumIdentity& identity = wrapper->spectrumIdentity(i);
95  unit_assert(identity.id == id);
96 
97  SpectrumPtr s = wrapper->spectrum(i);
98  unit_assert(s->id == id);
99  }
100 
101  // check FilterWrapper
102 
103  shared_ptr<FilterWrapper> filterWrapper(new FilterWrapper(simple));
104 
105  unit_assert(filterWrapper->size() == 5);
106 
107  for (size_t i=0; i<filterWrapper->size(); i++)
108  {
109  string id = "scan=" + lexical_cast<string>(i*2);
110  string scanNumber = lexical_cast<string>(i*2);
111 
112  unit_assert(filterWrapper->find(id) == i);
113  IndexList indexList = filterWrapper->findNameValue("scan", scanNumber);
114  unit_assert(indexList.size()==1 && indexList[0]==i);
115 
116  const SpectrumIdentity& identity = filterWrapper->spectrumIdentity(i);
117  unit_assert(identity.id == id);
118 
119  SpectrumPtr s = filterWrapper->spectrum(i);
120  unit_assert(s->id == id);
121  }
122 }
123 
124 
125 int main(int argc, const char* argv[])
126 {
127  TEST_PROLOG(argc, argv)
128 
129  try
130  {
131  test();
132  }
133  catch (exception& e)
134  {
135  TEST_FAILED(e.what())
136  }
137  catch (...)
138  {
139  TEST_FAILED("Caught unknown exception.")
140  }
141 
143 }
144 
145