FST  openfst-1.7.2
OpenFst Library
algo_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 various FST algorithms.
5 
6 #include <fst/test/algo_test.h>
7 
8 #include <cstdlib>
9 
10 #include <vector>
11 
12 #include <fst/flags.h>
13 
14 // DEFINEs determine which semirings are tested; these are controlled by
15 // the `defines` attributes of the associated build rules.
16 
17 DEFINE_int32(seed, -1, "random seed");
18 DEFINE_int32(repeat, 25, "number of test repetitions");
19 
20 using fst::AlgoTester;
21 using fst::ArcTpl;
22 using fst::GallicArc;
23 using fst::GallicWeight;
26 using fst::LogArc;
27 using fst::LogWeight;
28 using fst::MinMaxArc;
29 using fst::MinMaxWeight;
30 using fst::PowerWeight;
31 using fst::STRING_LEFT;
32 using fst::STRING_RIGHT;
33 using fst::StdArc;
34 using fst::StringArc;
35 using fst::StringWeight;
38 
39 int main(int argc, char **argv) {
40  FLAGS_fst_verify_properties = true;
41  std::set_new_handler(FailedNewHandler);
42  SET_FLAGS(argv[0], &argc, &argv, true);
43 
44  static const int kCacheGcLimit = 20;
45 
46  srand(FLAGS_seed);
47  LOG(INFO) << "Seed = " << FLAGS_seed;
48 
49  FLAGS_fst_default_cache_gc = rand() % 2;
50  FLAGS_fst_default_cache_gc_limit = rand() % kCacheGcLimit;
51  VLOG(1) << "default_cache_gc:" << FLAGS_fst_default_cache_gc;
52  VLOG(1) << "default_cache_gc_limit:" << FLAGS_fst_default_cache_gc_limit;
53 
54 #ifdef TEST_TROPICAL
55  using TropicalWeightGenerate = WeightGenerate<TropicalWeight>;
56  TropicalWeightGenerate tropical_generator(false);
58  tropical_generator, FLAGS_seed);
59  tropical_tester.Test();
60 #endif // TEST_TROPICAL
61 
62 #ifdef TEST_LOG
63  using LogWeightGenerate = WeightGenerate<LogWeight>;
64  LogWeightGenerate log_generator(false);
65  AlgoTester<LogArc, LogWeightGenerate> log_tester(log_generator, FLAGS_seed);
66  log_tester.Test();
67 #endif // TEST_LOG
68 
69 #ifdef TEST_MINMAX
70  using MinMaxWeightGenerate = WeightGenerate<MinMaxWeight>;
71  MinMaxWeightGenerate minmax_generator(false);
72  AlgoTester<MinMaxArc, MinMaxWeightGenerate> minmax_tester(minmax_generator,
73  FLAGS_seed);
74  minmax_tester.Test();
75 #endif
76 
77 #ifdef TEST_LEFT_STRING
78  using StringWeightGenerate = WeightGenerate<StringWeight<int, STRING_LEFT>>;
79  StringWeightGenerate left_string_generator(false);
80  AlgoTester<StringArc<>, StringWeightGenerate> left_string_tester(
81  left_string_generator, FLAGS_seed);
82  left_string_tester.Test();
83 #endif // TEST_LEFT_STRING
84 
85 #ifdef TEST_RIGHT_STRING
86  using StringWeightGenerate =
88  StringWeightGenerate right_string_generator(false);
89  AlgoTester<StringArc<STRING_RIGHT>, StringWeightGenerate>
90  right_string_tester(right_string_generator, FLAGS_seed);
91  right_string_tester.Test();
92 #endif // TEST_RIGHT_STRING
93 
94 #ifdef TEST_GALLIC
95  using StdGallicArc = GallicArc<StdArc>;
96  using TropicalGallicWeightGenerate =
98  TropicalGallicWeightGenerate tropical_gallic_generator(false);
100  tropical_gallic_generator, FLAGS_seed);
101  gallic_tester.Test();
102 #endif // TEST_GALLIC
103 
104 #ifdef TEST_LEXICOGRAPHIC
105  using TropicalLexicographicArc =
107  using TropicalLexicographicWeightGenerate =
109  TropicalLexicographicWeightGenerate lexicographic_generator(false);
111  lexicographic_tester(lexicographic_generator, FLAGS_seed);
112  lexicographic_tester.Test();
113 #endif // TEST_LEXICOGRAPHIC
114 
115 #ifdef TEST_POWER
116  using TropicalCubeWeight = PowerWeight<TropicalWeight, 3>;
117  using TropicalCubeArc = ArcTpl<TropicalCubeWeight>;
118  using TropicalCubeWeightGenerate = WeightGenerate<TropicalCubeWeight>;
119  TropicalCubeWeightGenerate tropical_cube_generator(false);
121  tropical_cube_generator, FLAGS_seed);
122  tropical_cube_tester.Test();
123 #endif // TEST_POWER
124 
125  std::cout << "PASS" << std::endl;
126 
127  return 0;
128 }
DEFINE_int32(seed,-1,"random seed")
int main(int argc, char **argv)
Definition: algo_test.cc:39
#define LOG(type)
Definition: log.h:48
ArcTpl< TropicalWeight > StdArc
Definition: arc.h:55
void FailedNewHandler()
Definition: compat.cc:25
ArcTpl< MinMaxWeight > MinMaxArc
Definition: arc.h:60
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:214
LogWeightTpl< float > LogWeight
Definition: float-weight.h:446
TropicalWeightTpl< float > TropicalWeight
Definition: float-weight.h:244
#define VLOG(level)
Definition: log.h:49
MinMaxWeightTpl< float > MinMaxWeight
Definition: float-weight.h:667
ArcTpl< LogWeight > LogArc
Definition: arc.h:56