More nullability work on Core - incl unit tests

nullableenabled
David Hall 2023-09-03 08:46:51 -06:00
parent dcb28cb409
commit e74b89840c
17 changed files with 145 additions and 136 deletions

View File

@ -391,12 +391,13 @@ public class NativeMemoryStream : Stream
/// <summary>Writes the specified array into the stream.</summary>
/// <param name="items">The items.</param>
/// <param name="method">The packing method for the strings.</param>
public void Write(IEnumerable<string> items, StringListPackMethod method = StringListPackMethod.Concatenated)
public void Write(IEnumerable<string?> items, StringListPackMethod method = StringListPackMethod.Concatenated)
{
if (access == FileAccess.Read) throw new NotSupportedException();
if (items == null) return;
if (method == StringListPackMethod.Concatenated)
{
if (items.Any(s => s == null)) throw new ArgumentException("Cannot use Concatenated method with null strings.");
items.MarshalToPtr(method, i => { EnsureCapacity(Position + i); return PositionPtr; }, out var sz, CharSet);
position += sz;
length += sz;

View File

@ -13,7 +13,7 @@ public class SafeByteArray : SafeMemoryHandle<CoTaskMemoryMethods>, IList<byte>,
{
/// <summary>Initializes a new instance of the <see cref="SafeByteArray"/> class from a copy of a managed byte array.</summary>
/// <param name="array">The array of bytes to copy.</param>
public SafeByteArray(byte[] array) : base(array?.Length ?? 0)
public SafeByteArray(byte[]? array) : base(array?.Length ?? 0)
{
if (array != null)
Marshal.Copy(array, 0, handle, array.Length);

View File

@ -45,9 +45,9 @@ public class EventedListTests
l.Sort();
var x = l[27];
Assert.That(l.BinarySearch(x), Is.EqualTo(27));
Assert.That(l.BinarySearch(x, new CEClass()), Is.EqualTo(27));
Assert.That(l.BinarySearch(10, 40, x, new CEClass()), Is.EqualTo(27));
Assert.That(l.BinarySearch(50, 40, x, new CEClass()), Is.Negative);
Assert.That(l.BinarySearch(x, Comparer<EClass?>.Default), Is.EqualTo(27));
Assert.That(l.BinarySearch(10, 40, x, Comparer<EClass?>.Default), Is.EqualTo(27));
Assert.That(l.BinarySearch(50, 40, x, Comparer<EClass?>.Default), Is.Negative);
}
[Test()]
@ -74,7 +74,7 @@ public class EventedListTests
{
var a = new[] { new EClass(1), new EClass(2) };
var l = new EventedList<EClass>(a);
Assert.That(l.ConvertAll(e => new EClass(e.Prop - 1)).Count, Is.EqualTo(2));
Assert.That(l.ConvertAll(e => new EClass(e!.Prop - 1)).Count, Is.EqualTo(2));
}
[Test()]
@ -124,8 +124,8 @@ public class EventedListTests
{
var a = new[] { new EClass(1), new EClass(2) };
var l = new EventedList<EClass>(a);
Assert.That(l.Exists(e => e.Prop == 1));
Assert.That(!l.Exists(e => e.Prop == 7));
Assert.That(l.Exists(e => e!.Prop == 1));
Assert.That(!l.Exists(e => e!.Prop == 7));
}
[Test()]
@ -133,8 +133,8 @@ public class EventedListTests
{
var a = new[] { new EClass(1), new EClass(2) };
var l = new EventedList<EClass>(a);
Assert.That(l.FindAll(e => e.Prop >= 1), Is.EquivalentTo(l));
Assert.That(l.FindAll(e => e.Prop < 1), Is.Empty);
Assert.That(l.FindAll(e => e!.Prop >= 1), Is.EquivalentTo(l));
Assert.That(l.FindAll(e => e!.Prop < 1), Is.Empty);
}
[Test()]
@ -142,9 +142,9 @@ public class EventedListTests
{
var a = new[] { new EClass(1), new EClass(2) };
var l = new EventedList<EClass>(a);
Assert.That(l.FindIndex(e => e.Prop == 2), Is.EqualTo(1));
Assert.That(l.FindIndex(e => e.Prop == 7), Is.EqualTo(-1));
Assert.That(l.FindIndex(1, 1, e => e.Prop == 2), Is.EqualTo(1));
Assert.That(l.FindIndex(e => e!.Prop == 2), Is.EqualTo(1));
Assert.That(l.FindIndex(e => e!.Prop == 7), Is.EqualTo(-1));
Assert.That(l.FindIndex(1, 1, e => e!.Prop == 2), Is.EqualTo(1));
}
[Test()]
@ -152,9 +152,9 @@ public class EventedListTests
{
var a = new[] { new EClass(1), new EClass(2) };
var l = new EventedList<EClass>(a);
Assert.That(l.FindLastIndex(e => e.Prop == 1), Is.EqualTo(0));
Assert.That(l.FindLastIndex(e => e.Prop == 7), Is.EqualTo(-1));
Assert.That(l.FindLastIndex(1, 1, e => e.Prop == 2), Is.EqualTo(1));
Assert.That(l.FindLastIndex(e => e!.Prop == 1), Is.EqualTo(0));
Assert.That(l.FindLastIndex(e => e!.Prop == 7), Is.EqualTo(-1));
Assert.That(l.FindLastIndex(1, 1, e => e!.Prop == 2), Is.EqualTo(1));
}
[Test()]
@ -162,8 +162,8 @@ public class EventedListTests
{
var a = new[] { new EClass(1), new EClass(2) };
var l = new EventedList<EClass>(a);
Assert.That(l.FindLast(e => e.Prop == 1), Is.EqualTo(a[0]));
Assert.That(l.FindLast(e => e.Prop == 7), Is.Null);
Assert.That(l.FindLast(e => e!.Prop == 1), Is.EqualTo(a[0]));
Assert.That(l.FindLast(e => e!.Prop == 7), Is.Null);
}
[Test()]
@ -171,8 +171,8 @@ public class EventedListTests
{
var a = new[] { new EClass(1), new EClass(2) };
var l = new EventedList<EClass>(a);
Assert.That(l.Find(e => e.Prop == 1), Is.EqualTo(a[0]));
Assert.That(l.Find(e => e.Prop == 7), Is.Null);
Assert.That(l.Find(e => e!.Prop == 1), Is.EqualTo(a[0]));
Assert.That(l.Find(e => e!.Prop == 7), Is.Null);
}
[Test()]
@ -181,7 +181,7 @@ public class EventedListTests
var i = 0;
var a = new[] { new EClass(1), new EClass(2) };
var l = new EventedList<EClass>(a);
l.ForEach(e => i += e.Prop);
l.ForEach(e => i += e!.Prop);
Assert.That(i, Is.EqualTo(3));
}
@ -200,7 +200,7 @@ public class EventedListTests
var l = new EventedList<EClass>(a);
var r = l.GetRange(1, 2);
Assert.That(r.Count, Is.EqualTo(2));
Assert.That(r[0].Prop, Is.EqualTo(2));
Assert.That(r[0]!.Prop, Is.EqualTo(2));
}
[Test()]
@ -219,8 +219,8 @@ public class EventedListTests
var l = new EventedList<EClass>(a);
l.InsertRange(1, a);
Assert.That(l.Count, Is.EqualTo(4));
Assert.That(l[1].Prop, Is.EqualTo(1));
Assert.That(l[2].Prop, Is.EqualTo(2));
Assert.That(l[1]!.Prop, Is.EqualTo(1));
Assert.That(l[2]!.Prop, Is.EqualTo(2));
}
[Test()]
@ -229,7 +229,7 @@ public class EventedListTests
var a = new[] { new EClass(1), new EClass(2) };
var l = new EventedList<EClass>(a);
l.Insert(1, new EClass(3));
Assert.That(l[1].Prop, Is.EqualTo(3));
Assert.That(l[1]!.Prop, Is.EqualTo(3));
}
[Test()]
@ -247,9 +247,9 @@ public class EventedListTests
{
var a = new[] { new EClass(1), new EClass(2), new EClass(3) };
var l = new EventedList<EClass>(a);
l.RemoveAll(e => e.Prop < 3);
l.RemoveAll(e => e!.Prop < 3);
Assert.That(l.Count, Is.EqualTo(1));
Assert.That(l[0].Prop, Is.EqualTo(3));
Assert.That(l[0]!.Prop, Is.EqualTo(3));
}
[Test()]
@ -268,7 +268,7 @@ public class EventedListTests
var l = new EventedList<EClass>(a);
l.RemoveRange(0, 2);
Assert.That(l.Count, Is.EqualTo(1));
Assert.That(l[0].Prop, Is.EqualTo(3));
Assert.That(l[0]!.Prop, Is.EqualTo(3));
}
[Test()]
@ -286,8 +286,8 @@ public class EventedListTests
var a = new[] { new EClass(1), new EClass(2), new EClass(3) };
var l = new EventedList<EClass>(a);
l.Reverse();
Assert.That(l[0].Prop, Is.EqualTo(3));
Assert.That(l[2].Prop, Is.EqualTo(1));
Assert.That(l[0]!.Prop, Is.EqualTo(3));
Assert.That(l[2]!.Prop, Is.EqualTo(1));
}
[Test()]
@ -296,8 +296,8 @@ public class EventedListTests
var a = new[] { new EClass(1), new EClass(2), new EClass(3) };
var l = new EventedList<EClass>(a);
l.Reverse(1, 2);
Assert.That(l[0].Prop, Is.EqualTo(1));
Assert.That(l[2].Prop, Is.EqualTo(2));
Assert.That(l[0]!.Prop, Is.EqualTo(1));
Assert.That(l[2]!.Prop, Is.EqualTo(2));
}
[Test()]
@ -306,8 +306,8 @@ public class EventedListTests
var a = new[] { new EClass(3), new EClass(1), new EClass(0) };
var l = new EventedList<EClass>(a);
l.Sort();
Assert.That(l[0].Prop, Is.EqualTo(0));
Assert.That(l[2].Prop, Is.EqualTo(3));
Assert.That(l[0]!.Prop, Is.EqualTo(0));
Assert.That(l[2]!.Prop, Is.EqualTo(3));
}
[Test()]
@ -315,9 +315,9 @@ public class EventedListTests
{
var a = new[] { new EClass(3), new EClass(1), new EClass(0) };
var l = new EventedList<EClass>(a);
l.Sort(1, 2, new CEClass());
Assert.That(l[0].Prop, Is.EqualTo(3));
Assert.That(l[2].Prop, Is.EqualTo(1));
l.Sort(1, 2, Comparer<EClass?>.Default);
Assert.That(l[0]!.Prop, Is.EqualTo(3));
Assert.That(l[2]!.Prop, Is.EqualTo(1));
}
[Test()]
@ -344,25 +344,17 @@ public class EventedListTests
{
var a = new[] { new EClass(1), new EClass(2), new EClass(3) };
var l = new EventedList<EClass>(a);
Assert.That(l.TrueForAll(e => e.Prop > 1), Is.False);
Assert.That(l.TrueForAll(e => e.Prop > 0), Is.True);
}
private class CEClass : Comparer<EClass>
{
public override int Compare(EClass x, EClass y) => x.CompareTo(y);
Assert.That(l.TrueForAll(e => e!.Prop > 1), Is.False);
Assert.That(l.TrueForAll(e => e!.Prop > 0), Is.True);
}
private class EClass : INotifyPropertyChanged, IComparable<EClass>
{
private int p;
public EClass(int prop = 0)
{
p = prop;
}
public EClass(int prop = 0) => p = prop;
public event PropertyChangedEventHandler PropertyChanged;
public event PropertyChangedEventHandler? PropertyChanged;
public int Prop
{
@ -374,16 +366,9 @@ public class EventedListTests
}
}
public int CompareTo(EClass other)
{
if (ReferenceEquals(this, other)) return 0;
if (ReferenceEquals(null, other)) return 1;
return p.CompareTo(other.p);
}
public int CompareTo(EClass? other) => ReferenceEquals(this, other) ? 0 : other is null ? 1 : p.CompareTo(other.p);
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null) =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}

