Skip to content

Commit bd54606

Browse files
committed
Move most tests to shared test project
1 parent 40378ae commit bd54606

46 files changed

Lines changed: 269 additions & 3116 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using System;
2+
using BCnEncoder.Shared;
3+
using BCnEncTests.Support;
4+
using CommunityToolkit.HighPerformance;
5+
using Xunit;
6+
7+
namespace BCnEncTests
8+
{
9+
public class MipMapperTests
10+
{
11+
[Fact]
12+
public void MipChainHasCorrectDimensions()
13+
{
14+
var image = ImageLoader.TestGradient1; // 512x416
15+
var numMips = 0;
16+
var chain = MipMapper.GenerateMipChain(image, ref numMips);
17+
18+
Assert.Equal(chain.Length, numMips);
19+
Assert.True(numMips > 1);
20+
21+
for (var i = 0; i < numMips; i++)
22+
{
23+
Assert.Equal(Math.Max(1, image.Width >> i), chain[i].Width);
24+
Assert.Equal(Math.Max(1, image.Height >> i), chain[i].Height);
25+
}
26+
27+
// Last level must be 1x1
28+
Assert.Equal(1, chain[numMips - 1].Width);
29+
Assert.Equal(1, chain[numMips - 1].Height);
30+
}
31+
32+
/// <summary>
33+
/// A solid-color image must downsample to exactly the same color at
34+
/// every mip level, regardless of how many times the box filter is applied.
35+
/// </summary>
36+
[Fact]
37+
public void SolidColorMipChainIsExact()
38+
{
39+
var color = new ColorRgba32(200, 100, 50, 255);
40+
var pixels = new ColorRgba32[16 * 16];
41+
for (var i = 0; i < pixels.Length; i++) pixels[i] = color;
42+
43+
var image = new Memory2D<ColorRgba32>(pixels, 16, 16);
44+
var numMips = 0;
45+
var chain = MipMapper.GenerateMipChain(image, ref numMips);
46+
47+
// 16x16 -> 8x8 -> 4x4 -> 2x2 -> 1x1 = 5 levels
48+
Assert.Equal(5, numMips);
49+
50+
for (var level = 0; level < numMips; level++)
51+
{
52+
var span = chain[level].Span;
53+
for (var y = 0; y < chain[level].Height; y++)
54+
for (var x = 0; x < chain[level].Width; x++)
55+
Assert.Equal(color, span[y, x]);
56+
}
57+
}
58+
59+
/// <summary>
60+
/// Verifies the exact 2x2 box-filter arithmetic with a known input.
61+
/// A 2x2 image of two values repeated both rows should average exactly.
62+
/// </summary>
63+
[Fact]
64+
public void KnownPatternDownsamplesCorrectly()
65+
{
66+
// 2-wide, 2-tall: left column = 100 red, right column = 200 red.
67+
// Expected 1x1 average: r = (100+200+100+200)/4 = 150, g=b=0, a=255.
68+
var pixels = new[]
69+
{
70+
new ColorRgba32(100, 0, 0, 255), new ColorRgba32(200, 0, 0, 255),
71+
new ColorRgba32(100, 0, 0, 255), new ColorRgba32(200, 0, 0, 255),
72+
};
73+
var image = new Memory2D<ColorRgba32>(pixels, 2, 2);
74+
var numMips = 0;
75+
var chain = MipMapper.GenerateMipChain(image, ref numMips);
76+
77+
Assert.Equal(2, numMips); // 2x2 -> 1x1
78+
Assert.Equal(1, chain[1].Width);
79+
Assert.Equal(1, chain[1].Height);
80+
Assert.Equal(new ColorRgba32(150, 0, 0, 255), chain[1].Span[0, 0]);
81+
}
82+
}
83+
}

