From 6829ec154df6daeec3e9d73d3799aaefe9285945 Mon Sep 17 00:00:00 2001 From: AnnulusGames Date: Tue, 20 Aug 2024 13:27:32 +0900 Subject: [PATCH] Add: WeightedListTests --- src/RandomExtensions/AssemblyInfo.cs | 3 ++ .../Collections/WeightedList.cs | 2 + .../WeightedListTests.cs | 38 +++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 src/RandomExtensions/AssemblyInfo.cs diff --git a/src/RandomExtensions/AssemblyInfo.cs b/src/RandomExtensions/AssemblyInfo.cs new file mode 100644 index 0000000..5136b73 --- /dev/null +++ b/src/RandomExtensions/AssemblyInfo.cs @@ -0,0 +1,3 @@ +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("RandomExtensions.Tests")] \ No newline at end of file diff --git a/src/RandomExtensions/Collections/WeightedList.cs b/src/RandomExtensions/Collections/WeightedList.cs index 08495c6..b002f2d 100644 --- a/src/RandomExtensions/Collections/WeightedList.cs +++ b/src/RandomExtensions/Collections/WeightedList.cs @@ -72,6 +72,8 @@ public WeightedList() readonly List> list; readonly ValueCollection values; + internal double TotalWeight => totalWeight; + public WeightedValue this[int index] { get => list[index]; diff --git a/tests/RandomExtensions.Tests/WeightedListTests.cs b/tests/RandomExtensions.Tests/WeightedListTests.cs index a577eee..55f3ec5 100644 --- a/tests/RandomExtensions.Tests/WeightedListTests.cs +++ b/tests/RandomExtensions.Tests/WeightedListTests.cs @@ -5,6 +5,44 @@ namespace RandomExtensions.Tests; public class WeightedListTests { + [Test] + public void Test_Add() + { + var list = new WeightedList(); + + list.Add(0, 1); + Assert.That(list.Count, Is.EqualTo(1)); + Assert.That(list[0].Value, Is.EqualTo(0)); + Assert.That(list.TotalWeight, Is.EqualTo(1)); + + list.Add(1, 2); + Assert.That(list.Count, Is.EqualTo(2)); + Assert.That(list[1].Value, Is.EqualTo(1)); + Assert.That(list.TotalWeight, Is.EqualTo(3)); + } + + [Test] + public void Test_Remove() + { + var list = new WeightedList + { + { 0, 1 }, + { 1, 1 }, + { 2, 1 }, + { 3, 1 }, + }; + + Assert.That(list.TotalWeight, Is.EqualTo(4)); + list.RemoveAt(0); + Assert.That(list.TotalWeight, Is.EqualTo(3)); + list.RemoveAt(0); + Assert.That(list.TotalWeight, Is.EqualTo(2)); + list.RemoveAt(0); + Assert.That(list.TotalWeight, Is.EqualTo(1)); + list.RemoveAt(0); + Assert.That(list.TotalWeight, Is.EqualTo(0)); + } + [Test] public void Test_RemoveRandom() {