FST  openfst-1.8.3
OpenFst Library
fst_test.cc
Go to the documentation of this file.
1 // Copyright 2005-2024 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the 'License');
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an 'AS IS' BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // See www.openfst.org for extensive documentation on this weighted
16 // finite-state transducer library.
17 //
18 // Regression test for FST classes.
19 
20 #include <fst/fst.h>
21 
22 #include <cstddef>
23 #include <cstdint>
24 #include <iostream>
25 #include <ostream>
26 #include <string>
27 
28 #include <fst/log.h>
29 #include <fst/arc.h>
30 #include <fst/cache.h>
31 #include <fst/compact-fst.h>
32 #include <fst/const-fst.h>
33 #include <fst/edit-fst.h>
34 #include <fst/float-weight.h>
35 #include <fst/fst-decl.h>
36 #include <fst/matcher-fst.h>
37 #include <fst/pair-weight.h>
38 #include <fst/product-weight.h>
39 #include <fst/register.h>
40 #include <fst/vector-fst.h>
41 #include <fst/test/compactors.h>
42 #include <fst/test/fst_test.h>
43 
44 namespace fst {
45 namespace {
46 
47 // A user-defined arc type.
48 struct CustomArc {
49  using Label = int16_t;
50  using Weight = ProductWeight<TropicalWeight, LogWeight>;
51  using StateId = int64_t;
52 
53  CustomArc(Label i, Label o, Weight w, StateId s)
54  : ilabel(i), olabel(o), weight(w), nextstate(s) {}
55  CustomArc() = default;
56 
57  static const std::string &Type() { // Arc type name
58  static const std::string *const type = new std::string("my");
59  return *type;
60  }
61 
62  Label ilabel; // Transition input label
63  Label olabel; // Transition output label
64  Weight weight; // Transition weight
65  StateId nextstate; // Transition destination state
66 };
67 
68 REGISTER_FST(VectorFst, CustomArc);
69 REGISTER_FST(ConstFst, CustomArc);
70 static fst::FstRegisterer<
71  CompactArcFst<StdArc, TrivialArcCompactor<StdArc>>>
72  CompactFst_StdArc_TrivialCompactor_registerer;
73 static fst::FstRegisterer<
74  CompactArcFst<CustomArc, TrivialArcCompactor<CustomArc>>>
75  CompactFst_CustomArc_TrivialCompactor_registerer;
77  ConstFst_StdArc_uint16_registerer;
78 static fst::FstRegisterer<
79  CompactArcFst<StdArc, TrivialArcCompactor<StdArc>, uint16_t>>
80  CompactFst_StdArc_TrivialCompactor_uint16_registerer;
82  CompactFst_StdArc_CustomCompactor_registerer;
83 static fst::FstRegisterer<
85  CompactFst_CustomArc_CustomCompactor_registerer;
86 
87 } // namespace
88 } // namespace fst
89 
90 using fst::CompactArcFst;
91 using fst::CompactFst;
92 using fst::ConstFst;
93 using fst::CustomArc;
94 using fst::EditFst;
95 using fst::FstTester;
96 using fst::StdArc;
100 using fst::VectorFst;
101 
102 int main(int argc, char **argv) {
103  SetFlag(&FST_FLAGS_fst_verify_properties, true);
104  SET_FLAGS(argv[0], &argc, &argv, true);
105 
106  LOG(INFO) << "Testing VectorFst<StdArc>.";
107  {
108  for (const size_t num_states : {0, 1, 2, 3, 128}) {
109  FstTester<VectorFst<StdArc>> std_vector_tester(num_states);
110  std_vector_tester.TestBase();
111  std_vector_tester.TestExpanded();
112  std_vector_tester.TestAssign();
113  std_vector_tester.TestCopy();
114  std_vector_tester.TestIO();
115  std_vector_tester.TestMutable();
116  }
117 
118  LOG(INFO) << "Testing empty StdVectorFst.";
119  FstTester<VectorFst<StdArc>> empty_tester(/*num_states=*/0);
120  {
121  const VectorFst<StdArc> empty_fst;
122  empty_tester.TestBase(empty_fst);
123  empty_tester.TestExpanded(empty_fst);
124  empty_tester.TestCopy(empty_fst);
125  empty_tester.TestIO(empty_fst);
126  empty_tester.TestAssign(empty_fst);
127  }
128  {
129  VectorFst<StdArc> empty_fst;
130  empty_tester.TestMutable(&empty_fst);
131  }
132  }
133 
134  LOG(INFO) << "Testing ConstFst<StdArc>.";
135  {
136  FstTester<ConstFst<StdArc>> std_const_tester;
137  std_const_tester.TestBase();
138  std_const_tester.TestExpanded();
139  std_const_tester.TestCopy();
140  std_const_tester.TestIO();
141  }
142 
143  LOG(INFO) << "Testing CompactArcFst<StdArc, TrivialArcCompactor<StdArc>>.";
144  {
146  std_compact_tester;
147  std_compact_tester.TestBase();
148  std_compact_tester.TestExpanded();
149  std_compact_tester.TestCopy();
150  std_compact_tester.TestIO();
151  }
152 
153  LOG(INFO) << "Testing CompactFst<StdArc, TrivialArcCompactor<StdArc>>.";
154  {
155  for (const size_t num_states : {0, 1, 2, 3, 128}) {
157  std_compact_tester(num_states);
158  std_compact_tester.TestBase();
159  std_compact_tester.TestExpanded();
160  std_compact_tester.TestCopy();
161  std_compact_tester.TestIO();
162  }
163 
164  LOG(INFO) << "Testing empty CompactFst.";
166  /*num_states=*/0);
167  {
169  empty_tester.TestBase(empty_fst);
170  empty_tester.TestExpanded(empty_fst);
171  empty_tester.TestCopy(empty_fst);
172  empty_tester.TestIO(empty_fst);
173  }
174  }
175 
176  // VectorFst<CustomArc> tests
177  {
178  FstTester<VectorFst<CustomArc>> std_vector_tester;
179  std_vector_tester.TestBase();
180  std_vector_tester.TestExpanded();
181  std_vector_tester.TestAssign();
182  std_vector_tester.TestCopy();
183  std_vector_tester.TestIO();
184  std_vector_tester.TestMutable();
185  }
186 
187  // ConstFst<CustomArc> tests
188  {
189  FstTester<ConstFst<CustomArc>> std_const_tester;
190  std_const_tester.TestBase();
191  std_const_tester.TestExpanded();
192  std_const_tester.TestCopy();
193  std_const_tester.TestIO();
194  }
195 
196  // CompactArcFst<CustomArc, TrivialArcCompactor<CustomArc>>
197  {
198  for (const size_t num_states : {0, 1, 2, 3, 128}) {
200  std_compact_tester(num_states);
201  std_compact_tester.TestBase();
202  std_compact_tester.TestExpanded();
203  std_compact_tester.TestCopy();
204  std_compact_tester.TestIO();
205  }
206 
207  // TODO(jrosenstock): Make this work.
208 #if 0
209  // Test with a default-constructed Fst, not a copied Fst.
211  empty_tester(/*num_states=*/0);
212  const CompactArcFst<CustomArc, CustomCompactor<CustomArc>> empty_fst;
213  empty_tester.TestBase(empty_fst);
214  empty_tester.TestExpanded(empty_fst);
215  empty_tester.TestCopy(empty_fst);
216  empty_tester.TestIO(empty_fst);
217 #endif
218  }
219 
220  // CompactFst<CustomArc, TrivialArcCompactor<CustomArc>>
221  {
222  for (const size_t num_states : {0, 1, 2, 3, 128}) {
224  std_compact_tester(num_states);
225  std_compact_tester.TestBase();
226  std_compact_tester.TestExpanded();
227  std_compact_tester.TestCopy();
228  std_compact_tester.TestIO();
229  }
230 
231  // TODO(jrosenstock): Add tests on default-constructed Fst.
232  }
233 
234  // ConstFst<StdArc, uint16_t> tests
235  {
236  FstTester<ConstFst<StdArc, uint16_t>> std_const_tester;
237  std_const_tester.TestBase();
238  std_const_tester.TestExpanded();
239  std_const_tester.TestCopy();
240  std_const_tester.TestIO();
241  }
242 
243  // CompactArcFst<StdArc, TrivialArcCompactor<StdArc>, uint16_t>
244  {
246  std_compact_tester;
247  std_compact_tester.TestBase();
248  std_compact_tester.TestExpanded();
249  std_compact_tester.TestCopy();
250  std_compact_tester.TestIO();
251  }
252 
253  // FstTester<StdArcLookAheadFst>
254  {
255  FstTester<StdArcLookAheadFst> std_matcher_tester;
256  std_matcher_tester.TestBase();
257  std_matcher_tester.TestExpanded();
258  std_matcher_tester.TestCopy();
259  }
260 
261  // EditFst<StdArc> tests
262  {
263  FstTester<EditFst<StdArc>> std_edit_tester;
264  std_edit_tester.TestBase();
265  std_edit_tester.TestExpanded();
266  std_edit_tester.TestAssign();
267  std_edit_tester.TestCopy();
268  std_edit_tester.TestMutable();
269  }
270 
271  std::cout << "PASS" << std::endl;
272 
273  return 0;
274 }
void TestExpanded(const G &fst) const
Definition: fst_test.h:120
void TestIO(const G &fst) const
Definition: fst_test.h:214
#define LOG(type)
Definition: log.h:53
ArcTpl< TropicalWeight > StdArc
Definition: arc.h:75
void TestAssign(const G &fst) const
Definition: fst_test.h:176
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:226
void TestCopy(const G &fst) const
Definition: fst_test.h:196
CompactFst< Arc, CompactArcCompactor< ArcCompactor, Unsigned, CompactStore >, CacheStore > CompactArcFst
Definition: fst-decl.h:97
int main(int argc, char **argv)
Definition: fst_test.cc:102
void TestMutable(G *fst) const
Definition: fst_test.h:134
void SetFlag(Type *flag, Value value)
Definition: flags.h:220
REGISTER_FST(PhiFst, StdArc)
void TestBase(const G &fst) const
Definition: fst_test.h:65
MatcherFst< ConstFst< StdArc >, ArcLookAheadMatcher< SortedMatcher< ConstFst< StdArc >>>, arc_lookahead_fst_type > StdArcLookAheadFst
Definition: matcher-fst.h:362