using System; using System.Collections.Generic; using System.Globalization; using Amphora; using Azimuth; using UnitTestSharp; namespace Prism.Glass.UnitTests { public class FunctionModelTests : TestFixture { /// /// Up from 0 at X 0 to 1 at X 0.5 and back down to 0 at X 1, joined by straight lines. /// public static FunctionModel Ramp() { return new FunctionModel(new PiecewiseLinear((0, 0), (0.5, 1), (1, 0))); } public static FunctionModel SmoothRamp() { return new FunctionModel(new MonotoneSpline((0, 0), (0.5, 1), (1, 0))); } /// /// Straight lines up to a peak so tall that no smooth line through the three points has a slope a number can /// hold. /// public static FunctionModel TooTallForSmooth() { return new FunctionModel(new PiecewiseLinear((0, 0), (0.5, 7e307), (1, 0))); } /// /// A smooth line over a tall hill at X 0.2, which has a slope only while no point is at X 0.5. /// public static FunctionModel TallHill() { return new FunctionModel(new MonotoneSpline((0, 0), (0.2, 7e307), (1, 0))); } /// /// A smooth line from 0 up to a height where a point at X 0.5 and near the top has no slope. /// public static FunctionModel TallLine() { return new FunctionModel(new MonotoneSpline((0, 0), (1, 8e307))); } public static string Xs(FunctionModel model) { var returnMe = new List(); for (int i = 0; i < model.PointCount; ++i) { returnMe.Add(model.GetX(i).Value.ToString(CultureInfo.InvariantCulture)); } return string.Join(" ", returnMe); } public static string Ys(FunctionModel model) { var returnMe = new List(); for (int i = 0; i < model.PointCount; ++i) { returnMe.Add(model.GetY(i).Value.ToString(CultureInfo.InvariantCulture)); } return string.Join(" ", returnMe); } /// /// Counts the Changed and SelectionChanged events a model raises. /// public class Listener { public int Changes; public int SelectionChanges; public int LastSelection = -1; public Listener(FunctionModel model) { model.Changed += _ => ++Changes; model.SelectionChanged += index => { ++SelectionChanges; LastSelection = index; }; } } public class CtorTests : TestFixture { public void WithoutAFunction_Throws() { CheckThrow(typeof(ArgumentNullException)); new FunctionModel(null); } public void AFunctionMadeOfCode_Throws() { CheckThrow(typeof(ArgumentException)); new FunctionModel(new CallbackFunction(x => x)); } public void TakesThePointsOfALinearFunction() { FunctionModel model = Ramp(); CheckEqual("0 0.5 1", Xs(model)); CheckEqual("0 1 0", Ys(model)); CheckEqual(FunctionMode.Linear, model.Mode); } public void TakesThePointsOfASmoothFunction() { FunctionModel model = SmoothRamp(); CheckEqual("0 0.5 1", Xs(model)); CheckEqual("0 1 0", Ys(model)); CheckEqual(FunctionMode.Smooth, model.Mode); } public void TakesThePointsOfAStepFunction() { var model = new FunctionModel(new PiecewiseStep((0, 2), (0.25, 3))); CheckEqual("0 0.25", Xs(model)); CheckEqual("2 3", Ys(model)); CheckEqual(FunctionMode.Step, model.Mode); } public void SelectsTheFirstPoint() { CheckEqual(0, Ramp().SelectedIndex); } public void HasTheFunctionItWasGiven() { var function = new PiecewiseLinear((0, 0), (1, 1)); CheckTrue(ReferenceEquals(function, new FunctionModel(function).Function)); } } public class LoadTests : TestFixture { public void WithoutAFunction_Throws() { CheckThrow(typeof(ArgumentNullException)); Ramp().Load(null); } public void AFunctionMadeOfCode_ThrowsAndKeepsWhatTheModelHad() { FunctionModel model = Ramp(); var listener = new Listener(model); bool threw = false; try { model.Load(new CallbackFunction(x => x)); } catch (ArgumentException) { threw = true; } CheckTrue(threw); CheckEqual("0 0.5 1", Xs(model)); CheckEqual(0, listener.Changes); } public void TakesThePointsAndModeOfTheFunction() { FunctionModel model = Ramp(); model.Load(new PiecewiseStep((0, 2), (0.25, 3))); CheckEqual("0 0.25", Xs(model)); CheckEqual("2 3", Ys(model)); CheckEqual(FunctionMode.Step, model.Mode); } public void HasTheFunctionItWasGiven() { FunctionModel model = Ramp(); var function = new PiecewiseLinear((0, 0), (1, 1)); model.Load(function); CheckTrue(ReferenceEquals(function, model.Function)); } public void SelectsTheFirstPoint() { FunctionModel model = Ramp(); model.Select(2); model.Load(new PiecewiseLinear((0, 0), (1, 1))); CheckEqual(0, model.SelectedIndex); } public void RaisesChangedWithTheFunctionThenSelectionChanged() { FunctionModel model = Ramp(); var order = new List(); var function = new PiecewiseLinear((0, 0), (1, 1)); PointFunction told = null; model.Changed += changed => { told = changed; order.Add("changed"); }; model.SelectionChanged += _ => order.Add("selection"); model.Load(function); CheckEqual("changed selection", string.Join(" ", order)); CheckTrue(ReferenceEquals(function, told)); } public void RaisesBothEvenForTheFunctionItAlreadyHas() { FunctionModel model = Ramp(); var listener = new Listener(model); model.Load(model.Function); CheckEqual(1, listener.Changes); CheckEqual(1, listener.SelectionChanges); CheckEqual(0, listener.LastSelection); } } public class TrySetModeTests : TestFixture { public void ToStep_BuildsAStepFunction() { FunctionModel model = Ramp(); var listener = new Listener(model); CheckTrue(model.SetMode(FunctionMode.Step).IsOk); CheckEqual(FunctionMode.Step, model.Mode); CheckTrue(model.Function is PiecewiseStep); CheckEqual(0.0, model.Function.Evaluate(0.25).Value); CheckEqual(1, listener.Changes); } public void ToSmooth_BuildsASmoothFunction() { FunctionModel model = Ramp(); CheckTrue(model.SetMode(FunctionMode.Smooth).IsOk); CheckEqual(FunctionMode.Smooth, model.Mode); CheckTrue(model.Function is MonotoneSpline); CheckEqual(1.0, model.Function.Evaluate(0.5).Value); } public void ToLinear_BuildsALinearFunction() { FunctionModel model = SmoothRamp(); CheckTrue(model.SetMode(FunctionMode.Linear).IsOk); CheckTrue(model.Function is PiecewiseLinear); CheckEqual(0.5, model.Function.Evaluate(0.25).Value); } public void TheSameMode_ChangesNothing() { FunctionModel model = Ramp(); PointFunction before = model.Function; var listener = new Listener(model); CheckTrue(model.SetMode(FunctionMode.Linear).IsOk); CheckEqual(0, listener.Changes); CheckTrue(ReferenceEquals(before, model.Function)); } public void ToSmoothWithPointsThatShareAnX_KeepsTheJump() { var model = new FunctionModel(new PiecewiseLinear((0, 0), (0.5, 0), (0.5, 1), (1, 1))); CheckTrue(model.SetMode(FunctionMode.Smooth).IsOk); CheckEqual(FunctionMode.Smooth, model.Mode); CheckEqual(0.0, model.Function.Evaluate(0.25).Value); CheckEqual(1.0, model.Function.Evaluate(0.5).Value); } public void ToSmoothThroughARiseTooTallForASlope_IsRefusedAndChangesNothing() { FunctionModel model = TooTallForSmooth(); PointFunction before = model.Function; var listener = new Listener(model); CheckEqual(FunctionFailures.SmoothSpansTooMuchY.Fail(), model.SetMode(FunctionMode.Smooth)); CheckEqual(FunctionMode.Linear, model.Mode); CheckEqual(0, listener.Changes); CheckTrue(ReferenceEquals(before, model.Function)); } public void AnUndefinedMode_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Ramp().SetMode((FunctionMode)99); } } public class SelectTests : TestFixture { public void SelectsThePoint_WithoutChangingTheFunction() { FunctionModel model = Ramp(); var listener = new Listener(model); model.Select(2); CheckEqual(2, model.SelectedIndex); CheckEqual(1, listener.SelectionChanges); CheckEqual(2, listener.LastSelection); CheckEqual(0, listener.Changes); } public void SelectingTheSelectedPoint_RaisesNothing() { FunctionModel model = Ramp(); var listener = new Listener(model); model.Select(0); CheckEqual(0, listener.SelectionChanges); } public void NotAPoint_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Ramp().Select(3); } } public class GetXTests : TestFixture { public void ReturnsTheXOfThePointAtTheIndex() { CheckEqual(0.5, Ramp().GetX(1).Value); } public void BeforeTheFirst_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Ramp().GetX(-1); } public void AfterTheLast_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Ramp().GetX(3); } } public class GetYTests : TestFixture { public void ReturnsTheYOfThePointAtTheIndex() { CheckEqual(1.0, Ramp().GetY(1).Value); } public void BeforeTheFirst_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Ramp().GetY(-1); } public void AfterTheLast_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Ramp().GetY(3); } } public class AddPointTests : TestFixture { public void InsertsInOrder_AndTheFunctionRunsThroughIt() { FunctionModel model = Ramp(); int index = model.AddPoint(0.25, 0.75).Value; CheckEqual(1, index); CheckEqual("0 0.25 0.5 1", Xs(model)); CheckEqual("0 0.75 1 0", Ys(model)); CheckEqual(0.75, model.Function.Evaluate(0.25).Value); } public void GoesAfterPointsAtTheSameX() { FunctionModel model = Ramp(); int index = model.AddPoint(0.5, 3).Value; CheckEqual(2, index); CheckEqual("0 0.5 0.5 1", Xs(model)); CheckEqual("0 1 3 0", Ys(model)); } public void HoldsXToZeroThroughOne() { FunctionModel model = Ramp(); CheckEqual(3, model.AddPoint(7, 2).Value); CheckEqual(1.0, model.GetX(3).Value); CheckEqual(1, model.AddPoint(-7, 2).Value); CheckEqual(0.0, model.GetX(1).Value); } public void AnInfiniteX_IsHeldToTheEnd() { FunctionModel model = Ramp(); CheckEqual(3, model.AddPoint(double.PositiveInfinity, 2).Value); CheckEqual(1.0, model.GetX(3).Value); } public void KeepsAYAboveOneOrBelowZero() { FunctionModel model = Ramp(); model.AddPoint(0.25, 5); model.AddPoint(0.75, -5); CheckEqual("0 5 1 -5 0", Ys(model)); } public void SelectsTheNewPoint_AndRaisesBothEvents() { FunctionModel model = Ramp(); var listener = new Listener(model); model.AddPoint(0.75, 0.5); CheckEqual(2, model.SelectedIndex); CheckEqual(1, listener.Changes); CheckEqual(1, listener.SelectionChanges); CheckEqual(2, listener.LastSelection); CheckEqual(4, model.Function.PointCount); } public void ANotANumberX_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Ramp().AddPoint(double.NaN, 0); } public void ANotANumberY_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Ramp().AddPoint(0.25, double.NaN); } public void AnInfiniteY_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Ramp().AddPoint(0.25, double.PositiveInfinity); } public void ToASmoothFunctionAtAFreeX_KeepsItSmooth() { FunctionModel model = SmoothRamp(); CheckEqual(1, model.AddPoint(0.25, 0.5).Value); CheckTrue(model.Function is MonotoneSpline); } public void ToASmoothFunctionAtAnXItHasAPointAt_JumpsThere() { FunctionModel model = SmoothRamp(); CheckEqual(2, model.AddPoint(0.5, 2).Value); CheckEqual("0 0.5 0.5 1", Xs(model)); CheckEqual("0 1 2 0", Ys(model)); CheckEqual(2, model.SelectedIndex); CheckEqual(0.5, model.Function.Evaluate(0.25).Value); CheckEqual(2.0, model.Function.Evaluate(0.5).Value); } public void ToASmoothFunctionAtARiseTooTallForASlope_IsRefusedAndChangesNothing() { FunctionModel model = TallLine(); PointFunction before = model.Function; var listener = new Listener(model); CheckEqual(FunctionFailures.SmoothSpansTooMuchY.Fail(), model.AddPoint(0.5, 7e307).Status); CheckEqual("0 1", Xs(model)); CheckEqual(0, model.SelectedIndex); CheckEqual(0, listener.Changes); CheckEqual(0, listener.SelectionChanges); CheckTrue(ReferenceEquals(before, model.Function)); } } public class RemovePointTests : TestFixture { public void RemovesThePoint_AndRaisesChanged() { FunctionModel model = Ramp(); var listener = new Listener(model); CheckTrue(model.RemovePoint(1).IsOk); CheckEqual("0 1", Xs(model)); CheckEqual("0 0", Ys(model)); CheckEqual(2, model.Function.PointCount); CheckEqual(1, listener.Changes); } public void TheLastPoint_Stays() { var model = new FunctionModel(new PiecewiseLinear((0.5, 2))); var listener = new Listener(model); CheckEqual(FunctionFailures.NeedsAPoint.Fail(), model.RemovePoint(0)); CheckEqual(1, model.PointCount); CheckEqual(0, listener.Changes); } public void BeforeTheSelectedPoint_KeepsTheSelectionOnIt() { FunctionModel model = Ramp(); model.Select(2); var listener = new Listener(model); model.RemovePoint(0); CheckEqual(1, model.SelectedIndex); CheckEqual(1, listener.SelectionChanges); CheckEqual(1, listener.LastSelection); } public void TheSelectedPoint_SelectsTheOneThatTakesItsPlace() { FunctionModel model = Ramp(); model.Select(1); var listener = new Listener(model); model.RemovePoint(1); CheckEqual(1, model.SelectedIndex); CheckEqual(1, listener.SelectionChanges); } public void TheSelectedLastPoint_SelectsTheNewLast() { FunctionModel model = Ramp(); model.Select(2); model.RemovePoint(2); CheckEqual(1, model.SelectedIndex); } public void AfterTheSelectedPoint_RaisesNoSelectionChange() { FunctionModel model = Ramp(); var listener = new Listener(model); model.RemovePoint(2); CheckEqual(0, model.SelectedIndex); CheckEqual(0, listener.SelectionChanges); } public void NotAPoint_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Ramp().RemovePoint(3); } public void WhenTheModeCannotJoinWhatIsLeft_IsRefusedAndChangesNothing() { var model = new FunctionModel(new MonotoneSpline((0, 0), (0.25, 0), (0.5, 8e307), (1, 0))); var listener = new Listener(model); CheckEqual(FunctionFailures.SmoothSpansTooMuchY.Fail(), model.RemovePoint(1)); CheckEqual(4, model.PointCount); CheckEqual(0, listener.Changes); } } public class MovePointTests : TestFixture { public void ToAnotherX_KeepsTheOrder() { FunctionModel model = Ramp(); int index = model.MovePoint(1, 0.25, 0.5).Value; CheckEqual(1, index); CheckEqual("0 0.25 1", Xs(model)); CheckEqual("0 0.5 0", Ys(model)); CheckEqual(0.5, model.Function.Evaluate(0.25).Value); } public void PastANeighbor_ReturnsTheNewIndex() { FunctionModel model = Ramp(); int index = model.MovePoint(0, 0.75, 0.25).Value; CheckEqual(1, index); CheckEqual("0.5 0.75 1", Xs(model)); CheckEqual("1 0.25 0", Ys(model)); } public void GoesAfterPointsAtTheSameX() { FunctionModel model = Ramp(); int index = model.MovePoint(0, 0.5, 7).Value; CheckEqual(1, index); CheckEqual("0.5 0.5 1", Xs(model)); CheckEqual("1 7 0", Ys(model)); } public void HoldsXToZeroThroughOne() { FunctionModel model = Ramp(); CheckEqual(2, model.MovePoint(1, 9, 1).Value); CheckEqual(1.0, model.GetX(2).Value); } public void ChangingOnlyY_KeepsThePointWhereItIs() { FunctionModel model = Ramp(); CheckEqual(1, model.MovePoint(1, 0.5, 2).Value); CheckEqual("0 2 0", Ys(model)); } public void ToWhereItIs_ChangesNothing() { FunctionModel model = Ramp(); PointFunction before = model.Function; var listener = new Listener(model); CheckEqual(1, model.MovePoint(1, 0.5, 1).Value); CheckEqual(0, listener.Changes); CheckTrue(ReferenceEquals(before, model.Function)); } public void RaisesChanged() { FunctionModel model = Ramp(); var listener = new Listener(model); model.MovePoint(1, 0.25, 0.5); CheckEqual(1, listener.Changes); } public void TheSelectedPoint_StaysSelectedWhereverItGoes() { FunctionModel model = Ramp(); model.Select(1); var listener = new Listener(model); model.MovePoint(1, 1, 1); CheckEqual(2, model.SelectedIndex); CheckEqual(1, listener.SelectionChanges); CheckEqual(2, listener.LastSelection); } public void ForwardPastTheSelectedPoint_KeepsTheSelectionOnIt() { FunctionModel model = Ramp(); model.Select(1); var listener = new Listener(model); model.MovePoint(0, 0.75, 0); CheckEqual(0, model.SelectedIndex); CheckEqual(1, listener.SelectionChanges); } public void BackPastTheSelectedPoint_KeepsTheSelectionOnIt() { FunctionModel model = Ramp(); model.Select(1); var listener = new Listener(model); model.MovePoint(2, 0.25, 0); CheckEqual(2, model.SelectedIndex); CheckEqual(1, listener.SelectionChanges); } public void ANotSelectedPointThatPassesNoSelection_RaisesNoSelectionChange() { FunctionModel model = Ramp(); model.Select(2); var listener = new Listener(model); model.MovePoint(0, 0.25, 0); CheckEqual(2, model.SelectedIndex); CheckEqual(0, listener.SelectionChanges); } public void NotAPoint_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Ramp().MovePoint(3, 0.5, 0.5); } public void ANotANumberX_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Ramp().MovePoint(1, double.NaN, 0.5); } public void AnInfiniteY_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); Ramp().MovePoint(1, 0.5, double.NegativeInfinity); } public void OfASmoothFunctionOntoAnotherPointsX_JumpsThere() { FunctionModel model = SmoothRamp(); CheckEqual(1, model.MovePoint(0, 0.5, 0).Value); CheckEqual("0.5 0.5 1", Xs(model)); CheckEqual("1 0 0", Ys(model)); CheckEqual(1, model.SelectedIndex); CheckEqual(0.0, model.Function.Evaluate(0.5).Value); } public void OfASmoothFunctionToARiseTooTallForASlope_IsRefusedAndChangesNothing() { var model = new FunctionModel(new MonotoneSpline((0, 0), (0.5, 0), (1, 0))); PointFunction before = model.Function; var listener = new Listener(model); CheckEqual(FunctionFailures.SmoothSpansTooMuchY.Fail(), model.MovePoint(1, 0.5, 8e307).Status); CheckEqual("0 0.5 1", Xs(model)); CheckEqual("0 0 0", Ys(model)); CheckEqual(0, listener.Changes); CheckEqual(0, listener.SelectionChanges); CheckTrue(ReferenceEquals(before, model.Function)); } } public class ApplyTests : TestFixture { private static readonly MessageFailureKind Declined = new MessageFailureKind( "Prism.Glass.UnitTests.FunctionModel.Declined", "The def can't take that."); private static FunctionModel Declining() { FunctionModel model = Ramp(); model.Apply = _ => Declined.Fail(); return model; } public void Starts_Null() { CheckNull(Ramp().Apply); } public void IsAskedWithTheFunctionAnEditWouldMake() { FunctionModel model = Ramp(); var asked = new List(); model.Apply = function => { asked.Add(function); return Status.Ok; }; model.AddPoint(0.25, 0.5); CheckEqual(1, asked.Count); CheckTrue(ReferenceEquals(model.Function, asked[0])); CheckEqual(4, asked[0].PointCount); } public void IsAskedBeforeTheModelChanges() { FunctionModel model = Ramp(); int pointsWhenAsked = -1; model.Apply = _ => { pointsWhenAsked = model.PointCount; return Status.Ok; }; model.AddPoint(0.25, 0.5); CheckEqual(3, pointsWhenAsked); } public void IsNotAskedWhenALoadReplacesTheFunction() { FunctionModel model = Ramp(); int asked = 0; model.Apply = _ => { ++asked; return Status.Ok; }; model.Load(new PiecewiseLinear((0, 1))); CheckEqual(0, asked); } public void IsNotAskedWhenTheModeCantBuildTheFunction() { FunctionModel model = TooTallForSmooth(); int asked = 0; model.Apply = _ => { ++asked; return Status.Ok; }; model.SetMode(FunctionMode.Smooth); CheckEqual(0, asked); } public void ASetModeItTurnsDown_ComesBackWithItsStatusAndChangesNothing() { FunctionModel model = Declining(); PointFunction before = model.Function; var listener = new Listener(model); CheckEqual(Declined.Fail(), model.SetMode(FunctionMode.Smooth)); CheckEqual(FunctionMode.Linear, model.Mode); CheckEqual(0, listener.Changes); CheckTrue(ReferenceEquals(before, model.Function)); } public void AnAddPointItTurnsDown_ComesBackWithItsStatusAndChangesNothing() { FunctionModel model = Declining(); PointFunction before = model.Function; var listener = new Listener(model); CheckEqual(Declined.Fail(), model.AddPoint(0.25, 0.5).Status); CheckEqual("0 0.5 1", Xs(model)); CheckEqual(0, model.SelectedIndex); CheckEqual(0, listener.Changes); CheckEqual(0, listener.SelectionChanges); CheckTrue(ReferenceEquals(before, model.Function)); } public void ARemovePointItTurnsDown_ComesBackWithItsStatusAndChangesNothing() { FunctionModel model = Declining(); PointFunction before = model.Function; var listener = new Listener(model); CheckEqual(Declined.Fail(), model.RemovePoint(1)); CheckEqual("0 0.5 1", Xs(model)); CheckEqual(0, listener.Changes); CheckEqual(0, listener.SelectionChanges); CheckTrue(ReferenceEquals(before, model.Function)); } public void AMovePointItTurnsDown_ComesBackWithItsStatusAndChangesNothing() { FunctionModel model = Declining(); PointFunction before = model.Function; var listener = new Listener(model); CheckEqual(Declined.Fail(), model.MovePoint(1, 0.25, 0.5).Status); CheckEqual("0 0.5 1", Xs(model)); CheckEqual("0 1 0", Ys(model)); CheckEqual(0, listener.Changes); CheckEqual(0, listener.SelectionChanges); CheckTrue(ReferenceEquals(before, model.Function)); } public void AfterATurnDown_AnEditItTakesGoesThrough() { FunctionModel model = Declining(); model.AddPoint(0.25, 0.5); model.Apply = null; model.AddPoint(0.25, 0.5); CheckEqual("0 0.25 0.5 1", Xs(model)); } } } }