Skip to content
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 @@ -2,13 +2,13 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.5" />
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,24 @@
[RankColumn]
public class MergingMethodsBenchmark
{
private Dictionary<int, string> _dictionaryA;
private Dictionary<int, string> _dictionaryB;
private readonly Dictionary<int, string> _dictionaryA = [];
private readonly Dictionary<int, string> _dictionaryB = [];

[GlobalSetup]
public void Setup()
{
_dictionaryA = new Dictionary<int, string>();
_dictionaryB = new Dictionary<int, string>();

// Generate random values for dictionaryA
for (var i = 0; i < 100; i++)
{
var randomValue = GenerateRandomValue();
_dictionaryA.Add(i, randomValue);
_dictionaryA.Add(i, randomValue);
}

// Generate random values for dictionaryB
for (int i = 100; i < 200; i++)
for (var i = 100; i < 200; i++)
{
var randomValue = GenerateRandomValue();
_dictionaryB.Add(i, randomValue);
_dictionaryB.Add(i, randomValue);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
BenchmarkDotNet.Running.BenchmarkRunner.Run<MergingMethodsBenchmark>();
BenchmarkDotNet.Running.BenchmarkRunner.Run<MergingMethodsBenchmark>(args: args);
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.11.0" />
<PackageReference Include="NUnit" Version="5.0.0" />
<PackageReference Include="NUnit3TestAdapter" Version="6.3.1" />
<PackageReference Include="NUnit.Analyzers" Version="4.15.0" />
<PackageReference Include="coverlet.collector" Version="10.1.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
[TestFixture]
public class MergingMethodsTests
{
private Dictionary<int, string> _dictionaryA;
private Dictionary<int, string> _dictionaryB;
private Dictionary<int, string> _expectedMergedDictionary;
private Dictionary<int, string> _dictionaryA = [];
private Dictionary<int, string> _dictionaryB = [];
private Dictionary<int, string> _expectedMergedDictionary = [];
private Dictionary<int, string> _clashingDictionary = [];

[SetUp]
public void SetUp()
Expand Down Expand Up @@ -33,77 +34,109 @@ public void SetUp()
{ 5, "Watermelon" },
{ 6, "Pineapple" }
};

// key 1 already exists in _dictionaryA, with a different value
_clashingDictionary = new Dictionary<int, string>()
{
{ 1, "Watermelon" }
};
}

[Test]
public void GivenConcatMethod_WhenDictionaryAB_ThenExpectedDictionary()
{
var mergedDictionary = ConcatMethod(_dictionaryA, _dictionaryB);

CollectionAssert.AreEqual(_expectedMergedDictionary, mergedDictionary);
Assert.That(mergedDictionary, Is.EqualTo(_expectedMergedDictionary));
}

[Test]
public void GivenForEachMethod_WhenDictionaryAB_ThenExpectedDictionary()
{
var mergedDictionary = ForEachMethod(_dictionaryA, _dictionaryB);

CollectionAssert.AreEqual(_expectedMergedDictionary, mergedDictionary);
Assert.That(mergedDictionary, Is.EqualTo(_expectedMergedDictionary));
}

[Test]
public void GivenGroupByMethod_WhenDictionaryAB_ThenExpectedDictionary()
{
var mergedDictionary = GroupByMethod(_dictionaryA, _dictionaryB);

CollectionAssert.AreEqual(_expectedMergedDictionary, mergedDictionary);
Assert.That(mergedDictionary, Is.EqualTo(_expectedMergedDictionary));
}

[Test]
public void GivenToLookupMethod_WhenDictionaryAB_ThenExpectedDictionary()
{
var mergedDictionary = LookupMethod(_dictionaryA, _dictionaryB);

CollectionAssert.AreEqual(_expectedMergedDictionary, mergedDictionary);
Assert.That(mergedDictionary, Is.EqualTo(_expectedMergedDictionary));
}

[Test]
public void GivenUnionMethod_WhenDictionaryAB_ThenExpectedDictionary()
{
var mergedDictionary = UnionMethod(_dictionaryA, _dictionaryB);

CollectionAssert.AreEqual(_expectedMergedDictionary, mergedDictionary);
Assert.That(mergedDictionary, Is.EqualTo(_expectedMergedDictionary));
}

[Test]
public void GivenListsMethod_WhenDictionaryAB_ThenExpectedDictionary()
{
var mergedDictionary = UsingListsMethod(_dictionaryA, _dictionaryB);

CollectionAssert.AreEqual(_expectedMergedDictionary, mergedDictionary);
Assert.That(mergedDictionary, Is.EqualTo(_expectedMergedDictionary));
}

[Test]
public void GivenConcatMethod_WhenDictionariesWithDublicateKeys_ThenException()
public void GivenConcatMethod_WhenDictionariesWithDuplicateKeys_ThenException()
{
Assert.Throws<ArgumentException>(() => ConcatMethod(_dictionaryA, _dictionaryA));
}

[Test]
public void GivenUnionMethod_WhenDictionariesWithDublicateKeys_ThenException()
public void GivenUnionMethod_WhenDictionariesWithDuplicateKeys_ThenException()
{
Assert.Throws<ArgumentException>(() => UnionMethod(_dictionaryA, _clashingDictionary));
}

[Test]
public void GivenUnionMethod_WhenSameKeyWithSameValue_ThenNoException()
{
Assert.Throws<ArgumentException>(() => UnionMethod(_dictionaryA, new Dictionary<int, string>()
{
{1, "Watermelon" }
//key:1 already exists in _dictionaryA,
//but UnionMethod needs different key value pair to throw exception.
})
);
var mergedDictionary = UnionMethod(_dictionaryA, _dictionaryA);

Assert.That(mergedDictionary, Is.EqualTo(_dictionaryA));
}

[Test]
public void GivenListsMethod_WhenDictionariesWithDublicateKeys_ThenException()
public void GivenListsMethod_WhenDictionariesWithDuplicateKeys_ThenException()
{
Assert.Throws<ArgumentException>(() => UsingListsMethod(_dictionaryA, _dictionaryA));
}
}

[Test]
public void GivenForEachMethod_WhenDictionariesWithDuplicateKeys_ThenFirstValueKept()
{
var mergedDictionary = ForEachMethod(_dictionaryA, _clashingDictionary);

Assert.That(mergedDictionary[1], Is.EqualTo("Apple"));
}

[Test]
public void GivenGroupByMethod_WhenDictionariesWithDuplicateKeys_ThenFirstValueKept()
{
var mergedDictionary = GroupByMethod(_dictionaryA, _clashingDictionary);

Assert.That(mergedDictionary[1], Is.EqualTo("Apple"));
}

[Test]
public void GivenToLookupMethod_WhenDictionariesWithDuplicateKeys_ThenFirstValueKept()
{
var mergedDictionary = LookupMethod(_dictionaryA, _clashingDictionary);

Assert.That(mergedDictionary[1], Is.EqualTo("Apple"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public partial class Methods
{
public static Dictionary<TKey, TValue> ConcatMethod<TKey, TValue>(params Dictionary<TKey, TValue>[] dictionaries)
where TKey : notnull
{
var mergedDictionary = dictionaries.Aggregate((dict1, dict2) =>
dict1.Concat(dict2).ToDictionary(kvp => kvp.Key, kvp => kvp.Value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public partial class Methods
{
public static Dictionary<TKey, TValue> ForEachMethod<TKey, TValue>(params Dictionary<TKey, TValue>[] dictionaries)
where TKey : notnull
{
var mergedDictionary = new Dictionary<TKey, TValue>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public partial class Methods
{
public static Dictionary<TKey, TValue> GroupByMethod<TKey, TValue>(params Dictionary<TKey, TValue>[] dictionaries)
where TKey : notnull
{
var mergedDictionary = dictionaries.SelectMany(dict => dict)
.GroupBy(kvp => kvp.Key)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public partial class Methods
{
public static Dictionary<TKey, TValue> LookupMethod<TKey, TValue>(params Dictionary<TKey, TValue>[] dictionaries)
where TKey : notnull
{
var mergedDictionary = dictionaries.SelectMany(dict => dict)
.ToLookup(pair => pair.Key, pair => pair.Value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public partial class Methods
{
public static Dictionary<TKey, TValue> UnionMethod<TKey, TValue>(params Dictionary<TKey, TValue>[] dictionaries)
where TKey : notnull
{
var mergedDictionary = dictionaries.Aggregate((result, dict) =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public partial class Methods
{
public static Dictionary<TKey, TValue> UsingListsMethod<TKey, TValue>(params Dictionary<TKey, TValue>[] dictionaries)
where TKey : notnull
{
var keys = new List<TKey>();
var values = new List<TValue>();
Expand Down
Loading