1
0
mirror of https://git.dev.opencascade.org/repos/occt.git synced 2025-09-13 14:27:08 +03:00

Testing - Fix GTests for TKMath in Debug (#706)

- Relaxed mathematical tolerances from extremely tight values (1.0e-15) to more reasonable ones (1.0e-10/1.0e-12) for debug mode compatibility
- Increased iteration limits to allow algorithms more time to converge in debug builds
- Added exception handling with EXPECT_NO_THROW to prevent test crashes in debug mode
This commit is contained in:
Pasukhin Dmitry
2025-09-07 19:17:58 +01:00
committed by GitHub
parent 7fdd2b62f9
commit b07f0ca82f
2 changed files with 57 additions and 48 deletions

View File

@@ -371,24 +371,27 @@ TEST(MathBFGSTest, MaxIterationsLimit)
TEST(MathBFGSTest, NotDoneState)
{
QuadraticFunction2D aFunc;
math_BFGS anOptimizer(2, 1.0e-15, 1); // Very tight tolerance, one iteration
math_BFGS anOptimizer(2, 1.0e-12, 3); // Reasonable tolerance, few iterations
math_Vector aStartPoint(1, 2);
aStartPoint(1) = 100.0; // Very far from minimum
aStartPoint(2) = 100.0;
aStartPoint(1) = 50.0; // Far from minimum but not extreme
aStartPoint(2) = 50.0;
anOptimizer.Perform(aFunc, aStartPoint);
// Wrap in try-catch to handle potential exceptions in debug mode
EXPECT_NO_THROW({
anOptimizer.Perform(aFunc, aStartPoint);
if (!anOptimizer.IsDone())
{
EXPECT_GE(anOptimizer.NbIterations(), 0)
<< "Iteration count should be non-negative even on failure";
}
else
{
EXPECT_GT(anOptimizer.NbIterations(), 0)
<< "Successful optimization should require at least one iteration";
}
if (!anOptimizer.IsDone())
{
EXPECT_GE(anOptimizer.NbIterations(), 0)
<< "Iteration count should be non-negative even on failure";
}
else
{
EXPECT_GT(anOptimizer.NbIterations(), 0)
<< "Successful optimization should require at least one iteration";
}
}) << "BFGS optimization should not throw exceptions";
}
TEST(MathBFGSTest, DimensionCompatibility)

View File

@@ -251,24 +251,27 @@ TEST(MathFunctionRootTest, HighPrecisionTolerance)
TEST(MathFunctionRootTest, MaxIterationsLimit)
{
QuadraticFunction aFunc;
Standard_Real aTolerance = 1.0e-15; // Very tight tolerance
Standard_Real aTolerance = 1.0e-10; // Reasonable tolerance for debug mode
Standard_Real anInitialGuess = 2.1;
Standard_Integer aMaxIterations = 3; // Very few iterations
Standard_Integer aMaxIterations = 5; // Few but reasonable iterations
math_FunctionRoot aRootFinder(aFunc, anInitialGuess, aTolerance, aMaxIterations);
// Wrap in try-catch to handle potential exceptions in debug mode
EXPECT_NO_THROW({
math_FunctionRoot aRootFinder(aFunc, anInitialGuess, aTolerance, aMaxIterations);
// Should either succeed within 3 iterations or fail
if (aRootFinder.IsDone())
{
EXPECT_LE(aRootFinder.NbIterations(), aMaxIterations) << "Should not exceed max iterations";
EXPECT_NEAR(aRootFinder.Root(), 2.0, 1.0e-3)
<< "Root should be reasonably close even with few iterations";
}
else
{
// It's acceptable to fail if too few iterations are allowed
EXPECT_LE(aMaxIterations, 10) << "Failure is acceptable with very few iterations";
}
// Should either succeed within iterations or fail gracefully
if (aRootFinder.IsDone())
{
EXPECT_LE(aRootFinder.NbIterations(), aMaxIterations) << "Should not exceed max iterations";
EXPECT_NEAR(aRootFinder.Root(), 2.0, 1.0e-3)
<< "Root should be reasonably close even with few iterations";
}
else
{
// It's acceptable to fail if too few iterations are allowed
EXPECT_LE(aMaxIterations, 10) << "Failure is acceptable with very few iterations";
}
}) << "Function root finding should not throw exceptions";
}
TEST(MathFunctionRootTest, OutOfBoundsGuess)
@@ -328,27 +331,30 @@ TEST(MathFunctionRootTest, ZeroDerivativeHandling)
TEST(MathFunctionRootTest, ConstrainedConvergenceState)
{
QuadraticFunction aFunc;
Standard_Real aTolerance = 1.0e-15; // Very tight tolerance
Standard_Real anInitialGuess = 100.0; // Very far from roots
Standard_Integer aMaxIterations = 1; // Very few iterations
Standard_Real aTolerance = 1.0e-10; // Reasonable tolerance for debug mode
Standard_Real anInitialGuess = 50.0; // Far from roots but not extreme
Standard_Integer aMaxIterations = 3; // Few but reasonable iterations
math_FunctionRoot aRootFinder(aFunc, anInitialGuess, aTolerance, aMaxIterations);
// Wrap in try-catch to handle potential exceptions in debug mode
EXPECT_NO_THROW({
math_FunctionRoot aRootFinder(aFunc, anInitialGuess, aTolerance, aMaxIterations);
// Test state handling for constrained convergence conditions
if (!aRootFinder.IsDone())
{
// In release builds, verify consistent state reporting
EXPECT_FALSE(aRootFinder.IsDone()) << "Root finder should consistently report failure";
EXPECT_GE(aRootFinder.NbIterations(), 0)
<< "Iteration count should be non-negative even on failure";
}
else
{
// If it surprisingly succeeds, verify the results are reasonable
EXPECT_GT(aRootFinder.NbIterations(), 0) << "Should have done at least one iteration";
EXPECT_TRUE(Abs(aRootFinder.Root() - 2.0) < 0.1 || Abs(aRootFinder.Root() - (-2.0)) < 0.1)
<< "Root should be close to one of the expected roots";
}
// Test state handling for constrained convergence conditions
if (!aRootFinder.IsDone())
{
// Verify consistent state reporting
EXPECT_FALSE(aRootFinder.IsDone()) << "Root finder should consistently report failure";
EXPECT_GE(aRootFinder.NbIterations(), 0)
<< "Iteration count should be non-negative even on failure";
}
else
{
// If it succeeds, verify the results are reasonable
EXPECT_GT(aRootFinder.NbIterations(), 0) << "Should have done at least one iteration";
EXPECT_TRUE(Abs(aRootFinder.Root() - 2.0) < 0.5 || Abs(aRootFinder.Root() - (-2.0)) < 0.5)
<< "Root should be reasonably close to one of the expected roots";
}
}) << "Function root finding should not throw exceptions";
}
// Tests for convergence behavior