From a7aa679494a28e6a9f527ee39ad073efcb118ccf Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Thu, 26 Dec 2024 10:03:47 +0100 Subject: [PATCH] Added a performance test --- test/Polar_test.cc | 57 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/test/Polar_test.cc b/test/Polar_test.cc index a470593..0586108 100644 --- a/test/Polar_test.cc +++ b/test/Polar_test.cc @@ -1,7 +1,7 @@ #if GTEST #include -#include #include +#include #include "Polar.h" #include "Spherical.h" @@ -173,4 +173,59 @@ TEST(Polar, Rotate) { EXPECT_FLOAT_EQ(r.angle.InDegrees(), 90.0f) << "Rotate(4 45, 45)"; } +// Performance Test +TEST(PolarOfTest, PerformanceTest) { + const int numIterations = 1000000; // Number of instances to test + std::vector> polarObjects; + + // Measure time for creating a large number of PolarOf objects + auto start = std::chrono::high_resolution_clock::now(); + + for (int i = 0; i < numIterations; ++i) { + float distance = + static_cast(rand() % 100); // Random distance from 0 to 100 + AngleOf angle = AngleOf::Degrees( + static_cast(rand() % 360)); // Random angle from 0 to 360 degrees + PolarOf p = PolarOf(distance, angle); + polarObjects.emplace_back(p); // Create and store the object + } + + auto end = std::chrono::high_resolution_clock::now(); + std::chrono::duration duration = end - start; + std::cout << "Time to construct " << numIterations + << " PolarOf objects: " << duration.count() << " seconds." + << std::endl; + + // Test completion with a message + ASSERT_GE(duration.count(), 0); // Ensure duration is non-negative + + // Assert that the duration is less than or equal to 1 second + ASSERT_LE(duration.count(), 1.0) + << "Performance test failed: Construction took longer than 1 second."; +} + +// Edge Case 1: Testing with distance = 0 and angle = 45 +TEST(PolarOfTest, TestDistanceZero) { + PolarOf p1(0.0f, AngleOf::Degrees(45.0f)); + EXPECT_EQ(p1.distance, 0.0f); // Ensure distance is 0 + EXPECT_EQ(p1.angle.InDegrees(), 0.0f); // Ensure angle is 0 when distance is 0 +} + +// Edge Case 2: Testing with negative distance, angle should be adjusted +// TEST(PolarOfTest, TestNegativeDistance) { +// PolarOf p2(-10.0f, AngleOf::Degrees(90.0f)); +// EXPECT_EQ(p2.distance, 10.0f); // Ensure distance is positive +// EXPECT_NEAR(p2.angle.InDegrees(), 270.0f, +// 0.0001f); // Ensure angle is normalized to 270 degrees (180 + +// 90) +// } + +// Edge Case 3: Testing with positive distance and angle = 180 +// TEST(PolarOfTest, TestPositiveDistance) { +// PolarOf p3(100.0f, AngleOf::Degrees(180.0f)); +// EXPECT_EQ(p3.distance, 100.0f); // Ensure distance is correct +// EXPECT_NEAR(p3.angle.InDegrees(), 180.0f, 0.0001f); // Ensure angle is +// correct +// } + #endif \ No newline at end of file