ProteoWizard
ProteinListCacheTest.cpp
Go to the documentation of this file.
1//
2// $Id$
3//
4//
5// Original author: Matt Chambers <matt.chambers <a.t> vanderbilt.edu>
6//
7// Copyright 2008 Vanderbilt University - Nashville, TN 37232
8//
9// Licensed under the Apache License, Version 2.0 (the "License");
10// you may not use this file except in compliance with the License.
11// You may obtain a copy of the License at
12//
13// http://www.apache.org/licenses/LICENSE-2.0
14//
15// Unless required by applicable law or agreed to in writing, software
16// distributed under the License is distributed on an "AS IS" BASIS,
17// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18// See the License for the specific language governing permissions and
19// limitations under the License.
20//
21
22
23#include "ProteinListCache.hpp"
26#include "Serializer_FASTA.hpp"
27
28
29using namespace pwiz::util;
30using namespace pwiz::proteome;
31
32
33ostream* os_ = 0;
34
35
36namespace std {
37
38ostream& operator<< (ostream& os, const ProteinListCache::CacheType& cache)
39{
40 os << "Protein cache indices (from MRU to LRU):";
41 for (ProteinListCache::CacheType::const_iterator itr = cache.begin(); itr != cache.end(); ++itr)
42 os << " " << itr->second->index;
43 return os;
44}
45
46} // namespace std
47
48
50{
51 // initialize list
52 ProteomeData pd;
53 shared_ptr<ProteinListSimple> sl(new ProteinListSimple);
54 sl->proteins.push_back(ProteinPtr(new Protein("P1", 0, "0", "ABC")));
55 sl->proteins.push_back(ProteinPtr(new Protein("P2", 1, "1", "DEF")));
56 sl->proteins.push_back(ProteinPtr(new Protein("P3", 2, "2", "GHI")));
57 sl->proteins.push_back(ProteinPtr(new Protein("P4", 3, "3", "JKL")));
58 pd.proteinListPtr = sl;
59
60 // ProteinListSimple returns the same shared_ptrs regardless of caching;
61 // serializing to FASTA and back will produce different shared_ptrs
62 boost::shared_ptr<stringstream> ss(new stringstream);
63 Serializer_FASTA serializer;
64 serializer.write(*ss, pd, 0);
65 serializer.read(ss, pd);
66
67 // access a series of proteins and make sure the cache behaves appropriately:
68 // in off mode, the cache should always be empty
69
70 ProteinPtr s;
71
73 const ProteinListCache::CacheType& cache = slc.cache();
74
75 unit_assert(cache.empty());
76
77 s = slc.protein(0, false);
78 s = slc.protein(1, true);
79 unit_assert_operator_equal("1", s->description);
80 unit_assert_operator_equal("DEF", s->sequence());
81 s = slc.protein(2, false);
82 s = slc.protein(3, true);
83
84 if (os_) *os_ << cache << endl;
85 unit_assert(cache.empty());
86}
87
88
90{
91 // initialize list
92 ProteomeData pd;
93 shared_ptr<ProteinListSimple> sl(new ProteinListSimple);
94 sl->proteins.push_back(ProteinPtr(new Protein("P1", 0, "0", "ABC")));
95 sl->proteins.push_back(ProteinPtr(new Protein("P2", 1, "1", "DEF")));
96 sl->proteins.push_back(ProteinPtr(new Protein("P3", 2, "2", "GHI")));
97 sl->proteins.push_back(ProteinPtr(new Protein("P4", 3, "3", "JKL")));
98 pd.proteinListPtr = sl;
99
100 // ProteinListSimple returns the same shared_ptrs regardless of caching;
101 // serializing to FASTA and back will produce different shared_ptrs
102 boost::shared_ptr<stringstream> ss(new stringstream);
103 Serializer_FASTA serializer;
104 serializer.write(*ss, pd, 0);
105 serializer.read(ss, pd);
106
107 // access a series of proteins and make sure the cache behaves appropriately:
108 // in metadata-only mode, entries in the cache should:
109 // - always have metadata
110 // - never have sequences
111
112 ProteinPtr s;
113
115 const ProteinListCache::CacheType& cache = slc.cache();
116
117 unit_assert(cache.empty());
119
120 s = slc.protein(0, false);
121
122 // pointers should be equal
123 unit_assert_operator_equal(slc.protein(0, false), s);
124
125 if (os_) *os_ << cache << endl;
126 unit_assert(!cache.empty());
128 unit_assert_operator_equal(0, cache.mru().second->index);
129 unit_assert_operator_equal("0", cache.mru().second->description);
130 unit_assert_operator_equal("", cache.mru().second->sequence());
131
132 // with-sequence access should return the sequence, but only cache the metadata
133 s = slc.protein(1, true);
134 unit_assert_operator_equal("DEF", s->sequence());
135
136 if (os_) *os_ << cache << endl;
138 unit_assert_operator_equal(1, cache.mru().second->index);
139 unit_assert_operator_equal("", cache.mru().second->sequence());
140 unit_assert_operator_equal(0, cache.lru().second->index);
141
142 s = slc.protein(2, false);
143
144 // pointers should be equal
145 unit_assert_operator_equal(slc.protein(2, false), s);
146
147 if (os_) *os_ << cache << endl;
149 unit_assert_operator_equal(2, cache.mru().second->index);
150 unit_assert_operator_equal("", cache.mru().second->sequence());
151 unit_assert_operator_equal(1, cache.lru().second->index);
152
153 s = slc.protein(3, true);
154 unit_assert_operator_equal("JKL", s->sequence());
155
156 if (os_) *os_ << cache << endl;
158 unit_assert_operator_equal(3, cache.mru().second->index);
159 unit_assert_operator_equal("", cache.mru().second->sequence());
160 unit_assert_operator_equal(2, cache.lru().second->index);
161
162 s = slc.protein(2, true);
163 unit_assert_operator_equal("GHI", s->sequence());
164
165 if (os_) *os_ << cache << endl;
167 unit_assert_operator_equal(2, cache.mru().second->index);
168 unit_assert_operator_equal("", cache.mru().second->sequence());
169 unit_assert_operator_equal(3, cache.lru().second->index);
170}
171
172
174{
175 // initialize list
176 ProteomeData pd;
177 shared_ptr<ProteinListSimple> sl(new ProteinListSimple);
178 sl->proteins.push_back(ProteinPtr(new Protein("P1", 0, "0", "ABC")));
179 sl->proteins.push_back(ProteinPtr(new Protein("P2", 1, "1", "DEF")));
180 sl->proteins.push_back(ProteinPtr(new Protein("P3", 2, "2", "GHI")));
181 sl->proteins.push_back(ProteinPtr(new Protein("P4", 3, "3", "JKL")));
182 pd.proteinListPtr = sl;
183
184 // ProteinListSimple returns the same shared_ptrs regardless of caching;
185 // serializing to FASTA and back will produce different shared_ptrs
186 boost::shared_ptr<stringstream> ss(new stringstream);
187 Serializer_FASTA serializer;
188 serializer.write(*ss, pd, 0);
189 serializer.read(ss, pd);
190
191 // access a series of proteins and make sure the cache behaves appropriately:
192 // in metadata-and-sequence mode, entries in the cache should:
193 // - always have metadata
194 // - always have sequences
195
196 ProteinPtr s;
197
198 ProteinListCache slc(pd.proteinListPtr, ProteinListCacheMode_MetaDataAndSequence, 2);
199 const ProteinListCache::CacheType& cache = slc.cache();
200
201 unit_assert(cache.empty());
203
204 // metadata-only access should not affect the cache
205 s = slc.protein(0, false);
206
207 if (os_) *os_ << cache << endl;
208 unit_assert(cache.empty());
210
211 s = slc.protein(1, true);
212
213 // pointers should be equal
214 unit_assert_operator_equal(slc.protein(1, true), s);
215
216 if (os_) *os_ << cache << endl;
218 unit_assert_operator_equal(1, cache.mru().second->index);
219 unit_assert_operator_equal("1", cache.mru().second->description);
220 unit_assert_operator_equal("DEF", cache.mru().second->sequence());
221
222 // metadata-only access should not affect the cache
223 s = slc.protein(2, false);
224
225 if (os_) *os_ << cache << endl;
227 unit_assert_operator_equal(1, cache.mru().second->index);
228 unit_assert_operator_equal("DEF", cache.mru().second->sequence());
229
230 s = slc.protein(3, true);
231
232 // pointers should be equal
233 unit_assert_operator_equal(slc.protein(3, true), s);
234
235 if (os_) *os_ << cache << endl;
237 unit_assert_operator_equal(3, cache.mru().second->index);
238 unit_assert_operator_equal("JKL", cache.mru().second->sequence());
239 unit_assert_operator_equal(1, cache.lru().second->index);
240
241 s = slc.protein(2, true);
242
243 if (os_) *os_ << cache << endl;
245 unit_assert_operator_equal(2, cache.mru().second->index);
246 unit_assert_operator_equal("GHI", cache.mru().second->sequence());
247 unit_assert_operator_equal(3, cache.lru().second->index);
248}
249
250
257
258
259int main(int argc, char* argv[])
260{
261 TEST_PROLOG(argc, argv)
262
263 try
264 {
265 if (argc>1 && !strcmp(argv[1],"-v")) os_ = &cout;
266 test();
267 }
268 catch (exception& e)
269 {
270 TEST_FAILED(e.what())
271 }
272 catch (...)
273 {
274 TEST_FAILED("Caught unknown exception.")
275 }
276
278}
ProteinListCacheMode_Off
ProteinListCacheMode_MetaDataOnly
int main(int argc, char *argv[])
void testModeMetaDataOnly()
void testModeOff()
ostream * os_
void test()
void testModeMetaDataAndSequence()
adds a level of flexible MRU caching to a ProteinList processor chain
const CacheType & cache() const
get a const-reference to the cache
virtual ProteinPtr protein(size_t index, bool getSequence=true) const
returns the requested spectrum which may or may not be cached depending on the current cache mode
ProteomeData <-> FASTA stream serialization.
void read(boost::shared_ptr< std::istream > is, ProteomeData &pd) const
read in ProteomeData object from a FASTA istream
void write(std::ostream &os, const ProteomeData &pd, const pwiz::util::IterationListenerRegistry *iterationListenerRegistry) const
write ProteomeData object to ostream as FASTA
bool empty() const
Definition mru_list.hpp:91
std::size_t max_size() const
Definition mru_list.hpp:93
const item_type & mru() const
Definition mru_list.hpp:96
std::size_t size() const
Definition mru_list.hpp:92
const item_type & lru() const
Definition mru_list.hpp:97
boost::shared_ptr< Protein > ProteinPtr
STL namespace.
ostream & operator<<(ostream &os, SpectrumListCache::CacheType &cache)
#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_operator_equal(expected, actual)
Definition unit.hpp:92
#define TEST_PROLOG(argc, argv)
Definition unit.hpp:175