ProteoWizard
ReaderTest.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 "Reader.hpp"
26
27
28using namespace pwiz::util;
29using namespace pwiz::proteome;
30
31
32ostream* os_ = 0;
33
34
35class Reader1 : public Reader
36{
37 public:
38
39 struct Config
40 {
41 string name;
42 mutable bool done;
43 Config() : name("default"), done(false) {}
44 };
45
46 Config config;
47
48 virtual std::string identify(const std::string& uri, boost::shared_ptr<std::istream> uriStreamPtr) const
49 {
50 bool result = (uri == "1");
51 if (os_) *os_ << "Reader1::identify(): " << boolalpha << result << endl;
52 return std::string (result?uri:std::string(""));
53 }
54
55 virtual void read(const std::string& uri,
56 boost::shared_ptr<std::istream> uriStreamPtr,
57 ProteomeData& result) const
58 {
59 if (os_) *os_ << "Reader1::read()\n";
60 config.done = true;
61 }
62
63 virtual const char *getType() const {return "Reader1";} // satisfy inheritance
64};
65
66
67class Reader2 : public Reader
68{
69 public:
70
71 struct Config
72 {
73 string color;
74 mutable bool done;
75 Config() : color("orange"), done(false) {}
76 };
77
78 Config config;
79
80 virtual std::string identify(const std::string& uri, boost::shared_ptr<std::istream> uriStreamPtr) const
81 {
82 bool result = (uri == "2");
83 if (os_) *os_ << "Reader2::identify(): " << boolalpha << result << endl;
84 return std::string (result?uri:std::string(""));
85 }
86
87 virtual void read(const std::string& uri,
88 boost::shared_ptr<std::istream> uriStreamPtr,
89 ProteomeData& result) const
90 {
91 if (os_) *os_ << "Reader2::read()\n";
92 config.done = true;
93 }
94
95 const char *getType() const {return "Reader2";} // satisfy inheritance
96};
97
98
99void testGet()
100{
101 if (os_) *os_ << "testGet()\n";
102
103 ReaderList readers;
104 readers.push_back(ReaderPtr(new Reader1));
105 readers.push_back(ReaderPtr(new Reader2));
106
107 unit_assert(readers.size() == 2);
108
109 Reader1* reader1 = readers.get<Reader1>();
110 unit_assert(reader1);
111 if (os_) *os_ << "reader1 config: " << reader1->config.name << endl;
112 unit_assert(reader1->config.name == "default");
113 reader1->config.name = "raw";
114 if (os_) *os_ << "reader1 config: " << reader1->config.name << endl;
115 unit_assert(reader1->config.name == "raw");
116
117 Reader2* reader2 = readers.get<Reader2>();
118 unit_assert(reader2);
119 if (os_) *os_ << "reader2 config: " << reader2->config.color << endl;
120 unit_assert(reader2->config.color == "orange");
121 reader2->config.color = "purple";
122 if (os_) *os_ << "reader2 config: " << reader2->config.color << endl;
123 unit_assert(reader2->config.color == "purple");
124
125 const ReaderList& const_readers = readers;
126 const Reader2* constReader2 = const_readers.get<Reader2>();
127 unit_assert(constReader2);
128 if (os_) *os_ << "constReader2 config: " << constReader2->config.color << endl;
129
130 if (os_) *os_ << endl;
131}
132
133
135{
136 if (os_) *os_ << "testAccept()\n";
137
138 ReaderList readers;
139 readers.push_back(ReaderPtr(new Reader1));
140 readers.push_back(ReaderPtr(new Reader2));
141
142 if (os_) *os_ << "accept 1:\n";
143 unit_assert(readers.accept("1", shared_ptr<istream>()));
144 if (os_) *os_ << "accept 2:\n";
145 unit_assert(readers.accept("2", shared_ptr<istream>()));
146 if (os_) *os_ << "accept 3:\n";
147 unit_assert(!readers.accept("3", shared_ptr<istream>()));
148
149 if (os_) *os_ << endl;
150}
151
152
154{
155 if (os_) *os_ << "testRead()\n";
156
157 ReaderList readers;
158 readers.push_back(ReaderPtr(new Reader1));
159 readers.push_back(ReaderPtr(new Reader2));
160
161 ProteomeData pd;
162
163 // note: composite pattern with accept/read will cause two calls
164 // to accept(); the alternative is to maintain state between accept()
165 // and read(), which opens possibility for misuse.
166
167 unit_assert(readers.get<Reader1>()->config.done == false);
168 if (readers.accept("1", shared_ptr<istream>()))
169 readers.read("1", shared_ptr<istream>(), pd);
170 unit_assert(readers.get<Reader1>()->config.done == true);
171
172 readers.get<Reader1>()->config.done = false;
173 unit_assert(readers.get<Reader2>()->config.done == false);
174 if (readers.accept("2", shared_ptr<istream>()))
175 readers.read("2", shared_ptr<istream>(), pd);
176 unit_assert(readers.get<Reader1>()->config.done == false);
177 unit_assert(readers.get<Reader2>()->config.done == true);
178
179 if (os_) *os_ << endl;
180}
181
182
183void test()
184{
185 testGet();
186 testAccept();
187 testRead();
188}
189
190
191int main(int argc, char* argv[])
192{
193 TEST_PROLOG_EX(argc, argv, "_ProteomeData")
194
195 try
196 {
197 if (argc==2 && !strcmp(argv[1],"-v")) os_ = &cout;
198 test();
199 }
200 catch (exception& e)
201 {
202 TEST_FAILED(e.what())
203 }
204 catch (...)
205 {
206 TEST_FAILED("Caught unknown exception.")
207 }
208
210}
211
virtual const char * getType() const
virtual std::string identify(const std::string &uri, boost::shared_ptr< std::istream > uriStreamPtr) const
Config config
virtual void read(const std::string &uri, boost::shared_ptr< std::istream > uriStreamPtr, ProteomeData &result) const
virtual std::string identify(const std::string &uri, boost::shared_ptr< std::istream > uriStreamPtr) const
virtual void read(const std::string &uri, boost::shared_ptr< std::istream > uriStreamPtr, ProteomeData &result) const
Config config
const char * getType() const
interface for file readers
Definition Reader.hpp:37
bool accept(const std::string &uri, boost::shared_ptr< std::istream > uriStreamPtr) const
return true iff Reader recognizes the file as one it should handle
Definition Reader.hpp:44
Reader container (composite pattern).
Definition Reader.hpp:100
virtual void read(const std::string &uri, ProteomeData &result) const
delegates to first child that identifies
reader_type * get()
returns pointer to Reader of the specified type
Definition Reader.hpp:134
boost::shared_ptr< Reader > ReaderPtr
Definition Reader.hpp:89
int main(int argc, char *argv[])
void testRead()
ostream * os_
void testAccept()
void test()
void testGet()
ostream * os_
#define unit_assert(x)
Definition unit.hpp:85
#define TEST_PROLOG_EX(argc, argv, suffix)
Definition unit.hpp:157
#define TEST_EPILOG
Definition unit.hpp:183
#define TEST_FAILED(x)
Definition unit.hpp:177