GraphTUI/tests/GraphTUI.Tests/BrailleCanvasTests.cs
2026-10-29 00:51:30 +03:00

162 lines
6.2 KiB
C#

using GraphTUI.Core.Models;
using GraphTUI.Rendering;
namespace GraphTUI.Tests;
public static class BrailleCanvasTests
{
public static void Register(TestRunner runner)
{
runner.Add("BrailleCanvas: 2x4 Subpixel Dot Mapping", () =>
{
var canvas = new BrailleCanvas(1, 1); // 1 cell = 2x4 subpixels
// Test dot (0, 0) -> bit 0 -> 0x01
canvas.Clear(ColorRgb.Background);
canvas.SetPixel(0, 0, ColorRgb.White);
var (ch0, _, _) = canvas.GetCell(0, 0);
Assert.Equal((char)(0x2800 | 0x01), ch0);
// Test dot (1, 0) -> bit 3 -> 0x08
canvas.Clear(ColorRgb.Background);
canvas.SetPixel(1, 0, ColorRgb.White);
var (ch1, _, _) = canvas.GetCell(0, 0);
Assert.Equal((char)(0x2800 | 0x08), ch1);
// Test dot (0, 3) -> bit 6 -> 0x40
canvas.Clear(ColorRgb.Background);
canvas.SetPixel(0, 3, ColorRgb.White);
var (ch2, _, _) = canvas.GetCell(0, 0);
Assert.Equal((char)(0x2800 | 0x40), ch2);
// Test dot (1, 3) -> bit 7 -> 0x80
canvas.Clear(ColorRgb.Background);
canvas.SetPixel(1, 3, ColorRgb.White);
var (ch3, _, _) = canvas.GetCell(0, 0);
Assert.Equal((char)(0x2800 | 0x80), ch3);
// Test all dots on -> 0xFF
canvas.Clear(ColorRgb.Background);
for (int y = 0; y < 4; y++)
{
for (int x = 0; x < 2; x++)
{
canvas.SetPixel(x, y, ColorRgb.White);
}
}
var (chAll, _, _) = canvas.GetCell(0, 0);
Assert.Equal((char)(0x2800 | 0xFF), chAll);
});
runner.Add("BrailleCanvas: Bresenham Line Rasterization", () =>
{
var canvas = new BrailleCanvas(10, 10);
canvas.Clear(ColorRgb.Background);
// Draw line from (0, 0) to (19, 39)
canvas.DrawLine(0, 0, 19, 39, ColorRgb.Cyan);
// Ensure start and end are set
var (startCell, _, _) = canvas.GetCell(0, 0);
Assert.True(startCell != ' ', "Start cell should not be empty");
var (endCell, _, _) = canvas.GetCell(9, 9);
Assert.True(endCell != ' ', "End cell should not be empty");
});
runner.Add("Viewport: Math to Subpixel Transformation", () =>
{
var vp = new Viewport
{
XMin = -10,
XMax = 10,
YMin = -5,
YMax = 5
};
// (0, 0) should be at center: subW = 20, subH = 20 -> center is (10, 10)
bool mapped = vp.MathToSubpixel(0, 0, 21, 21, out int sx, out int sy);
Assert.True(mapped);
Assert.Equal(10, sx);
Assert.Equal(10, sy);
// Top-left (-10, 5) -> (0, 0)
vp.MathToSubpixel(-10, 5, 21, 21, out int sx0, out int sy0);
Assert.Equal(0, sx0);
Assert.Equal(0, sy0);
// Bottom-right (10, -5) -> (20, 20)
vp.MathToSubpixel(10, -5, 21, 21, out int sx1, out int sy1);
Assert.Equal(20, sx1);
Assert.Equal(20, sy1);
});
runner.Add("BrailleCanvas: Dot Contamination Prevention", () =>
{
var canvas = new BrailleCanvas(1, 1);
canvas.Clear(ColorRgb.Background);
// 1. Grid dot at (0, 0)
canvas.SetPixel(0, 0, ColorRgb.GridLine, CanvasLayer.Grid);
// Verify grid alone produces dot 0 in GridLine color
var (chGrid, fgGrid, _) = canvas.GetCell(0, 0);
Assert.Equal(ColorRgb.GridLine, fgGrid);
Assert.Equal((char)(0x2800 | 0x01), chGrid);
// 2. Add Curve dot at (1, 3) in Cyan
canvas.SetPixel(1, 3, ColorRgb.Cyan, CanvasLayer.Curve);
// The cell must now prioritize the Curve:
// - Foreground must be Cyan
// - Mask must ONLY contain the curve dot (1, 3) which is 0x80
// - The grid dot (0, 0) which is 0x01 must NOT be in the mask (not contaminated)
var (chCurve, fgCurve, _) = canvas.GetCell(0, 0);
Assert.Equal(ColorRgb.Cyan, fgCurve);
Assert.Equal((char)(0x2800 | 0x80), chCurve);
Assert.True((chCurve & 0x01) == 0, "Grid dot 0x01 must NOT be contaminated into the curve mask");
// 3. Add Crosshair Guide dot at (0, 2) in Magenta
// Crosshair guide (layer 3) is lower than curve (layer 5), so curve dominates
canvas.SetPixel(0, 2, ColorRgb.Magenta, CanvasLayer.CrosshairGuide);
var (chAfterGuide, fgAfterGuide, _) = canvas.GetCell(0, 0);
Assert.Equal(ColorRgb.Cyan, fgAfterGuide);
Assert.Equal((char)(0x2800 | 0x80), chAfterGuide);
// 4. Center Crosshair (layer 7) at (0, 1) in White dominates
canvas.SetPixel(0, 1, ColorRgb.White, CanvasLayer.Crosshair);
var (chCenter, fgCenter, _) = canvas.GetCell(0, 0);
Assert.Equal(ColorRgb.White, fgCenter);
Assert.Equal((char)(0x2800 | 0x02), chCenter);
});
runner.Add("PlotRenderer: Render functions without stray grid contamination", () =>
{
var canvas = new BrailleCanvas(20, 20);
var vp = new Viewport();
var fns = new List<GraphFunction>
{
new(1, "y = x", ColorRgb.Cyan)
};
// Render with grid enabled
PlotRenderer.Render(canvas, vp, fns, _ => 0, showGrid: true);
// Check all cells: any cell with Cyan foreground should only have dots corresponding
// to the line, and must NOT contaminate background grid dots.
int cyanCells = 0;
for (int r = 0; r < canvas.CellHeight; r++)
{
for (int c = 0; c < canvas.CellWidth; c++)
{
var (ch, fg, _) = canvas.GetCell(c, r);
if (fg == ColorRgb.Cyan && ch != ' ')
{
cyanCells++;
}
}
}
Assert.True(cyanCells > 0, "Expected Cyan cells for line y=x");
});
}
}