aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2024-03-08 20:47:02 +0100
committerGitHub <noreply@github.com>2024-03-08 20:47:02 +0100
commit851d1211b453f3fd644971ce2025a4fc039fd2ce (patch)
treec0148c349b49eb0743ec7d7ff86e1042915e1cc2
parentc5dfea53336083c4d58c7663fb1a96ce00a8be7e (diff)
parentb7197a650077f28c1c3972d409b28d5704d3dbe2 (diff)
Merge pull request #30532 from vespa-engine/toregge/rewrite-random-unit-test-to-gtest
Rewrite random unit test to gtest.
-rw-r--r--vespalib/src/tests/random/CMakeLists.txt1
-rw-r--r--vespalib/src/tests/random/random_test.cpp63
2 files changed, 19 insertions, 45 deletions
diff --git a/vespalib/src/tests/random/CMakeLists.txt b/vespalib/src/tests/random/CMakeLists.txt
index 9984f3a52ba..75d9ce75f1a 100644
--- a/vespalib/src/tests/random/CMakeLists.txt
+++ b/vespalib/src/tests/random/CMakeLists.txt
@@ -4,6 +4,7 @@ vespa_add_executable(vespalib_random_test_app TEST
random_test.cpp
DEPENDS
vespalib
+ GTest::gtest
)
vespa_add_test(NAME vespalib_random_test_app COMMAND vespalib_random_test_app)
vespa_add_executable(vespalib_friendfinder_test_app
diff --git a/vespalib/src/tests/random/random_test.cpp b/vespalib/src/tests/random/random_test.cpp
index d505d51efd7..34b4e3d0389 100644
--- a/vespalib/src/tests/random/random_test.cpp
+++ b/vespalib/src/tests/random/random_test.cpp
@@ -1,30 +1,17 @@
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-#include <vespa/vespalib/testkit/testapp.h>
+#include <vespa/vespalib/gtest/gtest.h>
#include <vespa/vespalib/util/random.h>
using namespace vespalib;
+namespace {
+
+constexpr double eps = 1.0e-9;
-class Test : public vespalib::TestApp
-{
-public:
- int getFive() { return 5; }
- void testJavaCompatibility();
- void testFloatingPoint();
- void testNormalDistribution();
- int Main() override;
-};
-
-bool eqD(double a, double b) {
- if (a == b) return true;
- printf("is %f approx equal to %f ?\n", a, b);
- if ((a + 1.0e-9) > b && b > (a - 1.0e-9)) return true;
- return false;
}
-void
-Test::testJavaCompatibility()
+TEST(RandomTest, test_java_compatibility)
{
RandomGen rnd(1);
@@ -42,16 +29,16 @@ Test::testJavaCompatibility()
EXPECT_TRUE(rnd.nextInt32() == 655996946);
rnd.setSeed(1);
- EXPECT_TRUE(eqD(rnd.nextDouble(), 0.7308781907032909));
- EXPECT_TRUE(eqD(rnd.nextDouble(), 0.41008081149220166));
- EXPECT_TRUE(eqD(rnd.nextDouble(), 0.20771484130971707));
- EXPECT_TRUE(eqD(rnd.nextDouble(), 0.3327170559595112));
- EXPECT_TRUE(eqD(rnd.nextDouble(), 0.9677559094241207));
- EXPECT_TRUE(eqD(rnd.nextDouble(), 0.006117182265761301));
- EXPECT_TRUE(eqD(rnd.nextDouble(), 0.9637047970232077));
- EXPECT_TRUE(eqD(rnd.nextDouble(), 0.9398653887819098));
- EXPECT_TRUE(eqD(rnd.nextDouble(), 0.9471949176631939));
- EXPECT_TRUE(eqD(rnd.nextDouble(), 0.9370821488959696));
+ EXPECT_NEAR(rnd.nextDouble(), 0.7308781907032909, eps);
+ EXPECT_NEAR(rnd.nextDouble(), 0.41008081149220166, eps);
+ EXPECT_NEAR(rnd.nextDouble(), 0.20771484130971707, eps);
+ EXPECT_NEAR(rnd.nextDouble(), 0.3327170559595112, eps);
+ EXPECT_NEAR(rnd.nextDouble(), 0.9677559094241207, eps);
+ EXPECT_NEAR(rnd.nextDouble(), 0.006117182265761301, eps);
+ EXPECT_NEAR(rnd.nextDouble(), 0.9637047970232077, eps);
+ EXPECT_NEAR(rnd.nextDouble(), 0.9398653887819098, eps);
+ EXPECT_NEAR(rnd.nextDouble(), 0.9471949176631939, eps);
+ EXPECT_NEAR(rnd.nextDouble(), 0.9370821488959696, eps);
RandomGen rnd2(-1);
EXPECT_TRUE(rnd2.nextInt32() == 1155099827);
@@ -64,8 +51,7 @@ Test::testJavaCompatibility()
EXPECT_TRUE(rnd2.nextInt32() == 52699159);
}
-void
-Test::testFloatingPoint()
+TEST(RandoMTest, test_floating_point)
{
RandomGen rnd;
@@ -88,8 +74,7 @@ Test::testFloatingPoint()
}
}
-void
-Test::testNormalDistribution()
+TEST(RandoMTest, test_normal_distribution)
{
RandomGen rnd;
@@ -149,16 +134,4 @@ Test::testNormalDistribution()
EXPECT_TRUE(buckets[90] > buckets[100]);
}
-int
-Test::Main()
-{
- TEST_INIT("random_test");
- testJavaCompatibility();
- TEST_FLUSH();
- testFloatingPoint();
- TEST_FLUSH();
- testNormalDistribution();
- TEST_DONE();
-}
-
-TEST_APPHOOK(Test)
+GTEST_MAIN_RUN_ALL_TESTS()