ProteoWizard
Namespaces | Functions | Variables
ProteinListCacheTest.cpp File Reference
#include "ProteinListCache.hpp"
#include "pwiz/utility/misc/unit.hpp"
#include "pwiz/utility/misc/Std.hpp"
#include "Serializer_FASTA.hpp"

Go to the source code of this file.

Namespaces

namespace  std
 STL namespace.
 

Functions

ostream & std::operator<< (ostream &os, const ProteinListCache::CacheType &cache)
 
void testModeOff ()
 
void testModeMetaDataOnly ()
 
void testModeMetaDataAndSequence ()
 
void test ()
 
int main (int argc, char *argv[])
 

Variables

ostream * os_ = 0
 

Function Documentation

◆ testModeOff()

void testModeOff ( )

Definition at line 49 of file ProteinListCacheTest.cpp.

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}
ProteinListCacheMode_Off
ostream * os_
adds a level of flexible MRU caching to a ProteinList processor chain
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
boost::shared_ptr< Protein > ProteinPtr
#define unit_assert(x)
Definition unit.hpp:85
#define unit_assert_operator_equal(expected, actual)
Definition unit.hpp:92

References pwiz::proteome::ProteinListCache::cache(), pwiz::util::mru_list< Item, KeyExtractor >::empty(), os_, pwiz::proteome::ProteinListCache::protein(), ProteinListCacheMode_Off, pwiz::proteome::ProteomeData::proteinListPtr, pwiz::proteome::Serializer_FASTA::read(), unit_assert, unit_assert_operator_equal, and pwiz::proteome::Serializer_FASTA::write().

Referenced by test().

◆ testModeMetaDataOnly()

void testModeMetaDataOnly ( )

Definition at line 89 of file ProteinListCacheTest.cpp.

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}
ProteinListCacheMode_MetaDataOnly
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

References pwiz::proteome::ProteinListCache::cache(), pwiz::util::mru_list< Item, KeyExtractor >::empty(), pwiz::util::mru_list< Item, KeyExtractor >::lru(), pwiz::util::mru_list< Item, KeyExtractor >::max_size(), pwiz::util::mru_list< Item, KeyExtractor >::mru(), os_, pwiz::proteome::ProteinListCache::protein(), ProteinListCacheMode_MetaDataOnly, pwiz::proteome::ProteomeData::proteinListPtr, pwiz::proteome::Serializer_FASTA::read(), pwiz::util::mru_list< Item, KeyExtractor >::size(), unit_assert, unit_assert_operator_equal, and pwiz::proteome::Serializer_FASTA::write().

Referenced by test().

◆ testModeMetaDataAndSequence()

void testModeMetaDataAndSequence ( )

Definition at line 173 of file ProteinListCacheTest.cpp.

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}

References pwiz::proteome::ProteinListCache::cache(), pwiz::util::mru_list< Item, KeyExtractor >::empty(), pwiz::util::mru_list< Item, KeyExtractor >::lru(), pwiz::util::mru_list< Item, KeyExtractor >::max_size(), pwiz::util::mru_list< Item, KeyExtractor >::mru(), os_, pwiz::proteome::ProteinListCache::protein(), pwiz::proteome::ProteomeData::proteinListPtr, pwiz::proteome::Serializer_FASTA::read(), pwiz::util::mru_list< Item, KeyExtractor >::size(), unit_assert, unit_assert_operator_equal, and pwiz::proteome::Serializer_FASTA::write().

Referenced by test().

◆ test()

void test ( )

Definition at line 251 of file ProteinListCacheTest.cpp.

252{
253 testModeOff();
256}
void testModeMetaDataOnly()
void testModeOff()
void testModeMetaDataAndSequence()

References testModeMetaDataAndSequence(), testModeMetaDataOnly(), and testModeOff().

Referenced by main().

◆ main()

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

Definition at line 259 of file ProteinListCacheTest.cpp.

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}
void test()
#define TEST_EPILOG
Definition unit.hpp:183
#define TEST_FAILED(x)
Definition unit.hpp:177
#define TEST_PROLOG(argc, argv)
Definition unit.hpp:175

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

Variable Documentation

◆ os_

ostream* os_ = 0