Skip to content
Merged
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
@@ -1,18 +1,17 @@
<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="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.10.1" />
<PackageReference Include="MSTest" Version="4.5.1" />
<PackageReference Include="coverlet.collector" Version="10.1.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Concurrent;
using UpdateDictionaryValue;

namespace Tests
Expand Down Expand Up @@ -35,5 +36,41 @@ public void GivenUpdatingNewToppingValue_ThenNewToppingIsAdded()

Assert.AreEqual(12, dictionary.Toppings["mushroom"]);
}

[TestMethod]
public void GivenExistingTopping_WhenAddToppingIfMissing_ThenValueIsUnchanged()
{
var dictionary = new ToppingsDictionary();

dictionary.AddToppingIfMissing("pepperoni", 100);
dictionary.AddToppingIfMissing("mushroom", 2);

Assert.AreEqual(4, dictionary.Toppings["pepperoni"]);
Assert.AreEqual(2, dictionary.Toppings["mushroom"]);
}

[TestMethod]
public void GivenNewAndExistingToppings_WhenAddToppingsByRef_ThenBothAreUpdated()
{
var dictionary = new ToppingsDictionary();

dictionary.AddToppingsByRef("mushroom", 5);
dictionary.AddToppingsByRef("pepperoni", 4);

Assert.AreEqual(5, dictionary.Toppings["mushroom"]);
Assert.AreEqual(8, dictionary.Toppings["pepperoni"]);
}

[TestMethod]
public void GivenConcurrentDictionary_WhenAddOrUpdate_ThenAddsThenUpdates()
{
var toppings = new ConcurrentDictionary<string, int>();

var first = toppings.AddOrUpdate("pepperoni", 4, (_, currentAmount) => currentAmount + 4);
var second = toppings.AddOrUpdate("pepperoni", 4, (_, currentAmount) => currentAmount + 4);

Assert.AreEqual(4, first);
Assert.AreEqual(8, second);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
using UpdateDictionaryValue;
using System.Collections.Concurrent;
using UpdateDictionaryValue;

var toppingsDictionary = new ToppingsDictionary();
toppingsDictionary.AddToppings("pepperoni", 4);
toppingsDictionary.AddToppings("olive", 3);

PrintToppings(toppingsDictionary);

try
{
toppingsDictionary.Toppings["jalepeno"] = toppingsDictionary.Toppings["jalepeno"] + 1;
// The indexer-only version of AddToppings reads the value before it writes it
toppingsDictionary.Toppings["jalapeno"] = toppingsDictionary.Toppings["jalapeno"] + 1;
}
catch (Exception e)
catch (KeyNotFoundException e)
{
Console.WriteLine(e);
Console.WriteLine($"{e.GetType()}: {e.Message}");
}

toppingsDictionary.AddToppings("jalepeno", 1);
toppingsDictionary.AddToppings("jalapeno", 1);

PrintToppings(toppingsDictionary);

var toppings = new ConcurrentDictionary<string, int>();

foreach (var topping in toppingsDictionary.Toppings)
Console.WriteLine(toppings.AddOrUpdate("pepperoni", 4, (_, currentAmount) => currentAmount + 4));
Console.WriteLine(toppings.AddOrUpdate("pepperoni", 4, (_, currentAmount) => currentAmount + 4));

static void PrintToppings(ToppingsDictionary toppingsDictionary)
{
Console.WriteLine($"{topping.Key} = {topping.Value}");
}
foreach (var topping in toppingsDictionary.Toppings)
{
Console.WriteLine($"{topping.Key} = {topping.Value}");
}

Console.WriteLine();
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
namespace UpdateDictionaryValue
using System.Runtime.InteropServices;

namespace UpdateDictionaryValue
{
public class ToppingsDictionary
{
public Dictionary<string, int> Toppings { get; private set; }
public Dictionary<string, int> Toppings { get; }

public ToppingsDictionary()
{
Expand All @@ -25,5 +27,21 @@ public void AddToppings(string toppingType, int amount)
Toppings.Add(toppingType, amount);
}
}

public void AddToppingIfMissing(string toppingType, int amount)
{
if (!Toppings.TryAdd(toppingType, amount))
{
Console.WriteLine($"{toppingType} is already on the pizza.");
}
}

public void AddToppingsByRef(string toppingType, int amount)
{
ref var currentAmount = ref CollectionsMarshal
.GetValueRefOrAddDefault(Toppings, toppingType, out _);

currentAmount += amount;
}
}
}
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
Loading