This commit is contained in:
2026-03-29 18:36:48 +03:00
parent 770dd629f5
commit 64ab493037
13 changed files with 816 additions and 19 deletions

View File

@@ -0,0 +1,76 @@
using Minint.Core.Models;
using Minint.Core.Services.Impl;
namespace Minint.Tests;
public class CompositorTests
{
private readonly Compositor _compositor = new();
[Fact]
public void Composite_EmptyLayer_AllTransparent()
{
var doc = new MinintDocument("test");
doc.Layers.Add(new MinintLayer("L1", 4));
uint[] result = _compositor.Composite(doc, 2, 2);
Assert.All(result, px => Assert.Equal(0u, px));
}
[Fact]
public void Composite_SingleOpaquePixel()
{
var doc = new MinintDocument("test");
var red = new RgbaColor(255, 0, 0, 255);
doc.EnsureColorCached(red);
var layer = new MinintLayer("L1", 4);
layer.Pixels[0] = 1;
doc.Layers.Add(layer);
uint[] result = _compositor.Composite(doc, 2, 2);
// ARGB packed as 0xAARRGGBB
uint expected = 0xFF_FF_00_00u;
Assert.Equal(expected, result[0]);
Assert.Equal(0u, result[1]); // rest is transparent
}
[Fact]
public void Composite_HiddenLayer_Ignored()
{
var doc = new MinintDocument("test");
doc.EnsureColorCached(new RgbaColor(0, 255, 0, 255));
var layer = new MinintLayer("L1", 4);
layer.Pixels[0] = 1;
layer.IsVisible = false;
doc.Layers.Add(layer);
uint[] result = _compositor.Composite(doc, 2, 2);
Assert.Equal(0u, result[0]);
}
[Fact]
public void Composite_TwoLayers_TopOverBottom()
{
var doc = new MinintDocument("test");
var red = new RgbaColor(255, 0, 0, 255);
var blue = new RgbaColor(0, 0, 255, 255);
int redIdx = doc.EnsureColorCached(red);
int blueIdx = doc.EnsureColorCached(blue);
var bottom = new MinintLayer("bottom", 1);
bottom.Pixels[0] = redIdx;
var top = new MinintLayer("top", 1);
top.Pixels[0] = blueIdx;
doc.Layers.Add(bottom);
doc.Layers.Add(top);
uint[] result = _compositor.Composite(doc, 1, 1);
// Blue on top, fully opaque, should overwrite red
Assert.Equal(0xFF_00_00_FFu, result[0]);
}
}

View File

@@ -0,0 +1,63 @@
using Minint.Core.Models;
using Minint.Core.Services.Impl;
namespace Minint.Tests;
public class DrawingTests
{
private readonly DrawingService _drawing = new();
[Fact]
public void ApplyBrush_Radius0_SetsSinglePixel()
{
var layer = new MinintLayer("L1", 9);
_drawing.ApplyBrush(layer, 1, 1, 0, 1, 3, 3);
Assert.Equal(1, layer.Pixels[1 * 3 + 1]);
Assert.Equal(0, layer.Pixels[0]); // (0,0) untouched
}
[Fact]
public void ApplyBrush_Radius1_SetsCircle()
{
var layer = new MinintLayer("L1", 25);
_drawing.ApplyBrush(layer, 2, 2, 1, 1, 5, 5);
// Center + 4 neighbors should be set
Assert.Equal(1, layer.Pixels[2 * 5 + 2]); // center
Assert.Equal(1, layer.Pixels[1 * 5 + 2]); // top
Assert.Equal(1, layer.Pixels[3 * 5 + 2]); // bottom
Assert.Equal(1, layer.Pixels[2 * 5 + 1]); // left
Assert.Equal(1, layer.Pixels[2 * 5 + 3]); // right
}
[Fact]
public void ApplyEraser_SetsToZero()
{
var layer = new MinintLayer("L1", 9);
Array.Fill(layer.Pixels, 5);
_drawing.ApplyEraser(layer, 1, 1, 0, 3, 3);
Assert.Equal(0, layer.Pixels[1 * 3 + 1]);
Assert.Equal(5, layer.Pixels[0]); // untouched
}
[Fact]
public void GetBrushMask_Radius0_SinglePixel()
{
var mask = _drawing.GetBrushMask(2, 2, 0, 5, 5);
Assert.Single(mask);
Assert.Equal((2, 2), mask[0]);
}
[Fact]
public void GetBrushMask_OutOfBounds_Clamped()
{
var mask = _drawing.GetBrushMask(0, 0, 2, 3, 3);
Assert.All(mask, p =>
{
Assert.InRange(p.X, 0, 2);
Assert.InRange(p.Y, 0, 2);
});
}
}

