Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dictionary tests for non-string reference type #2609

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,7 +15,9 @@
namespace System.Collections
{
[BenchmarkCategory(Categories.Libraries, Categories.Collections, Categories.GenericCollections)]
[GenericTypeArguments(typeof(int), typeof(int))] // value type
[GenericTypeArguments(typeof(int), typeof(int))] // primitive value type
[GenericTypeArguments(typeof(BigStruct), typeof(BigStruct))] // big value type
[GenericTypeArguments(typeof(SmallClass), typeof(SmallClass))] // reference type
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
[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,9 @@
namespace System.Collections
{
[BenchmarkCategory(Categories.Libraries, Categories.Collections, Categories.GenericCollections)]
[GenericTypeArguments(typeof(int), typeof(int))] // value type
[GenericTypeArguments(typeof(int), typeof(int))] // primitive value type
[GenericTypeArguments(typeof(BigStruct), typeof(BigStruct))] // big value type
[GenericTypeArguments(typeof(SmallClass), typeof(SmallClass))] // reference type
[GenericTypeArguments(typeof(string), typeof(string))] // reference type
public class TryGetValueTrue<TKey, TValue>
{
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 @@ -222,6 +223,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
Loading