From e3ed0a6569b4814d820e7b02fa520f9ac3379bd4 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Thu, 15 Sep 2022 21:26:43 -0400 Subject: [PATCH] Add dictionary tests for non-string reference type --- .../{Sort.Types.cs => DataTypes.cs} | 15 +++++++++++++++ .../TryGetValue/TryGetValueFalse.cs | 1 + .../TryGetValue/TryGetValueTrue.cs | 3 ++- .../BenchmarkDotNet.Extensions/ValuesGenerator.cs | 4 ++++ 4 files changed, 22 insertions(+), 1 deletion(-) rename src/benchmarks/micro/libraries/System.Collections/{Sort.Types.cs => DataTypes.cs} (74%) diff --git a/src/benchmarks/micro/libraries/System.Collections/Sort.Types.cs b/src/benchmarks/micro/libraries/System.Collections/DataTypes.cs similarity index 74% rename from src/benchmarks/micro/libraries/System.Collections/Sort.Types.cs rename to src/benchmarks/micro/libraries/System.Collections/DataTypes.cs index 4444291e7e5..e0b8f6da24c 100644 --- a/src/benchmarks/micro/libraries/System.Collections/Sort.Types.cs +++ b/src/benchmarks/micro/libraries/System.Collections/DataTypes.cs @@ -47,4 +47,19 @@ public BigStruct(int value) public int CompareTo(BigStruct other) => _int1.CompareTo(other._int1); } + + public class SmallClass : IEquatable, IComparable + { + public int Value; + + public SmallClass(int value) => Value = value; + + public int CompareTo(SmallClass other) => other == null ? -1 : Value.CompareTo(other.Value); + + public bool Equals(SmallClass other) => other != null && Value == other.Value; + + public override bool Equals(object obj) => obj is SmallClass other && Equals(other); + + public override int GetHashCode() => Value; + } } diff --git a/src/benchmarks/micro/libraries/System.Collections/TryGetValue/TryGetValueFalse.cs b/src/benchmarks/micro/libraries/System.Collections/TryGetValue/TryGetValueFalse.cs index da071905754..71decdef534 100644 --- a/src/benchmarks/micro/libraries/System.Collections/TryGetValue/TryGetValueFalse.cs +++ b/src/benchmarks/micro/libraries/System.Collections/TryGetValue/TryGetValueFalse.cs @@ -15,6 +15,7 @@ namespace System.Collections { [BenchmarkCategory(Categories.Libraries, Categories.Collections, Categories.GenericCollections)] [GenericTypeArguments(typeof(int), typeof(int))] // value type + [GenericTypeArguments(typeof(SmallClass), typeof(SmallClass))] // reference type [GenericTypeArguments(typeof(string), typeof(string))] // reference type public class TryGetValueFalse { diff --git a/src/benchmarks/micro/libraries/System.Collections/TryGetValue/TryGetValueTrue.cs b/src/benchmarks/micro/libraries/System.Collections/TryGetValue/TryGetValueTrue.cs index b40e38ba26f..9e79c430b84 100644 --- a/src/benchmarks/micro/libraries/System.Collections/TryGetValue/TryGetValueTrue.cs +++ b/src/benchmarks/micro/libraries/System.Collections/TryGetValue/TryGetValueTrue.cs @@ -15,7 +15,8 @@ namespace System.Collections { [BenchmarkCategory(Categories.Libraries, Categories.Collections, Categories.GenericCollections)] [GenericTypeArguments(typeof(int), typeof(int))] // value type - [GenericTypeArguments(typeof(string), typeof(string))] // reference type + [GenericTypeArguments(typeof(SmallClass), typeof(SmallClass))] // reference type + [GenericTypeArguments(typeof(string), typeof(string))] // string type public class TryGetValueTrue { private TKey[] _found; diff --git a/src/harness/BenchmarkDotNet.Extensions/ValuesGenerator.cs b/src/harness/BenchmarkDotNet.Extensions/ValuesGenerator.cs index f1d9d310b5a..5da915c1984 100644 --- a/src/harness/BenchmarkDotNet.Extensions/ValuesGenerator.cs +++ b/src/harness/BenchmarkDotNet.Extensions/ValuesGenerator.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; using System.Runtime.CompilerServices; using System.Text; @@ -201,6 +202,9 @@ private static T GenerateValue(Random random) if (typeof(T) == typeof(Guid)) return (T)(object)GenerateRandomGuid(random); // note: may return malformed Guids (not logically valid per RFC 4122 formatting) + if (typeof(T).GetConstructor(new[] { typeof(int) }) is ConstructorInfo ctor) + return (T)ctor.Invoke(new[] { (object)random.Next() }); + throw new NotImplementedException($"{typeof(T).Name} is not implemented"); }