Skip to content

Commit 1ac2494

Browse files
author
dahall
committed
Updated unit tests
1 parent 1050afa commit 1ac2494

File tree

14 files changed

+115
-87
lines changed

14 files changed

+115
-87
lines changed

UnitTests/CodeGen/CodeGenTests.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -664,11 +664,13 @@ namespace Vanara.PInvoke
664664
/// <summary>A 64-bit test dll</summary>
665665
public static partial class Test64
666666
{
667-
//public static extern NTStatus HidP_GetButtonArray([Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 5), SizeDef(nameof(ButtonDataLength), SizingMethod.CheckLastError)] BOOL[] ButtonData,
668-
// ref ushort ButtonDataLength, [Optional, Ignore] int LinkCollection, [In, SizeDef(nameof(ReportLength))] IntPtr Report, uint ReportLength);
667+
//public static extern NTStatus TwoDiffSizeDefParams([In, AddAsMember] HTEST hAlgorithm, out SafeHTEST phHash, [Out, Optional, SizeDef(nameof(cbHashObject), SizingMethod.Bytes)] IntPtr pbHashObject,
668+
// [Optional] uint cbHashObject, [In, Optional, SizeDef(nameof(cbSecret), SizingMethod.Bytes)] IntPtr pbSecret, [Optional] uint cbSecret, uint dwFlags = 0);
669669
670-
public static extern NTStatus BCryptCreateHash([In, AddAsMember] HTEST hAlgorithm, out SafeHTEST phHash, [Out, Optional, SizeDef(nameof(cbHashObject), SizingMethod.Bytes)] IntPtr pbHashObject,
671-
[Optional] uint cbHashObject, [In, Optional, SizeDef(nameof(cbSecret), SizingMethod.Bytes)] IntPtr pbSecret, [Optional] uint cbSecret, uint dwFlags = 0);
670+
public static extern uint TwoSizeDefParamsWithSameSizer(string szFileName, int nIconIndex, int cxIcon, int cyIcon,
671+
[Optional, Out, MarshalAs(UnmanagedType.LPArray), SizeDef(nameof(nIcons), SizingMethod.QueryResultInReturn)] SafeHICON[]? phicon,
672+
[Optional, Out, MarshalAs(UnmanagedType.LPArray), SizeDef(nameof(nIcons), SizingMethod.QueryResultInReturn)] uint[]? piconid,
673+
[Optional] uint nIcons, [Optional] LoadImageOptions flags);
672674
}
673675
}
674676
""";

UnitTests/CodeGen/PointerGenTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public static void Test()
8383
}
8484
}
8585

86-
#nullable enable
86+
#nullable enable
8787
#pragma warning disable 1591
8888
using System;
8989
using System.ComponentModel.DataAnnotations;

UnitTests/Core/Extensions/IOExtensionsTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public void WriteTest()
1818
bw.Write<string?>(null);
1919
Assert.That(() => bw.Write(DateTime.Today), Throws.ArgumentException);
2020
var buf = ms.ToArray();
21-
Assert.That(buf.Length == Marshal.SizeOf(typeof(int)) + Marshal.SizeOf(typeof(RECT)) + Marshal.SizeOf(typeof(PRECT)));
21+
Assert.That(buf.Length == Marshal.SizeOf<int>() + Marshal.SizeOf<RECT>() + Marshal.SizeOf<PRECT>());
2222
Assert.That(buf[0] == 1 && buf[1] == 1 && buf[4] == 1);
2323
}
2424

UnitTests/Core/Extensions/InteropExtensionsTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Vanara.Extensions.Tests;
88
[TestFixture()]
99
public class InteropExtensionsTests
1010
{
11-
private readonly int intSz = Marshal.SizeOf(typeof(int));
11+
private readonly int intSz = Marshal.SizeOf<int>();
1212

1313
[Test()]
1414
public void IsBlittableTest()
@@ -42,7 +42,7 @@ public void IsNullableTest()
4242
[Test()]
4343
public void MarshalToPtrTest()
4444
{
45-
var h = new SafeHGlobalHandle(Marshal.SizeOf(typeof(RECT)) * 2 + intSz);
45+
var h = new SafeHGlobalHandle(Marshal.SizeOf<RECT>() * 2 + intSz);
4646
var rs = new[] { new RECT(), new RECT(10, 11, 12, 13) };
4747
((IntPtr)h).Write(rs, intSz, h.Size);
4848
Assert.That(Marshal.ReadInt32((IntPtr)h, 4 * intSz), Is.EqualTo(0));
@@ -63,7 +63,7 @@ public void MarshalToPtr_StructArray_Test()
6363
var rs = new[] { new RECT(), new RECT(10, 11, 12, 13) };
6464
h = rs.MarshalToPtr(Marshal.AllocHGlobal, out var a, intSz);
6565
Assert.That(h, Is.Not.EqualTo(IntPtr.Zero));
66-
Assert.That(a, Is.EqualTo(Marshal.SizeOf(typeof(RECT)) * rs.Length + intSz));
66+
Assert.That(a, Is.EqualTo(Marshal.SizeOf<RECT>() * rs.Length + intSz));
6767
Assert.That(Marshal.ReadInt32((IntPtr)h, 4 * intSz), Is.EqualTo(0));
6868
Assert.That(Marshal.ReadInt32((IntPtr)h, 5 * intSz), Is.EqualTo(10));
6969
Assert.That(Marshal.ReadInt32((IntPtr)h, 7 * intSz), Is.EqualTo(12));
@@ -420,7 +420,7 @@ public void ToSecureStringTest(string? sval)
420420
[Test]
421421
public unsafe void AsUnmanagedArrayPointerTest()
422422
{
423-
var h = new SafeHGlobalHandle(Marshal.SizeOf(typeof(RECT)) * 2 + intSz);
423+
var h = new SafeHGlobalHandle(Marshal.SizeOf<RECT>() * 2 + intSz);
424424
var rs = new[] { new RECT(0, 1, 2, 3), new RECT(10, 11, 12, 13) };
425425
((IntPtr)h).Write(rs, intSz, h.Size);
426426

UnitTests/Core/InteropServices/NativeMemoryStreamTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ public void RefStructTest()
281281
Assert.That(ms.Pointer.ToArray(IntPtr.Size == 4 ? typeof(uint) : typeof(ulong), pcnt), Is.EquivalentTo(new[] { 0, 0, 0 }));
282282
var newGuid = Guid.NewGuid();
283283
Assert.That(() => ms.WriteReference(newGuid), Throws.Nothing);
284-
isz += Marshal.SizeOf(typeof(Guid)); pcnt++;
284+
isz += Marshal.SizeOf<Guid>(); pcnt++;
285285
Assert.That(ms.Length, Is.EqualTo(Len()));
286286
Assert.That(ms.Position, Is.EqualTo(Pos()));
287287
Assert.That(ms.Capacity, Is.EqualTo(62));
@@ -468,7 +468,7 @@ public void StructTest()
468468
Assert.That(ms.Capacity, Is.EqualTo(24));
469469
var newGuid = Guid.NewGuid();
470470
Assert.That(() => ms.Write(newGuid), Throws.Nothing);
471-
isz += Marshal.SizeOf(typeof(Guid));
471+
isz += Marshal.SizeOf<Guid>();
472472
Assert.That(ms.Length, Is.EqualTo(Len()));
473473
Assert.That(ms.Position, Is.EqualTo(Pos()));
474474
Assert.That(ms.Capacity, Is.EqualTo(40));

UnitTests/Core/InteropServices/SafeCoTaskMemHandleTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@ public void CreateFromListTest()
1414
var r = new[] { 5, 5, 5, 5 };
1515
var h = SafeCoTaskMemHandle.CreateFromList(r, r.Length);
1616
Assert.That(!h.IsClosed && !h.IsInvalid);
17-
Assert.That((int)h.Size, Is.EqualTo(Marshal.SizeOf(typeof(int)) * r.Length));
17+
Assert.That((int)h.Size, Is.EqualTo(Marshal.SizeOf<int>() * r.Length));
1818
Assert.That(h.ToStructure<int>(), Is.EqualTo(5));
1919
Assert.That(h.ToEnumerable<int>(4), Has.Exactly(4).EqualTo(5).And.Exactly(4).Items);
2020

2121
var d = new[] { new RECT(1, 1, 1, 1), new RECT(2, 2, 2, 2) };
2222
h = SafeCoTaskMemHandle.CreateFromList(d, d.Length);
2323
Assert.That(!h.IsClosed && !h.IsInvalid);
24-
Assert.That((int)h.Size, Is.EqualTo(Marshal.SizeOf(typeof(RECT)) * d.Length));
24+
Assert.That((int)h.Size, Is.EqualTo(Marshal.SizeOf<RECT>() * d.Length));
2525
Assert.That(h.ToStructure<RECT>().X, Is.EqualTo(1));
2626
Assert.That(h.ToArray<RECT>(2), Has.Exactly(2).Items);
2727

2828
var p = new[] { new PRECT(1, 1, 1, 1), new PRECT(2, 2, 2, 2) };
2929
h = SafeCoTaskMemHandle.CreateFromList(p);
3030
Assert.That(!h.IsClosed && !h.IsInvalid);
31-
Assert.That((int)h.Size, Is.EqualTo(Marshal.SizeOf(typeof(PRECT)) * p.Length));
31+
Assert.That((int)h.Size, Is.EqualTo(Marshal.SizeOf<PRECT>() * p.Length));
3232
Assert.That(h.ToArray<RECT>(2), Has.Exactly(2).Items);
3333

3434
Assert.That(() => SafeCoTaskMemHandle.CreateFromList(new[] { "X" }), Throws.ArgumentException);
@@ -76,7 +76,7 @@ public void CreateFromStructureTest()
7676
var r = new RECT(5, 5, 5, 5);
7777
var h = SafeCoTaskMemHandle.CreateFromStructure(r);
7878
Assert.That(!h.IsClosed && !h.IsInvalid);
79-
Assert.That((int)h.Size, Is.EqualTo(Marshal.SizeOf(typeof(RECT))));
79+
Assert.That((int)h.Size, Is.EqualTo(Marshal.SizeOf<RECT>()));
8080
Assert.That(h.ToStructure<RECT>().X, Is.EqualTo(5));
8181
}
8282

UnitTests/Core/InteropServices/SafeHGlobalHandleTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@ public void CreateFromListTest()
1414
var r = new[] { 5, 5, 5, 5 };
1515
var h = SafeHGlobalHandle.CreateFromList(r, r.Length);
1616
Assert.That(!h.IsClosed && !h.IsInvalid);
17-
Assert.That((int)h.Size, Is.EqualTo(Marshal.SizeOf(typeof(int)) * r.Length));
17+
Assert.That((int)h.Size, Is.EqualTo(Marshal.SizeOf<int>() * r.Length));
1818
Assert.That(h.ToStructure<int>(), Is.EqualTo(5));
1919
Assert.That(h.ToEnumerable<int>(4), Has.Exactly(4).EqualTo(5).And.Exactly(4).Items);
2020

2121
var d = new[] { new RECT(1, 1, 1, 1), new RECT(2, 2, 2, 2) };
2222
h = SafeHGlobalHandle.CreateFromList(d, d.Length);
2323
Assert.That(!h.IsClosed && !h.IsInvalid);
24-
Assert.That((int)h.Size, Is.EqualTo(Marshal.SizeOf(typeof(RECT)) * d.Length));
24+
Assert.That((int)h.Size, Is.EqualTo(Marshal.SizeOf<RECT>() * d.Length));
2525
Assert.That(h.ToStructure<RECT>().X, Is.EqualTo(1));
2626
Assert.That(h.ToArray<RECT>(2), Has.Exactly(2).Items);
2727

2828
var p = new[] { new PRECT(1, 1, 1, 1), new PRECT(2, 2, 2, 2) };
2929
h = SafeHGlobalHandle.CreateFromList(p);
3030
Assert.That(!h.IsClosed && !h.IsInvalid);
31-
Assert.That((int)h.Size, Is.EqualTo(Marshal.SizeOf(typeof(PRECT)) * p.Length));
31+
Assert.That((int)h.Size, Is.EqualTo(Marshal.SizeOf<PRECT>() * p.Length));
3232
Assert.That(h.ToArray<RECT>(2), Has.Exactly(2).Items);
3333

3434
Assert.That(() => SafeHGlobalHandle.CreateFromList(new[] { "X" }), Throws.ArgumentException);
@@ -76,7 +76,7 @@ public void CreateFromStructureTest()
7676
var r = new RECT(5, 5, 5, 5);
7777
var h = SafeHGlobalHandle.CreateFromStructure(r);
7878
Assert.That(!h.IsClosed && !h.IsInvalid);
79-
Assert.That((int)h.Size, Is.EqualTo(Marshal.SizeOf(typeof(RECT))));
79+
Assert.That((int)h.Size, Is.EqualTo(Marshal.SizeOf<RECT>()));
8080
Assert.That(h.ToStructure<RECT>().X, Is.EqualTo(5));
8181
}
8282

UnitTests/Core/InteropServices/StrPtrTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class StrPtrTests
88
[Test()]
99
public void StrPtrTest()
1010
{
11-
Assert.That(Marshal.SizeOf(typeof(StrPtrAuto)) == Marshal.SizeOf(typeof(IntPtr)));
11+
Assert.That(Marshal.SizeOf<StrPtrAuto>() == Marshal.SizeOf<IntPtr>());
1212
var p0 = new StrPtrAuto();
1313
Assert.That(p0.IsNull);
1414
var p1 = new StrPtrAuto("Test");
@@ -73,7 +73,7 @@ public void FreeTest()
7373
[Test()]
7474
public void StrPtrUniTest()
7575
{
76-
Assert.That(Marshal.SizeOf(typeof(StrPtrUni)) == Marshal.SizeOf(typeof(IntPtr)));
76+
Assert.That(Marshal.SizeOf<StrPtrUni>() == Marshal.SizeOf<IntPtr>());
7777
var p0 = new StrPtrUni();
7878
Assert.That(p0.IsNull);
7979
var p1 = new StrPtrUni("Test");

UnitTests/PInvoke/ComDlg32/ComDlg32Tests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public void ChooseColorTest()
2323
{
2424
var cc = new CHOOSECOLOR
2525
{
26-
lStructSize = (uint)Marshal.SizeOf(typeof(CHOOSECOLOR)),
26+
lStructSize = (uint)Marshal.SizeOf<CHOOSECOLOR>(),
2727
rgbResult = System.Drawing.Color.Red,
2828
Flags = CC.CC_RGBINIT,
2929
hwndOwner = GetDesktopWindow()
@@ -37,7 +37,7 @@ public void GetOpenFileNameTest()
3737
using var fn = new SafeCoTaskMemString(261);
3838
var ofn = new OPENFILENAME
3939
{
40-
lStructSize = (uint)Marshal.SizeOf(typeof(OPENFILENAME)),
40+
lStructSize = (uint)Marshal.SizeOf<OPENFILENAME>(),
4141
lpstrFile = (IntPtr)fn,
4242
nMaxFile = (uint)fn.Capacity,
4343
lpstrFilter = "All\0*.*\0Text\0*.txt\0",
@@ -55,7 +55,7 @@ public void FindTextTest()
5555
var fw = new SafeCoTaskMemString(261);
5656
var fr = new FINDREPLACE
5757
{
58-
lStructSize = (uint)Marshal.SizeOf(typeof(FINDREPLACE)),
58+
lStructSize = (uint)Marshal.SizeOf<FINDREPLACE>(),
5959
hwndOwner = wnd.MessageWindowHandle,
6060
lpstrFindWhat = (IntPtr)fw,
6161
wFindWhatLen = (ushort)fw.Length,

UnitTests/PInvoke/Gdi32/BitmapTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public void GetDIBitsTest()
166166

167167
//pbi.Zero();
168168
//Assert.That(GetDIBits(hdcScreen, hbmp, 0, 15, default, pbi, DIBColorMode.DIB_RGB_COLORS), ResultIs.Value(1));
169-
//Assert.That(h.biSize, Is.EqualTo(Marshal.SizeOf(typeof(BITMAPINFOHEADER))));
169+
//Assert.That(h.biSize, Is.EqualTo(Marshal.SizeOf<BITMAPINFOHEADER>()));
170170
//Assert.That(pbch.bcWidth, Is.EqualTo(16));
171171
//Assert.That(pbch.bcHeight, Is.EqualTo(16));
172172
//Assert.That(pbch.bcPlanes, Is.EqualTo(1));
@@ -176,7 +176,7 @@ public void GetDIBitsTest()
176176
Assert.That(GetDIBits(hdcScreen, hbmp, 0, 15, default, pbi, DIBColorMode.DIB_RGB_COLORS), ResultIs.Value(1));
177177

178178
pbi.Zero();
179-
pbi.HeaderSize = Marshal.SizeOf(typeof(BITMAPV5HEADER)) + 4;
179+
pbi.HeaderSize = Marshal.SizeOf<BITMAPV5HEADER>() + 4;
180180
Assert.That(GetDIBits(hdcScreen, hbmp, 0, 15, default, pbi, DIBColorMode.DIB_RGB_COLORS), ResultIs.Value(1));
181181
ref BITMAPV5HEADER pbV5Header = ref pbi.GetHeaderAsRef<BITMAPV5HEADER>();
182182
Assert.That(pbV5Header.bV5RedMask, Is.Zero);
@@ -312,7 +312,7 @@ public void GetDIBitsTest()
312312
//h.biCompression = BI_PNG;
313313
//Assert.That(GetDIBits(hdcScreen, hbmp, 0, 5, default, pbi, DIBColorMode.DIB_RGB_COLORS), ResultIs.Value(0));
314314

315-
//h.biSize = Marshal.SizeOf(typeof(BITMAPINFOHEADER));
315+
//h.biSize = Marshal.SizeOf<BITMAPINFOHEADER>();
316316
//h.biWidth = 4;
317317
//h.biHeight = 4;
318318
//h.biPlanes = 1;
@@ -326,7 +326,7 @@ public void GetDIBitsTest()
326326
//cjSizeImage = ((h.biWidth * h.biBitCount + 31) / 32) * 4 * h.biHeight;
327327
//pvBits = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 512);
328328
//Assert.That(GetDIBits(hdcScreen, hbmp, 0, 4, pvBits, pbi, DIBColorMode.DIB_RGB_COLORS), ResultIs.Value(4));
329-
//Assert.That(h.biSize, Is.EqualTo(Marshal.SizeOf(typeof(BITMAPINFOHEADER))));
329+
//Assert.That(h.biSize, Is.EqualTo(Marshal.SizeOf<BITMAPINFOHEADER>()));
330330
//Assert.That(h.biWidth, Is.EqualTo(4));
331331
//Assert.That(h.biHeight, Is.EqualTo(4));
332332
//Assert.That(h.biPlanes, Is.EqualTo(1));
@@ -356,7 +356,7 @@ public void GetDIBitsTest()
356356
//Assert.That(GetDIBits(hdcScreen, hbmp, 0, 4, pvBits, pbi, DIBColorMode.DIB_RGB_COLORS), ResultIs.Value(0));
357357

358358
//pbi.Zero();
359-
//h.biSize = Marshal.SizeOf(typeof(BITMAPINFOHEADER));
359+
//h.biSize = Marshal.SizeOf<BITMAPINFOHEADER>();
360360
//h.biBitCount = 5;
361361
//Assert.That(GetDIBits(hdcScreen, hbmp, 0, 4, pvBits, pbi, DIBColorMode.DIB_RGB_COLORS), ResultIs.Value(0));
362362

@@ -366,7 +366,7 @@ public void GetDIBitsTest()
366366
//hbmp = CreateBitmap(3, 5, 1, 4, default);
367367
//Assert.That(hbmp != 0, "failed to create bitmap\n");
368368
//pbi.Zero();
369-
//h.biSize = Marshal.SizeOf(typeof(BITMAPINFOHEADER));
369+
//h.biSize = Marshal.SizeOf<BITMAPINFOHEADER>();
370370
//Assert.That(GetDIBits(hdcScreen, hbmp, 0, 0, default, pbi, DIBColorMode.DIB_RGB_COLORS), ResultIs.Value(1));
371371
//Assert.That(h.biWidth, Is.EqualTo(3));
372372
//Assert.That(h.biHeight, Is.EqualTo(5));

0 commit comments

Comments
 (0)