Skip to content

Commit

Permalink
Made BitMask reject negative indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
Lehonti Ramos committed Aug 7, 2023
1 parent baf4143 commit b043e5e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
9 changes: 8 additions & 1 deletion Pinta.Core/Effects/BitMask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,12 @@ public void Set (RectangleI rect, bool newValue)
Set (x, y, newValue);
}

private int GetIndex (int x, int y) => (y * Width) + x;
private int GetIndex (int x, int y)
{
if (x < 0)
throw new ArgumentOutOfRangeException (nameof(x));
if (y < 0)
throw new ArgumentOutOfRangeException (nameof (y));
return (y * Width) + x;
}
}
46 changes: 46 additions & 0 deletions tests/Pinta.Core.Tests/BitMaskTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Gtk;
using NUnit.Framework;

namespace Pinta.Core.Tests;
Expand Down Expand Up @@ -103,6 +104,44 @@ public void BitGetsSetXY (int maskWidth, int maskHeight, int bitToSetX, int bitT
}
}

[TestCaseSource (nameof (invalid_indexing))]
public void RejectsInvalidIndexing_PairIndexer (int width, int height, int x, int y)
{
var bitmask = new BitMask (width, height);
Assert.Throws<ArgumentOutOfRangeException> (() => _ = bitmask[x, y]);
}

[TestCaseSource (nameof (invalid_indexing))]
public void RejectsInvalidIndexing_PointIndexer (int width, int height, int x, int y)
{
var bitmask = new BitMask (width, height);
var point = new PointI (x, y);
Assert.Throws<ArgumentOutOfRangeException> (() => _ = bitmask[point]);
}

[TestCaseSource (nameof (invalid_indexing))]
public void RejectsInvalidIndexing_GetMethod (int width, int height, int x, int y)
{
var bitmask = new BitMask (width, height);
Assert.Throws<ArgumentOutOfRangeException> (() => _ = bitmask.Get (x, y));
}

[TestCaseSource (nameof (invalid_indexing))]
public void RejectsInvalidIndexing_Invert (int width, int height, int x, int y)
{
var bitmask = new BitMask (width, height);
Assert.Throws<ArgumentOutOfRangeException> (() => bitmask.Invert (x, y));
}

[TestCaseSource (nameof (invalid_indexing))]
public void RejectsInvalidIndexing_SetPair (int width, int height, int x, int y)
{
var bitmask1 = new BitMask (width, height);
var bitmask2 = new BitMask (width, height);
Assert.Throws<ArgumentOutOfRangeException> (() => bitmask1.Set (x, y, true));
Assert.Throws<ArgumentOutOfRangeException> (() => bitmask2.Set (x, y, false));
}

static readonly IReadOnlyList<TestCaseData> out_of_bounds_access_cases = new TestCaseData[]
{
new (0, 0),
Expand All @@ -122,4 +161,11 @@ public void BitGetsSetXY (int maskWidth, int maskHeight, int bitToSetX, int bitT
new (DEFAULT_SIZE, DEFAULT_OFFSET),
new (2, 1),
};

static readonly IReadOnlyList<TestCaseData> invalid_indexing = new TestCaseData[]
{
new (DEFAULT_WIDTH, DEFAULT_HEIGHT, -1, 0),
new (DEFAULT_WIDTH, DEFAULT_HEIGHT, 0, -1),
new (DEFAULT_WIDTH, DEFAULT_HEIGHT, -1, -1),
};
}

0 comments on commit b043e5e

Please sign in to comment.