1// algo_test.h
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// Copyright 2005-2010 Google, Inc.
16// Author: riley@google.com (Michael Riley)
17//
18// \file
19// Regression test for various FST algorithms.
20
21#include "./algo_test.h"
22
23// These determine which semirings are tested. Defining at least
24// TEST_TROPICAL and TEST_LOG is recommended. More increase the
25// comprehensiveness, but also increase the compilation time.
26
27#define TEST_TROPICAL
28#define TEST_LOG
29// #define TEST_MINMAX
30// #define TEST_LEFT_STRING
31// #define TEST_RIGHT_STRING
32// #define TEST_GALLIC
33// #define TEST_LEXICOGRAPHIC
34// #define TEST_POWER
35
36DEFINE_int32(seed, -1, "random seed");
37DEFINE_int32(repeat, 25, "number of test repetitions");
38
39using fst::StdArc;
40using fst::TropicalWeightGenerator;
41
42using fst::LogArc;
43using fst::LogWeightGenerator;
44
45using fst::MinMaxArc;
46using fst::MinMaxWeightGenerator;
47
48using fst::StringArc;
49using fst::StringWeightGenerator;
50using fst::STRING_LEFT;
51using fst::STRING_RIGHT;
52
53using fst::GallicArc;
54using fst::GallicWeightGenerator;
55
56using fst::LexicographicArc;
57using fst::TropicalWeight;
58using fst::LexicographicWeightGenerator;
59
60using fst::ArcTpl;
61using fst::PowerWeight;
62using fst::PowerWeightGenerator;
63
64using fst::AlgoTester;
65
66int main(int argc, char **argv) {
67  FLAGS_fst_verify_properties = true;
68  std::set_new_handler(FailedNewHandler);
69  SET_FLAGS(argv[0], &argc, &argv, true);
70
71  static const int kCacheGcLimit = 20;
72
73  int seed = FLAGS_seed >= 0 ? FLAGS_seed : time(0);
74  srand(seed);
75  LOG(INFO) << "Seed = " << seed;
76
77  FLAGS_fst_default_cache_gc = rand() % 2;
78  FLAGS_fst_default_cache_gc_limit = rand() % kCacheGcLimit;
79  VLOG(1) << "default_cache_gc:" << FLAGS_fst_default_cache_gc;
80  VLOG(1) << "default_cache_gc_limit:" << FLAGS_fst_default_cache_gc_limit;
81
82#ifdef TEST_TROPICAL
83  TropicalWeightGenerator tropical_generator(seed, false);
84  AlgoTester<StdArc, TropicalWeightGenerator>
85    tropical_tester(tropical_generator, seed);
86  tropical_tester.Test();
87#endif  // TEST_TROPICAL
88
89#ifdef TEST_LOG
90  LogWeightGenerator log_generator(seed, false);
91  AlgoTester<LogArc, LogWeightGenerator>
92    log_tester(log_generator, seed);
93  log_tester.Test();
94#endif  // TEST_LOG
95
96#ifdef TEST_MINMAX
97  MinMaxWeightGenerator minmax_generator(seed, false);
98  AlgoTester<MinMaxArc, MinMaxWeightGenerator>
99      minmax_tester(minmax_generator, seed);
100  minmax_tester.Test();
101#endif
102
103#ifdef TEST_LEFT_STRING
104  StringWeightGenerator<int> left_string_generator(seed, false);
105  AlgoTester<StringArc<>, StringWeightGenerator<int> >
106    left_string_tester(left_string_generator, seed);
107  left_string_tester.Test();
108#endif  // TEST_LEFT_STRING
109
110#ifdef TEST_RIGHT_STRING
111  StringWeightGenerator<int, STRING_RIGHT> right_string_generator(seed, false);
112  AlgoTester<StringArc<STRING_RIGHT>,
113    StringWeightGenerator<int, STRING_RIGHT> >
114    right_string_tester(right_string_generator, seed);
115  right_string_tester.Test();
116#endif  // TEST_RIGHT_STRING
117
118#ifdef TEST_GALLIC
119  typedef GallicArc<StdArc> StdGallicArc;
120  typedef GallicWeightGenerator<int, TropicalWeightGenerator>
121    TropicalGallicWeightGenerator;
122
123  TropicalGallicWeightGenerator tropical_gallic_generator(seed, false);
124  AlgoTester<StdGallicArc, TropicalGallicWeightGenerator>
125    gallic_tester(tropical_gallic_generator, seed);
126  gallic_tester.Test();
127#endif  // TEST_GALLIC
128
129#ifdef TEST_LEXICOGRAPHIC
130  typedef LexicographicArc<TropicalWeight, TropicalWeight>
131      TropicalLexicographicArc;
132  typedef LexicographicWeightGenerator<TropicalWeightGenerator,
133      TropicalWeightGenerator> TropicalLexicographicWeightGenerator;
134  TropicalLexicographicWeightGenerator lexicographic_generator(seed, false);
135  AlgoTester<TropicalLexicographicArc, TropicalLexicographicWeightGenerator>
136      lexicographic_tester(lexicographic_generator, seed);
137  lexicographic_tester.Test();
138#endif  // TEST_LEXICOGRAPHIC
139
140#ifdef TEST_POWER
141  typedef PowerWeight<TropicalWeight, 3> TropicalCubeWeight;
142  typedef ArcTpl<TropicalCubeWeight> TropicalCubeArc;
143  typedef PowerWeightGenerator<TropicalWeightGenerator, 3>
144    TropicalCubeWeightGenerator;
145
146  TropicalCubeWeightGenerator tropical_cube_generator(seed, false);
147  AlgoTester<TropicalCubeArc, TropicalCubeWeightGenerator>
148    tropical_cube_tester(tropical_cube_generator, seed);
149  tropical_cube_tester.Test();
150#endif  // TEST_POWER
151
152  cout << "PASS" << endl;
153
154  return 0;
155}
156