20 #ifndef FST_TEST_WEIGHT_TESTER_H_ 21 #define FST_TEST_WEIGHT_TESTER_H_ 34 template <
class Weight,
class WeightGenerator = WeightGenerate<Weight>>
38 : weight_generator_(std::move(generator)) {}
40 void Test(
int iterations) {
41 for (
int i = 0; i < iterations; ++i) {
43 const Weight w1(weight_generator_());
44 const Weight w2(weight_generator_());
45 const Weight w3(weight_generator_());
47 VLOG(1) <<
"weight type = " << Weight::Type();
48 VLOG(1) <<
"w1 = " << w1;
49 VLOG(1) <<
"w2 = " << w2;
50 VLOG(1) <<
"w3 = " << w3;
52 TestSemiring(w1, w2, w3);
55 TestEquality(w1, w2, w3);
66 void TestSemiring(Weight w1, Weight w2, Weight w3) {
82 CHECK(!Weight::NoWeight().Member());
83 CHECK(!
Plus(w1, Weight::NoWeight()).Member());
84 CHECK(!
Plus(Weight::NoWeight(), w1).Member());
85 CHECK(!
Times(w1, Weight::NoWeight()).Member());
86 CHECK(!
Times(Weight::NoWeight(), w1).Member());
95 CHECK(
Times(w1, Weight::Zero()) == Weight::Zero());
96 CHECK(
Times(Weight::Zero(), w1) == Weight::Zero());
121 if (Weight::Properties() &
kPath)
133 void TestDivision(Weight w1, Weight w2) {
134 Weight p =
Times(w1, w2);
135 VLOG(1) <<
"TestDivision: p = " << p;
160 void TestReverse(Weight w1, Weight w2) {
161 using ReverseWeight =
typename Weight::ReverseWeight;
163 ReverseWeight rw1 = w1.Reverse();
164 ReverseWeight rw2 = w2.Reverse();
166 CHECK(rw1.Reverse() == w1);
172 void TestEquality(Weight w1, Weight w2, Weight w3) {
177 CHECK((w1 == w2) == (w2 == w1));
180 if (w1 == w2 && w2 == w3)
CHECK(w1 == w3);
183 CHECK((w1 == w2) ^ (w1 != w2));
187 CHECK(w1.Hash() == w2.Hash());
200 void TestIO(Weight w) {
203 std::ostringstream os;
206 std::istringstream is(os.str());
214 std::ostringstream os;
216 std::istringstream is(os.str());
217 Weight v(Weight::One());
224 void TestCopy(Weight w) {
236 WeightGenerator weight_generator_;
241 #endif // FST_TEST_WEIGHT_TESTER_H_ constexpr uint64_t kSemiring
ErrorWeight Plus(const ErrorWeight &, const ErrorWeight &)
ErrorWeight Times(const ErrorWeight &, const ErrorWeight &)
constexpr uint64_t kIdempotent
void Reverse(const Fst< Arc > &ifst, const std::vector< std::pair< typename Arc::Label, typename Arc::Label >> &parens, std::vector< typename Arc::Label > *assignments, MutableFst< RevArc > *ofst)
constexpr uint64_t kRightSemiring
WeightTester(WeightGenerator generator)
constexpr uint64_t kCommutative
ErrorWeight Divide(const ErrorWeight &, const ErrorWeight &)
void Test(int iterations)
constexpr TropicalWeightTpl< T > Power(const TropicalWeightTpl< T > &w, V n)
constexpr uint64_t kLeftSemiring
bool ApproxEqual(const ErrorWeight &, const ErrorWeight &, float)