using System; using Azimuth; using UnitTestSharp; namespace Prism.Glass.UnitTests { public class FunctionPlotterTests : TestFixture { private const int PlotSize = 5; private static readonly Interval Unit = new Interval(0, 1); private static readonly PlotColors Colors = new PlotColors { Background = Color.FromArgb(1, 2, 3), Guide = Color.FromArgb(4, 5, 6), Line = Color.FromArgb(7, 8, 9), }; private static byte[] Drawn(IScalarFunction function, Interval range) { var pixels = new byte[Rgba.ByteCount(PlotSize, PlotSize)]; FunctionPlotter.Draw(pixels, PlotSize, PlotSize, function, range, Colors); return pixels; } private static string PaintAt(byte[] pixels, int column, int row) { int offset = (row * PlotSize + column) * Rgba.BytesPerPixel; int argb = Color.FromArgb(pixels[offset + 3], pixels[offset], pixels[offset + 1], pixels[offset + 2]) .ToArgb(); if (argb == Colors.Line.ToArgb()) { return "line"; } if (argb == Colors.Guide.ToArgb()) { return "guide"; } return argb == Colors.Background.ToArgb() ? "background" : "something else"; } public class RangeOfTests : TestFixture { public void OneEverywhere_IsZeroToOne() { Interval range = FunctionPlotter.RangeOf(new PiecewiseLinear((0, 1)), PlotSize); CheckEqual(0.0, range.Min.Value); CheckEqual(1.0, range.Max.Value); } public void AFunctionAboveOne_GrowsTheTop() { Interval range = FunctionPlotter.RangeOf(new PiecewiseLinear((0, 0), (1, 2)), PlotSize); CheckEqual(0.0, range.Min.Value); CheckEqual(2.0, range.Max.Value); } public void AFunctionBelowZero_GrowsTheBottom() { Interval range = FunctionPlotter.RangeOf(new PiecewiseLinear((0, 0), (1, -1)), PlotSize); CheckEqual(-1.0, range.Min.Value); CheckEqual(1.0, range.Max.Value); } public void ValuesThatAreNotFinite_AreLeftOut() { var function = new CallbackFunction( x => x == 0.5 ? double.PositiveInfinity : x == 1 ? double.NaN : 0.5); Interval range = FunctionPlotter.RangeOf(function, PlotSize); CheckEqual(0.0, range.Min.Value); CheckEqual(1.0, range.Max.Value); } public void WithoutAFunction_Throws() { CheckThrow(typeof(ArgumentNullException)); FunctionPlotter.RangeOf(null, PlotSize); } public void ANarrowerPlotThanTwoColumns_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); FunctionPlotter.RangeOf(new PiecewiseLinear((0, 1)), 1); } } public class RowOfTests : TestFixture { public void TheTopOfTheRange_IsTheFirstRow() { CheckEqual(0, FunctionPlotter.RowOf(1, Unit, PlotSize)); } public void TheBottomOfTheRange_IsTheLastRow() { CheckEqual(4, FunctionPlotter.RowOf(0, Unit, PlotSize)); } public void TheMiddle_IsTheMiddleRow() { CheckEqual(2, FunctionPlotter.RowOf(0.5, Unit, PlotSize)); } public void BetweenRows_RoundsToTheNearest() { CheckEqual(3, FunctionPlotter.RowOf(0.25, Unit, PlotSize)); CheckEqual(4, FunctionPlotter.RowOf(0.125, Unit, PlotSize)); CheckEqual(3, FunctionPlotter.RowOf(0.2, Unit, PlotSize)); } public void ARangeBelowZero_PutsZeroPartWayDown() { CheckEqual(2, FunctionPlotter.RowOf(0, new Interval(-1, 1), PlotSize)); } public void PastTheTop_IsHeldToTheFirstRow() { CheckEqual(0, FunctionPlotter.RowOf(2, Unit, PlotSize)); } public void PastTheBottom_IsHeldToTheLastRow() { CheckEqual(4, FunctionPlotter.RowOf(-1, Unit, PlotSize)); } public void ARangeWithNoHeight_Throws() { CheckThrow(typeof(ArgumentException)); FunctionPlotter.RowOf(1, new Interval(1, 1), PlotSize); } public void AnInfiniteRange_Throws() { CheckThrow(typeof(ArgumentException)); FunctionPlotter.RowOf(1, new Interval(0, Scalar.PositiveInfinity), PlotSize); } public void ARangeWithNoBottom_Throws() { CheckThrow(typeof(ArgumentException)); FunctionPlotter.RowOf(1, new Interval(Scalar.NegativeInfinity, 1), PlotSize); } public void NoRows_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); FunctionPlotter.RowOf(1, Unit, 0); } } public class DrawTests : TestFixture { public void OneEverywhere_RunsAlongTheTopRowWithTheZeroGuideBelow() { byte[] pixels = Drawn(new PiecewiseLinear((0, 1)), Unit); for (int column = 0; column < PlotSize; column++) { CheckEqual("line", PaintAt(pixels, column, 0)); CheckEqual("background", PaintAt(pixels, column, 2)); CheckEqual("guide", PaintAt(pixels, column, 4)); } } public void ARampFromZeroToOne_RunsCornerToCorner() { byte[] pixels = Drawn(new PiecewiseLinear((0, 0), (1, 1)), Unit); CheckEqual("line", PaintAt(pixels, 0, 4)); CheckEqual("line", PaintAt(pixels, 1, 3)); CheckEqual("line", PaintAt(pixels, 2, 2)); CheckEqual("line", PaintAt(pixels, 3, 1)); CheckEqual("line", PaintAt(pixels, 4, 0)); CheckEqual("background", PaintAt(pixels, 1, 2)); CheckEqual("background", PaintAt(pixels, 3, 2)); } public void ARangeAboveOne_MovesTheGuides() { byte[] pixels = Drawn(new PiecewiseLinear((0, 0), (1, 2)), new Interval(0, 2)); CheckEqual("line", PaintAt(pixels, 0, 4)); CheckEqual("line", PaintAt(pixels, 2, 2)); CheckEqual("line", PaintAt(pixels, 4, 0)); CheckEqual("guide", PaintAt(pixels, 1, 2)); CheckEqual("guide", PaintAt(pixels, 3, 4)); } public void ARangeBelowZero_MovesTheGuides() { byte[] pixels = Drawn(new PiecewiseLinear((0, 0), (1, -1)), new Interval(-1, 1)); CheckEqual("line", PaintAt(pixels, 0, 2)); CheckEqual("line", PaintAt(pixels, 1, 3)); CheckEqual("line", PaintAt(pixels, 2, 3)); CheckEqual("line", PaintAt(pixels, 3, 4)); CheckEqual("line", PaintAt(pixels, 4, 4)); CheckEqual("guide", PaintAt(pixels, 3, 0)); CheckEqual("guide", PaintAt(pixels, 3, 2)); } public void AJump_IsAVerticalRunInTheColumnItHappensIn() { byte[] pixels = Drawn(new PiecewiseStep((0, 0), (0.5, 1)), Unit); CheckEqual("line", PaintAt(pixels, 1, 4)); CheckEqual("line", PaintAt(pixels, 2, 3)); CheckEqual("line", PaintAt(pixels, 2, 2)); CheckEqual("line", PaintAt(pixels, 2, 1)); CheckEqual("line", PaintAt(pixels, 2, 0)); CheckEqual("line", PaintAt(pixels, 3, 0)); CheckEqual("guide", PaintAt(pixels, 2, 4)); CheckEqual("background", PaintAt(pixels, 1, 3)); CheckEqual("background", PaintAt(pixels, 3, 2)); } public void ANotANumber_LeavesAGapAndTheLineDoesNotReachAcrossIt() { byte[] pixels = Drawn(new CallbackFunction(x => x < 0.3 ? 0 : x < 0.6 ? double.NaN : 1), Unit); CheckEqual("line", PaintAt(pixels, 0, 4)); CheckEqual("line", PaintAt(pixels, 1, 4)); CheckEqual("guide", PaintAt(pixels, 2, 4)); CheckEqual("line", PaintAt(pixels, 3, 0)); CheckEqual("line", PaintAt(pixels, 4, 0)); CheckEqual("background", PaintAt(pixels, 3, 1)); } public void AnInfinity_LeavesAGap() { byte[] pixels = Drawn(new CallbackFunction(x => x == 0.5 ? double.PositiveInfinity : 0.5), Unit); CheckEqual("line", PaintAt(pixels, 1, 2)); CheckEqual("background", PaintAt(pixels, 2, 2)); CheckEqual("line", PaintAt(pixels, 3, 2)); CheckEqual("guide", PaintAt(pixels, 2, 0)); CheckEqual("guide", PaintAt(pixels, 2, 4)); } public void AValueAboveTheRange_IsDrawnOnTheTopRow() { byte[] pixels = Drawn(new PiecewiseLinear((0, 5)), Unit); for (int column = 0; column < PlotSize; column++) { CheckEqual("line", PaintAt(pixels, column, 0)); } } public void AValueBelowTheRange_IsDrawnOnTheBottomRow() { byte[] pixels = Drawn(new PiecewiseLinear((0, -5)), Unit); for (int column = 0; column < PlotSize; column++) { CheckEqual("line", PaintAt(pixels, column, 4)); } } public void WithoutAFunction_Throws() { CheckThrow(typeof(ArgumentNullException)); var pixels = new byte[Rgba.ByteCount(PlotSize, PlotSize)]; FunctionPlotter.Draw(pixels, PlotSize, PlotSize, null, Unit, Colors); } public void WithoutABuffer_Throws() { CheckThrow(typeof(ArgumentNullException)); FunctionPlotter.Draw(null, PlotSize, PlotSize, new PiecewiseLinear((0, 1)), Unit, Colors); } public void ABufferOfTheWrongSize_Throws() { CheckThrow(typeof(ArgumentException)); FunctionPlotter.Draw(new byte[3], PlotSize, PlotSize, new PiecewiseLinear((0, 1)), Unit, Colors); } public void ATooNarrowPlot_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); FunctionPlotter.Draw(new byte[Rgba.ByteCount(1, 1)], 1, 1, new PiecewiseLinear((0, 1)), Unit, Colors); } public void ARangeWithNoHeight_Throws() { CheckThrow(typeof(ArgumentException)); Drawn(new PiecewiseLinear((0, 1)), new Interval(1, 1)); } } } }