ProteoWizard
Serializer_FASTA_Test.cpp
Go to the documentation of this file.
1//
2// $Id$
3//
4//
5// Original author: Matt Chambers <matt.chambers .@. vanderbilt.edu>
6//
7// Copyright 2009 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 "Serializer_FASTA.hpp"
24#include "Diff.hpp"
25#include "examples.hpp"
29#include "boost/thread/thread.hpp"
30#include "boost/thread/barrier.hpp"
31
32
33using namespace pwiz::util;
34using namespace pwiz::proteome;
35using namespace pwiz::data;
36
37
38ostream* os_ = 0;
39
40
42{
43 ProteomeData pd;
45
46 Serializer_FASTA serializer(config);
47
48 ostringstream oss;
49 serializer.write(oss, pd, NULL);
50
51 if (os_) *os_ << "oss:\n" << oss.str() << endl;
52
53 shared_ptr<istringstream> iss(new istringstream(oss.str()));
54 ProteomeData pd2;
55 serializer.read(iss, pd2);
56
58 if (os_ && diff) *os_ << diff << endl;
60}
61
62
64{
65 if (os_) *os_ << "testWriteRead() MemoryIndex" << endl;
67 testWriteRead(config);
68
69 if (os_) *os_ << "testWriteRead() BinaryIndexStream" << endl;
70 shared_ptr<stringstream> indexStringStream(new stringstream);
71 config.indexPtr.reset(new BinaryIndexStream(indexStringStream));
72 testWriteRead(config);
73}
74
75
76void testThreadSafetyWorker(pair<boost::barrier*, ProteomeData*>* args)
77{
78 args->first->wait(); // wait until all threads have started
79
80 try
81 {
83
84 for (int i=0; i < 3; ++i)
85 {
86 for (size_t j=0; j < args->second->proteinListPtr->size(); ++j)
87 unit_assert(args->second->proteinListPtr->protein(j)->index == j);
88 }
89 }
90 catch (exception& e)
91 {
92 cerr << "Exception in worker thread: " << e.what() << endl;
93 }
94 catch (...)
95 {
96 cerr << "Unhandled exception in worker thread." << endl;
97 }
98}
99
100void testThreadSafety(const int& testThreadCount)
101{
102 ProteomeData pd;
104
105 boost::barrier testBarrier(testThreadCount);
106 boost::thread_group testThreadGroup;
107 for (int i=0; i < testThreadCount; ++i)
108 testThreadGroup.add_thread(new boost::thread(&testThreadSafetyWorker, new pair<boost::barrier*, ProteomeData*>(&testBarrier, &pd)));
109 testThreadGroup.join_all();
110}
111
113{
114 ProteomeData pd;
115 pd.id = "tiny";
116
117 shared_ptr<ProteinListSimple> proteinListPtr(new ProteinListSimple);
118 pd.proteinListPtr = proteinListPtr;
119
120 proteinListPtr->proteins.push_back(ProteinPtr(new Protein("ABC123", 0, "One two three.", "ELVISLIVES")));
121 proteinListPtr->proteins.push_back(ProteinPtr(new Protein("ZEBRA", 1, "Has stripes:", "BLACKANDWHITE")));
122 proteinListPtr->proteins.push_back(ProteinPtr(new Protein("DEFCON42", 2, "", "DNTPANIC")));
123 proteinListPtr->proteins.push_back(ProteinPtr(new Protein("ZEBRA", 1, "Black and white", "STRIPES")));
124
125 Serializer_FASTA serializer;
126
127 ostringstream oss;
128 serializer.write(oss, pd, NULL);
129
130 if (os_) *os_ << "oss:\n" << oss.str() << endl;
131
132 shared_ptr<istringstream> iss(new istringstream(oss.str()));
133 ProteomeData pd2;
134
135 unit_assert_throws_what(serializer.read(iss, pd2), runtime_error,
136 "[ProteinList_FASTA::createIndex] duplicate protein id \"ZEBRA\"");
137}
138
139
140int main(int argc, char* argv[])
141{
142 TEST_PROLOG(argc, argv)
143
144 try
145 {
146 if (argc>1 && !strcmp(argv[1],"-v")) os_ = &cout;
152 }
153 catch (exception& e)
154 {
155 TEST_FAILED(e.what())
156 }
157 catch (...)
158 {
159 TEST_FAILED("Caught unknown exception.")
160 }
161
163}
void testThreadSafety()
void diff(const string &filename1, const string &filename2)
int main(int argc, char *argv[])
void testThreadSafetyWorker(pair< boost::barrier *, ProteomeData * > *args)
void testDuplicateId()
ostream * os_
void testWriteRead()
index implementation in a stream (intended for fstreams but any iostream works); find(string id) is O...
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
PWIZ_API_DECL void initializeTiny(ProteomeData &pd)
boost::shared_ptr< Protein > ProteinPtr
Calculate diffs of objects in a ProteoWizard data model hierarchy.
Definition diff_std.hpp:143
Serializer_FASTA configuration.
#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_throws_what(x, exception, whatStr)
Definition unit.hpp:119
#define TEST_PROLOG(argc, argv)
Definition unit.hpp:175