FST  openfst-1.8.4
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/test/fst_test.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/compact-fst.h>
31 #include <fst/const-fst.h>
32 #include <fst/edit-fst.h>
33 #include <fst/float-weight.h>
34 #include <fst/fst-decl.h>
35 #include <fst/matcher-fst.h>
36 #include <fst/product-weight.h>
37 #include <fst/register.h>
38 #include <fst/test-properties.h>
39 #include <fst/vector-fst.h>
40 #include <fst/test/compactors.h>
41 
42 namespace fst {
43 namespace {
44 
45 // A user-defined arc type.
46 struct CustomArc {
47  using Label = int16_t;
48  using Weight = ProductWeight<TropicalWeight, LogWeight>;
49  using StateId = int64_t;
50 
51  CustomArc(Label i, Label o, Weight w, StateId s)
52  : ilabel(i), olabel(o), weight(w), nextstate(s) {}
53  CustomArc() = default;
54 
55  static const std::string &Type() { // Arc type name
56  static const std::string *const type = new std::string("my");
57  return *type;
58  }
59 
60  Label ilabel; // Transition input label
61  Label olabel; // Transition output label
62  Weight weight; // Transition weight
63  StateId nextstate; // Transition destination state
64 };
65 
66 REGISTER_FST(VectorFst, CustomArc);
67 REGISTER_FST(ConstFst, CustomArc);
68 static fst::FstRegisterer<
69  CompactArcFst<StdArc, TrivialArcCompactor<StdArc>>>
70  CompactFst_StdArc_TrivialCompactor_registerer;
71 static fst::FstRegisterer<
72  CompactArcFst<CustomArc, TrivialArcCompactor<CustomArc>>>
73  CompactFst_CustomArc_TrivialCompactor_registerer;
75  ConstFst_StdArc_uint16_registerer;
76 static fst::FstRegisterer<
77  CompactArcFst<StdArc, TrivialArcCompactor<StdArc>, uint16_t>>
78  CompactFst_StdArc_TrivialCompactor_uint16_registerer;
80  CompactFst_StdArc_CustomCompactor_registerer;
81 static fst::FstRegisterer<
83  CompactFst_CustomArc_CustomCompactor_registerer;
84 
85 } // namespace
86 } // namespace fst
87 
88 using fst::CompactArcFst;
89 using fst::CompactFst;
90 using fst::ConstFst;
91 using fst::CustomArc;
92 using fst::EditFst;
93 using fst::FstTester;
94 using fst::StdArc;
98 using fst::VectorFst;
99 
100 int main(int argc, char **argv) {
101  SetFlag(&FST_FLAGS_fst_verify_properties, true);
102  SET_FLAGS(argv[0], &argc, &argv, true);
103 
104  LOG(INFO) << "Testing VectorFst<StdArc>.";
105  {
106  for (const size_t num_states : {0, 1, 2, 3, 128}) {
107  FstTester<VectorFst<StdArc>> std_vector_tester(num_states);
108  std_vector_tester.TestBase();
109  std_vector_tester.TestExpanded();
110  std_vector_tester.TestAssign();
111  std_vector_tester.TestCopy();
112  std_vector_tester.TestIO();
113  std_vector_tester.TestMutable();
114  }
115 
116  LOG(INFO) << "Testing empty StdVectorFst.";
117  FstTester<VectorFst<StdArc>> empty_tester(/*num_states=*/0);
118  {
119  const VectorFst<StdArc> empty_fst;
120  empty_tester.TestBase(empty_fst);
121  empty_tester.TestExpanded(empty_fst);
122  empty_tester.TestCopy(empty_fst);
123  empty_tester.TestIO(empty_fst);
124  empty_tester.TestAssign(empty_fst);
125  }
126  {
127  VectorFst<StdArc> empty_fst;
128  empty_tester.TestMutable(&empty_fst);
129  }
130  }
131 
132  LOG(INFO) << "Testing ConstFst<StdArc>.";
133  {
134  FstTester<ConstFst<StdArc>> std_const_tester;
135  std_const_tester.TestBase();
136  std_const_tester.TestExpanded();
137  std_const_tester.TestCopy();
138  std_const_tester.TestIO();
139  }
140 
141  LOG(INFO) << "Testing CompactArcFst<StdArc, TrivialArcCompactor<StdArc>>.";
142  {
144  std_compact_tester;
145  std_compact_tester.TestBase();
146  std_compact_tester.TestExpanded();
147  std_compact_tester.TestCopy();
148  std_compact_tester.TestIO();
149  }
150 
151  LOG(INFO) << "Testing CompactFst<StdArc, TrivialArcCompactor<StdArc>>.";
152  {
153  for (const size_t num_states : {0, 1, 2, 3, 128}) {
155  std_compact_tester(num_states);
156  std_compact_tester.TestBase();
157  std_compact_tester.TestExpanded();
158  std_compact_tester.TestCopy();
159  std_compact_tester.TestIO();
160  }
161 
162  LOG(INFO) << "Testing empty CompactFst.";
164  /*num_states=*/0);
165  {
167  empty_tester.TestBase(empty_fst);
168  empty_tester.TestExpanded(empty_fst);
169  empty_tester.TestCopy(empty_fst);
170  empty_tester.TestIO(empty_fst);
171  }
172  }
173 
174  // VectorFst<CustomArc> tests
175  {
176  FstTester<VectorFst<CustomArc>> std_vector_tester;
177  std_vector_tester.TestBase();
178  std_vector_tester.TestExpanded();
179  std_vector_tester.TestAssign();
180  std_vector_tester.TestCopy();
181  std_vector_tester.TestIO();
182  std_vector_tester.TestMutable();
183  }
184 
185  // ConstFst<CustomArc> tests
186  {
187  FstTester<ConstFst<CustomArc>> std_const_tester;
188  std_const_tester.TestBase();
189  std_const_tester.TestExpanded();
190  std_const_tester.TestCopy();
191  std_const_tester.TestIO();
192  }
193 
194  // CompactArcFst<CustomArc, TrivialArcCompactor<CustomArc>>
195  {
196  for (const size_t num_states : {0, 1, 2, 3, 128}) {
198  std_compact_tester(num_states);
199  std_compact_tester.TestBase();
200  std_compact_tester.TestExpanded();
201  std_compact_tester.TestCopy();
202  std_compact_tester.TestIO();
203  }
204 
205  // TODO(jrosenstock): Make this work.
206 #if 0
207  // Test with a default-constructed Fst, not a copied Fst.
209  empty_tester(/*num_states=*/0);
210  const CompactArcFst<CustomArc, CustomCompactor<CustomArc>> empty_fst;
211  empty_tester.TestBase(empty_fst);
212  empty_tester.TestExpanded(empty_fst);
213  empty_tester.TestCopy(empty_fst);
214  empty_tester.TestIO(empty_fst);
215 #endif
216  }
217 
218  // CompactFst<CustomArc, TrivialArcCompactor<CustomArc>>
219  {
220  for (const size_t num_states : {0, 1, 2, 3, 128}) {
222  std_compact_tester(num_states);
223  std_compact_tester.TestBase();
224  std_compact_tester.TestExpanded();
225  std_compact_tester.TestCopy();
226  std_compact_tester.TestIO();
227  }
228 
229  // TODO(jrosenstock): Add tests on default-constructed Fst.
230  }
231 
232  // ConstFst<StdArc, uint16_t> tests
233  {
234  FstTester<ConstFst<StdArc, uint16_t>> std_const_tester;
235  std_const_tester.TestBase();
236  std_const_tester.TestExpanded();
237  std_const_tester.TestCopy();
238  std_const_tester.TestIO();
239  }
240 
241  // CompactArcFst<StdArc, TrivialArcCompactor<StdArc>, uint16_t>
242  {
244  std_compact_tester;
245  std_compact_tester.TestBase();
246  std_compact_tester.TestExpanded();
247  std_compact_tester.TestCopy();
248  std_compact_tester.TestIO();
249  }
250 
251  // FstTester<StdArcLookAheadFst>
252  {
253  FstTester<StdArcLookAheadFst> std_matcher_tester;
254  std_matcher_tester.TestBase();
255  std_matcher_tester.TestExpanded();
256  std_matcher_tester.TestCopy();
257  }
258 
259  // EditFst<StdArc> tests
260  {
261  FstTester<EditFst<StdArc>> std_edit_tester;
262  std_edit_tester.TestBase();
263  std_edit_tester.TestExpanded();
264  std_edit_tester.TestAssign();
265  std_edit_tester.TestCopy();
266  std_edit_tester.TestMutable();
267  }
268 
269  std::cout << "PASS" << std::endl;
270 
271  return 0;
272 }
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:100
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