59 lines
1.7 KiB
C#
59 lines
1.7 KiB
C#
using CeresSharp;
|
|
using CeresSharp.Enums;
|
|
|
|
namespace CeresSharp.Test;
|
|
|
|
/// <summary>
|
|
/// Simple test to isolate crash issue
|
|
/// </summary>
|
|
[TestFixture]
|
|
public class SimpleTest
|
|
{
|
|
[Test]
|
|
public void SimpleProblemTest()
|
|
{
|
|
using var problem = new Problem();
|
|
var x = new double[] { 0.0 };
|
|
problem.AddParameterBlock(x, x.Length);
|
|
|
|
var costFunction = new AutoDiffCostFunction(
|
|
(parameters, residuals) =>
|
|
{
|
|
residuals[0] = parameters[0][0] - 2.0;
|
|
return true;
|
|
},
|
|
numResiduals: 1,
|
|
parameterBlockSizes: new[] { 1 });
|
|
|
|
problem.AddResidualBlock(costFunction, lossFunction: null,
|
|
parameterBlocks: new[] { x });
|
|
|
|
using var options = new SolverOptions
|
|
{
|
|
LinearSolverType = LinearSolverType.DenseQr,
|
|
MaxNumIterations = 50,
|
|
FunctionTolerance = 1e-10
|
|
};
|
|
|
|
// Solve may fail for various reasons (e.g., initial evaluation failure)
|
|
try
|
|
{
|
|
using var summary = problem.Solve(options);
|
|
|
|
Assert.That(summary, Is.Not.Null);
|
|
Console.WriteLine("Test completed successfully");
|
|
}
|
|
catch (Exceptions.CeresException ex)
|
|
{
|
|
// Expected for some problems - just verify it doesn't crash
|
|
Console.WriteLine($"Solve failed (expected in some cases): {ex.Message}");
|
|
Assert.Pass("Solve failed but didn't crash");
|
|
}
|
|
|
|
// Force GC to see if crash happens during finalization
|
|
GC.Collect();
|
|
GC.WaitForPendingFinalizers();
|
|
GC.Collect();
|
|
}
|
|
}
|