Skip to content

Commit

Permalink
support Jagged Arrays for ArgumentsSource, fixes #769
Browse files Browse the repository at this point in the history
  • Loading branch information
adamsitnik committed May 29, 2018
1 parent 5f08c2e commit cabef06
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/BenchmarkDotNet/Parameters/SmartParamBuilder.cs
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using BenchmarkDotNet.Code;
Expand All @@ -7,7 +8,7 @@

namespace BenchmarkDotNet.Parameters
{
static internal class SmartParamBuilder
internal static class SmartParamBuilder
{
internal static object[] CreateForParams(MemberInfo source, object[] values)
{
Expand All @@ -21,7 +22,7 @@ internal static ParameterInstances CreateForArguments(MethodInfo benchmark, Para
{
var unwrappedValue = valuesInfo.values[sourceIndex];

if (unwrappedValue is object[] array)
if (unwrappedValue is object[] array && !IsJagged(array))
{
if (parameterDefinitions.Length != array.Length)
throw new InvalidOperationException($"Benchmark {benchmark.Name} has invalid number of arguments provided by [ArgumentsSource({valuesInfo.source.Name})]! {array.Length} instead of {parameterDefinitions.Length}.");
Expand All @@ -44,6 +45,13 @@ private static ParameterInstance Create(ParameterDefinition[] parameterDefinitio

return new ParameterInstance(parameterDefinitions[argumentIndex], new SmartArgument(parameterDefinitions, value, source, sourceIndex, argumentIndex));
}

private static bool IsJagged(object[] array)
{
var uniqueTypes = array.Select(element => element.GetType()).Distinct().ToArray();

return uniqueTypes.Length == 1 && uniqueTypes[0].IsArray;
}
}

internal class SmartArgument : IParam
Expand Down
Expand Up @@ -7,9 +7,9 @@

namespace BenchmarkDotNet.IntegrationTests
{
public class ArgumentsAttributeTests : BenchmarkTestExecutor
public class ArgumentsTests : BenchmarkTestExecutor
{
public ArgumentsAttributeTests(ITestOutputHelper output) : base(output) { }
public ArgumentsTests(ITestOutputHelper output) : base(output) { }

[Fact]
public void ArgumentsArePassedToBenchmarks() => CanExecute<WithArguments>();
Expand Down Expand Up @@ -135,5 +135,41 @@ public void AcceptingArray(int[] array)
throw new InvalidOperationException($"Incorrect array element at index {i}, was {array[i]} instead of {i}");
}
}

[Fact]
public void JaggedArrayCanBeUsedAnArgument() => CanExecute<WithJaggedArray>();

public class WithJaggedArray
{
[Benchmark]
[ArgumentsSource(nameof(CreateMatrix))]
public void Test(int[][] array)
{
if(array == null)
throw new ArgumentNullException(nameof(array));

for (int i = 0; i < 10; i++)
for (int j = 0; j < i; j++)
if(array[i][j] != i)
throw new ArgumentException("Invalid value");
}

public IEnumerable<object> CreateMatrix()
{
int[][] jagged = new int[10][];

for (int i = 0; i < jagged.Length; i++)
{
int[] row = new int[i];

for (int j = 0; j < i; j++)
row[j] = i;

jagged[i] = row;
}

yield return jagged;
}
}
}
}

0 comments on commit cabef06

Please sign in to comment.