BCnEncTests.Framework/Support/TestHelper.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,5 +254,20 @@ public static unsafe void SaveAsPng(Memory2D<ColorRgba32> image, Stream stream)
254254
bmp.UnlockBits(data);
255255
bmp.Save(stream, ImageFormat.Png);
256256
}
257+
258+
public static void SaveAsPng(ColorRgbFloat[] pixels, int width, int height, Stream stream)
259+
{
260+
var rgba = new ColorRgba32[pixels.Length];
261+
for (var i = 0; i < pixels.Length; i++)
262+
{
263+
var p = pixels[i];
264+
byte r = (byte)(Math.Max(0, Math.Min(1, p.r)) * 255 + 0.5f);
265+
byte g = (byte)(Math.Max(0, Math.Min(1, p.g)) * 255 + 0.5f);
266+
byte b = (byte)(Math.Max(0, Math.Min(1, p.b)) * 255 + 0.5f);
267+
rgba[i] = new ColorRgba32(r, g, b, 255);
268+
}
269+
var mem = new Memory2D<ColorRgba32>(rgba, height, width);
270+
SaveAsPng(mem, stream);
271+
}
257272
}
258273
}

BCnEncTests.Shared/BCnEncTests.Shared.projitems

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,29 @@
66
<SharedGUID>D954291E-2A0B-460D-934E-DC6B0785DB48</SharedGUID>
77
</PropertyGroup>
88
<ItemGroup>
9+
<Compile Include="$(MSBuildThisFileDirectory)AtcTests.cs" />
10+
<Compile Include="$(MSBuildThisFileDirectory)BC1Tests.cs" />
11+
<Compile Include="$(MSBuildThisFileDirectory)Bc5Tests.cs" />
12+
<Compile Include="$(MSBuildThisFileDirectory)Bc6EncoderTests.cs" />
13+
<Compile Include="$(MSBuildThisFileDirectory)Bc6HDecoderTests.cs" />
14+
<Compile Include="$(MSBuildThisFileDirectory)Bc7BlockTests.cs" />
15+
<Compile Include="$(MSBuildThisFileDirectory)BgraTests.cs" />
916
<Compile Include="$(MSBuildThisFileDirectory)BlockTests.cs" />
17+
<Compile Include="$(MSBuildThisFileDirectory)CancellationTests.cs" />
18+
<Compile Include="$(MSBuildThisFileDirectory)ClusterTests.cs" />
1019
<Compile Include="$(MSBuildThisFileDirectory)ColorTest.cs" />
20+
<Compile Include="$(MSBuildThisFileDirectory)DecodingAsyncTests.cs" />
21+
<Compile Include="$(MSBuildThisFileDirectory)DecodingTests.cs" />
22+
<Compile Include="$(MSBuildThisFileDirectory)DdsReadTests.cs" />
23+
<Compile Include="$(MSBuildThisFileDirectory)DdsWritingTests.cs" />
24+
<Compile Include="$(MSBuildThisFileDirectory)EncoderOptionsTests.cs" />
25+
<Compile Include="$(MSBuildThisFileDirectory)EncodingAsyncTest.cs" />
26+
<Compile Include="$(MSBuildThisFileDirectory)EncodingTest.cs" />
1127
<Compile Include="$(MSBuildThisFileDirectory)IntHelperTests.cs" />
1228
<Compile Include="$(MSBuildThisFileDirectory)MathHelperTests.cs" />
1329
<Compile Include="$(MSBuildThisFileDirectory)PcaTests.cs" />
30+
<Compile Include="$(MSBuildThisFileDirectory)ProgressTests.cs" />
31+
<Compile Include="$(MSBuildThisFileDirectory)RawTests.cs" />
32+
<Compile Include="$(MSBuildThisFileDirectory)SingleBlockTests.cs" />
1433
</ItemGroup>
1534
</Project>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ public void Encode()
265265
{
266266
var signed = true;
267267
var image = HdrLoader.TestHdrKiara;
268-
var blocks = ImageToBlocks.ImageTo4X4(image.pixels.AsMemory().AsMemory2D(image.height, image.width), out var bW, out var bH);
268+
var blocks = ImageToBlocks.ImageTo4X4(new Memory2D<ColorRgbFloat>(image.pixels, image.height, image.width), out var bW, out var bH);
269269

270270
for (var i = 0; i < blocks.Length; i++)
271271
{
Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,19 @@ public void DecodeDds()
2424
var decoder = new BcDecoder();
2525
var decoded = decoder.DecodeHdr(HdrLoader.TestHdrKiaraDds);
2626

27-
var hdr = new HdrImage((int)HdrLoader.TestHdrKiaraDds.header.dwWidth,
28-
(int)HdrLoader.TestHdrKiaraDds.header.dwHeight);
27+
var width = (int)HdrLoader.TestHdrKiaraDds.header.dwWidth;
28+
var height = (int)HdrLoader.TestHdrKiaraDds.header.dwHeight;
29+
var hdr = new HdrImage(width, height);
2930

3031
Assert.Equal(hdr.pixels.Length, decoded.Length);
3132

3233
hdr.pixels = decoded;
3334
using var sfs = File.OpenWrite("decoding_test_dds_bc6h.hdr");
3435
hdr.Write(sfs);
3536

37+
using var pngFs = File.OpenWrite("decoding_test_dds_bc6h.png");
38+
TestHelper.SaveAsPng(decoded, width, height, pngFs);
39+
3640
TestHelper.AssertPixelsEqual(HdrLoader.TestHdrKiara.pixels, decoded, CompressionQuality.Fast, output);
3741
}
3842

@@ -42,15 +46,19 @@ public void DecodeKtx()
4246
var decoder = new BcDecoder();
4347
var decoded = decoder.DecodeHdr(HdrLoader.TestHdrKiaraKtx);
4448

45-
var hdr = new HdrImage((int)HdrLoader.TestHdrKiaraKtx.header.PixelWidth,
46-
(int)HdrLoader.TestHdrKiaraKtx.header.PixelHeight);
49+
var width = (int)HdrLoader.TestHdrKiaraKtx.header.PixelWidth;
50+
var height = (int)HdrLoader.TestHdrKiaraKtx.header.PixelHeight;
51+
var hdr = new HdrImage(width, height);
4752

4853
Assert.Equal(hdr.pixels.Length, decoded.Length);
4954

5055
hdr.pixels = decoded;
5156
using var sfs = File.OpenWrite("decoding_test_ktx_bc6h.hdr");
5257
hdr.Write(sfs);
5358

59+
using var pngFs = File.OpenWrite("decoding_test_ktx_bc6h.png");
60+
TestHelper.SaveAsPng(decoded, width, height, pngFs);
61+
5462
TestHelper.AssertPixelsEqual(HdrLoader.TestHdrKiara.pixels, decoded, CompressionQuality.BestQuality, output);
5563
}
5664

