ProteoWizard
TraDataFileTest.cpp
Go to the documentation of this file.
1 //
2 // $Id: TraDataFileTest.cpp 4129 2012-11-20 00:05:37Z chambm $
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 "TraDataFile.hpp"
24 #include "Diff.hpp"
25 #include "IO.hpp"
26 #include "examples.hpp"
30 #include <boost/iostreams/filtering_stream.hpp>
31 #include <boost/iostreams/filter/gzip.hpp>
32 #include <boost/iostreams/device/file_descriptor.hpp>
33 #include <boost/iostreams/copy.hpp>
34 
35 
36 using namespace pwiz::util;
37 using namespace pwiz::cv;
38 using namespace pwiz::data;
39 using namespace pwiz::tradata;
40 
41 
42 ostream* os_ = 0;
43 
44 
45 string filenameBase_ = "temp.TraDataFileTest";
46 
48 {
49  // remove metadata ptrs appended on read
50  //vector<SourceFilePtr>& sfs = msd.fileDescription.sourceFilePtrs;
51  //if (!sfs.empty()) sfs.erase(sfs.end()-1);
52  vector<SoftwarePtr>& sws = td.softwarePtrs;
53  if (!sws.empty()) sws.erase(sws.end()-1);
54 }
55 
56 void test()
57 {
58  TraDataFile::WriteConfig writeConfig;
59 
60  if (os_) *os_ << "test()\n " << writeConfig << endl;
61 
62  string filename1 = filenameBase_ + ".1";
63  string filename2 = filenameBase_ + ".2";
64 
65  {
66  // create TraData object in memory
67  TraData tiny;
69 
70  // write to file #1 (static)
71  TraDataFile::write(tiny, filename1, writeConfig);
72 
73  // read back into an TraDataFile object
74  TraDataFile td1(filename1);
76 
77  // compare
79  if (diff && os_) *os_ << diff << endl;
80  unit_assert(!diff);
81 
82  // write to file #2 (member)
83  td1.write(filename2, writeConfig);
84 
85  // read back into another TraDataFile object
86  TraDataFile td2(filename2);
88 
89  // compare
90  diff(tiny, td2);
91  if (diff && os_) *os_ << diff << endl;
92  unit_assert(!diff);
93 
94  // now give the gzip read a workout
95  bio::filtering_istream tinyGZ(bio::gzip_compressor() | bio::file_descriptor_source(filename1));
96  bio::copy(tinyGZ, bio::file_descriptor_sink(filename1+".gz", ios::out|ios::binary));
97 
98  TraDataFile td3(filename1);
100 
101  // compare
102  diff(tiny, td3);
103  if (diff && os_) *os_ << diff << endl;
104  unit_assert(!diff);
105  }
106 
107  // remove temp files
108  boost::filesystem::remove(filename1);
109  boost::filesystem::remove(filename2);
110  boost::filesystem::remove(filename1 + ".gz");
111 }
112 
113 
114 int main(int argc, char* argv[])
115 {
116  TEST_PROLOG(argc, argv)
117 
118  try
119  {
120  if (argc>1 && !strcmp(argv[1],"-v")) os_ = &cout;
121  test();
122  }
123  catch (exception& e)
124  {
125  TEST_FAILED(e.what())
126  }
127  catch (...)
128  {
129  TEST_FAILED("Caught unknown exception.")
130  }
131 
133 }