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( () => { Graphics.CopyTexture( src, dst, srcMipSlice: 1, srcArraySlice: 0, dstMipSlice: 0, dstArraySlice: 0 ); } ); // Out-of-range array slice on src Assert.ThrowsException( () => { Graphics.CopyTexture( src, dst, srcMipSlice: 0, srcArraySlice: 1, dstMipSlice: 0, dstArraySlice: 0 ); } ); // Out-of-range mip on dst Assert.ThrowsException( () => { Graphics.CopyTexture( src, dst, srcMipSlice: 0, srcArraySlice: 0, dstMipSlice: 1, dstArraySlice: 0 ); } ); // Out-of-range array slice on dst Assert.ThrowsException( () => { 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( () => { texture.GetPixels( (0, 0, -1, 1), 0, 0, buffer.AsSpan(), ImageFormat.RGBA8888 ); } ); Assert.ThrowsException( () => { texture.GetPixels( (0, 0, 1, -1), 0, 0, buffer.AsSpan(), ImageFormat.RGBA8888 ); } ); Assert.ThrowsException( () => { texture.GetPixels( (0, 0, -1, -1), 0, 0, buffer.AsSpan(), ImageFormat.RGBA8888 ); } ); Assert.ThrowsException( () => { texture.GetPixels( (0, 0, 0, 1), 0, 0, buffer.AsSpan(), ImageFormat.RGBA8888 ); } ); Assert.ThrowsException( () => { 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( () => { texture.GetPixels3D( (0, 0, 0, -1, 1, 1), 0, buffer.AsSpan(), ImageFormat.RGBA8888 ); } ); Assert.ThrowsException( () => { texture.GetPixels3D( (0, 0, 0, 1, -1, 1), 0, buffer.AsSpan(), ImageFormat.RGBA8888 ); } ); Assert.ThrowsException( () => { texture.GetPixels3D( (0, 0, 0, 1, 1, -1), 0, buffer.AsSpan(), ImageFormat.RGBA8888 ); } ); Assert.ThrowsException( () => { 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( () => { texture.GetPixelsAsync( _ => { }, ImageFormat.RGBA8888, (0, 0, -1, 1), 0, 0 ); } ); Assert.ThrowsException( () => { texture.GetPixelsAsync( _ => { }, ImageFormat.RGBA8888, (0, 0, 1, -1), 0, 0 ); } ); Assert.ThrowsException( () => { texture.GetPixelsAsync( _ => { }, ImageFormat.RGBA8888, (0, 0, -1, -1), 0, 0 ); } ); } [TestMethod] public void GetPixelsAsync3DNegativeDimensions() { var texture = Texture.CreateVolume( 128, 128, 4 ).Finish(); Assert.ThrowsException( () => { texture.GetPixelsAsync3D( _ => { }, ImageFormat.RGBA8888, (0, 0, 0, -1, 1, 1), 0 ); } ); Assert.ThrowsException( () => { texture.GetPixelsAsync3D( _ => { }, ImageFormat.RGBA8888, (0, 0, 0, 1, -1, 1), 0 ); } ); Assert.ThrowsException( () => { texture.GetPixelsAsync3D( _ => { }, ImageFormat.RGBA8888, (0, 0, 0, -1, -1, 1), 0 ); } ); Assert.ThrowsException( () => { texture.GetPixelsAsync3D( _ => { }, 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( () => { texture.GetPixels( (0, 0, 4, 4), 0, 0, tooSmallBuffer.AsSpan(), ImageFormat.RGBA8888, (0, 0, 4, 4), 4 ); } ); } }