@@ -63,7 +71,11 @@ public void AllBlocksDecodesExact()
6371
var decoder = new BcDecoder();
6472
var decoded = decoder.DecodeHdr(HdrLoader.TestHdrKiaraDds);
6573

74+
#if NETCOREAPP
75+
using var fs = File.OpenRead("../../../testImages/test_hdr_kiara_dds_float16_data.bin");
76+
#else
6677
using var fs = File.OpenRead("../../../../BCnEncTests/testImages/test_hdr_kiara_dds_float16_data.bin");
78+
#endif
6779
using var ms = new MemoryStream();
6880
fs.CopyTo(ms);
6981
var length = (int)ms.Position;
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
using BCnEncoder.Shared;
77
using BCnEncoder.Shared.ImageFiles;
88
using BCnEncTests.Support;
9-
using CommunityToolkit.HighPerformance;
109
using Xunit;
1110

11+
using CommunityToolkit.HighPerformance;
12+
1213
namespace BCnEncTests
1314
{
1415
public class Bc7BlockTests
@@ -85,7 +86,7 @@ public void DecodeErrorBlock()
8586
var pixels = decoder.DecodeRaw(buffer, width * 4, height * 4, CompressionFormat.Bc7);
8687
Assert.Contains(new ColorRgba32(255, 0, 255), pixels);
8788

88-
var decoded = pixels.AsMemory().AsMemory2D(height * 4, width * 4);
89+
var decoded = new Memory2D<ColorRgba32>(pixels, height * 4, width * 4);
8990
using var fs = File.OpenWrite("test_decode_bc7_error.png");
9091
TestHelper.SaveAsPng(decoded, fs);
9192
}

0 commit comments

Comments
 (0)