View File

@ -4,6 +4,7 @@ using System.Linq;
namespace Vanara.Collections.Generic.Tests;
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
[TestFixture()]
public class HashSetTests
{

View File

@ -63,11 +63,11 @@ public class HistoryTests
Assert.That(history.CanSeekBackward, Is.False);
Assert.That(() => history.Current, Throws.Exception);
void GetPropVal(object sender, PropertyChangedEventArgs e)
void GetPropVal(object? sender, PropertyChangedEventArgs e)
{
var pi = history.GetType().GetProperty(e.PropertyName);
object obj = null;
try { obj = pi.GetValue(history); } catch (Exception ex) { obj = ex.GetType().Name; }
var pi = history.GetType().GetProperty(e.PropertyName!);
object? obj = null;
try { obj = pi!.GetValue(history); } catch (Exception ex) { obj = ex.GetType().Name; }
TestContext.WriteLine($"{e.PropertyName}={obj}");
}
}

View File

@ -15,7 +15,7 @@ public class IOExtensionsTests
bw.Write(257);
bw.Write(new RECT(1, 1, 1, 1));
bw.Write(new PRECT(1, 1, 1, 1));
bw.Write<string>(null);
bw.Write<string?>(null);
Assert.That(() => bw.Write(DateTime.Today), Throws.ArgumentException);
var buf = ms.ToArray();
Assert.That(buf.Length == Marshal.SizeOf(typeof(int)) + Marshal.SizeOf(typeof(RECT)) + Marshal.SizeOf(typeof(PRECT)));