View File

@@ -0,0 +1,68 @@
using System.Text;
using Minint.Core.Models;
using Minint.Core.Services.Impl;
using Minint.Infrastructure.Export;
namespace Minint.Tests;
public class ExportTests
{
[Fact]
public void BmpExport_WritesValidBmp()
{
var exporter = new BmpExporter();
var pixels = new uint[] { 0xFF_FF_00_00, 0xFF_00_FF_00, 0xFF_00_00_FF, 0xFF_FF_FF_FF };
var ms = new MemoryStream();
exporter.Export(ms, pixels, 2, 2);
ms.Position = 0;
byte[] data = ms.ToArray();
Assert.True(data.Length > 0);
Assert.Equal((byte)'B', data[0]);
Assert.Equal((byte)'M', data[1]);
int fileSize = BitConverter.ToInt32(data, 2);
Assert.Equal(data.Length, fileSize);
}
[Fact]
public void GifExport_WritesValidGif()
{
var exporter = new GifExporter();
var frame1 = new uint[16]; // 4x4 transparent
var frame2 = new uint[16];
Array.Fill(frame2, 0xFF_FF_00_00u);
var frames = new List<(uint[] Pixels, uint DelayMs)>
{
(frame1, 100),
(frame2, 200),
};
var ms = new MemoryStream();
exporter.Export(ms, frames, 4, 4);
ms.Position = 0;
byte[] data = ms.ToArray();
Assert.True(data.Length > 0);
string sig = Encoding.ASCII.GetString(data, 0, 6);
Assert.Equal("GIF89a", sig);
Assert.Equal(0x3B, data[^1]); // GIF trailer
}
[Fact]
public void BmpExport_DimensionMismatch_Throws()
{
var exporter = new BmpExporter();
var pixels = new uint[3]; // does not match 2x2
Assert.Throws<ArgumentException>(() =>
{
var ms = new MemoryStream();
exporter.Export(ms, pixels, 2, 2);
});
}
}

View File

@@ -0,0 +1,52 @@
using Minint.Core.Models;
using Minint.Core.Services.Impl;
namespace Minint.Tests;
public class FloodFillTests
{
private readonly FloodFillService _fill = new();
[Fact]
public void Fill_EmptyLayer_FillsAll()
{
var layer = new MinintLayer("L1", 9); // 3x3, all zeros
_fill.Fill(layer, 0, 0, 1, 3, 3);
Assert.All(layer.Pixels, px => Assert.Equal(1, px));
}
[Fact]
public void Fill_SameColor_NoOp()
{
var layer = new MinintLayer("L1", 4);
Array.Fill(layer.Pixels, 2);
_fill.Fill(layer, 0, 0, 2, 2, 2);
Assert.All(layer.Pixels, px => Assert.Equal(2, px));
}
[Fact]
public void Fill_Bounded_DoesNotCrossBorder()
{
// 3x3 grid with a wall:
// 0 0 0
// 1 1 1
// 0 0 0
var layer = new MinintLayer("L1", 9);
layer.Pixels[3] = 1; // (0,1)
layer.Pixels[4] = 1; // (1,1)
layer.Pixels[5] = 1; // (2,1)
_fill.Fill(layer, 0, 0, 2, 3, 3);
// Top row should be filled
Assert.Equal(2, layer.Pixels[0]);
Assert.Equal(2, layer.Pixels[1]);
Assert.Equal(2, layer.Pixels[2]);
// Wall untouched
Assert.Equal(1, layer.Pixels[3]);
// Bottom row untouched (blocked by wall)
Assert.Equal(0, layer.Pixels[6]);
}
}

View File

