|
| 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