Skip to content

Commit f5c021d

Browse files
committed
Fixed SciSharp#293 and added unit test.
1 parent 8d09a18 commit f5c021d

3 files changed

Lines changed: 18 additions & 1 deletion

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,3 +353,4 @@ src/NumSharp.Python/NumSharpPy/NumSharp.dll
353353
docfx_project/src
354354
/coverage.xml
355355
test/NumSharp.Benchmark/LocalBenches.cs
356+
test/NumSharp.UnitTest/DevTests.cs

src/NumSharp.Core/Backends/TypedArrayStorage.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1185,12 +1185,14 @@ public void SetData(object value, params int[] indices)
11851185
{
11861186
if (np.isscalar(nd))
11871187
{
1188-
SetInternalValueUnsafe(nd.Array.GetValue(0), _Shape.GetIndexInShape(Slice, indices));
1188+
SetOrConvertInternalValue(nd.Array.GetValue(0), _Shape.GetIndexInShape(Slice, indices));
11891189
return;
11901190
}
11911191

11921192
var targetIndex = _Shape.GetIndexInShape(Slice, indices);
11931193

1194+
//todo! we need to handle more dims.
1195+
11941196
int offset = 0;
11951197
if (Shape.NDim == 1)
11961198
offset = targetIndex + (Slice?.Start ?? 0);

test/NumSharp.UnitTest/Selection/NDArray.Indexing.Test.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,5 +734,19 @@ public void Transpose10x10()
734734
}
735735
}).Should().NotThrow("It has to run completely.");
736736
}
737+
738+
/// <summary>
739+
/// Based on issue https://github.com/SciSharp/NumSharp/issues/293
740+
/// </summary>
741+
[TestMethod]
742+
public void CastingWhenSettingDifferentType()
743+
{
744+
NDArray output = np.zeros(5);
745+
double newValDouble = 2;
746+
int newValInt = 4;
747+
output[3] = newValDouble; // This works fine
748+
new Action(()=>output[4] = newValInt).Should().NotThrow<NullReferenceException>(); // throws System.NullReferenceException
749+
output.Array.GetValue(4).Should().Be(newValInt);
750+
}
737751
}
738752
}

0 commit comments

Comments
 (0)