Added a performance test

This commit is contained in:
Pascal Serrarens 2024-12-26 10:03:47 +01:00
parent 2c57c3f6c0
commit a7aa679494

View File

@ -1,7 +1,7 @@
#if GTEST #if GTEST
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <math.h>
#include <limits> #include <limits>
#include <math.h>
#include "Polar.h" #include "Polar.h"
#include "Spherical.h" #include "Spherical.h"
@ -173,4 +173,59 @@ TEST(Polar, Rotate) {
EXPECT_FLOAT_EQ(r.angle.InDegrees(), 90.0f) << "Rotate(4 45, 45)"; 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<PolarOf<float>> 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<float>(rand() % 100); // Random distance from 0 to 100
AngleOf<float> angle = AngleOf<float>::Degrees(
static_cast<float>(rand() % 360)); // Random angle from 0 to 360 degrees
PolarOf<float> p = PolarOf<float>(distance, angle);
polarObjects.emplace_back(p); // Create and store the object
}
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> 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<float> p1(0.0f, AngleOf<float>::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<float> p2(-10.0f, AngleOf<float>::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<float> p3(100.0f, AngleOf<float>::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 #endif