View File

@ -49,7 +49,7 @@ public class InteropExtensionsTests
Assert.AreEqual(Marshal.ReadInt32((IntPtr)h, 5 * intSz), 10);
Assert.AreEqual(Marshal.ReadInt32((IntPtr)h, 7 * intSz), 12);
var ro = ((IntPtr)h).ToArray<RECT>(2, intSz);
Assert.AreEqual(ro.Length, 2);
Assert.AreEqual(ro!.Length, 2);
Assert.AreEqual(ro[0].left, 0);
Assert.AreEqual(ro[1].right, 12);
}
@ -97,7 +97,7 @@ public class InteropExtensionsTests
var chSz = 2;
Assert.That(a, Is.EqualTo(chSz * (rs[0].Length + 1) * rs.Length + chSz + intSz));
var ro = h.ToByteArray(a - intSz, intSz);
var chars = Encoding.Unicode.GetChars(ro);
var chars = Encoding.Unicode.GetChars(ro!);
Assert.That(chars.Length, Is.EqualTo((a - intSz) / chSz));
Assert.That(chars[0], Is.EqualTo('s'));
Assert.That(chars[4], Is.EqualTo('\0'));
@ -110,7 +110,7 @@ public class InteropExtensionsTests
chSz = 1;
Assert.That(a, Is.EqualTo(chSz * (rs[0].Length + 1) * rs.Length + chSz + intSz));
ro = h.ToByteArray(a - intSz, intSz);
chars = Encoding.ASCII.GetChars(ro);
chars = Encoding.ASCII.GetChars(ro!);
Assert.That(chars.Length, Is.EqualTo((a - intSz) / chSz));
Assert.That(chars[0], Is.EqualTo('s'));
Assert.That(chars[4], Is.EqualTo('\0'));
@ -123,7 +123,7 @@ public class InteropExtensionsTests
Assert.That(a, Is.EqualTo(intSz + 2));
Assert.That(() => new[] { "" }.MarshalToPtr(StringListPackMethod.Concatenated, Marshal.AllocHGlobal, out a, CharSet.Unicode, intSz), Throws.ArgumentException);
Assert.That(() => new string[] { null }.MarshalToPtr(StringListPackMethod.Concatenated, Marshal.AllocHGlobal, out a, CharSet.Unicode, intSz), Throws.ArgumentException);
Assert.That(() => new string?[] { null }.MarshalToPtr(StringListPackMethod.Concatenated, Marshal.AllocHGlobal, out a, CharSet.Unicode, intSz), Throws.ArgumentException);
}
finally
{
@ -185,7 +185,7 @@ public class InteropExtensionsTests
var rs = new[] { 10, 11, 12, 13, 14 };
var h = SafeHGlobalHandle.CreateFromList(rs, rs.Length, intSz);
var ro = ((IntPtr)h).ToArray<int>(4, intSz);
Assert.That(ro.Length, Is.EqualTo(4));
Assert.That(ro!.Length, Is.EqualTo(4));
Assert.That(ro[2], Is.EqualTo(rs[2]));
Assert.That(((IntPtr)h).ToArray<int>(0, intSz), Is.Empty);
@ -288,7 +288,7 @@ public class InteropExtensionsTests
Assert.That(() => ptr.ToStringEnum(CharSet.Unicode, intSz, sa.Size - 5).ToArray(), Throws.TypeOf<InsufficientMemoryException>());
Assert.That(() => ptr.ToStringEnum(CharSet.Unicode, intSz, sa.Size - 1).ToArray(), Throws.TypeOf<InsufficientMemoryException>());
}
using (var sa = SafeHGlobalHandle.CreateFromStringList(null, StringListPackMethod.Concatenated, CharSet.Unicode, intSz))
using (var sa = SafeHGlobalHandle.CreateFromStringList(Enumerable.Empty<string>(), StringListPackMethod.Concatenated, CharSet.Unicode, intSz))
{
var ptr = sa.DangerousGetHandle();
Assert.That(ptr, Is.Not.EqualTo(IntPtr.Zero));
@ -304,21 +304,21 @@ public class InteropExtensionsTests
public void ToStringEnumPackTest()
{
var rs = new[] { "str1", "str2", null, "", "str3" };
using (var sa = SafeHGlobalHandle.CreateFromStringList(rs, StringListPackMethod.Packed, CharSet.Ansi, intSz))
using (var sa = SafeHGlobalHandle.CreateFromStringList(rs!, StringListPackMethod.Packed, CharSet.Ansi, intSz))
{
var ptr = sa.DangerousGetHandle();
Assert.That(ptr, Is.Not.EqualTo(IntPtr.Zero));
var se = ptr.ToStringEnum(rs.Length, CharSet.Ansi, intSz);
Assert.That(se, Is.EquivalentTo(rs));
}
using (var sa = SafeHGlobalHandle.CreateFromStringList(rs, StringListPackMethod.Packed, CharSet.Unicode, intSz))
using (var sa = SafeHGlobalHandle.CreateFromStringList(rs!, StringListPackMethod.Packed, CharSet.Unicode, intSz))
{
var ptr = sa.DangerousGetHandle();
Assert.That(ptr, Is.Not.EqualTo(IntPtr.Zero));
var se = ptr.ToStringEnum(rs.Length, CharSet.Unicode, intSz);
Assert.That(se, Is.EquivalentTo(rs));
}
using (var sa = SafeHGlobalHandle.CreateFromStringList(null, StringListPackMethod.Packed, CharSet.Unicode, intSz))
using (var sa = SafeHGlobalHandle.CreateFromStringList(Enumerable.Empty<string>(), StringListPackMethod.Packed, CharSet.Unicode, intSz))
{
var ptr = sa.DangerousGetHandle();
Assert.That(ptr, Is.Not.EqualTo(IntPtr.Zero));
@ -330,14 +330,14 @@ public class InteropExtensionsTests
[TestCase("Some string value")]
[TestCase("")]
[TestCase((string)null)]
public void ToInsecureStringTest(string sval)
[TestCase((string?)null)]
public void ToInsecureStringTest(string? sval)
{
var ss = sval.ToSecureString();
var ss = sval?.ToSecureString();
if (sval != null)
{
Assert.That(ss, Is.Not.Null);
Assert.That(ss.Length, Is.EqualTo(sval.Length));
Assert.That(ss!.Length, Is.EqualTo(sval.Length));
var s = ss.ToInsecureString();
Assert.That(s, Is.EqualTo(sval));
}
@ -351,20 +351,20 @@ public class InteropExtensionsTests
public void ToInsecureStringTest()
{
Assert.That(IntPtr.Zero.ToSecureString(4), Is.Null);
Assert.That(((System.Security.SecureString)null).ToInsecureString(), Is.Null);
Assert.That(((System.Security.SecureString?)null)?.ToInsecureString(), Is.Null);
}
[TestCase("Some string value")]
[TestCase("")]
[TestCase((string)null)]
public void ToSecureStringTest(string sval)
[TestCase((string?)null)]
public void ToSecureStringTest(string? sval)
{
var ms = new SafeCoTaskMemString(sval);
var ss = ms.DangerousGetHandle().ToSecureString();
if (sval != null)
{
Assert.That(ss, Is.Not.Null);
Assert.That(ss.Length, Is.EqualTo(sval.Length));
Assert.That(ss!.Length, Is.EqualTo(sval.Length));
var s = ss.ToInsecureString();
Assert.That(s, Is.EqualTo(sval));
@ -372,7 +372,7 @@ public class InteropExtensionsTests
{
ss = ms.DangerousGetHandle().ToSecureString(1);
Assert.That(ss, Is.Not.Null);
Assert.That(ss.Length, Is.EqualTo(1));
Assert.That(ss!.Length, Is.EqualTo(1));
s = ss.ToInsecureString();
Assert.That(s, Is.EqualTo(sval.Substring(0, 1)));
}
@ -404,7 +404,7 @@ public class InteropExtensionsTests
var h = mem.DangerousGetHandle();
// null
Assert.That(h.Write((object)null), Is.EqualTo(0));
Assert.That(h.Write((object?)null), Is.EqualTo(0));
// bytes
Assert.That(h.Write((object)new byte[] { 1, 2, 4, 5 }), Is.EqualTo(4));

View File

@ -13,7 +13,7 @@ public class ReflectionExtensionsTests
{
public class X
{
public string Str { get; set; }
public string? Str { get; set; }
}
[Test]
@ -36,7 +36,7 @@ public class ReflectionExtensionsTests
Assert.That(dt.GetPropertyValue("Ticks", 0L), Is.EqualTo(dt.Ticks));
Assert.That(dt.GetPropertyValue<long>("Ticks"), Is.EqualTo(dt.Ticks));
// Has private value
Assert.That(dt.GetPropertyValue<long>("InternalTicks"), Is.EqualTo(dt.Ticks));
Assert.That(dt.GetPropertyValue<ulong>("UTicks"), Is.EqualTo(dt.Ticks));
// Bad propname
Assert.That(() => dt.GetPropertyValue<long?>("Tacks"), Throws.Exception);
Assert.That(() => dt.GetPropertyValue<long?>("Tacks", null), Is.Null);
@ -182,17 +182,19 @@ public class ReflectionExtensionsTests
Assert.That(dt.InvokeMethod<long>(new object[] { 2017, 1, 1 }, "ToBinary"), Is.Not.EqualTo(0));
Assert.That(dt.InvokeMethod<DateTime>(new object[] { 2017, 1, 1 }, "AddHours", (double)1), Is.EqualTo(new DateTime(2017, 1, 1) + TimeSpan.FromHours(1)));
}
[Test()]
public void InvokeNotOverrideTest()
{
var t = new TestDerived();
var mi = typeof(TestBase).GetMethod("GetValue", Type.EmptyTypes);
Assert.That(mi.InvokeNotOverride(t), Is.EqualTo(0));
Assert.That(() => mi.InvokeNotOverride(t, 2), Throws.Exception);
Assert.NotNull(mi);
Assert.That(mi!.InvokeNotOverride(t), Is.EqualTo(0));
Assert.That(() => mi!.InvokeNotOverride(t, 2), Throws.Exception);
mi = typeof(TestBase).GetMethod("GetValue", new[] { typeof(int), typeof(int) });
Assert.That(mi.InvokeNotOverride(t, 1, 2), Is.EqualTo(0));
Assert.That(mi!.InvokeNotOverride(t, 1, 2), Is.EqualTo(0));
mi = typeof(TestBase).GetMethod("GetValue", new[] { typeof(int), typeof(int) });
Assert.That(() => mi.InvokeNotOverride(t, 'c', 2), Throws.Exception);
Assert.That(() => mi!.InvokeNotOverride(t, 'c', 2), Throws.Exception);
}
[Test()]

View File

@ -21,7 +21,9 @@ public class GenericSafeHandleTests
[Test]
public void GenericSafeHandleCloseMethodNull()
{
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
Assert.Throws<ArgumentNullException>(() => new GenericSafeHandle((IntPtr)1, null));
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
}
[Test]

View File

@ -52,12 +52,14 @@ namespace Vanara.InteropServices.Tests;
Assert.That(ms.Position, Is.EqualTo(sizeof(int)));
ms.Seek(0, SeekOrigin.Begin);
byte[] ba = new byte[] { 0x2 };
Assert.That(() => ms.Poke(null, 0), Throws.ArgumentNullException);
Assert.That(() => ms.Poke(ba, 1000), Throws.ArgumentException);
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
Assert.That(() => ms.Poke(null, 0), Throws.ArgumentNullException);
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
Assert.That(() => ms.Poke(ba, 1000), Throws.ArgumentException);
Assert.That(() => ms.Poke(ba, -1), Throws.TypeOf<ArgumentOutOfRangeException>());
ms.Poke(ba, 1);
Assert.That(ms.Read<int>(), Is.EqualTo(0x00000102));
Assert.That(() => ms.Read<ulong>(), Throws.TypeOf<InsufficientMemoryException>());
Assert.That(ms.Read<ulong>, Throws.TypeOf<InsufficientMemoryException>());
}
[Test()]
@ -77,8 +79,10 @@ namespace Vanara.InteropServices.Tests;
byte[] buf = new byte[24];
ms.Read(buf, 0, buf.Length);
Assert.That(buf, Is.EquivalentTo(new byte[] { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3 }));
Assert.That(() => ms.Read(null, 0, 0), Throws.ArgumentNullException);
Assert.That(() => ms.Read(buf, 0, 30), Throws.ArgumentException);
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
Assert.That(() => ms.Read(null, 0, 0), Throws.ArgumentNullException);
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
Assert.That(() => ms.Read(buf, 0, 30), Throws.ArgumentException);
Assert.That(() => ms.Read(buf, -1, 0), Throws.TypeOf<ArgumentOutOfRangeException>());
ms.Position = m.Size - 10;
Assert.That(() => ms.Read(buf, 0, buf.Length), Throws.Nothing);
@ -101,15 +105,17 @@ namespace Vanara.InteropServices.Tests;
using (SafeHGlobalHandle m = new(10))
using (MarshalingStream ms = new(m, m.Size))
{
Assert.That(() => ms.Write(null, 0, 0), Throws.ArgumentNullException);
byte[] bytes = new byte[] { 0, 0, 0, 0, 0, 0, 0, 3 };
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
Assert.That(() => ms.Write(null, 0, 0), Throws.ArgumentNullException);
byte[] bytes = new byte[] { 0, 0, 0, 0, 0, 0, 0, 3 };
Assert.That(() => ms.Write(bytes, 1, 8), Throws.ArgumentException);
Assert.That(() => ms.Write(bytes, -1, 8), Throws.TypeOf<ArgumentOutOfRangeException>());
Assert.That(() => ms.Write(bytes, 1, -8), Throws.TypeOf<ArgumentOutOfRangeException>());
Assert.That(() => ms.Write(new byte[22]), Throws.TypeOf<InsufficientMemoryException>());
ms.Write((SafeHGlobalHandle)null);
ms.Write((SafeHGlobalHandle?)null);
Assert.That(ms.Position, Is.Zero);
ms.Write((string[])null);
ms.Write((string[]?)null);
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
Assert.That(ms.Position, Is.Zero);
Assert.That(() => ms.Write(0L), Throws.Nothing);
}

View File

@ -5,6 +5,7 @@ using System.Linq;
namespace Vanara.InteropServices.Tests;
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
[TestFixture()]
public class NativeMemoryStreamTests
{
@ -76,7 +77,7 @@ public class NativeMemoryStreamTests
Assert.That(() => ms.Read<int>(), Throws.InstanceOf<ObjectDisposedException>());
Assert.That(() => ms.Read(typeof(int)), Throws.InstanceOf<ObjectDisposedException>());
Assert.That(() => ms.Read(new byte[3], 0, 3), Throws.InstanceOf<ObjectDisposedException>());
Assert.That(() => ms.ReadByte(), Throws.InstanceOf<ObjectDisposedException>());
Assert.That(ms.ReadByte, Throws.InstanceOf<ObjectDisposedException>());
Assert.That(() => ms.ReadArray(typeof(byte), 1, false), Throws.InstanceOf<ObjectDisposedException>());
Assert.That(() => ms.ReadReference<int>(), Throws.InstanceOf<ObjectDisposedException>());
Assert.That(() => ms.ReadReference<string>(), Throws.InstanceOf<ObjectDisposedException>());
@ -201,7 +202,7 @@ public class NativeMemoryStreamTests
Assert.That(() => ms.Read<int>(), Throws.InstanceOf<NotSupportedException>());
Assert.That(() => ms.Read(typeof(int)), Throws.InstanceOf<NotSupportedException>());
Assert.That(() => ms.Read(new byte[3], 0, 3), Throws.InstanceOf<NotSupportedException>());
Assert.That(() => ms.ReadByte(), Throws.InstanceOf<NotSupportedException>());
Assert.That(ms.ReadByte, Throws.InstanceOf<NotSupportedException>());
Assert.That(() => ms.ReadArray(typeof(byte), 1, false), Throws.InstanceOf<NotSupportedException>());
Assert.That(() => ms.ReadReference<int>(), Throws.InstanceOf<NotSupportedException>());
Assert.That(() => ms.ReadReference<string>(), Throws.InstanceOf<NotSupportedException>());
@ -219,14 +220,14 @@ public class NativeMemoryStreamTests
Assert.That(() => ms.WriteReference(testStr), Throws.Nothing);
Assert.That(ms.Length, Is.EqualTo(24 + IntPtr.Size * 2));
Assert.That(ms.Position, Is.EqualTo(IntPtr.Size * 2));
Assert.That(() => ms.WriteReference((string)null), Throws.Nothing);
Assert.That(() => ms.WriteReference((string?)null), Throws.Nothing);
Assert.That(ms.Length, Is.EqualTo(24 + IntPtr.Size * 3));
Assert.That(ms.Position, Is.EqualTo(IntPtr.Size * 3));
var ptrs = ms.Pointer.ToArray<IntPtr>(3);
Assert.That(ptrs, Is.EquivalentTo(new[] { IntPtr.Zero, IntPtr.Zero, IntPtr.Zero }));
ms.Flush();
Assert.That(ms.Capacity, Is.EqualTo((ms.Length / 20 + 1) * 20));
ptrs = ms.Pointer.ToArray<IntPtr>(3);
ptrs = ms.Pointer.ToArray<IntPtr>(3)!;
Assert.That(ptrs, Is.Not.EquivalentTo(new[] { IntPtr.Zero, IntPtr.Zero, IntPtr.Zero }));
Assert.That(StringHelper.GetString(ptrs[0], ms.CharSet), Is.EqualTo(""));
Assert.That(StringHelper.GetString(ptrs[1], ms.CharSet), Is.EqualTo(testStr));
@ -313,7 +314,7 @@ public class NativeMemoryStreamTests
public void StringEnumTest()
{
var abc = new[] { "A", "B", "C" };
var a_c = new[] { "A", null, "C" };
string?[] a_c = new[] { "A", null, "C" };
using var ms = new NativeMemoryStream(128, 128);
Assert.That(() => ms.Write(null, StringListPackMethod.Concatenated), Throws.Nothing);
@ -423,7 +424,7 @@ public class NativeMemoryStreamTests
Assert.That(() => ms.ReadArray(null, 0, false), Throws.ArgumentNullException);
Assert.That(() => ms.ReadArray(typeof(int), -1, false), Throws.InstanceOf<ArgumentOutOfRangeException>());
Assert.That(() => ms.ReadArray(typeof(int), 0, false), Throws.Nothing);
Assert.That(ms.ReadArray(typeof(int), 0, false).Length, Is.Zero);
Assert.That(ms.ReadArray(typeof(int), 0, false)!.Length, Is.Zero);
Assert.That(() => ms.ReadArray(typeof(Guid), 100, false), Throws.InstanceOf<ArgumentOutOfRangeException>());
Assert.That(ms.ReadArray(typeof(long), arr.Length, false), Is.EquivalentTo(arr));

View File

@ -19,7 +19,7 @@ public class SafeByteArrayTests
Assert.That(a[4] == 4);
Assert.That(a.IsReadOnly, Is.False);
byte[] nullBytes = null;
byte[]? nullBytes = null;
a = new SafeByteArray(nullBytes);
Assert.That(a.Count == 0);
}
@ -44,7 +44,9 @@ public class SafeByteArrayTests
Assert.That(a.Count == bytes.Length);
Assert.That(a[4] == 4);
#pragma warning disable CS8604 // Possible null reference argument.
Assert.That(() => new SafeByteArray(t), Throws.ArgumentNullException);
#pragma warning restore CS8604 // Possible null reference argument.
a.Dispose();
Assert.That(() => new SafeByteArray(a), Throws.ArgumentException);
@ -72,11 +74,12 @@ public class SafeByteArrayTests
Assert.That(() => l.RemoveAt(0), Throws.TypeOf<NotSupportedException>());
//Assert.That(l.Count, Is.EqualTo(16));
var gl = l as IList<byte>;
var gl = (IList<byte>)l;
Assert.That(() => gl.Insert(0, 100), Throws.TypeOf<NotSupportedException>());
Assert.That(() => gl.RemoveAt(0), Throws.TypeOf<NotSupportedException>());
}
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
[Test]
public void ICollectionPropTest()
{
@ -113,6 +116,7 @@ public class SafeByteArrayTests
Assert.That(c.CompareTo(sb3, Comparer<byte>.Default), Is.LessThan(0));
Assert.That(c.CompareTo(new List<byte>(bytes), Comparer<byte>.Default), Is.EqualTo(0));
}
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
[Test]
public void IStructuralEquatablePropTest()
@ -122,8 +126,10 @@ public class SafeByteArrayTests
var iec = EqualityComparer<byte>.Default;
Assert.That(e.Equals(sb, iec), Is.True);
var h1 = e.GetHashCode(iec);
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
Assert.That(() => e.GetHashCode(null), Throws.ArgumentNullException);
Assert.That(() => e.Equals(null, null), Throws.ArgumentNullException);
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
Assert.That(e.Equals(null, iec), Is.False);
Assert.That(() => e.Equals(1, iec), Throws.TypeOf<ArgumentOutOfRangeException>());
var sb2 = new SafeByteArray(3);
@ -159,7 +165,7 @@ public class SafeByteArrayTests
var a = t.Clone() as SafeByteArray;
t = null;
Assert.That(a?.Count == bytes.Length);
Assert.That(a[4] == 4);
Assert.That(a![4] == 4);
}
[Test()]
@ -184,7 +190,9 @@ public class SafeByteArrayTests
var b3 = new byte[8];
Assert.That(() => t.CopyTo(b3, 0), Throws.TypeOf<ArgumentOutOfRangeException>());
Assert.That(() => ((ICollection)t).CopyTo(new byte[4,4], 0), Throws.TypeOf<ArgumentOutOfRangeException>());
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
Assert.That(() => t.CopyTo(null, 0), Throws.ArgumentNullException);
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
}
[Test()]

View File

@ -1,4 +1,5 @@
using NUnit.Framework;
using System.Linq;
using System.Security;
using Vanara.PInvoke;
@ -65,7 +66,7 @@ public class SafeCoTaskMemHandleTests
Assert.That((int)h.Size, Is.EqualTo(IntPtr.Size + r.Length * (4 + IntPtr.Size)));
Assert.That(h.ToStringEnum(4, CharSet.Unicode), Has.Exactly(4).EqualTo("5").And.Exactly(4).Items);
h = SafeCoTaskMemHandle.CreateFromStringList(null);
h = SafeCoTaskMemHandle.CreateFromStringList(Enumerable.Empty<string>());
Assert.That((int)h.Size, Is.EqualTo(StringHelper.GetCharSize()));
}

View File

@ -10,22 +10,22 @@ public class SafeCoTaskMemStringTests
public void SafeCoTaskMemStringTest()
{
const int sz = 100;
var cs = new string('x', sz);
string cs = new string('x', sz);
var s = new SafeCoTaskMemString(cs);
SafeCoTaskMemString s = new SafeCoTaskMemString(cs);
Assert.That(s.Capacity, Is.EqualTo(sz + 1));
Assert.That((int)s.Size, Is.EqualTo(2 * (sz + 1)));
Assert.That((string)s, Is.EqualTo(cs));
Assert.That((string?)s, Is.EqualTo(cs));
Assert.That((IntPtr)s, Is.Not.EqualTo(IntPtr.Zero));
s = new SafeCoTaskMemString(cs, CharSet.Ansi);
Assert.That(s.Capacity, Is.EqualTo(sz + 1));
Assert.That((int)s.Size, Is.EqualTo(sz + 1));
Assert.That((string)s, Is.EqualTo(cs));
Assert.That((string?)s, Is.EqualTo(cs));
s = new SafeCoTaskMemString((string)null);
s = new SafeCoTaskMemString((string?)null);
Assert.That(s.Capacity, Is.EqualTo(0));
Assert.That((string)s, Is.Null);
Assert.That((string?)s, Is.Null);
Assert.That((IntPtr)s, Is.EqualTo(IntPtr.Zero));
}
@ -35,11 +35,11 @@ public class SafeCoTaskMemStringTests
const int sz = 100;
var s = new SafeCoTaskMemString(sz);
Assert.That(s.Capacity, Is.EqualTo(sz));
Assert.That((string)s, Is.EqualTo(string.Empty));
Assert.That((string?)s, Is.EqualTo(string.Empty));
s = new SafeCoTaskMemString(sz, CharSet.Ansi);
Assert.That(s.Capacity, Is.EqualTo(sz));
Assert.That((string)s, Is.EqualTo(string.Empty));
Assert.That((string?)s, Is.EqualTo(string.Empty));
}
[Test()]
@ -51,11 +51,13 @@ public class SafeCoTaskMemStringTests
var s = new SafeCoTaskMemString(ss);
Assert.That(s.Capacity, Is.EqualTo(sz));
Assert.That((string)s, Is.EqualTo(new string('x', sz)));
Assert.That((string?)s, Is.EqualTo(new string('x', sz)));
ss = null;
#pragma warning disable CS8604 // Possible null reference argument.
s = new SafeCoTaskMemString(ss, CharSet.Ansi);
Assert.That((string)s, Is.Null);
#pragma warning restore CS8604 // Possible null reference argument.
Assert.That((string?)s, Is.Null);
Assert.That(s.Capacity, Is.Zero);
}

