Files
Relic ad77bdf8d0 Fix off-by-one in Texture.GetPixels dest array bounds check
The bounds check in GetPixels was rejecting valid destination arrays
that exactly fit the requested source rectangle.
2026-07-28 12:15:30 +02:00

193 lines
5.2 KiB
C#

using System;
namespace TextureTests;
[TestClass]
public class TextureTest
{
[TestMethod]
public void Copy()
{
var src = Texture.Create( 1, 1 ).Finish();
var dst = Texture.Create( 1, 1 ).Finish();
try
{
Graphics.CopyTexture( src, dst );
}
catch ( Exception ex )
{
Assert.Fail( $"Valid CopyTexture call threw an exception: {ex}" );
}
try
{
Graphics.CopyTexture( src, dst, srcMipSlice: 0, srcArraySlice: 0, dstMipSlice: 0, dstArraySlice: 0 );
}
catch ( Exception ex )
{
Assert.Fail( $"Valid CopyTexture call threw an exception: {ex}" );
}
// Out-of-range mip on src
Assert.ThrowsException<ArgumentException>( () =>
{
Graphics.CopyTexture( src, dst, srcMipSlice: 1, srcArraySlice: 0, dstMipSlice: 0, dstArraySlice: 0 );
} );
// Out-of-range array slice on src
Assert.ThrowsException<ArgumentException>( () =>
{
Graphics.CopyTexture( src, dst, srcMipSlice: 0, srcArraySlice: 1, dstMipSlice: 0, dstArraySlice: 0 );
} );
// Out-of-range mip on dst
Assert.ThrowsException<ArgumentException>( () =>
{
Graphics.CopyTexture( src, dst, srcMipSlice: 0, srcArraySlice: 0, dstMipSlice: 1, dstArraySlice: 0 );
} );
// Out-of-range array slice on dst
Assert.ThrowsException<ArgumentException>( () =>
{
Graphics.CopyTexture( src, dst, srcMipSlice: 0, srcArraySlice: 0, dstMipSlice: 0, dstArraySlice: 1 );
} );
}
[TestMethod]
public void GetPixelsNegativeDimensions()
{
var texture = Texture.Create( 128, 128 ).Finish();
var buffer = new Color32[1];
Assert.ThrowsException<ArgumentException>( () =>
{
texture.GetPixels( (0, 0, -1, 1), 0, 0, buffer.AsSpan(), ImageFormat.RGBA8888 );
} );
Assert.ThrowsException<ArgumentException>( () =>
{
texture.GetPixels( (0, 0, 1, -1), 0, 0, buffer.AsSpan(), ImageFormat.RGBA8888 );
} );
Assert.ThrowsException<ArgumentException>( () =>
{
texture.GetPixels( (0, 0, -1, -1), 0, 0, buffer.AsSpan(), ImageFormat.RGBA8888 );
} );
Assert.ThrowsException<ArgumentException>( () =>
{
texture.GetPixels( (0, 0, 0, 1), 0, 0, buffer.AsSpan(), ImageFormat.RGBA8888 );
} );
Assert.ThrowsException<ArgumentException>( () =>
{
texture.GetPixels( (0, 0, 1, 0), 0, 0, buffer.AsSpan(), ImageFormat.RGBA8888 );
} );
}
[TestMethod]
public void GetPixels3DNegativeDimensions()
{
var texture = Texture.CreateVolume( 128, 128, 4 ).Finish();
var buffer = new Color32[1];
Assert.ThrowsException<ArgumentException>( () =>
{
texture.GetPixels3D( (0, 0, 0, -1, 1, 1), 0, buffer.AsSpan(), ImageFormat.RGBA8888 );
} );
Assert.ThrowsException<ArgumentException>( () =>
{
texture.GetPixels3D( (0, 0, 0, 1, -1, 1), 0, buffer.AsSpan(), ImageFormat.RGBA8888 );
} );
Assert.ThrowsException<ArgumentException>( () =>
{
texture.GetPixels3D( (0, 0, 0, 1, 1, -1), 0, buffer.AsSpan(), ImageFormat.RGBA8888 );
} );
Assert.ThrowsException<ArgumentException>( () =>
{
texture.GetPixels3D( (0, 0, 0, -1, -1, -1), 0, buffer.AsSpan(), ImageFormat.RGBA8888 );
} );
}
[TestMethod]
public void GetPixelsAsyncNegativeDimensions()
{
var texture = Texture.Create( 128, 128 ).Finish();
Assert.ThrowsException<ArgumentException>( () =>
{
texture.GetPixelsAsync<Color32>( _ => { }, ImageFormat.RGBA8888, (0, 0, -1, 1), 0, 0 );
} );
Assert.ThrowsException<ArgumentException>( () =>
{
texture.GetPixelsAsync<Color32>( _ => { }, ImageFormat.RGBA8888, (0, 0, 1, -1), 0, 0 );
} );
Assert.ThrowsException<ArgumentException>( () =>
{
texture.GetPixelsAsync<Color32>( _ => { }, ImageFormat.RGBA8888, (0, 0, -1, -1), 0, 0 );
} );
}
[TestMethod]
public void GetPixelsAsync3DNegativeDimensions()
{
var texture = Texture.CreateVolume( 128, 128, 4 ).Finish();
Assert.ThrowsException<ArgumentException>( () =>
{
texture.GetPixelsAsync3D<Color32>( _ => { }, ImageFormat.RGBA8888, (0, 0, 0, -1, 1, 1), 0 );
} );
Assert.ThrowsException<ArgumentException>( () =>
{
texture.GetPixelsAsync3D<Color32>( _ => { }, ImageFormat.RGBA8888, (0, 0, 0, 1, -1, 1), 0 );
} );
Assert.ThrowsException<ArgumentException>( () =>
{
texture.GetPixelsAsync3D<Color32>( _ => { }, ImageFormat.RGBA8888, (0, 0, 0, -1, -1, 1), 0 );
} );
Assert.ThrowsException<ArgumentException>( () =>
{
texture.GetPixelsAsync3D<Color32>( _ => { }, ImageFormat.RGBA8888, (0, 0, 0, 1, 1, -1), 0 );
} );
}
[TestMethod]
public void GetPixelsDestArrayBoundsCheck()
{
var texture = Texture.Create( 4, 4 ).Finish();
// Exactly-sized buffer for the whole texture should not trigger the bounds check.
// The native ReadTexturePixels fails in headless mode. However, we only want to
// validate that the ArgumentExcpetion does not get thrown for getting the entire
// texture.
var exactBuffer = new Color32[4 * 4];
try
{
texture.GetPixels( (0, 0, 4, 4), 0, 0, exactBuffer.AsSpan(), ImageFormat.RGBA8888, (0, 0, 4, 4), 4 );
}
catch ( ArgumentException )
{
Assert.Fail( "Should not reject a dest array that exactly fits the requested rect" );
}
catch ( Exception )
{
}
// Undersized buffer should throw
var tooSmallBuffer = new Color32[4 * 4 - 1];
Assert.ThrowsException<ArgumentException>( () =>
{
texture.GetPixels( (0, 0, 4, 4), 0, 0, tooSmallBuffer.AsSpan(), ImageFormat.RGBA8888, (0, 0, 4, 4), 4 );
} );
}
}