//
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
//
// Copyright (c) 2009-2016 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Solvers;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.StopCriterion
{
///
/// Iteration count stop criterion tests.
///
[TestFixture, Category("LASolver")]
public sealed class IterationCountStopCriterionTest
{
///
/// Create with illegal minimum iterations throws ArgumentOutOfRangeException.
///
[Test]
public void CreateWithIllegalMinimumIterationsThrowsArgumentOutOfRangeException()
{
Assert.That(() => new IterationCountStopCriterion(-1), Throws.TypeOf());
}
///
/// Can create.
///
[Test]
public void Create()
{
var criterion = new IterationCountStopCriterion(10);
Assert.IsNotNull(criterion, "A criterion should have been created");
}
///
/// Can reset maximum iterations.
///
[Test]
public void ResetMaximumIterations()
{
var criterion = new IterationCountStopCriterion(10);
Assert.IsNotNull(criterion, "A criterion should have been created");
Assert.AreEqual(10, criterion.MaximumNumberOfIterations, "Incorrect maximum number of iterations");
criterion.ResetMaximumNumberOfIterationsToDefault();
Assert.AreNotEqual(10, criterion.MaximumNumberOfIterations, "Should have reset");
Assert.AreEqual(IterationCountStopCriterion.DefaultMaximumNumberOfIterations, criterion.MaximumNumberOfIterations, "Reset to the wrong value");
}
///
/// Determine status with illegal iteration number throws ArgumentOutOfRangeException.
///
[Test]
public void DetermineStatusWithIllegalIterationNumberThrowsArgumentOutOfRangeException()
{
var criterion = new IterationCountStopCriterion(10);
Assert.IsNotNull(criterion, "A criterion should have been created");
Assert.That(() => criterion.DetermineStatus(-1, Vector.Build.Dense(3, 1), Vector.Build.Dense(3, 2), Vector.Build.Dense(3, 3)), Throws.TypeOf());
}
///
/// Can determine status.
///
[Test]
public void DetermineStatus()
{
var criterion = new IterationCountStopCriterion(10);
Assert.IsNotNull(criterion, "A criterion should have been created");
var status = criterion.DetermineStatus(5, Vector.Build.Dense(3, 1), Vector.Build.Dense(3, 2), Vector.Build.Dense(3, 3));
Assert.AreEqual(IterationStatus.Continue, status, "Should be running");
var status2 = criterion.DetermineStatus(10, Vector.Build.Dense(3, 1), Vector.Build.Dense(3, 2), Vector.Build.Dense(3, 3));
Assert.AreEqual(IterationStatus.StoppedWithoutConvergence, status2, "Should be finished");
}
///
/// Can reset calculation state.
///
[Test]
public void ResetCalculationState()
{
var criterion = new IterationCountStopCriterion(10);
Assert.IsNotNull(criterion, "A criterion should have been created");
var status = criterion.DetermineStatus(5, Vector.Build.Dense(3, 1), Vector.Build.Dense(3, 2), Vector.Build.Dense(3, 3));
Assert.AreEqual(IterationStatus.Continue, status, "Should be running");
criterion.Reset();
Assert.AreEqual(IterationStatus.Continue, criterion.Status, "Should not have started");
}
///
/// Can clone a stop criterion.
///
[Test]
public void Clone()
{
var criterion = new IterationCountStopCriterion(10);
Assert.IsNotNull(criterion, "A criterion should have been created");
Assert.AreEqual(10, criterion.MaximumNumberOfIterations, "Incorrect maximum");
var clone = criterion.Clone();
Assert.IsInstanceOf(typeof (IterationCountStopCriterion), clone, "Wrong criterion type");
var clonedCriterion = clone as IterationCountStopCriterion;
Assert.IsNotNull(clonedCriterion);
// ReSharper disable once PossibleNullReferenceException
Assert.AreEqual(criterion.MaximumNumberOfIterations, clonedCriterion.MaximumNumberOfIterations, "Clone failed");
}
}
}