View File

@ -66,7 +66,7 @@ public class SafeHGlobalHandleTests
Assert.That((int)h.Size, Is.EqualTo(IntPtr.Size + r.Length * (4 + IntPtr.Size)));
Assert.That(h.ToStringEnum(4, CharSet.Unicode), Has.Exactly(4).EqualTo("5").And.Exactly(4).Items);
h = SafeHGlobalHandle.CreateFromStringList(null);
h = SafeHGlobalHandle.CreateFromStringList(Enumerable.Empty<string>());
Assert.That((int)h.Size, Is.EqualTo(StringHelper.GetCharSize()));
}

View File

@ -18,14 +18,14 @@ public class SafeMemStructTests
{
var immutVal = new FILETIME { dwHighDateTime = 0x2000000, dwLowDateTime = 0x33333333 };
var s = new SafeCoTaskStruct<FILETIME>(immutVal);
var bytes = s.DangerousGetHandle().ToByteArray(8);
var bytes = s.DangerousGetHandle().ToByteArray(8)!;
Assert.That(BitConverter.ToUInt32(bytes, 4), Is.EqualTo(immutVal.dwHighDateTime));
ref FILETIME r = ref s.AsRef();
Assert.That(r.dwHighDateTime, Is.EqualTo(immutVal.dwHighDateTime));
r.dwHighDateTime = 0;
bytes = s.DangerousGetHandle().ToByteArray(8);
bytes = s.DangerousGetHandle().ToByteArray(8)!;
Assert.That(BitConverter.ToUInt32(bytes, 4), Is.Zero);
var newhVal = 0x22222222U;
@ -38,12 +38,12 @@ public class SafeMemStructTests
{
var immutVal = new FILETIME { dwHighDateTime = 0x2000000, dwLowDateTime = 0x33333333 };
var s = new SafeCoTaskStruct<FILETIME>(immutVal);
var bytes = s.DangerousGetHandle().ToByteArray(8);
var bytes = s.DangerousGetHandle().ToByteArray(8)!;
Assert.That(BitConverter.ToUInt32(bytes, 4), Is.EqualTo(immutVal.dwHighDateTime));
var sp = s.AsSpan();
sp.Fill(default);
bytes = s.DangerousGetHandle().ToByteArray(8);
bytes = s.DangerousGetHandle().ToByteArray(8)!;
Assert.That(BitConverter.ToUInt32(bytes, 4), Is.Zero);
var bsp = MemoryMarshal.AsBytes(sp);

View File

@ -13,7 +13,7 @@ public class StrPtrTests
Assert.That(p0.IsNull);
var p1 = new StrPtrAuto("Test");
Assert.That(!p1.IsNull);
Assert.That((string) p1, Is.EqualTo("Test"));
Assert.That((string?) p1, Is.EqualTo("Test"));
p1.Free();
}
@ -22,10 +22,10 @@ public class StrPtrTests
{
var p1 = new StrPtrAuto(256);
Assert.That(!p1.IsNull);
Assert.That((string) p1, Is.EqualTo(""));
Assert.That((string?) p1, Is.EqualTo(""));
var bytes = Marshal.SystemDefaultCharSize == 1 ? Encoding.ASCII.GetBytes("Test\0") : Encoding.Unicode.GetBytes("Test\0");
Marshal.Copy(bytes, 0, (IntPtr)p1, bytes.Length);
Assert.That((string) p1, Is.EqualTo("Test"));
Assert.That((string?) p1, Is.EqualTo("Test"));
p1.Free();
}
@ -36,15 +36,15 @@ public class StrPtrTests
Assert.That(p0.IsNull);
p0.Assign("Test", out var cc);
Assert.That(!p0.IsNull);
Assert.That((string)p0, Is.EqualTo("Test"));
Assert.That((string?)p0, Is.EqualTo("Test"));
Assert.That(cc, Is.EqualTo(5));
p0.Free();
var p1 = new StrPtrAuto("Test");
Assert.That(!p1.IsNull);
Assert.That((string) p1, Is.EqualTo("Test"));
Assert.That((string?) p1, Is.EqualTo("Test"));
p1.Assign("Test2");
Assert.That(!p1.IsNull);
Assert.That((string) p1, Is.EqualTo("Test2"));
Assert.That((string?) p1, Is.EqualTo("Test2"));
p1.Free();
}
@ -56,7 +56,7 @@ public class StrPtrTests
p0.AssignConstant(1);
Assert.That(!p0.IsNull);
Assert.That((IntPtr)p0, Is.EqualTo(new IntPtr(1)));
Assert.That(() => p0.Free(), Throws.Nothing);
Assert.That(p0.Free, Throws.Nothing);
}
[Test()]
@ -78,7 +78,7 @@ public class StrPtrTests
Assert.That(p0.IsNull);
var p1 = new StrPtrUni("Test");
Assert.That(!p1.IsNull);
Assert.That((string) p1, Is.EqualTo("Test"));
Assert.That((string?) p1, Is.EqualTo("Test"));
p1.Free();
}
@ -87,10 +87,10 @@ public class StrPtrTests
{
var p1 = new StrPtrUni(256);
Assert.That(!p1.IsNull);
Assert.That((string) p1, Is.EqualTo(""));
Assert.That((string?) p1, Is.EqualTo(""));
var bytes = Marshal.SystemDefaultCharSize == 1 ? Encoding.ASCII.GetBytes("Test\0") : Encoding.Unicode.GetBytes("Test\0");
Marshal.Copy(bytes, 0, (IntPtr)p1, bytes.Length);
Assert.That((string) p1, Is.EqualTo("Test"));
Assert.That((string?) p1, Is.EqualTo("Test"));
p1.Free();
}
@ -101,15 +101,15 @@ public class StrPtrTests
Assert.That(p0.IsNull);
p0.Assign("Test", out var cc);
Assert.That(!p0.IsNull);
Assert.That((string)p0, Is.EqualTo("Test"));
Assert.That((string?)p0, Is.EqualTo("Test"));
Assert.That(cc, Is.EqualTo(5));
p0.Free();
var p1 = new StrPtrUni("Test");
Assert.That(!p1.IsNull);
Assert.That((string) p1, Is.EqualTo("Test"));
Assert.That((string?) p1, Is.EqualTo("Test"));
p1.Assign("Test2");
Assert.That(!p1.IsNull);
Assert.That((string) p1, Is.EqualTo("Test2"));
Assert.That((string?) p1, Is.EqualTo("Test2"));
p1.Free();
}
@ -121,7 +121,7 @@ public class StrPtrTests
p0.AssignConstant(1);
Assert.That(!p0.IsNull);
Assert.That((IntPtr)p0, Is.EqualTo(new IntPtr(1)));
Assert.That(() => p0.Free(), Throws.Nothing);
Assert.That(p0.Free, Throws.Nothing);
}
[Test()]