@@ -0,0 +1,68 @@
using Minint.Core.Models;
using Minint.Core.Services.Impl;
namespace Minint.Tests;
public class FragmentServiceTests
{
private readonly FragmentService _fragment = new();
[Fact]
public void CopyFragment_SameDocument_CopiesPixels()
{
var doc = new MinintDocument("test");
var red = new RgbaColor(255, 0, 0, 255);
doc.EnsureColorCached(red);
var src = new MinintLayer("src", 16);
src.Pixels[0] = 1; // (0,0) = red
src.Pixels[1] = 1; // (1,0) = red
doc.Layers.Add(src);
var dst = new MinintLayer("dst", 16);
doc.Layers.Add(dst);
_fragment.CopyFragment(doc, 0, 0, 0, 2, 1, doc, 1, 2, 2, 4, 4);
Assert.Equal(1, dst.Pixels[2 * 4 + 2]); // (2,2)
Assert.Equal(1, dst.Pixels[2 * 4 + 3]); // (3,2)
}
[Fact]
public void CopyFragment_DifferentDocuments_MergesPalette()
{
var srcDoc = new MinintDocument("src");
var blue = new RgbaColor(0, 0, 255, 255);
int blueIdx = srcDoc.EnsureColorCached(blue);
var srcLayer = new MinintLayer("L1", 4);
srcLayer.Pixels[0] = blueIdx;
srcDoc.Layers.Add(srcLayer);
var dstDoc = new MinintDocument("dst");
var dstLayer = new MinintLayer("L1", 4);
dstDoc.Layers.Add(dstLayer);
_fragment.CopyFragment(srcDoc, 0, 0, 0, 1, 1, dstDoc, 0, 0, 0, 2, 2);
int dstBlueIdx = dstDoc.FindColorCached(blue);
Assert.True(dstBlueIdx > 0);
Assert.Equal(dstBlueIdx, dstLayer.Pixels[0]);
}
[Fact]
public void CopyFragment_TransparentPixels_Skipped()
{
var doc = new MinintDocument("test");
var src = new MinintLayer("src", 4); // all zeros (transparent)
doc.Layers.Add(src);
var dst = new MinintLayer("dst", 4);
Array.Fill(dst.Pixels, 0);
dst.Pixels[0] = 0; // explicitly 0
doc.Layers.Add(dst);
_fragment.CopyFragment(doc, 0, 0, 0, 2, 2, doc, 1, 0, 0, 2, 2);
Assert.Equal(0, dst.Pixels[0]); // stays transparent
}
}

View File

@@ -0,0 +1,65 @@
using Minint.Core.Models;
using Minint.Core.Services.Impl;
namespace Minint.Tests;
public class ImageEffectsTests
{
private readonly ImageEffectsService _effects = new();
[Fact]
public void ApplyGrayscale_ConvertsColors()
{
var doc = new MinintDocument("test");
var red = new RgbaColor(255, 0, 0, 255);
doc.EnsureColorCached(red);
_effects.ApplyGrayscale(doc);
var gray = doc.Palette[1];
Assert.Equal(gray.R, gray.G);
Assert.Equal(gray.G, gray.B);
Assert.Equal(255, gray.A);
// BT.601: 0.299*255 ≈ 76
Assert.InRange(gray.R, 74, 78);
}
[Fact]
public void ApplyGrayscale_PreservesTransparentIndex()
{
var doc = new MinintDocument("test");
doc.EnsureColorCached(new RgbaColor(100, 200, 50, 255));
_effects.ApplyGrayscale(doc);
Assert.Equal(RgbaColor.Transparent, doc.Palette[0]);
}
[Fact]
public void ApplyContrast_IncreasesContrast()
{
var doc = new MinintDocument("test");
var midGray = new RgbaColor(128, 128, 128, 255);
var lightGray = new RgbaColor(192, 192, 192, 255);
doc.EnsureColorCached(midGray);
doc.EnsureColorCached(lightGray);
_effects.ApplyContrast(doc, 2.0);
// midGray (128) stays ~128: factor*(128-128)+128 = 128
Assert.InRange(doc.Palette[1].R, 126, 130);
// lightGray (192): factor*(192-128)+128 = 2*64+128 = 256 → clamped to 255
Assert.Equal(255, doc.Palette[2].R);
}
[Fact]
public void ApplyContrast_PreservesAlpha()
{
var doc = new MinintDocument("test");
doc.EnsureColorCached(new RgbaColor(100, 100, 100, 200));
_effects.ApplyContrast(doc, 1.5);
Assert.Equal(200, doc.Palette[1].A);
}
}

View File

@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4" />
</ItemGroup>
<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Minint.Core\Minint.Core.csproj" />
<ProjectReference Include="..\Minint.Infrastructure\Minint.Infrastructure.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,41 @@
using Minint.Core.Models;
using Minint.Core.Services;
using Minint.Core.Services.Impl;
namespace Minint.Tests;
public class PatternGeneratorTests
{
private readonly PatternGenerator _gen = new();
[Theory]
[InlineData(PatternType.Checkerboard)]
[InlineData(PatternType.HorizontalGradient)]
[InlineData(PatternType.VerticalGradient)]
[InlineData(PatternType.HorizontalStripes)]
[InlineData(PatternType.VerticalStripes)]
[InlineData(PatternType.ConcentricCircles)]
[InlineData(PatternType.Tile)]
public void Generate_AllTypes_ProducesValidDocument(PatternType type)
{
var colors = new[] { new RgbaColor(255, 0, 0, 255), new RgbaColor(0, 0, 255, 255) };
var doc = _gen.Generate(type, 16, 16, colors, 4, 4);
Assert.Equal($"Pattern ({type})", doc.Name);
Assert.Single(doc.Layers);
Assert.Equal(256, doc.Layers[0].Pixels.Length);
Assert.True(doc.Palette.Count >= 2);
}
[Fact]
public void Generate_Checkerboard_AlternatesColors()
{
var colors = new[] { new RgbaColor(255, 0, 0, 255), new RgbaColor(0, 255, 0, 255) };
var doc = _gen.Generate(PatternType.Checkerboard, 4, 4, colors, 2);
var layer = doc.Layers[0];
int topLeft = layer.Pixels[0];
int topRight = layer.Pixels[2]; // cellSize=2, so (2,0) is next cell
Assert.NotEqual(topLeft, topRight);
}
}

