From 09d601971a937c127b5a151feaab790e96bac66c Mon Sep 17 00:00:00 2001 From: Jesse Rosenstock Date: Wed, 8 Apr 2026 07:19:43 -0700 Subject: [PATCH] weight_test: Rewrite as gUnit test Shard test 4x in bazel, for 4x speedup (24s -> 6s) of this single test. Total CMake test time unchanged at ~1:18. PiperOrigin-RevId: 896484546 --- openfst/extensions/categorial/BUILD.bazel | 1 + openfst/extensions/categorial/weight_test.cc | 47 +-- openfst/test/BUILD.bazel | 3 + openfst/test/CMakeLists.txt | 19 +- openfst/test/power-weight_test.cc | 19 +- openfst/test/weight-tester.h | 184 ++++++---- openfst/test/weight_test.cc | 346 +++++++------------ 7 files changed, 280 insertions(+), 339 deletions(-) diff --git a/openfst/extensions/categorial/BUILD.bazel b/openfst/extensions/categorial/BUILD.bazel index eaf207e8..7c443b6f 100644 --- a/openfst/extensions/categorial/BUILD.bazel +++ b/openfst/extensions/categorial/BUILD.bazel @@ -34,6 +34,7 @@ cc_test( size = "large", srcs = ["weight_test.cc"], linkstatic = True, + shard_count = 3, deps = [ ":categorial-weight", "//openfst/lib:weight", diff --git a/openfst/extensions/categorial/weight_test.cc b/openfst/extensions/categorial/weight_test.cc index 4ff2c55e..e01b9a5f 100644 --- a/openfst/extensions/categorial/weight_test.cc +++ b/openfst/extensions/categorial/weight_test.cc @@ -19,6 +19,7 @@ #include "openfst/lib/weight.h" +#include #include #include #include @@ -30,36 +31,42 @@ #include "openfst/extensions/categorial/categorial-weight.h" #include "openfst/test/weight-tester.h" -ABSL_FLAG(int, seed, -1, "Random seed."); -ABSL_FLAG(int, repeat, 10000, "Number of test repetitions."); +ABSL_FLAG(uint64_t, seed, 403, "Random seed."); +ABSL_FLAG(int32_t, repeat, 10000, "Number of test repetitions."); namespace fst { + +template <> +struct WeightTestTraits> { + static WeightGenerate> Generator(uint64_t seed) { + return WeightGenerate>(); + } + static bool IoRequiresParens() { return false; } +}; + +template <> +struct WeightTestTraits> { + static WeightGenerate> Generator( + uint64_t seed) { + return WeightGenerate>(); + } + static bool IoRequiresParens() { return false; } +}; + namespace { -TEST(CategorialWeight, LeftTest) { - WeightGenerate> left_category_generate; - WeightTester> left_category_tester( - left_category_generate); - left_category_tester.Test(absl::GetFlag(FLAGS_repeat)); - std::cout << "PASS left categorial test" << std::endl; -} +using CategorialWeightTypes = + ::testing::Types, + CategorialWeight>; -TEST(CategorialWeight, RightTest) { - WeightGenerate> - right_category_generate; - WeightTester> - right_category_tester(right_category_generate); - right_category_tester.Test(absl::GetFlag(FLAGS_repeat)); - std::cout << "PASS right categorial test" << std::endl; -} +INSTANTIATE_TYPED_TEST_SUITE_P(Categorial, WeightTest, CategorialWeightTypes, ); } // namespace } // namespace fst -int main(int argc, char **argv) { +int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); - int seed = absl::GetFlag(FLAGS_seed) >= 0 ? absl::GetFlag(FLAGS_seed) - : time(nullptr); + int seed = absl::GetFlag(FLAGS_seed); std::srand(seed); LOG(INFO) << "Seed = " << absl::GetFlag(FLAGS_seed); return RUN_ALL_TESTS(); diff --git a/openfst/test/BUILD.bazel b/openfst/test/BUILD.bazel index 335cc37e..8bd8ef91 100644 --- a/openfst/test/BUILD.bazel +++ b/openfst/test/BUILD.bazel @@ -226,6 +226,7 @@ cc_library( hdrs = ["weight-tester.h"], deps = [ "//openfst/lib:weight", + "@com_google_absl//absl/flags:flag", "@com_google_absl//absl/log", "@com_google_googletest//:gtest", ], @@ -1463,6 +1464,7 @@ cc_test( ":test_main", ":weight_tester", "//openfst/lib:weight", + "@com_google_absl//absl/flags:flag", "@com_google_googletest//:gtest", ], ) @@ -2359,6 +2361,7 @@ cc_test( size = "large", srcs = ["weight_test.cc"], linkstatic = True, + shard_count = 4, deps = [ ":weight_tester", "//openfst/lib:weight", diff --git a/openfst/test/CMakeLists.txt b/openfst/test/CMakeLists.txt index a72e386f..6e2cff79 100644 --- a/openfst/test/CMakeLists.txt +++ b/openfst/test/CMakeLists.txt @@ -158,21 +158,10 @@ foreach(TEST_NAME ${TESTS}) ) endif() - # TODO: Make these use GUnit TEST() instead of a custom RunTest(), - # then use gtest_discover_tests. - if(TEST_NAME STREQUAL "weight_test") - add_test( - NAME ${TEST_NAME} - COMMAND ${TEST_NAME} - WORKING_DIRECTORY "${CMAKE_BINARY_DIR}" - ) - else() - gtest_discover_tests( - ${TEST_NAME} - WORKING_DIRECTORY "${CMAKE_BINARY_DIR}" - ) - endif() - + gtest_discover_tests( + ${TEST_NAME} + WORKING_DIRECTORY "${CMAKE_BINARY_DIR}" + ) endforeach() # algo_test variants diff --git a/openfst/test/power-weight_test.cc b/openfst/test/power-weight_test.cc index 7e424830..7c8fd96b 100644 --- a/openfst/test/power-weight_test.cc +++ b/openfst/test/power-weight_test.cc @@ -21,12 +21,16 @@ #include "gmock/gmock.h" #include "gtest/gtest.h" +#include "absl/flags/flag.h" #include "openfst/lib/float-weight.h" #include "openfst/lib/sparse-power-weight.h" #include "openfst/lib/weight.h" #include "openfst/test/power-weight-util.h" #include "openfst/test/weight-tester.h" +ABSL_FLAG(uint64_t, seed, 403, "random seed"); +ABSL_FLAG(int32_t, repeat, 2000, "number of test repetitions"); + namespace fst { namespace { @@ -40,20 +44,7 @@ using PowerWeightTypes = testing::Types, PowerWeight>; -template -class GeneratePowerWeightTest : public testing::Test { - public: - using Weight = PowerWeightT; -}; -TYPED_TEST_SUITE(GeneratePowerWeightTest, PowerWeightTypes); - -TYPED_TEST(GeneratePowerWeightTest, WeightTester) { - using Weight = typename TestFixture::Weight; - using Generator = WeightGenerate; - using WeightTester = WeightTester; - WeightTester tester((Generator())); - tester.Test(2000 /* iterations */); -} +INSTANTIATE_TYPED_TEST_SUITE_P(Power, WeightTest, PowerWeightTypes, ); template class GetPowerWeightComponentTest : public testing::Test { diff --git a/openfst/test/weight-tester.h b/openfst/test/weight-tester.h index acf0a9ff..e5b0a0e0 100644 --- a/openfst/test/weight-tester.h +++ b/openfst/test/weight-tester.h @@ -20,51 +20,61 @@ #ifndef OPENFST_TEST_WEIGHT_TESTER_H_ #define OPENFST_TEST_WEIGHT_TESTER_H_ +#include #include #include #include "gtest/gtest.h" +#include "absl/flags/declare.h" +#include "absl/flags/flag.h" #include "absl/log/log.h" #include "openfst/lib/weight.h" +// These flags must be defined in the *test.cc including this file. +ABSL_DECLARE_FLAG(uint64_t, seed); +ABSL_DECLARE_FLAG(int32_t, repeat); + 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 WeightTester { - public: - explicit WeightTester(WeightGenerator generator) - : weight_generator_(std::move(generator)) {} - - void Test(int iterations) { - 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); - TestDivision(w1, w2); - TestReverse(w1, w2); - TestEquality(w1, w2, w3); - TestIO(w1); - TestCopy(w1); - } +// Traits class to control test behavior. This may be specialized for weights +// that require different implementations. +template +struct WeightTestTraits { + static WeightGenerate Generator(uint64_t seed) { + return WeightGenerate(seed); } + static bool IoRequiresParens() { return false; } +}; + +template +class WeightTest : public ::testing::Test { + public: + using WeightGenerator = WeightGenerate; + + WeightTest() + : seed_(absl::GetFlag(FLAGS_seed)), + generate_(WeightTestTraits::Generator(seed_)) {} + + protected: + const uint64_t seed_; + WeightGenerator generate_; +}; + +TYPED_TEST_SUITE_P(WeightTest); - 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. +TYPED_TEST_P(WeightTest, Semiring) { + using Weight = TypeParam; + for (int i = 0; i < absl::GetFlag(FLAGS_repeat); ++i) { + const Weight w1(this->generate_()); + const Weight w2(this->generate_()); + const Weight w3(this->generate_()); + + VLOG(1) << "weight type = " << Weight::Type(); + VLOG(1) << "w1 = " << w1; + VLOG(1) << "w2 = " << w2; + VLOG(1) << "w3 = " << w3; - // Tests (Plus, Times, Zero, One) defines a commutative semiring. - void TestSemiring(Weight w1, Weight w2, Weight w3) { // Checks that the operations are closed. EXPECT_TRUE(Plus(w1, w2).Member()); EXPECT_TRUE(Times(w1, w2).Member()); @@ -136,9 +146,13 @@ class WeightTester { EXPECT_TRUE(Weight::Properties() & kSemiring); } } +} - // Tests division operation. - void TestDivision(Weight w1, Weight w2) { +TYPED_TEST_P(WeightTest, Division) { + using Weight = TypeParam; + for (int i = 0; i < absl::GetFlag(FLAGS_repeat); ++i) { + const Weight w1(this->generate_()); + const Weight w2(this->generate_()); Weight p = Times(w1, w2); VLOG(1) << "TestDivision: p = " << p; @@ -171,10 +185,14 @@ class WeightTester { } } } +} - // Tests reverse operation. - void TestReverse(Weight w1, Weight w2) { - using ReverseWeight = typename Weight::ReverseWeight; +TYPED_TEST_P(WeightTest, Reverse) { + using Weight = TypeParam; + using ReverseWeight = typename Weight::ReverseWeight; + for (int i = 0; i < absl::GetFlag(FLAGS_repeat); ++i) { + const Weight w1(this->generate_()); + const Weight w2(this->generate_()); const ReverseWeight rw1 = w1.Reverse(); const ReverseWeight rw2 = w2.Reverse(); @@ -183,9 +201,15 @@ class WeightTester { EXPECT_EQ(Plus(w1, w2).Reverse(), Plus(rw1, rw2)); EXPECT_EQ(Times(w1, w2).Reverse(), Times(rw2, rw1)); } +} + +TYPED_TEST_P(WeightTest, OperatorEqualsIsEquivalenceRelation) { + using Weight = TypeParam; + for (int i = 0; i < absl::GetFlag(FLAGS_repeat); ++i) { + const Weight w1(this->generate_()); + const Weight w2(this->generate_()); + const Weight w3(this->generate_()); - // Tests == is an equivalence relation. - void TestEquality(Weight w1, Weight w2, Weight w3) { // Checks reflexivity. EXPECT_EQ(w1, w1); @@ -213,33 +237,60 @@ class WeightTester { EXPECT_NE(w1, w2); } } +} + +TYPED_TEST_P(WeightTest, BinaryIO) { + using Weight = TypeParam; + for (int i = 0; i < absl::GetFlag(FLAGS_repeat); ++i) { + const Weight w(this->generate_()); + std::ostringstream os; + w.Write(os); + os.flush(); + std::istringstream is(os.str()); + Weight v; + v.Read(is); + EXPECT_EQ(w, v); + } +} + +TYPED_TEST_P(WeightTest, TextIOWithParens) { + absl::SetFlag(&FLAGS_fst_weight_parentheses, "()"); + + using Weight = TypeParam; + for (int i = 0; i < absl::GetFlag(FLAGS_repeat); ++i) { + const Weight w(this->generate_()); + std::ostringstream os; + os << w; + std::istringstream is(os.str()); + Weight v(Weight::One()); + is >> v; + EXPECT_TRUE(ApproxEqual(w, v)); + } +} - // 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); - EXPECT_EQ(w, v); - } +TYPED_TEST_P(WeightTest, TextIOWithoutParens) { + using Weight = TypeParam; + if (WeightTestTraits::IoRequiresParens()) { + GTEST_SKIP() << "Parens required for I/O"; + } - // Tests textual I/O. - { - std::ostringstream os; - os << w; - std::istringstream is(os.str()); - Weight v(Weight::One()); - is >> v; - EXPECT_TRUE(ApproxEqual(w, v)); - } + absl::SetFlag(&FLAGS_fst_weight_parentheses, ""); + + for (int i = 0; i < absl::GetFlag(FLAGS_repeat); ++i) { + const Weight w(this->generate_()); + std::ostringstream os; + os << w; + std::istringstream is(os.str()); + Weight v(Weight::One()); + is >> v; + EXPECT_TRUE(ApproxEqual(w, v)); } +} - // Tests copy constructor and assignment operator - void TestCopy(Weight w) { +TYPED_TEST_P(WeightTest, Copy) { + using Weight = TypeParam; + for (int i = 0; i < absl::GetFlag(FLAGS_repeat); ++i) { + const Weight w(this->generate_()); Weight x = w; EXPECT_EQ(w, x); @@ -249,10 +300,11 @@ class WeightTester { x.operator=(x); EXPECT_EQ(w, x); } +} - // Generates weights used in testing. - WeightGenerator weight_generator_; -}; +REGISTER_TYPED_TEST_SUITE_P(WeightTest, Semiring, Division, Reverse, + OperatorEqualsIsEquivalenceRelation, BinaryIO, + TextIOWithParens, TextIOWithoutParens, Copy); } // namespace fst diff --git a/openfst/test/weight_test.cc b/openfst/test/weight_test.cc index 6fb55a31..4b42b3d4 100644 --- a/openfst/test/weight_test.cc +++ b/openfst/test/weight_test.cc @@ -43,29 +43,22 @@ ABSL_FLAG(uint64_t, seed, 403, "random seed"); ABSL_FLAG(int32_t, repeat, 10000, "number of test repetitions"); namespace fst { -namespace { +// If this test fails, it is possible that x == x will not +// hold for FloatWeight, breaking NaturalLess and probably more. +// To trigger these failures, use g++ -O -m32 -mno-sse. template -void TestTemplatedWeights(uint64_t seed, int repeat) { - WeightGenerate> tropical_generate(seed); - WeightTester> tropical_tester(tropical_generate); - tropical_tester.Test(repeat); - - WeightGenerate> log_generate(seed); - WeightTester> log_tester(log_generate); - log_tester.Test(repeat); - - WeightGenerate> real_generate(seed); - WeightTester> real_tester(real_generate); - real_tester.Test(repeat); - - WeightGenerate> minmax_generate(seed, true); - WeightTester> minmax_tester(minmax_generate); - minmax_tester.Test(repeat); - - WeightGenerate> signedlog_generate(seed, true); - WeightTester> signedlog_tester(signedlog_generate); - signedlog_tester.Test(repeat); +bool FloatEqualityIsReflexive(T m) { + // The idea here is that x is spilled to memory, but + // y remains in an 80-bit register with extra precision, + // causing it to compare unequal to x. + volatile T x = 1.111; + x *= m; + + T y = 1.111; + y *= m; + + return x == y; } template @@ -240,23 +233,6 @@ void TestSparsePowerWeightGetSetValue() { EXPECT_EQ(default_value, w.Value(31)); } -// If this test fails, it is possible that x == x will not -// hold for FloatWeight, breaking NaturalLess and probably more. -// To trigger these failures, use g++ -O -m32 -mno-sse. -template -bool FloatEqualityIsReflexive(T m) { - // The idea here is that x is spilled to memory, but - // y remains in an 80-bit register with extra precision, - // causing it to compare unequal to x. - volatile T x = 1.111; - x *= m; - - T y = 1.111; - y *= m; - - return x == y; -} - void TestFloatEqualityIsReflexive() { // Use a volatile test_value to avoid excessive inlining / optimization // breaking what we're trying to test. @@ -265,18 +241,110 @@ void TestFloatEqualityIsReflexive() { EXPECT_TRUE(FloatEqualityIsReflexive(test_value)); } -void RunTest() { - TestTemplatedWeights(absl::GetFlag(FLAGS_seed), - absl::GetFlag(FLAGS_repeat)); - TestTemplatedWeights(absl::GetFlag(FLAGS_seed), - absl::GetFlag(FLAGS_repeat)); - absl::SetFlag(&FLAGS_fst_weight_parentheses, "()"); - TestTemplatedWeights(absl::GetFlag(FLAGS_seed), - absl::GetFlag(FLAGS_repeat)); - TestTemplatedWeights(absl::GetFlag(FLAGS_seed), - absl::GetFlag(FLAGS_repeat)); - absl::SetFlag(&FLAGS_fst_weight_parentheses, ""); +struct UnionWeightOptions { + // These are used, but getting a `-Wunused-local-typedef` false-positive. + // Suppress with `[[maybe_unused]]`. + using Compare [[maybe_unused]] = NaturalLess; + using ReverseOptions [[maybe_unused]] = UnionWeightOptions; + + struct Merge { + TropicalWeight operator()(const TropicalWeight& w1, + const TropicalWeight& w2) const { + return w1; + } + }; +}; + +using LeftStringWeight = StringWeight; +using RightStringWeight = StringWeight; +using IUSetWeight = SetWeight; +using UISetWeight = SetWeight; +using BoolSetWeight = SetWeight; +using TropicalGallicWeight = GallicWeight; +using TropicalGenGallicWeight = GallicWeight; +using TropicalProductWeight = ProductWeight; +using TropicalLexicographicWeight = + LexicographicWeight; +using TropicalCubeWeight = PowerWeight; +using FirstNestedProductWeight = + ProductWeight; +using SecondNestedProductWeight = + ProductWeight; +using NestedProductCubeWeight = PowerWeight; +using SparseNestedProductCubeWeight = + SparsePowerWeight; +using LogSparsePowerWeight = SparsePowerWeight; +using LogLogExpectationWeight = ExpectationWeight; +using RealRealExpectationWeight = ExpectationWeight; +using LogLogSparseExpectationWeight = + ExpectationWeight; +using TropicalUnionWeight = UnionWeight; + +// Trait specializations are only needed if one of the functions needs to +// be changed. +template <> +struct WeightTestTraits { + static WeightGenerate Generator(uint64_t seed) { + // TODO: Document why allow_zero is false. + return WeightGenerate(seed, /*allow_zero=*/false); + } + static bool IoRequiresParens() { return true; } +}; + +template <> +struct WeightTestTraits { + static WeightGenerate Generator(uint64_t seed) { + return WeightGenerate(seed); + } + static bool IoRequiresParens() { return true; } +}; + +template <> +struct WeightTestTraits { + static WeightGenerate Generator(uint64_t seed) { + return WeightGenerate(seed); + } + static bool IoRequiresParens() { return true; } +}; + +template <> +struct WeightTestTraits { + static WeightGenerate Generator( + uint64_t seed) { + return WeightGenerate(seed); + } + static bool IoRequiresParens() { return true; } +}; +template <> +struct WeightTestTraits { + static WeightGenerate Generator(uint64_t seed) { + return WeightGenerate(seed); + } + static bool IoRequiresParens() { return true; } +}; + +using BaseWeights = + ::testing::Types, TropicalWeightTpl, + LogWeightTpl, LogWeightTpl, + RealWeightTpl, RealWeightTpl, + MinMaxWeightTpl, MinMaxWeightTpl, + SignedLogWeightTpl, SignedLogWeightTpl>; + +using SpecialWeights = ::testing::Types< + LeftStringWeight, RightStringWeight, IUSetWeight, UISetWeight, + BoolSetWeight, TropicalGallicWeight, TropicalGenGallicWeight, + TropicalProductWeight, TropicalLexicographicWeight, TropicalCubeWeight, + FirstNestedProductWeight, SecondNestedProductWeight, + NestedProductCubeWeight, SparseNestedProductCubeWeight, + LogSparsePowerWeight, LogLogExpectationWeight, RealRealExpectationWeight, + LogLogSparseExpectationWeight, TropicalUnionWeight>; + +// The extra "," is needed to avoid an empty "..." param in C++17. +INSTANTIATE_TYPED_TEST_SUITE_P(Base, WeightTest, BaseWeights, ); +INSTANTIATE_TYPED_TEST_SUITE_P(Special, WeightTest, SpecialWeights, ); + +TEST(WeightMiscTest, TypeNames) { // Makes sure type names for templated weights are consistent. EXPECT_EQ(TropicalWeight::Type(), "tropical"); EXPECT_NE(TropicalWeightTpl::Type(), @@ -288,10 +356,14 @@ void RunTest() { TropicalWeightTpl w(2.0L); TropicalWeight tw(2.0F); EXPECT_EQ(w.Value(), tw.Value()); +} +// It could be cleaner to split this up, but this test case is very fast. +TEST(WeightMiscTest, FreeFunctions) { TestAdder(1000); TestAdder(1000); TestAdder(1000); + TestSignedAdder(1000); TestWeightConstructorsFromScalar(); @@ -311,38 +383,9 @@ void RunTest() { TestWeightConversion(TropicalWeight(2.0)); - using LeftStringWeight = StringWeight; - WeightGenerate left_string_generate( - absl::GetFlag(FLAGS_seed)); - WeightTester left_string_tester(left_string_generate); - left_string_tester.Test(absl::GetFlag(FLAGS_repeat)); - - using RightStringWeight = StringWeight; - WeightGenerate right_string_generate( - absl::GetFlag(FLAGS_seed)); - WeightTester right_string_tester(right_string_generate); - right_string_tester.Test(absl::GetFlag(FLAGS_repeat)); - - // STRING_RESTRICT not tested since it requires equal strings, - // so would fail. - - using IUSetWeight = SetWeight; WeightGenerate iu_set_generate(absl::GetFlag(FLAGS_seed)); - WeightTester iu_set_tester(iu_set_generate); - iu_set_tester.Test(absl::GetFlag(FLAGS_repeat)); - - using UISetWeight = SetWeight; WeightGenerate ui_set_generate(absl::GetFlag(FLAGS_seed)); - WeightTester ui_set_tester(ui_set_generate); - ui_set_tester.Test(absl::GetFlag(FLAGS_repeat)); - - // SET_INTERSECT_UNION_RESTRICT not tested since it requires equal sets, - // so would fail. - - using BoolSetWeight = SetWeight; WeightGenerate bool_set_generate(absl::GetFlag(FLAGS_seed)); - WeightTester bool_set_tester(bool_set_generate); - bool_set_tester.Test(absl::GetFlag(FLAGS_repeat)); TestWeightConversion(iu_set_generate()); @@ -360,160 +403,15 @@ void RunTest() { TestWeightMove(bool_set_generate()); TestWeightMove(bool_set_generate()); - // COMPOSITE WEIGHTS AND TESTERS - DEFINITIONS - - using TropicalGallicWeight = GallicWeight; - WeightGenerate tropical_gallic_generate( - absl::GetFlag(FLAGS_seed), true); - WeightTester tropical_gallic_tester( - tropical_gallic_generate); - - using TropicalGenGallicWeight = GallicWeight; - WeightGenerate tropical_gen_gallic_generate( - absl::GetFlag(FLAGS_seed), false); - WeightTester tropical_gen_gallic_tester( - tropical_gen_gallic_generate); - - using TropicalProductWeight = ProductWeight; - WeightGenerate tropical_product_generate( - absl::GetFlag(FLAGS_seed)); - WeightTester tropical_product_tester( - tropical_product_generate); - - using TropicalLexicographicWeight = - LexicographicWeight; - WeightGenerate tropical_lexicographic_generate( - absl::GetFlag(FLAGS_seed)); - WeightTester tropical_lexicographic_tester( - tropical_lexicographic_generate); - - using TropicalCubeWeight = PowerWeight; - WeightGenerate tropical_cube_generate( - absl::GetFlag(FLAGS_seed)); - WeightTester tropical_cube_tester(tropical_cube_generate); - - using FirstNestedProductWeight = - ProductWeight; - WeightGenerate first_nested_product_generate( - absl::GetFlag(FLAGS_seed)); - WeightTester first_nested_product_tester( - first_nested_product_generate); - - using SecondNestedProductWeight = - ProductWeight; - WeightGenerate second_nested_product_generate( - absl::GetFlag(FLAGS_seed)); - WeightTester second_nested_product_tester( - second_nested_product_generate); - - using NestedProductCubeWeight = PowerWeight; - WeightGenerate nested_product_cube_generate( - absl::GetFlag(FLAGS_seed)); - WeightTester nested_product_cube_tester( - nested_product_cube_generate); - - using SparseNestedProductCubeWeight = - SparsePowerWeight; - WeightGenerate - sparse_nested_product_cube_generate(absl::GetFlag(FLAGS_seed)); - WeightTester sparse_nested_product_cube_tester( - sparse_nested_product_cube_generate); - - using LogSparsePowerWeight = SparsePowerWeight; - WeightGenerate log_sparse_power_generate( - absl::GetFlag(FLAGS_seed)); - WeightTester log_sparse_power_tester( - log_sparse_power_generate); - - using LogLogExpectationWeight = ExpectationWeight; - WeightGenerate log_log_expectation_generate( - absl::GetFlag(FLAGS_seed)); - WeightTester log_log_expectation_tester( - log_log_expectation_generate); - - using RealRealExpectationWeight = ExpectationWeight; - WeightGenerate real_real_expectation_generate( - absl::GetFlag(FLAGS_seed)); - WeightTester real_real_expectation_tester( - real_real_expectation_generate); - - using LogLogSparseExpectationWeight = - ExpectationWeight; - WeightGenerate - log_log_sparse_expectation_generate(absl::GetFlag(FLAGS_seed)); - WeightTester log_log_sparse_expectation_tester( - log_log_sparse_expectation_generate); - - struct UnionWeightOptions { - // These are used, but getting a `-Wunused-local-typedef` false-positive. - // Suppress with `[[maybe_unused]]`. - using Compare [[maybe_unused]] = NaturalLess; - using ReverseOptions [[maybe_unused]] = UnionWeightOptions; - - struct Merge { - TropicalWeight operator()(const TropicalWeight& w1, - const TropicalWeight& w2) const { - return w1; - } - }; - }; - - using TropicalUnionWeight = UnionWeight; - WeightGenerate tropical_union_generate( - absl::GetFlag(FLAGS_seed)); - WeightTester tropical_union_tester( - tropical_union_generate); - - // COMPOSITE WEIGHTS AND TESTERS - TESTING - - // Tests composite weight I/O with parentheses. - absl::SetFlag(&FLAGS_fst_weight_parentheses, "()"); - - // Unnested composite. - tropical_gallic_tester.Test(absl::GetFlag(FLAGS_repeat)); - tropical_gen_gallic_tester.Test(absl::GetFlag(FLAGS_repeat)); - tropical_product_tester.Test(absl::GetFlag(FLAGS_repeat)); - tropical_lexicographic_tester.Test(absl::GetFlag(FLAGS_repeat)); - tropical_cube_tester.Test(absl::GetFlag(FLAGS_repeat)); - log_sparse_power_tester.Test(absl::GetFlag(FLAGS_repeat)); - log_log_expectation_tester.Test(absl::GetFlag(FLAGS_repeat)); - real_real_expectation_tester.Test(absl::GetFlag(FLAGS_repeat)); - tropical_union_tester.Test(absl::GetFlag(FLAGS_repeat)); - - // Nested composite. - first_nested_product_tester.Test(absl::GetFlag(FLAGS_repeat)); - second_nested_product_tester.Test(absl::GetFlag(FLAGS_repeat)); - nested_product_cube_tester.Test(absl::GetFlag(FLAGS_repeat)); - sparse_nested_product_cube_tester.Test(absl::GetFlag(FLAGS_repeat)); - log_log_sparse_expectation_tester.Test(absl::GetFlag(FLAGS_repeat)); - - // ... and tests composite weight I/O without parentheses. - absl::SetFlag(&FLAGS_fst_weight_parentheses, ""); - - // Unnested composite. - tropical_gallic_tester.Test(absl::GetFlag(FLAGS_repeat)); - tropical_product_tester.Test(absl::GetFlag(FLAGS_repeat)); - tropical_lexicographic_tester.Test(absl::GetFlag(FLAGS_repeat)); - tropical_cube_tester.Test(absl::GetFlag(FLAGS_repeat)); - log_sparse_power_tester.Test(absl::GetFlag(FLAGS_repeat)); - log_log_expectation_tester.Test(absl::GetFlag(FLAGS_repeat)); - tropical_union_tester.Test(absl::GetFlag(FLAGS_repeat)); - - // Nested composite. - second_nested_product_tester.Test(absl::GetFlag(FLAGS_repeat)); - log_log_sparse_expectation_tester.Test(absl::GetFlag(FLAGS_repeat)); - TestPowerWeightGetSetValue(); TestSparsePowerWeightGetSetValue(); TestFloatEqualityIsReflexive(); } -} // namespace } // namespace fst int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); - fst::RunTest(); - return 0; + return RUN_ALL_TESTS(); }