Skip to content

Commit

Permalink
Fix boxing empty blittable arrays (#1158)
Browse files Browse the repository at this point in the history
* Fix boxing blittable empty arrays

* Update testwinrt repo
  • Loading branch information
manodasanW authored Apr 5, 2022
1 parent fc8fc3b commit 574fc76
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/Tests/UnitTest/TestComponentCSharp_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,28 @@ public void TestValueSet()
{
Assert.Equal(valueSet[item.Key], item.Value);
}
}

[Fact]
public void TestValueSetArrays()
{
var map = new Dictionary<string, long[]>
{
["foo"] = new long[] { 1, 2, 3 },
["hello"] = new long[0],
["world"] = new long[] { 1, 2, 3 },
["bar"] = new long[0]
};
var valueSet = new Windows.Foundation.Collections.ValueSet();
foreach (var item in map)
{
valueSet[item.Key] = item.Value;
}
Assert.Equal(map.Count, valueSet.Count);
foreach (var item in map)
{
Assert.Equal(valueSet[item.Key], item.Value);
}
}

[Fact]
Expand Down
68 changes: 68 additions & 0 deletions src/Tests/UnitTest/TestComponent_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,74 @@ public void Box_DateTime()
Box_type(DateTimeOffset.Now, Tests.Box17);
}

[Fact]
public void Box_LongArray()
{
long[] arr = new long[] { 2, 4, 6 };
Box_type(arr, Tests.Box18);

long[] arr2 = new long[] { 2, 4, 6 };
Box_type(arr2, Tests.Box18);
Box_type(arr2, Tests.Box18);

long[] arr3 = new long[0];
Box_type(arr3, Tests.Box18);

long[] arr4 = new long[0];
Box_type(arr4, Tests.Box18);
}

[Fact]
public void Box_BoolArray()
{
bool[] arr = new bool[] { true, false, true };
Box_type(arr, Tests.Box19);

bool[] arr2 = new bool[] { true, false, true };
Box_type(arr2, Tests.Box19);
Box_type(arr2, Tests.Box19);

bool[] arr3 = new bool[0];
Box_type(arr3, Tests.Box19);

bool[] arr4 = new bool[0];
Box_type(arr4, Tests.Box19);
}

[Fact]
public void Box_StringArray()
{
string[] arr = new string[] { "one", "two", "three" };
Box_type(arr, Tests.Box20);

string[] arr2 = new string[] { "four", "five", "six" };
Box_type(arr2, Tests.Box20);
Box_type(arr2, Tests.Box20);

string[] arr3 = new string[0];
Box_type(arr3, Tests.Box20);

string[] arr4 = new string[0];
Box_type(arr4, Tests.Box20);
}

[Fact]
public void Box_TimeSpanArray()
{
TimeSpan[] arr = new TimeSpan[] { TimeSpan.FromMilliseconds(4), TimeSpan.FromMilliseconds(5), TimeSpan.FromMilliseconds(6) };
Box_type(arr, Tests.Box21);

TimeSpan[] arr2 = new TimeSpan[] { TimeSpan.FromMilliseconds(4), TimeSpan.FromMilliseconds(5), TimeSpan.FromMilliseconds(6) };
Box_type(arr2, Tests.Box21);
Box_type(arr2, Tests.Box21);

TimeSpan[] arr3 = new TimeSpan[0];
Box_type(arr3, Tests.Box21);

TimeSpan[] arr4 = new TimeSpan[0];
Box_type(arr4, Tests.Box21);
}

[Fact]
public void Fast_Abi_Simple()
{
Expand Down
9 changes: 9 additions & 0 deletions src/WinRT.Runtime/Marshalers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,15 @@ public static unsafe T[] FromAbiArray(object box)
{
return null;
}

// For empty arrays, we can end up returning the same managed object
// when using ReadOnlySpan.ToArray. But a unique object is expected
// by the caller for RCW creation.
if (abi.length == 0)
{
return new T[0];
}

var abiSpan = new ReadOnlySpan<T>(abi.data.ToPointer(), abi.length);
return abiSpan.ToArray();
}
Expand Down
2 changes: 1 addition & 1 deletion src/get_testwinrt.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ git checkout -f master
if ErrorLevel 1 popd & exit /b !ErrorLevel!
git fetch -f
if ErrorLevel 1 popd & exit /b !ErrorLevel!
git reset -q --hard 53e87ecb7dd2fc883062ea9d95296768b604f058
git reset -q --hard d7acaf7d18d7902f7181ee094a27966b2b5b87f0
if ErrorLevel 1 popd & exit /b !ErrorLevel!
echo Restoring Nuget
%this_dir%.nuget\nuget.exe restore
Expand Down

0 comments on commit 574fc76

Please sign in to comment.