Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/NHibernate.Test/UtilityTest/SetSnapShotFixture.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System.Collections.Generic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using NHibernate.Collection.Generic.SetHelpers;
using NSubstitute.ExceptionExtensions;
using NUnit.Framework;

namespace NHibernate.Test.UtilityTest
Expand Down Expand Up @@ -70,6 +73,29 @@ public void TestCopyTo()
Assert.That(list, Is.EquivalentTo(array));
}

[Test]
public void TestCopyToObjectArray()
{
var list = new List<string> { "test1", null, "test2" };
ICollection sn = new SetSnapShot<string>(list);

var array = new object[3];
sn.CopyTo(array, 0);
Assert.That(list, Is.EquivalentTo(array));
}

[Test]
public void WhenCopyToIsCalledWithIncompatibleArrayTypeThenThrowArgumentOrInvalidCastException()
{
var list = new List<string> { "test1", null, "test2" };
ICollection sn = new SetSnapShot<string>(list);

var array = new int[3];
Assert.That(
() => sn.CopyTo(array, 0),
Throws.ArgumentException.Or.TypeOf<InvalidCastException>());
}

[Test]
public void TestSerialization()
{
Expand Down
30 changes: 24 additions & 6 deletions src/NHibernate/Collection/Generic/SetHelpers/SetSnapShot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,16 @@ IEnumerator IEnumerable.GetEnumerator()

void ICollection.CopyTo(Array array, int index)
{
if (!(array is T[] typedArray))
if (array is T[] typedArray)
{
throw new ArgumentException($"Array must be of type {typeof(T[])}.", nameof(array));
CopyTo(typedArray, index);
return;
}

CopyTo(typedArray, index);
if (_hasNull)
array.SetValue(default(T), index);
ICollection keysCollection = _values.Keys;
keysCollection.CopyTo(array, index + (_hasNull ? 1 : 0));
}

public int Count => _values.Count + (_hasNull ? 1 : 0);
Expand Down Expand Up @@ -153,12 +157,26 @@ protected SetSnapShot(SerializationInfo info, StreamingContext context) : base(i

void ICollection.CopyTo(Array array, int index)
{
if (!(array is T[] typedArray))
if (array is T[] typedArray)
{
throw new ArgumentException($"Array must be of type {typeof(T[])}.", nameof(array));
CopyTo(typedArray, index);
return;
}

CopyTo(typedArray, index);
if (array == null)
throw new ArgumentNullException(nameof(array));

if (index < 0)
throw new ArgumentOutOfRangeException(nameof(index), index, "Array index cannot be negative");

if (index > array.Length || Count > array.Length - index)
throw new ArgumentException("Provided array is too small", nameof(array));

foreach (var value in this)
{
array.SetValue(value, index);
index++;
}
}

bool ICollection.IsSynchronized => false;
Expand Down