diff --git a/src/GameUtils/Types/Bitmap.cs b/src/GameUtils/Types/Bitmap.cs index 8a1abc2..71a9f5f 100644 --- a/src/GameUtils/Types/Bitmap.cs +++ b/src/GameUtils/Types/Bitmap.cs @@ -1,3 +1,4 @@ +using System; using System.Numerics; namespace GameUtils.Types; @@ -5,22 +6,39 @@ namespace GameUtils.Types; /// /// Bitmap image class /// -public class Bitmap(int width, int height) +public class Bitmap { /// /// Width of the image in pixels /// - public int Width { get; } = width; + public int Width { get; } /// /// Height of the image in pixels /// - public int Height { get; } = height; + public int Height { get; } /// /// Pixel data /// - public Vector3[] Data { get; } = new Vector3[width * height]; + public Vector3[] Data { get; } + + /// + /// Initializes a new instance of the class with the specified width and height. + /// + public Bitmap(int width, int height) + { + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(width); + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(height); + if ((long)width * height > int.MaxValue) + { + throw new ArgumentOutOfRangeException(nameof(height), "Width and height product exceeds maximum array size."); + } + + Width = width; + Height = height; + Data = new Vector3[width * height]; + } /// /// Gets or sets a pixel at the specified coordinates. If the coordinates are out of bounds, no operation is performed and Vector3.Zero is returned. diff --git a/tests/GameUtils.Tests/Types/BitmapTests.cs b/tests/GameUtils.Tests/Types/BitmapTests.cs index 73e49c7..4144f20 100644 --- a/tests/GameUtils.Tests/Types/BitmapTests.cs +++ b/tests/GameUtils.Tests/Types/BitmapTests.cs @@ -17,6 +17,17 @@ public void Write_WithTraversalPath_ThrowsUnauthorizedAccessException() Assert.ThrowsExactly(() => bitmap.Write(invalidPath)); } + [TestMethod] + [DataRow(-1, 10)] + [DataRow(0, 10)] + [DataRow(10, -1)] + [DataRow(10, 0)] + [DataRow(100000, 100000)] + public void Constructor_InvalidDimensions_ThrowsArgumentOutOfRangeException(int width, int height) + { + Assert.ThrowsExactly(() => new Bitmap(width, height)); + } + [TestMethod] public void Write_WithValidPath_DoesNotThrow() {