Blame view
tools/openfst-1.6.7/src/test/weight-tester.h
6.01 KB
8dcb6dfcb first commit |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
// See www.openfst.org for extensive documentation on this weighted // finite-state transducer library. // // Utility class for regression testing of FST weights. #ifndef FST_TEST_WEIGHT_TESTER_H_ #define FST_TEST_WEIGHT_TESTER_H_ #include <iostream> #include <sstream> #include <utility> #include <fst/log.h> #include <fst/weight.h> namespace fst { // This class tests a variety of identities and properties that must // hold for the Weight class to be well-defined. It calls function object // WEIGHT_GENERATOR to select weights that are used in the tests. template <class Weight, class WeightGenerator> class WeightTester { public: WeightTester(WeightGenerator generator) : weight_generator_(std::move(generator)) {} void Test(int iterations, bool test_division = true) { for (int i = 0; i < iterations; ++i) { // Selects the test weights. const Weight w1(weight_generator_()); const Weight w2(weight_generator_()); const Weight w3(weight_generator_()); VLOG(1) << "weight type = " << Weight::Type(); VLOG(1) << "w1 = " << w1; VLOG(1) << "w2 = " << w2; VLOG(1) << "w3 = " << w3; TestSemiring(w1, w2, w3); if (test_division) TestDivision(w1, w2); TestReverse(w1, w2); TestEquality(w1, w2, w3); TestIO(w1); TestCopy(w1); } } private: // Note in the tests below we use ApproxEqual rather than == and add // kDelta to inequalities where the weights might be inexact. // Tests (Plus, Times, Zero, One) defines a commutative semiring. void TestSemiring(Weight w1, Weight w2, Weight w3) { // Checks that the operations are closed. CHECK(Plus(w1, w2).Member()); CHECK(Times(w1, w2).Member()); // Checks that the operations are associative. CHECK(ApproxEqual(Plus(w1, Plus(w2, w3)), Plus(Plus(w1, w2), w3))); CHECK(ApproxEqual(Times(w1, Times(w2, w3)), Times(Times(w1, w2), w3))); // Checks the identity elements. CHECK(Plus(w1, Weight::Zero()) == w1); CHECK(Plus(Weight::Zero(), w1) == w1); CHECK(Times(w1, Weight::One()) == w1); CHECK(Times(Weight::One(), w1) == w1); // Check the no weight element. CHECK(!Weight::NoWeight().Member()); CHECK(!Plus(w1, Weight::NoWeight()).Member()); CHECK(!Plus(Weight::NoWeight(), w1).Member()); CHECK(!Times(w1, Weight::NoWeight()).Member()); CHECK(!Times(Weight::NoWeight(), w1).Member()); // Checks that the operations commute. CHECK(ApproxEqual(Plus(w1, w2), Plus(w2, w1))); if (Weight::Properties() & kCommutative) CHECK(ApproxEqual(Times(w1, w2), Times(w2, w1))); // Checks Zero() is the annihilator. CHECK(Times(w1, Weight::Zero()) == Weight::Zero()); CHECK(Times(Weight::Zero(), w1) == Weight::Zero()); // Check Power(w, 0) is Weight::One() CHECK(Power(w1, 0) == Weight::One()); // Check Power(w, 1) is w CHECK(Power(w1, 1) == w1); // Check Power(w, 3) is Times(w, Times(w, w)) CHECK(Power(w1, 3) == Times(w1, Times(w1, w1))); // Checks distributivity. if (Weight::Properties() & kLeftSemiring) { CHECK(ApproxEqual(Times(w1, Plus(w2, w3)), Plus(Times(w1, w2), Times(w1, w3)))); } if (Weight::Properties() & kRightSemiring) CHECK(ApproxEqual(Times(Plus(w1, w2), w3), Plus(Times(w1, w3), Times(w2, w3)))); if (Weight::Properties() & kIdempotent) CHECK(Plus(w1, w1) == w1); if (Weight::Properties() & kPath) CHECK(Plus(w1, w2) == w1 || Plus(w1, w2) == w2); // Ensure weights form a left or right semiring. CHECK(Weight::Properties() & (kLeftSemiring | kRightSemiring)); // Check when Times() is commutative that it is marked as a semiring. if (Weight::Properties() & kCommutative) CHECK(Weight::Properties() & kSemiring); } // Tests division operation. void TestDivision(Weight w1, Weight w2) { Weight p = Times(w1, w2); if (Weight::Properties() & kLeftSemiring) { Weight d = Divide(p, w1, DIVIDE_LEFT); if (d.Member()) CHECK(ApproxEqual(p, Times(w1, d))); CHECK(!Divide(w1, Weight::NoWeight(), DIVIDE_LEFT).Member()); CHECK(!Divide(Weight::NoWeight(), w1, DIVIDE_LEFT).Member()); } if (Weight::Properties() & kRightSemiring) { Weight d = Divide(p, w2, DIVIDE_RIGHT); if (d.Member()) CHECK(ApproxEqual(p, Times(d, w2))); CHECK(!Divide(w1, Weight::NoWeight(), DIVIDE_RIGHT).Member()); CHECK(!Divide(Weight::NoWeight(), w1, DIVIDE_RIGHT).Member()); } if (Weight::Properties() & kCommutative) { Weight d = Divide(p, w1, DIVIDE_RIGHT); if (d.Member()) CHECK(ApproxEqual(p, Times(d, w1))); } } // Tests reverse operation. void TestReverse(Weight w1, Weight w2) { typedef typename Weight::ReverseWeight ReverseWeight; ReverseWeight rw1 = w1.Reverse(); ReverseWeight rw2 = w2.Reverse(); CHECK(rw1.Reverse() == w1); CHECK(Plus(w1, w2).Reverse() == Plus(rw1, rw2)); CHECK(Times(w1, w2).Reverse() == Times(rw2, rw1)); } // Tests == is an equivalence relation. void TestEquality(Weight w1, Weight w2, Weight w3) { // Checks reflexivity. CHECK(w1 == w1); // Checks symmetry. CHECK((w1 == w2) == (w2 == w1)); // Checks transitivity. if (w1 == w2 && w2 == w3) CHECK(w1 == w3); } // Tests binary serialization and textual I/O. void TestIO(Weight w) { // Tests binary I/O { std::ostringstream os; w.Write(os); os.flush(); std::istringstream is(os.str()); Weight v; v.Read(is); CHECK_EQ(w, v); } // Tests textual I/O. { std::ostringstream os; os << w; std::istringstream is(os.str()); Weight v(Weight::One()); is >> v; CHECK(ApproxEqual(w, v)); } } // Tests copy constructor and assignment operator void TestCopy(Weight w) { Weight x = w; CHECK(w == x); x = Weight(w); CHECK(w == x); x.operator=(x); CHECK(w == x); } // Generates weights used in testing. WeightGenerator weight_generator_; }; } // namespace fst #endif // FST_TEST_WEIGHT_TESTER_H_ |