Home | History | Annotate | Download | only in test
      1 // fst_test.cc
      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 (at) google.com (Michael Riley)
     17 //
     18 // \file
     19 // Regression test for FST classes.
     20 
     21 #include "./fst_test.h"
     22 
     23 #include <fst/compact-fst.h>
     24 #include <fst/const-fst.h>
     25 #include <fst/edit-fst.h>
     26 #include <fst/matcher-fst.h>
     27 
     28 
     29 namespace fst {
     30 
     31 // A user-defined arc type.
     32 struct CustomArc {
     33   typedef short Label;
     34   typedef ProductWeight<TropicalWeight, LogWeight> Weight;
     35   typedef int64 StateId;
     36 
     37   CustomArc(Label i, Label o, Weight w, StateId s) :
     38     ilabel(i), olabel(o), weight(w), nextstate(s) {}
     39   CustomArc() {}
     40 
     41   static const string &Type() {  // Arc type name
     42     static const string type = "my";
     43     return type;
     44   }
     45 
     46   Label ilabel;       // Transition input label
     47   Label olabel;       // Transition output label
     48   Weight weight;      // Transition weight
     49   StateId nextstate;  // Transition destination state
     50 };
     51 
     52 
     53 // A user-defined compactor for test FST.
     54 template <class A>
     55 class CustomCompactor {
     56  public:
     57   typedef A Arc;
     58   typedef typename A::Label Label;
     59   typedef typename A::StateId StateId;
     60   typedef typename A::Weight Weight;
     61   typedef pair<Label, Weight> Element;
     62 
     63   Element Compact(StateId s, const A &arc) const {
     64     return make_pair(arc.ilabel, arc.weight);
     65   }
     66 
     67   Arc Expand(StateId s, const Element &p, uint32 f = kArcValueFlags) const {
     68     return p.first == kNoLabel ?
     69         Arc(kNoLabel, kNoLabel, p.second, kNoStateId) :
     70         Arc(p.first, 0, p.second, s);
     71   }
     72 
     73   ssize_t Size() const { return -1;}
     74 
     75   uint64 Properties() const { return 0ULL;}
     76 
     77   bool Compatible(const Fst<A> &fst) const {
     78     return true;
     79   }
     80 
     81   static const string &Type() {
     82     static const string type = "my";
     83     return type;
     84   }
     85 
     86   bool Write(ostream &strm) const { return true; }
     87 
     88   static CustomCompactor *Read(istream &strm) {
     89     return new CustomCompactor;
     90   }
     91 };
     92 
     93 
     94 REGISTER_FST(VectorFst, CustomArc);
     95 REGISTER_FST(ConstFst, CustomArc);
     96 static fst::FstRegisterer<
     97   CompactFst<StdArc, CustomCompactor<StdArc> > >
     98 CompactFst_StdArc_CustomCompactor_registerer;
     99 static fst::FstRegisterer<
    100   CompactFst<CustomArc, CustomCompactor<CustomArc> > >
    101 CompactFst_CustomArc_CustomCompactor_registerer;
    102 static fst::FstRegisterer<ConstFst<StdArc, uint16> >
    103 ConstFst_StdArc_uint16_registerer;
    104 static fst::FstRegisterer<
    105   CompactFst<StdArc, CustomCompactor<StdArc>, uint16> >
    106 CompactFst_StdArc_CustomCompactor_uint16_registerer;
    107 
    108 }  // namespace fst
    109 
    110 
    111 using fst::FstTester;
    112 using fst::VectorFst;
    113 using fst::ConstFst;
    114 using fst::MatcherFst;
    115 using fst::CompactFst;
    116 using fst::Fst;
    117 using fst::StdArc;
    118 using fst::CustomArc;
    119 using fst::CustomCompactor;
    120 using fst::StdArcLookAheadFst;
    121 using fst::EditFst;
    122 
    123 int main(int argc, char **argv) {
    124   FLAGS_fst_verify_properties = true;
    125   std::set_new_handler(FailedNewHandler);
    126   SET_FLAGS(argv[0], &argc, &argv, true);
    127 
    128   // VectorFst<StdArc> tests
    129   {
    130     FstTester< VectorFst<StdArc> > std_vector_tester;
    131     std_vector_tester.TestBase();
    132     std_vector_tester.TestExpanded();
    133     std_vector_tester.TestAssign();
    134     std_vector_tester.TestCopy();
    135     std_vector_tester.TestIO();
    136     std_vector_tester.TestMutable();
    137   }
    138 
    139   // ConstFst<StdArc> tests
    140   {
    141     FstTester< ConstFst<StdArc> > std_const_tester;
    142     std_const_tester.TestBase();
    143     std_const_tester.TestExpanded();
    144     std_const_tester.TestCopy();
    145     std_const_tester.TestIO();
    146   }
    147 
    148   // CompactFst<StdArc, CustomCompactor<StdArc> >
    149   {
    150     FstTester< CompactFst<StdArc, CustomCompactor<StdArc> > >
    151         std_compact_tester;
    152     std_compact_tester.TestBase();
    153     std_compact_tester.TestExpanded();
    154     std_compact_tester.TestCopy();
    155     std_compact_tester.TestIO();
    156   }
    157 
    158   // VectorFst<CustomArc> tests
    159   {
    160     FstTester< VectorFst<CustomArc> > std_vector_tester;
    161     std_vector_tester.TestBase();
    162     std_vector_tester.TestExpanded();
    163     std_vector_tester.TestAssign();
    164     std_vector_tester.TestCopy();
    165     std_vector_tester.TestIO();
    166     std_vector_tester.TestMutable();
    167   }
    168 
    169   // ConstFst<CustomArc> tests
    170   {
    171     FstTester< ConstFst<CustomArc> > std_const_tester;
    172     std_const_tester.TestBase();
    173     std_const_tester.TestExpanded();
    174     std_const_tester.TestCopy();
    175     std_const_tester.TestIO();
    176   }
    177 
    178   // CompactFst<CustomArc, CustomCompactor<CustomArc> >
    179   {
    180     FstTester< CompactFst<CustomArc, CustomCompactor<CustomArc> > >
    181         std_compact_tester;
    182     std_compact_tester.TestBase();
    183     std_compact_tester.TestExpanded();
    184     std_compact_tester.TestCopy();
    185     std_compact_tester.TestIO();
    186   }
    187 
    188   // ConstFst<StdArc, uint16> tests
    189   {
    190     FstTester< ConstFst<StdArc, uint16> > std_const_tester;
    191     std_const_tester.TestBase();
    192     std_const_tester.TestExpanded();
    193     std_const_tester.TestCopy();
    194     std_const_tester.TestIO();
    195   }
    196 
    197   // CompactFst<StdArc, CustomCompactor<StdArc>, uint16>
    198   {
    199     FstTester< CompactFst<StdArc, CustomCompactor<StdArc>, uint16> >
    200         std_compact_tester;
    201     std_compact_tester.TestBase();
    202     std_compact_tester.TestExpanded();
    203     std_compact_tester.TestCopy();
    204     std_compact_tester.TestIO();
    205   }
    206 
    207   // FstTester<StdArcLookAheadFst>
    208   {
    209     FstTester<StdArcLookAheadFst> std_matcher_tester;
    210     std_matcher_tester.TestBase();
    211     std_matcher_tester.TestExpanded();
    212     std_matcher_tester.TestCopy();
    213   }
    214 
    215   // EditFst<StdArc> tests
    216   {
    217     FstTester< EditFst<StdArc> > std_edit_tester;
    218     std_edit_tester.TestBase();
    219     std_edit_tester.TestExpanded();
    220     std_edit_tester.TestAssign();
    221     std_edit_tester.TestCopy();
    222     std_edit_tester.TestMutable();
    223   }
    224 
    225   cout << "PASS" << endl;
    226 
    227   return 0;
    228 }
    229