20 #ifndef FST_TEST_FST_TEST_H_ 21 #define FST_TEST_FST_TEST_H_ 50 using Arc =
typename F::Arc;
53 using Label =
typename Arc::Label;
55 explicit FstTester(
size_t num_states = 128,
bool weighted =
true)
56 : num_states_(num_states), weighted_(weighted) {
58 InitFst(&vfst, num_states);
59 testfst_ = std::make_unique<F>(vfst);
70 bool has_states =
false;
71 for (; !siter.
Done(); siter.
Next()) {
78 CHECK_EQ(fst.Final(s), NthWeight(s));
81 for (; !aiter.
Done(); aiter.
Next()) {
90 CHECK_EQ(arc.nextstate, s == num_states_ - 1 ? 0 : s + 1);
102 CHECK_EQ(fst.NumInputEpsilons(s), 0);
103 CHECK_EQ(fst.NumOutputEpsilons(s), s + 1);
121 CHECK_EQ(fst.NumStates(), num_states_);
138 size_t ni = fst->NumInputEpsilons(s);
140 for (; !aiter.
Done(); aiter.
Next()) {
149 CHECK_EQ(fst->NumInputEpsilons(s), ni + 1);
152 CHECK_EQ(fst->NumInputEpsilons(s), ni);
157 std::unique_ptr<G> cfst1(fst->Copy());
158 cfst1->DeleteStates();
162 std::unique_ptr<G> cfst2(fst->Copy());
165 cfst2->DeleteArcs(s);
167 CHECK_EQ(cfst2->NumInputEpsilons(s), 0);
168 CHECK_EQ(cfst2->NumOutputEpsilons(s), 0);
184 afst2 =
static_cast<const Fst<Arc> &
>(fst);
188 afst2.operator=(afst2);
202 const G c2fst(
static_cast<const Fst<Arc> &
>(fst));
206 std::unique_ptr<const G> c3fst(fst.Copy());
215 const std::string filename = FST_FLAGS_tmpdir +
"/test.fst";
216 const std::string aligned =
217 FST_FLAGS_tmpdir +
"/aligned.fst";
220 CHECK(fst.Write(filename));
234 CHECK(gfst->Write(filename));
243 std::ofstream ostr(aligned);
247 CHECK(fst.Write(ostr, opts));
249 std::ifstream istr(aligned);
261 std::ofstream ostr(aligned);
265 CHECK(fst.Write(ostr, opts));
267 std::ifstream istr(aligned);
285 if (fst.Properties(
kMutable,
false)) {
318 for (
StateId s = 0; s < nstates; ++s) {
321 for (
size_t i = 1; i <= s; ++i) {
322 Arc arc(i, 0, NthWeight(i), s);
326 s,
Arc(s + 1, 0, NthWeight(s + 1), s == nstates - 1 ? 0 : s + 1));
334 Weight NthWeight(
int n)
const {
335 if (!weighted_)
return Weight::One();
336 Weight w = Weight::Zero();
337 for (
int i = 0; i < n; ++i) w =
Plus(w, Weight::One());
341 size_t num_states_ = 0;
342 bool weighted_ =
true;
343 std::unique_ptr<F> testfst_;
348 #endif // FST_TEST_FST_TEST_H_ void TestExpanded(const G &fst) const
MatchType Type(bool test) const
constexpr uint64_t kMutable
constexpr uint64_t kOEpsilons
ErrorWeight Plus(const ErrorWeight &, const ErrorWeight &)
void TestIO(const G &fst) const
FstTester(size_t num_states=128, bool weighted=true)
virtual void SetStart(StateId)=0
void TestAssign(const G &fst) const
void SetValue(const Arc &arc)
std::unique_ptr< T > WrapUnique(T *ptr)
const Arc & Value() const
const Arc & Value() const
void TestCopy(const G &fst) const
constexpr uint64_t kNoOEpsilons
constexpr uint64_t kNotAcceptor
typename Arc::Weight Weight
void TestExpanded() const
bool Equal(const Fst< Arc > &fst1, const Fst< Arc > &fst2, WeightEqual weight_equal, uint8_t etype=kEqualFsts)
bool Verify(const Fst< Arc > &fst, bool allow_negative_labels=false)
static FileReadMode ReadMode(std::string_view mode)
virtual void AddArc(StateId, const Arc &)=0
void TestMutable(G *fst) const
virtual StateId AddState()=0
virtual void SetFinal(StateId s, Weight weight=Weight::One())=0
virtual void DeleteStates(const std::vector< StateId > &)=0
typename Arc::StateId StateId
const Arc & Value() const
void TestBase(const G &fst) const
typename Arc::Label Label
constexpr uint64_t kExpanded
constexpr uint64_t kAcceptor