mirror of https://github.com/dahall/Vanara.git
Updated TestHelper with Linq based structure size methods
parent
c24dbf8659
commit
58a5eb7afb
|
@ -4,7 +4,6 @@ using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using Vanara.Extensions;
|
using Vanara.Extensions;
|
||||||
using Vanara.InteropServices;
|
using Vanara.InteropServices;
|
||||||
|
@ -15,8 +14,9 @@ namespace Vanara.PInvoke.Tests
|
||||||
{
|
{
|
||||||
private const string testApp = @"C:\Users\dahall\Documents\Visual Studio 2017\Projects\TestSysConsumption\bin\Debug\netcoreapp3.0\TestSysConsumption.exe";
|
private const string testApp = @"C:\Users\dahall\Documents\Visual Studio 2017\Projects\TestSysConsumption\bin\Debug\netcoreapp3.0\TestSysConsumption.exe";
|
||||||
|
|
||||||
private static Lazy<JsonSerializerSettings> jsonSet = new Lazy<JsonSerializerSettings>(() =>
|
private static readonly Lazy<JsonSerializerSettings> jsonSet = new(() =>
|
||||||
new JsonSerializerSettings() {
|
new JsonSerializerSettings()
|
||||||
|
{
|
||||||
Converters = new JsonConverter[] { new Newtonsoft.Json.Converters.StringEnumConverter(), new SizeTConverter() },
|
Converters = new JsonConverter[] { new Newtonsoft.Json.Converters.StringEnumConverter(), new SizeTConverter() },
|
||||||
ReferenceLoopHandling = ReferenceLoopHandling.Serialize
|
ReferenceLoopHandling = ReferenceLoopHandling.Serialize
|
||||||
});
|
});
|
||||||
|
@ -25,19 +25,18 @@ namespace Vanara.PInvoke.Tests
|
||||||
|
|
||||||
public static void SetThrottle(string type, bool on)
|
public static void SetThrottle(string type, bool on)
|
||||||
{
|
{
|
||||||
using (var evt = new EventWaitHandle(false, EventResetMode.AutoReset, (on ? "" : "End") + type))
|
using var evt = new EventWaitHandle(false, EventResetMode.AutoReset, (on ? "" : "End") + type);
|
||||||
evt.Set();
|
evt.Set();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IList<string> GetNestedStructSizes(this Type type, params string[] filters)
|
public static IList<string> GetNestedStructSizes(this Type type, params string[] filters) =>
|
||||||
|
type.GetNestedTypes(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic).GetStructSizes(false, filters);
|
||||||
|
|
||||||
|
public static IList<string> GetStructSizes(this Type[] types, bool fullName = false, params string[] filters)
|
||||||
{
|
{
|
||||||
var output = new List<string>();
|
System.Reflection.TypeAttributes attr = System.Reflection.TypeAttributes.SequentialLayout | System.Reflection.TypeAttributes.ExplicitLayout;
|
||||||
var attr = System.Reflection.TypeAttributes.SequentialLayout | System.Reflection.TypeAttributes.ExplicitLayout;
|
return types.Where(t => t.IsValueType && !t.IsEnum && !t.IsGenericType && (t.Attributes & attr) != 0 && ((filters?.Length ?? 0) == 0 || filters.Any(s => t.Name.Contains(s)))).
|
||||||
foreach (var t in type.GetNestedTypes(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic).
|
OrderBy(t => (fullName ? t.FullName : t.Name)).Select(t => $"{(fullName ? t.FullName : t.Name)} = {InteropExtensions.SizeOf(t)}").ToList();
|
||||||
Where(t => t.IsValueType && !t.IsEnum && (t.Attributes & attr) != 0 && ((filters?.Length ?? 0) == 0 || filters.Any(s => t.Name.Contains(s)))))
|
|
||||||
output.Add($"{t.Name} = {Marshal.SizeOf(t)}");
|
|
||||||
output.Sort();
|
|
||||||
return output;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void RunForEach<TEnum>(Type lib, string name, Func<TEnum, object[]> makeParam, Action<TEnum, object, object[]> action = null, Action<Exception> error = null) where TEnum : Enum =>
|
public static void RunForEach<TEnum>(Type lib, string name, Func<TEnum, object[]> makeParam, Action<TEnum, object, object[]> action = null, Action<Exception> error = null) where TEnum : Enum =>
|
||||||
|
@ -45,17 +44,17 @@ namespace Vanara.PInvoke.Tests
|
||||||
|
|
||||||
public static void RunForEach<TEnum>(Type lib, string name, Func<TEnum, object[]> makeParam, Action<TEnum, Exception> error = null, Action<TEnum, object, object[]> action = null, CorrespondingAction? filter = null) where TEnum : Enum
|
public static void RunForEach<TEnum>(Type lib, string name, Func<TEnum, object[]> makeParam, Action<TEnum, Exception> error = null, Action<TEnum, object, object[]> action = null, CorrespondingAction? filter = null) where TEnum : Enum
|
||||||
{
|
{
|
||||||
var mi = lib.GetMethods(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static).Where(m => m.IsGenericMethod && m.Name == name).First();
|
System.Reflection.MethodInfo mi = lib.GetMethods(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static).Where(m => m.IsGenericMethod && m.Name == name).First();
|
||||||
if (mi is null) throw new ArgumentException("Unable to find method.");
|
if (mi is null) throw new ArgumentException("Unable to find method.");
|
||||||
foreach (var e in Enum.GetValues(typeof(TEnum)).Cast<TEnum>())
|
foreach (TEnum e in Enum.GetValues(typeof(TEnum)).Cast<TEnum>())
|
||||||
{
|
{
|
||||||
var type = (filter.HasValue ? CorrespondingTypeAttribute.GetCorrespondingTypes(e, filter.Value) : CorrespondingTypeAttribute.GetCorrespondingTypes(e)).FirstOrDefault();
|
Type type = (filter.HasValue ? CorrespondingTypeAttribute.GetCorrespondingTypes(e, filter.Value) : CorrespondingTypeAttribute.GetCorrespondingTypes(e)).FirstOrDefault();
|
||||||
if (type is null)
|
if (type is null)
|
||||||
{
|
{
|
||||||
TestContext.WriteLine($"No corresponding type found for {e}.");
|
TestContext.WriteLine($"No corresponding type found for {e}.");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
var gmi = mi.MakeGenericMethod(type);
|
System.Reflection.MethodInfo gmi = mi.MakeGenericMethod(type);
|
||||||
var param = makeParam(e);
|
var param = makeParam(e);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -85,7 +84,7 @@ namespace Vanara.PInvoke.Tests
|
||||||
goto Simple;
|
goto Simple;
|
||||||
|
|
||||||
case DateTime:
|
case DateTime:
|
||||||
case Decimal:
|
case decimal:
|
||||||
case var v when v.GetType().IsPrimitive || v.GetType().IsEnum:
|
case var v when v.GetType().IsPrimitive || v.GetType().IsEnum:
|
||||||
Simple:
|
Simple:
|
||||||
return $"{value.GetType().Name} : [{value}]";
|
return $"{value.GetType().Name} : [{value}]";
|
||||||
|
|
Loading…
Reference in New Issue