ProteoWizard
MatrixIOTest.cpp
Go to the documentation of this file.
1 //
2 // $Id$
3 //
4 //
5 // Original author: Austin Keller <atkeller .@. uw.edu>
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 
23 #include <boost/filesystem/operations.hpp>
24 #include <boost/make_shared.hpp>
25 
26 using namespace pwiz::util;
27 using namespace pwiz::analysis;
28 
29 class MatrixIOTest {
30 public:
31  void Run()
32  {
33  SetUp();
34  SingleReadWrite();
35  MultiReadWrite();
36  TearDown();
37  }
38 
39 protected:
40 
41  virtual void SetUp()
42  {
43  }
44 
45  void TearDown()
46  {
47  remove("TestMatrixIOTemp.log");
48  }
49 
51  {
52  std::string testFilename = "TestMatrixIOTemp.log";
53  DemuxTypes::MatrixPtr matrix = boost::make_shared<DemuxTypes::MatrixType>(3, 3);
54  *matrix << 1, 2, 3,
55  4, 5, 6,
56  7, 8, 9;
57  MatrixIO::WriteBinary(testFilename, matrix);
58 
59  DemuxTypes::MatrixPtr matrixIn = boost::make_shared<DemuxTypes::MatrixType>(1, 1);
60  MatrixIO::ReadBinary(testFilename, matrixIn);
61 
62  unit_assert(matrixIn->isApprox(*matrix));
63  }
64 
66  {
67  std::string testFilename = "TestMatrixIOTemp.log";
68 
69  boost::filesystem::path full_path(boost::filesystem::current_path());
70 
71  DemuxTypes::MatrixPtr A = boost::make_shared<DemuxTypes::MatrixType>(3, 4);
72  DemuxTypes::MatrixPtr B = boost::make_shared<DemuxTypes::MatrixType>(4, 3);
73  DemuxTypes::MatrixPtr C = boost::make_shared<DemuxTypes::MatrixType>(3, 3);
74  *A << -14.834628974133, -15.729764770592, 56.292839002858, 30.766363712773,
75  79.595747995303, -8.356622426449, 20.840197237638, 83.801095382748,
76  87.889866880787, 13.75327399942, 86.730656404499, -0.46420627108677;
77 
78  *B << 23.588885367543, 49.667231605868, -86.700220187964,
79  51.392601274063, -77.511392742378, 23.389497301117,
80  -78.475202879706, -62.60684915327, -42.39206607192,
81  59.595164405161, 2.1025961854091, 65.787705013259;
82 
83  *C = *A * *B;
84 
85  std::ofstream out;
86  MatrixIO::GetWriteStream(out, testFilename);
87  unit_assert(out.is_open());
88  MatrixIO::WriteBinary(out, A);
89  MatrixIO::WriteBinary(out, B);
90  MatrixIO::WriteBinary(out, C);
91  out.flush();
92  out.close();
93 
94  DemuxTypes::MatrixPtr AIn = boost::make_shared<DemuxTypes::MatrixType>(1, 1);
95  DemuxTypes::MatrixPtr BIn = boost::make_shared<DemuxTypes::MatrixType>(1, 1);
96  DemuxTypes::MatrixPtr CIn = boost::make_shared<DemuxTypes::MatrixType>(1, 1);
97 
98  std::ifstream in;
99  MatrixIO::GetReadStream(in, testFilename);
100  unit_assert(in.is_open());
101  MatrixIO::ReadBinary(in, AIn);
102  MatrixIO::ReadBinary(in, BIn);
103  MatrixIO::ReadBinary(in, CIn);
104  in.close();
105 
106  unit_assert(AIn->isApprox(*A));
107  unit_assert(BIn->isApprox(*B));
108  unit_assert(CIn->isApprox(*C));
109  }
110 };
111 
112 int main(int argc, char* argv[])
113 {
114  TEST_PROLOG(argc, argv)
115 
116  try
117  {
118  MatrixIOTest tester;
119  tester.Run();
120  }
121  catch (exception& e)
122  {
123  TEST_FAILED(e.what())
124  }
125  catch (...)
126  {
127  TEST_FAILED("Caught unknown exception.")
128  }
129 
131 }
DemuxTypes::MatrixPtr
boost::shared_ptr< MatrixType > MatrixPtr
Definition: DemuxTypes.hpp:39
MatrixIOTest::SetUp
virtual void SetUp()
Definition: MatrixIOTest.cpp:41
MatrixIOTest::MultiReadWrite
void MultiReadWrite()
Definition: MatrixIOTest.cpp:65
pwiz::analysis
Definition: ChromatogramList_Filter.hpp:37
MatrixIOTest::Run
void Run()
Definition: MatrixIOTest.cpp:31
testFilename
void testFilename()
Definition: PeptideID_pepXMLTest.cpp:69
pwiz::util
Definition: almost_equal.hpp:33
TEST_EPILOG
#define TEST_EPILOG
Definition: unit.hpp:183
MatrixIOTest::SingleReadWrite
void SingleReadWrite()
Definition: MatrixIOTest.cpp:50
Std.hpp
main
int main(int argc, char *argv[])
Definition: MatrixIOTest.cpp:112
B
B
Definition: Chemistry.hpp:81
C
C
Definition: Chemistry.hpp:80
TEST_FAILED
#define TEST_FAILED(x)
Definition: unit.hpp:177
TEST_PROLOG
#define TEST_PROLOG(argc, argv)
Definition: unit.hpp:175
MatrixIOTest::TearDown
void TearDown()
Definition: MatrixIOTest.cpp:45
unit.hpp
unit_assert
#define unit_assert(x)
Definition: unit.hpp:85
A
#define A
Definition: FilesystemTest.cpp:50
MatrixIO.hpp
MatrixIOTest
Definition: MatrixIOTest.cpp:29