FST  openfst-1.8.3
OpenFst Library
algo_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 various FST algorithms.
19 
20 #include <fst/test/algo_test.h>
21 
22 #include <random>
23 
24 #include <fst/flags.h>
25 #include <fst/log.h>
26 #include <fst/arc.h>
27 #include <fst/cache.h>
28 #include <fst/float-weight.h>
29 #include <fst/fst-decl.h>
31 #include <fst/power-weight.h>
32 #include <fst/string-weight.h>
33 #include <fst/tuple-weight.h>
34 #include <fst/weight.h>
35 
36 // DEFINEs determine which semirings are tested; these are controlled by
37 // the `defines` attributes of the associated build rules.
38 
39 DEFINE_uint64(seed, 403, "random seed");
40 DEFINE_int32(repeat, 25, "number of test repetitions");
41 
42 namespace {
43 
44 using fst::AlgoTester;
46 
47 } // namespace
48 
49 int main(int argc, char **argv) {
50  SetFlag(&FST_FLAGS_fst_verify_properties, true);
51  SET_FLAGS(argv[0], &argc, &argv, true);
52 
53  static const int kCacheGcLimit = 20;
54 
55  LOG(INFO) << "Seed = " << FST_FLAGS_seed;
56 
57  std::mt19937_64 rand(FST_FLAGS_seed);
58 
59  SetFlag(&FST_FLAGS_fst_default_cache_gc,
60  std::bernoulli_distribution(.5)(rand));
61  SetFlag(&FST_FLAGS_fst_default_cache_gc_limit,
62  std::uniform_int_distribution<>(0, kCacheGcLimit)(rand));
63  VLOG(1) << "default_cache_gc:" << FST_FLAGS_fst_default_cache_gc;
64  VLOG(1) << "default_cache_gc_limit:"
65  << FST_FLAGS_fst_default_cache_gc_limit;
66 #if defined(TEST_TROPICAL)
67  using Arc = fst::StdArc;
68 #elif defined(TEST_LOG)
69  using Arc = fst::LogArc;
70 #elif defined(TEST_MINMAX)
71  using Arc = fst::MinMaxArc;
72 #elif defined(TEST_LEFT_STRING)
74 #elif defined(TEST_RIGHT_STRING)
76 #elif defined(TEST_GALLIC)
77  using Arc = fst::GallicArc<fst::StdArc>;
78 #elif defined(TEST_LEXICOGRAPHIC)
80  using fst::TropicalWeight;
81  using Arc = LexicographicArc<TropicalWeight, TropicalWeight>;
82 #elif defined(TEST_POWER)
83  using fst::ArcTpl;
84  using fst::PowerWeight;
85  using fst::TropicalWeight;
86  using Arc = ArcTpl<PowerWeight<TropicalWeight, 3>>;
87 #else
88  #error "Must have one of the TEST_* macros defined."
89 #endif
90  WeightGenerate<Arc::Weight> weight_generator(FST_FLAGS_seed,
91  /*allow_zero=*/false);
92  AlgoTester<Arc> arc_tester(weight_generator, FST_FLAGS_seed);
93  arc_tester.Test();
94 
95  return 0;
96 }
DEFINE_int32(repeat, 25,"number of test repetitions")
int main(int argc, char **argv)
Definition: algo_test.cc:49
#define LOG(type)
Definition: log.h:53
ArcTpl< TropicalWeight > StdArc
Definition: arc.h:75
ArcTpl< MinMaxWeight > MinMaxArc
Definition: arc.h:83
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:226
DEFINE_uint64(seed, 403,"random seed")
TropicalWeightTpl< float > TropicalWeight
Definition: float-weight.h:268
#define VLOG(level)
Definition: log.h:54
ArcTpl< LexicographicWeight< Weight1, Weight2 >> LexicographicArc
Definition: arc.h:149
void SetFlag(Type *flag, Value value)
Definition: flags.h:220
ArcTpl< LogWeight > LogArc
Definition: arc.h:76