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