Cambridge SMT System
szfstream.gtest.cpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use these files except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 // Copyright 2012 - Gonzalo Iglesias, AdriĆ  de Gispert, William Byrne
14 
22 #include <googletesting.h>
23 
24 #ifndef GMAINTEST
25 #include "main.custom_assert.hpp"
26 #include "main.logger.hpp"
27 #endif
28 
29 namespace bfs = boost::filesystem;
30 
32 TEST ( iszfstream, basictest ) {
33  std::stringstream ss;
34  ss << "expecto patronum" << endl << "obliviate" << endl << "avada kedavra";
35  uu::iszfstream teststream ( ss );
36  EXPECT_EQ ( teststream.is_open(), true );
37  teststream.close();
38  EXPECT_EQ ( teststream.is_open(), false );
39  teststream.open ( ss );
40  EXPECT_EQ ( teststream.is_open(), true );
41  EXPECT_EQ ( teststream.eof(), false );
42  std::string line;
43  getline ( teststream, line );
44  EXPECT_EQ ( line, "expecto patronum" );
45  teststream >> line;
46  EXPECT_EQ ( line, "obliviate" );
47  getline ( teststream, line );
48  EXPECT_EQ ( line, "" );
49  getline ( teststream, line );
50  EXPECT_EQ ( line, "avada kedavra" );
51  EXPECT_EQ ( teststream.eof(), true );
52 }
53 
54 TEST ( oszfstream, basictest ) {
55  std::stringstream ss;
56  uu::oszfstream teststream ( ss );
57  EXPECT_EQ ( teststream.is_open(), true );
58  teststream.close();
59  EXPECT_EQ ( teststream.is_open(), false );
60  teststream.open ( ss );
61  EXPECT_EQ ( teststream.is_open(), true );
62  std::string line = "obliviate";
63  teststream << line << endl;
64  std::string line2;
65  * static_cast<std::stringstream *> ( teststream.getStream() ) >> line2;
66  EXPECT_EQ ( line, line2 );
67  teststream.close();
68 }
69 
70 TEST (ioszfstream, combinedtest) {
71  uu::oszfstream o ("test.gz");
72  o << "expecto" << endl ;
73  o << "patronum" << endl;
74  o.close();
75  uu::iszfstream i ("test.gz");
76  std::string line;
77  getline (i, line);
78  EXPECT_EQ ("expecto", line);
79  getline (i, line);
80  EXPECT_EQ ("patronum", line);
81  bfs::remove ( bfs::path ( "test.gz") );
82 }
83 
85 TEST ( iszfstream, fastforwardread ) {
86  //Do not delete!
87  std::stringstream *ss = new std::stringstream;
88  *ss << "one" << endl;
89  *ss << "two" << endl;
90  *ss << "three" << endl;
91  *ss << "four" << endl;
92  *ss << "five" ;
93  {
94  uu::FastForwardRead<> ffr ( ss );
95  std::string line;
96  bool finished;
97  finished = ffr ( 2, &line );
98  EXPECT_EQ ( line, "two" );
99  EXPECT_EQ ( finished, false );
100  finished = ffr ( 4, &line );
101  EXPECT_EQ ( line, "four" );
102  EXPECT_EQ ( finished, false );
103  finished = ffr ( 5, &line );
104  EXPECT_EQ ( line, "five" );
105  EXPECT_EQ ( finished, false );
106  finished = ffr ( 6, &line );
107  //No more readings
108  EXPECT_EQ ( line, "" );
109  EXPECT_EQ ( finished, true );
110  // Cannot read backwards
111  user_check_ok = true;
112  finished = ffr ( 1, &line );
113  EXPECT_EQ ( line, "" );
114  EXPECT_EQ ( user_check_ok, false );
115  user_check_ok = true;
116  }
117  ss = new std::stringstream;
118  *ss << "one" << endl;
119  {
120  std::string line;
121  bool finished;
122  uu::FastForwardRead<> ffr ( ss );
123  finished = ffr ( 1, &line );
124  EXPECT_EQ (finished, false);
125  EXPECT_EQ (line, "one");
126  finished = ffr ( 2, &line );
127  EXPECT_EQ (finished, true);
128  EXPECT_EQ (line, "");
129  }
130 };
131 
132 #ifndef GMAINTEST
133 
138 int main ( int argc, char **argv ) {
139  ::testing::InitGoogleTest ( &argc, argv );
140  return RUN_ALL_TESTS();
141 }
142 #endif
Wrapper stream class that writes to pipes, text files or gzipped files.
Definition: szfstream.hpp:200
Convenience class that reads "quickly" until a queried line.
Definition: szfstream.hpp:381
std::ostream * getStream()
Returns internal stream.
Definition: szfstream.hpp:241
void open(const std::stringstream &ss)
Definition: szfstream.hpp:75
int main(int argc, char **argv)
main function. If compiled individualy, will kickoff any tests in this file.
bool user_check_ok
bool is_open()
Checks if the file/pipe is open.
Definition: szfstream.hpp:132
iszfstream & getline(iszfstream &izs, std::string &line)
Definition: szfstream.hpp:178
Static variables for logger. Include only once from main file.
bool is_open()
Checks whether the file is open.
Definition: szfstream.hpp:318
void close()
Closes file.
Definition: szfstream.hpp:147
TEST(iszfstream, basictest)
Test iszfstream.
void open(const std::stringstream &ss)
Definition: szfstream.hpp:245
virtual int eof()
Checks for end-of-file.
Definition: szfstream.hpp:142
Wrapper stream class that reads pipes, text files or gzipped files.
Definition: szfstream.hpp:34
Unit testing: google testing common header.
Static variable for custom_assert. Include only once from main file.
void close()
Closes the file.
Definition: szfstream.hpp:323