FST  openfst-1.7.2
OpenFst Library
fst_test.cc
Go to the documentation of this file.
1 // See www.openfst.org for extensive documentation on this weighted
2 // finite-state transducer library.
3 //
4 // Regression test for FST classes.
5 
6 #include <fst/test/fst_test.h>
7 
8 #include <utility>
9 
10 #include <fst/flags.h>
11 #include <fst/log.h>
12 #include <fst/compact-fst.h>
13 #include <fst/const-fst.h>
14 #include <fst/edit-fst.h>
15 #include <fst/matcher-fst.h>
16 
17 namespace fst {
18 namespace {
19 
20 // A user-defined arc type.
21 struct CustomArc {
22  typedef int16 Label;
23  typedef ProductWeight<TropicalWeight, LogWeight> Weight;
24  typedef int64 StateId;
25 
26  CustomArc(Label i, Label o, Weight w, StateId s)
27  : ilabel(i), olabel(o), weight(std::move(w)), nextstate(s) {}
28  CustomArc() {}
29 
30  static const string &Type() { // Arc type name
31  static const string *const type = new string("my");
32  return *type;
33  }
34 
35  Label ilabel; // Transition input label
36  Label olabel; // Transition output label
37  Weight weight; // Transition weight
38  StateId nextstate; // Transition destination state
39 };
40 
41 // A user-defined compactor for test FST.
42 template <class A>
43 class CustomCompactor {
44  public:
45  typedef A Arc;
46  typedef typename A::Label Label;
47  typedef typename A::StateId StateId;
48  typedef typename A::Weight Weight;
49  typedef std::pair<Label, Weight> Element;
50 
51  Element Compact(StateId s, const A &arc) const {
52  return std::make_pair(arc.ilabel, arc.weight);
53  }
54 
55  Arc Expand(StateId s, const Element &p, uint32 f = kArcValueFlags) const {
56  return p.first == kNoLabel ? Arc(kNoLabel, kNoLabel, p.second, kNoStateId)
57  : Arc(p.first, 0, p.second, s);
58  }
59 
60  ssize_t Size() const { return -1; }
61 
62  uint64 Properties() const { return 0ULL; }
63 
64  bool Compatible(const Fst<A> &fst) const { return true; }
65 
66  static const string &Type() {
67  static const string *const type = new string("my");
68  return *type;
69  }
70 
71  bool Write(std::ostream &strm) const { return true; }
72 
73  static CustomCompactor *Read(std::istream &strm) {
74  return new CustomCompactor;
75  }
76 };
77 
78 REGISTER_FST(VectorFst, CustomArc);
79 REGISTER_FST(ConstFst, CustomArc);
81  CompactFst_StdArc_CustomCompactor_registerer;
83  CompactFst_CustomArc_CustomCompactor_registerer;
85  ConstFst_StdArc_uint16_registerer;
86 static fst::FstRegisterer<
88  CompactFst_StdArc_CustomCompactor_uint16_registerer;
89 
90 } // namespace
91 } // namespace fst
92 
93 using fst::FstTester;
94 using fst::VectorFst;
95 using fst::ConstFst;
96 using fst::MatcherFst;
97 using fst::CompactFst;
98 using fst::Fst;
99 using fst::StdArc;
100 using fst::CustomArc;
101 using fst::CustomCompactor;
103 using fst::EditFst;
104 
105 int main(int argc, char **argv) {
106  FLAGS_fst_verify_properties = true;
107  std::set_new_handler(FailedNewHandler);
108  SET_FLAGS(argv[0], &argc, &argv, true);
109 
110  // VectorFst<StdArc> tests
111  {
112  FstTester<VectorFst<StdArc>> std_vector_tester;
113  std_vector_tester.TestBase();
114  std_vector_tester.TestExpanded();
115  std_vector_tester.TestAssign();
116  std_vector_tester.TestCopy();
117  std_vector_tester.TestIO();
118  std_vector_tester.TestMutable();
119  }
120 
121  // ConstFst<StdArc> tests
122  {
123  FstTester<ConstFst<StdArc>> std_const_tester;
124  std_const_tester.TestBase();
125  std_const_tester.TestExpanded();
126  std_const_tester.TestCopy();
127  std_const_tester.TestIO();
128  }
129 
130  // CompactFst<StdArc, CustomCompactor<StdArc>>
131  {
133  std_compact_tester.TestBase();
134  std_compact_tester.TestExpanded();
135  std_compact_tester.TestCopy();
136  std_compact_tester.TestIO();
137  }
138 
139  // VectorFst<CustomArc> tests
140  {
141  FstTester<VectorFst<CustomArc>> std_vector_tester;
142  std_vector_tester.TestBase();
143  std_vector_tester.TestExpanded();
144  std_vector_tester.TestAssign();
145  std_vector_tester.TestCopy();
146  std_vector_tester.TestIO();
147  std_vector_tester.TestMutable();
148  }
149 
150  // ConstFst<CustomArc> tests
151  {
152  FstTester<ConstFst<CustomArc>> std_const_tester;
153  std_const_tester.TestBase();
154  std_const_tester.TestExpanded();
155  std_const_tester.TestCopy();
156  std_const_tester.TestIO();
157  }
158 
159  // CompactFst<CustomArc, CustomCompactor<CustomArc>>
160  {
162  std_compact_tester;
163  std_compact_tester.TestBase();
164  std_compact_tester.TestExpanded();
165  std_compact_tester.TestCopy();
166  std_compact_tester.TestIO();
167  }
168 
169  // ConstFst<StdArc, uint16> tests
170  {
171  FstTester<ConstFst<StdArc, uint16>> std_const_tester;
172  std_const_tester.TestBase();
173  std_const_tester.TestExpanded();
174  std_const_tester.TestCopy();
175  std_const_tester.TestIO();
176  }
177 
178  // CompactFst<StdArc, CustomCompactor<StdArc>, uint16>
179  {
181  std_compact_tester;
182  std_compact_tester.TestBase();
183  std_compact_tester.TestExpanded();
184  std_compact_tester.TestCopy();
185  std_compact_tester.TestIO();
186  }
187 
188  // FstTester<StdArcLookAheadFst>
189  {
190  FstTester<StdArcLookAheadFst> std_matcher_tester;
191  std_matcher_tester.TestBase();
192  std_matcher_tester.TestExpanded();
193  std_matcher_tester.TestCopy();
194  }
195 
196  // EditFst<StdArc> tests
197  {
198  FstTester<EditFst<StdArc>> std_edit_tester;
199  std_edit_tester.TestBase();
200  std_edit_tester.TestExpanded();
201  std_edit_tester.TestAssign();
202  std_edit_tester.TestCopy();
203  std_edit_tester.TestMutable();
204  }
205 
206  std::cout << "PASS" << std::endl;
207 
208  return 0;
209 }
void TestExpanded(const G &fst) const
Definition: fst_test.h:95
int16_t int16
Definition: types.h:25
void TestAssign(G *fst) const
Definition: fst_test.h:150
constexpr int kNoLabel
Definition: fst.h:179
uint64_t uint64
Definition: types.h:32
uint16_t uint16
Definition: types.h:30
void TestIO(const G &fst) const
Definition: fst_test.h:189
ArcTpl< TropicalWeight > StdArc
Definition: arc.h:55
void FailedNewHandler()
Definition: compat.cc:25
constexpr int kNoStateId
Definition: fst.h:180
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:214
int64_t int64
Definition: types.h:27
void TestCopy(const G &fst) const
Definition: fst_test.h:170
int main(int argc, char **argv)
Definition: fst_test.cc:105
uint32_t uint32
Definition: types.h:31
void TestMutable(G *fst) const
Definition: fst_test.h:108
REGISTER_FST(VectorFst, StdArc)
void Expand(const Fst< Arc > &ifst, const std::vector< std::pair< typename Arc::Label, typename Arc::Label >> &parens, const std::vector< typename Arc::Label > &assignments, MutableFst< Arc > *ofst, const MPdtExpandOptions &opts)
Definition: expand.h:302
void TestBase(const G &fst) const
Definition: fst_test.h:47
MatcherFst< ConstFst< StdArc >, ArcLookAheadMatcher< SortedMatcher< ConstFst< StdArc >>>, arc_lookahead_fst_type > StdArcLookAheadFst
Definition: matcher-fst.h:320