Skip to content

Commit 94303f4

Browse files
authored
chore: add ConfigUnionTests and add fix equatable issue of SimpleColumnProvider (#2955)
1 parent d75bb3b commit 94303f4

File tree

2 files changed

+132
-3
lines changed

2 files changed

+132
-3
lines changed
Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using BenchmarkDotNet.Reports;
45

56
namespace BenchmarkDotNet.Columns
67
{
7-
public class SimpleColumnProvider : IColumnProvider
8+
public class SimpleColumnProvider : IColumnProvider, IEquatable<SimpleColumnProvider>
89
{
910
private readonly IColumn[] columns;
1011

@@ -14,5 +15,27 @@ public SimpleColumnProvider(params IColumn[] columns)
1415
}
1516

1617
public IEnumerable<IColumn> GetColumns(Summary summary) => columns.Where(c => c.IsAvailable(summary));
18+
19+
public bool Equals(SimpleColumnProvider? other)
20+
{
21+
if (ReferenceEquals(this, other))
22+
return true;
23+
if (other is null)
24+
return false;
25+
26+
return columns.SequenceEqual(other.columns);
27+
}
28+
29+
public override bool Equals(object? obj)
30+
=> Equals(obj as SimpleColumnProvider);
31+
32+
public override int GetHashCode()
33+
{
34+
// Compute hashcode of each column.
35+
var hash = new HashCode();
36+
foreach (var column in columns)
37+
hash.Add(column);
38+
return hash.ToHashCode();
39+
}
1740
}
18-
}
41+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
using BenchmarkDotNet.Analysers;
2+
using BenchmarkDotNet.Columns;
3+
using BenchmarkDotNet.Configs;
4+
using BenchmarkDotNet.Diagnosers;
5+
using BenchmarkDotNet.EventProcessors;
6+
using BenchmarkDotNet.Exporters;
7+
using BenchmarkDotNet.Filters;
8+
using BenchmarkDotNet.Loggers;
9+
using BenchmarkDotNet.Running;
10+
using BenchmarkDotNet.Validators;
11+
using System.Linq;
12+
using Xunit;
13+
14+
namespace BenchmarkDotNet.Tests.Configs;
15+
16+
public class ConfigUnionTests
17+
{
18+
[Fact]
19+
public void UnionConfig()
20+
{
21+
// Arrange
22+
var config = CreateDummyConfig();
23+
var expected = config.CreateImmutableConfig();
24+
25+
// Act
26+
config = ManualConfig.Union(config, CreateDummyConfig());
27+
var result = config.CreateImmutableConfig();
28+
29+
// Assert
30+
Validate(expected, result);
31+
}
32+
33+
[Fact]
34+
public void UnionConfigByAddMethods()
35+
{
36+
var p1 = new SimpleColumnProvider(TargetMethodColumn.Namespace).GetHashCode();
37+
var p2 = new SimpleColumnProvider(TargetMethodColumn.Namespace).GetHashCode();
38+
39+
// Arrange
40+
var config = CreateDummyConfig();
41+
var otherConfig = CreateDummyConfig();
42+
var expected = config.CreateImmutableConfig();
43+
44+
// Act
45+
config.AddAnalyser(otherConfig.GetAnalysers().ToArray())
46+
.AddColumnProvider(otherConfig.GetColumnProviders().ToArray())
47+
.AddDiagnoser(otherConfig.GetDiagnosers().ToArray())
48+
.AddExporter(otherConfig.GetExporters().ToArray())
49+
.AddEventProcessor(otherConfig.GetEventProcessors().ToArray())
50+
.AddFilter(otherConfig.GetFilters().ToArray())
51+
.AddHardwareCounters(otherConfig.GetHardwareCounters().ToArray())
52+
.AddLogger(otherConfig.GetLoggers().ToArray())
53+
.AddLogicalGroupRules(otherConfig.GetLogicalGroupRules().ToArray())
54+
.AddValidator(otherConfig.GetValidators().ToArray());
55+
56+
var result = config.CreateImmutableConfig();
57+
58+
// Assert
59+
Validate(expected, result);
60+
}
61+
62+
private static void Validate(ImmutableConfig expected, ImmutableConfig result)
63+
{
64+
Assert.Equal(expected.GetAnalysers(), result.GetAnalysers());
65+
Assert.Equal(expected.GetColumnProviders(), result.GetColumnProviders());
66+
Assert.Equal(expected.GetDiagnosers(), result.GetDiagnosers());
67+
Assert.Equal(expected.GetExporters(), result.GetExporters());
68+
Assert.Equal(expected.GetEventProcessors(), result.GetEventProcessors());
69+
Assert.Equal(expected.GetFilters(), result.GetFilters());
70+
Assert.Equal(expected.GetHardwareCounters(), result.GetHardwareCounters());
71+
Assert.Equal(expected.GetLoggers(), result.GetLoggers());
72+
Assert.Equal(expected.GetLogicalGroupRules(), result.GetLogicalGroupRules());
73+
Assert.Equal(expected.GetValidators(), result.GetValidators());
74+
}
75+
76+
private static ManualConfig CreateDummyConfig() =>
77+
ManualConfig.CreateEmpty()
78+
.AddAnalyser([EnvironmentAnalyser.Default])
79+
.AddColumn(TargetMethodColumn.Namespace)
80+
.AddColumnProvider(DefaultColumnProviders.Instance)
81+
.AddDiagnoser([MemoryDiagnoser.Default])
82+
.AddExporter(MarkdownExporter.Default)
83+
.AddEventProcessor(DummyEventProcessor.Instance)
84+
.AddFilter(DummyFilter.Instance)
85+
.AddHardwareCounters([HardwareCounter.BranchMispredictions])
86+
.AddLogger(ConsoleLogger.Default)
87+
.AddLogicalGroupRules([BenchmarkLogicalGroupRule.ByCategory])
88+
.AddValidator([BaselineValidator.FailOnError])
89+
.HideColumns([Column.Id]);
90+
91+
private class DummyFilter : IFilter
92+
{
93+
public static DummyFilter Instance = new DummyFilter();
94+
95+
private DummyFilter() { }
96+
97+
public bool Predicate(BenchmarkCase benchmarkCase) => true;
98+
}
99+
100+
private class DummyEventProcessor : EventProcessor
101+
{
102+
public static DummyEventProcessor Instance = new DummyEventProcessor();
103+
104+
private DummyEventProcessor() { }
105+
}
106+
}

0 commit comments

Comments
 (0)