using System; using Azimuth; using Blacklight.Core; using Glass; using Prism; using UnitTestSharp; namespace Konis.Testbed.UnitTests { public class FunctionPlotTests : TestFixture { private const int PlotSize = 5; private static byte[] Drawn(IScalarFunction function) { var pixels = new byte[Rgba.ByteCount(PlotSize, PlotSize)]; FunctionPlot.Draw(pixels, PlotSize, PlotSize, function); return pixels; } private static string PaintAt(byte[] pixels, int column, int row) { int offset = (row * PlotSize + column) * Rgba.BytesPerPixel; int argb = Prism.Color.FromArgb( pixels[offset + 3], pixels[offset], pixels[offset + 1], pixels[offset + 2]).ToArgb(); if (argb == FunctionPlot.Line.ToArgb()) { return "line"; } if (argb == FunctionPlot.Guide.ToArgb()) { return "guide"; } return argb == FunctionPlot.Background.ToArgb() ? "background" : "something else"; } public class CtorTests : TestFixture { public void WithoutAWindow_Throws() { CheckThrow(typeof(ArgumentNullException)); new FunctionPlot(null, 16, 8); } public void ANarrowerPlotThanTwoColumns_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new FunctionPlot(new NullWindowHandle(), 1, 8); } public void AFlatterPlotThanOneRow_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); new FunctionPlot(new NullWindowHandle(), 16, 0); } public void MakesATextureOfTheGivenSize() { var plot = new FunctionPlot(new NullWindowHandle(), 16, 8); var texture = ((TextureMaterial)plot.Material).Texture; CheckEqual(16, texture.Size.X); CheckEqual(8, texture.Size.Y); } } public class DrawTests : TestFixture { public void DrawsInThePlotsColors() { byte[] pixels = Drawn(new PiecewiseLinear((0, 0), (1, 1))); CheckEqual("line", PaintAt(pixels, 0, 4)); CheckEqual("line", PaintAt(pixels, 4, 0)); CheckEqual("background", PaintAt(pixels, 1, 2)); } public void AFunctionAboveOne_GrowsThePlotToHoldIt() { byte[] pixels = Drawn(new PiecewiseLinear((0, 0), (1, 2))); 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 ANotANumber_DoesNotStretchThePlot() { byte[] pixels = Drawn(new CallbackFunction(x => x == 0.5 ? double.PositiveInfinity : 0.5)); CheckEqual("background", PaintAt(pixels, 2, 2)); CheckEqual("guide", PaintAt(pixels, 2, 0)); CheckEqual("guide", PaintAt(pixels, 2, 4)); } public void NoFunction_IsDrawnAsOneEverywhere() { CheckEqual(Drawn(new PiecewiseLinear((0, 1))), Drawn(null)); } public void ABufferOfTheWrongSize_Throws() { CheckThrow(typeof(ArgumentException)); FunctionPlot.Draw(new byte[3], PlotSize, PlotSize, null); } public void ATooNarrowPlot_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); FunctionPlot.Draw(new byte[Rgba.ByteCount(1, 1)], 1, 1, null); } } public class ShowTests : TestFixture { public void AFunction_IsDrawnAndUploaded() { var plot = new FunctionPlot(new NullWindowHandle(), 16, 8); plot.Show(new MonotoneSpline((0, 0), (1, 1))); } public void NoFunction_IsDrawnAsOne() { var plot = new FunctionPlot(new NullWindowHandle(), 16, 8); plot.Show(null); } } } }