Cambridge SMT System
fstio.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 
21 #include <openfst.h>
22 #include <googletesting.h>
23 
24 #ifndef GMAINTEST
25 #include "main.custom_assert.hpp"
26 #include "main.logger.hpp"
27 #endif
28 
29 #include "szfstream.hpp"
30 #include "fstio.hpp"
31 
32 namespace bfs = boost::filesystem;
33 
34 namespace googletesting {
35 
38 TEST ( FstIo, basic_test ) {
39  fst::VectorFst<fst::StdArc> aux;
40  aux.AddState();
41  aux.AddState();
42  aux.SetStart ( 0 );
43  aux.SetFinal ( 1, fst::StdArc::Weight::One() );
44  aux.AddArc ( 0, fst::StdArc ( 10, 10, 0.5, 1 ) );
45  FstWrite ( aux, "obliviate.fst.gz" );
46  boost::scoped_ptr< fst::VectorFst<fst::StdArc> > aux2
47  (fst::VectorFstRead<fst::StdArc> ( "obliviate.fst.gz" ) );
48  EXPECT_TRUE ( Equivalent ( aux, *aux2 ) );
49  bfs::remove ( bfs::path ( "obliviate.fst.gz" ) );
50  fst::ConstFst<fst::StdArc> caux ( aux );
51  FstWrite ( caux, "const.obliviate.fst.gz" );
52  FstWrite ( caux, "const.obliviate.txt" );
53  // // \todo This test is no longer working since openfst 1.3.4 but works in openfst.1.3.2. Needs investigation.
54  // FstWrite ( caux, "const.obliviate.fst" );
55  // ucam::util::iszfstream file ( "const.obliviate.fst.gz" );
56  // fst::FstReadOptions fro;
57  // fst::ConstFst<fst::StdArc> *h = fst::ConstFst<fst::StdArc>::Read ( *file.getStream(), fro );
58  // if (h == NULL) {
59  // LERROR("Ufa?");
60  // exit(0);
61  // }
62  // boost::scoped_ptr< fst::ConstFst<fst::StdArc> > caux2( fst::ConstFstRead<fst::StdArc> ( "const.obliviate.fst.gz" ) ) ;
63  //EXPECT_TRUE ( Equivalent ( caux, *caux2 ) );
64 
65  bfs::remove ( bfs::path ( "const.obliviate.fst.gz" ) );
66 
67  ucam::util::iszfstream isz ( "const.obliviate.txt" );
68  std::string t, t2;
69  while ( getline ( isz, t ) ) t2 += t + "\n";
70  LDEBUG("t2=" << t2);
71  EXPECT_TRUE ( t2 == "0\t1\t10\t10\t0.5\n1\n"
72  || t2 == "0\t1\t10\t0.5\n1\t0\n" ); //openfst 1.5.0
73  bfs::remove ( bfs::path ( "const.obliviate.txt" ) );
74 }
75 
76 };
77 
78 #ifndef GMAINTEST
79 
80 int main ( int argc, char **argv ) {
81  ::testing::InitGoogleTest ( &argc, argv );
82  return RUN_ALL_TESTS();
83 }
84 #endif
Unit testing: google testing common header.
Contains convenience functions to write and read fsts.
Stream wrapper for pipe/text/compressed files.
#define LDEBUG(msg)
TEST(FstIo, basic_test)
Definition: fstio.gtest.cpp:38
iszfstream & getline(iszfstream &izs, std::string &line)
Definition: szfstream.hpp:178
int main(int argc, char **argv)
Definition: fstio.gtest.cpp:80
test-specific classes and functions
Definition: fstio.gtest.cpp:34
Static variables for logger. Include only once from main file.
void FstWrite(const Fst< Arc > &fst, const std::string &filename, const std::string &txtname="txt")
Templated method that writes an fst either in binary or text format.
Definition: fstio.hpp:111
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.