Skip to content

Commit

Permalink
Add dictionary tests for non-string reference type
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub committed Sep 16, 2022
1 parent b905d2c commit e3ed0a6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,19 @@ public BigStruct(int value)

public int CompareTo(BigStruct other) => _int1.CompareTo(other._int1);
}

public class SmallClass : IEquatable<SmallClass>, IComparable<SmallClass>
{
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<TKey, TValue>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TKey, TValue>
{
private TKey[] _found;
Expand Down
4 changes: 4 additions & 0 deletions src/harness/BenchmarkDotNet.Extensions/ValuesGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;

Expand Down Expand Up @@ -201,6 +202,9 @@ private static T GenerateValue<T>(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");
}

Expand Down

0 comments on commit e3ed0a6

Please sign in to comment.