View File

@@ -0,0 +1,121 @@
using Minint.Core.Models;
using Minint.Infrastructure.Serialization;
namespace Minint.Tests;
public class SerializerTests
{
private readonly MinintSerializer _serializer = new();
[Fact]
public void RoundTrip_EmptyDocument_PreservesStructure()
{
var container = new MinintContainer(32, 16);
container.AddNewDocument("Doc1");
var result = RoundTrip(container);
Assert.Equal(32, result.Width);
Assert.Equal(16, result.Height);
Assert.Single(result.Documents);
Assert.Equal("Doc1", result.Documents[0].Name);
Assert.Single(result.Documents[0].Layers);
Assert.Equal(32 * 16, result.Documents[0].Layers[0].Pixels.Length);
}
[Fact]
public void RoundTrip_MultipleDocuments_PreservesAll()
{
var container = new MinintContainer(8, 8);
var doc1 = container.AddNewDocument("Frame1");
doc1.FrameDelayMs = 200;
var doc2 = container.AddNewDocument("Frame2");
doc2.FrameDelayMs = 500;
var result = RoundTrip(container);
Assert.Equal(2, result.Documents.Count);
Assert.Equal("Frame1", result.Documents[0].Name);
Assert.Equal(200u, result.Documents[0].FrameDelayMs);
Assert.Equal("Frame2", result.Documents[1].Name);
Assert.Equal(500u, result.Documents[1].FrameDelayMs);
}
[Fact]
public void RoundTrip_PaletteAndPixels_Preserved()
{
var container = new MinintContainer(4, 4);
var doc = container.AddNewDocument("Test");
var red = new RgbaColor(255, 0, 0, 255);
doc.EnsureColorCached(red);
var layer = doc.Layers[0];
layer.Pixels[0] = 1; // red
var result = RoundTrip(container);
var rdoc = result.Documents[0];
Assert.Equal(2, rdoc.Palette.Count); // transparent + red
Assert.Equal(RgbaColor.Transparent, rdoc.Palette[0]);
Assert.Equal(red, rdoc.Palette[1]);
Assert.Equal(1, rdoc.Layers[0].Pixels[0]);
Assert.Equal(0, rdoc.Layers[0].Pixels[1]);
}
[Fact]
public void RoundTrip_LayerProperties_Preserved()
{
var container = new MinintContainer(2, 2);
var doc = container.AddNewDocument("Test");
doc.Layers[0].Name = "Background";
doc.Layers[0].IsVisible = false;
doc.Layers[0].Opacity = 128;
var result = RoundTrip(container);
var layer = result.Documents[0].Layers[0];
Assert.Equal("Background", layer.Name);
Assert.False(layer.IsVisible);
Assert.Equal(128, layer.Opacity);
}
[Fact]
public void RoundTrip_LargePalette_Uses2ByteIndices()
{
var container = new MinintContainer(2, 2);
var doc = container.AddNewDocument("BigPalette");
for (int i = 0; i < 300; i++)
doc.EnsureColorCached(new RgbaColor((byte)(i % 256), (byte)(i / 256), 0, 255));
Assert.Equal(2, doc.IndexByteWidth);
int lastIdx = doc.Palette.Count - 1;
doc.Layers[0].Pixels[0] = lastIdx;
var result = RoundTrip(container);
Assert.Equal(lastIdx, result.Documents[0].Layers[0].Pixels[0]);
}
[Fact]
public void Read_InvalidSignature_Throws()
{
var ms = new MemoryStream("BADDATA"u8.ToArray());
Assert.Throws<InvalidDataException>(() => _serializer.Read(ms));
}
[Fact]
public void Read_TruncatedStream_Throws()
{
var ms = new MemoryStream("MINI"u8.ToArray());
Assert.Throws<InvalidDataException>(() => _serializer.Read(ms));
}
private MinintContainer RoundTrip(MinintContainer container)
{
var ms = new MemoryStream();
_serializer.Write(ms, container);
ms.Position = 0;
return _serializer.Read(ms);
}
}