Skip to content

Commit 3e6288c

Browse files
authored
Merge pull request #135 from ColmBhandal/bugfix/sparse-array-write-not-persisted
Fixed Sparse Array Default Write Persist Bug
2 parents 2af2624 + 79f990d commit 3e6288c

File tree

5 files changed

+50
-0
lines changed

5 files changed

+50
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4545
- Added a shift operator for sparse arrays (#122)
4646
- Added a pre-access wrapper, which wraps an object and ensures an action is run before any access to that object (#131)
4747
- Added zip methods for sparse array and sparse array 2D (#132)
48+
- Fixed bug: default values were not overwriting non-defaults for sparse arrays (#134)
4849

4950
### Added
5051
- Support for writing a 1D one-based array to a 2D one-based array (#9)

CsharpExtras/Map/Sparse/ISparseArray.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public interface ISparseArray<TVal>
2020
{
2121
/// <summary>
2222
/// Gets/sets the underlying value at the given sequence of coordinates
23+
/// Note: if a value is set to the default value, then it is no longer considered used
2324
/// </summary>
2425
/// <param name="coordinates">A sequence of coordinates which must match the dimension of this array</param>
2526
/// <returns>The last value that was written to those coordinates, or else the default if no value was written</returns>

CsharpExtras/Map/Sparse/SparseArrayImpl.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ private void SetValue(int[] coordinates, TVal value)
4141
ValidIndex[] validatedCoordinates = GetValidateCoordinated(coordinates);
4242
if (EqualityComparer<TVal>.Default.Equals(value, DefaultValue))
4343
{
44+
_backingDictionary.Remove(validatedCoordinates);
4445
return;
4546
}
4647
if (!_backingDictionary.Update(value, validatedCoordinates))

CsharpExtras/Map/Sparse/TwoDimensional/ISparseArray2D.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public interface ISparseArray2D<TVal>
1212
{
1313
/// <summary>
1414
/// Get or set the value at the given row, column coordinates
15+
/// Note: if a value is set to the default value, then it is no longer considered used
1516
/// </summary>
1617
TVal this[int row, int column] { get; set; }
1718

@@ -68,6 +69,7 @@ public interface ISparseArray2D<TVal>
6869
/// <summary>
6970
/// Sets an area to the given values at the given coordinates.
7071
/// If coordinates within the area are invalid, throws an exception.
72+
/// Note: If a value is set to the default value, then it is no longer considered used
7173
/// </summary>
7274
/// <param name="area">The are to write to this array</param>
7375
/// <param name="leftmostRow">The row coordinate of the top-left corner of the rectangle to which to write</param>

CsharpExtrasTest/Map/Sparse/SparseArrayTest.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,51 @@ public class SparseArrayTest
1717
{
1818
private ICsharpExtrasApi Api { get; } = new CsharpExtrasApi();
1919

20+
[Test]
21+
public void GIVEN_SparseArray_WHEN_SetToDefault_THEN_IsUsedIsFalse()
22+
{
23+
//Arrange
24+
ISparseArray<string> array = Api.NewSparseArrayBuilder((PositiveInteger)3, "").Build();
25+
array[1, 2, 3] = "Initial Non-default value";
26+
Assert.IsTrue(array.IsUsed(1, 2, 3),
27+
"GIVEN: Coordinates should be used initially as a non-default value exists there");
28+
29+
//Act
30+
array[1, 2, 3] = "";
31+
32+
//Assert
33+
Assert.IsFalse(array.IsUsed(1, 2, 3));
34+
}
35+
36+
[Test]
37+
public void GIVEN_SingletonArray_WHEN_SetUniqueValueToDefault_THEN_CountIsZero()
38+
{
39+
//Arrange
40+
ISparseArray<string> array = Api.NewSparseArrayBuilder((PositiveInteger)3, "").Build();
41+
array[1, 2, 3] = "Initial Non-default value";
42+
Assert.AreEqual(1, (int) array.UsedValuesCount, "GIVEN: Singleton array count should be 1");
43+
44+
//Act
45+
array[1, 2, 3] = "";
46+
47+
//Assert
48+
Assert.AreEqual(0, (int) array.UsedValuesCount, "After writing default value, used count should be zero");
49+
}
50+
51+
[Test]
52+
public void GIVEN_SparseArray_WHEN_SetToDefault_THEN_GetReturnsDefault()
53+
{
54+
//Arrange
55+
ISparseArray<string> array = Api.NewSparseArrayBuilder((PositiveInteger)3, "").Build();
56+
array[1, 2, 3] = "Initial Non-default value";
57+
58+
//Act
59+
array[1, 2, 3] = "";
60+
61+
//Assert
62+
Assert.AreEqual("", array[1, 2, 3]);
63+
}
64+
2065
[Test, TestCase(false, 0, 0, 0), TestCase(true, 3, 4, 6), TestCase(true, 1, 1, 1)]
2166
public void GIVEN_FilledArray_WHEN_IsUsedEnum_THEN_ExpectedResult(bool expected, params int[] coordinates)
2267
{

0 commit comments

Comments
 (0)