From d2100afc5075bd4fc7043f5458b416837df16c85 Mon Sep 17 00:00:00 2001 From: GCollins994 <44612657+GCollins994@users.noreply.github.com> Date: Wed, 8 May 2024 00:13:44 -0400 Subject: [PATCH 1/3] tests/new-unit-tests Added the following unit tests: ListDictionaryTest, RollingWindowTest, ValueStringBuilderTest. --- .../ListDictionaryTest.cs | 162 +++++++++++++ .../RollingWindowTest.cs | 219 ++++++++++++++++++ .../ValueStringBuilderTest.cs | 176 ++++++++++++++ 3 files changed, 557 insertions(+) create mode 100644 tests/Nucs.Essentials.UnitTests/ListDictionaryTest.cs create mode 100644 tests/Nucs.Essentials.UnitTests/RollingWindowTest.cs create mode 100644 tests/Nucs.Essentials.UnitTests/ValueStringBuilderTest.cs diff --git a/tests/Nucs.Essentials.UnitTests/ListDictionaryTest.cs b/tests/Nucs.Essentials.UnitTests/ListDictionaryTest.cs new file mode 100644 index 0000000..a793050 --- /dev/null +++ b/tests/Nucs.Essentials.UnitTests/ListDictionaryTest.cs @@ -0,0 +1,162 @@ +using System; +using System.Collections.Generic; +using System.Collections.Specialized; +using FluentAssertions; +using Nucs.Collections; +using Xunit; +using Xunit.Abstractions; + +namespace Nucs.Essentials.UnitTests; + +//Keys: Ctrl + E : check what func requires, Ctrl + W : select word, Ctrl + Shift + W : unselect word +//TODO: Read about generics, warning: its a deepdive so just get the general idea and dive back to tests +public class ListDictionaryTest +{ + [Fact] + public void AddTest() + { + //Arrange + var listDictionary = new ListedDictionary(); + + //Act + listDictionary.Add("item1", "value1"); + + //Assert + Assert.True(listDictionary.Contains(new KeyValuePair("item1", "value1"))); + Assert.False(listDictionary.Contains(new KeyValuePair("item2", "value2"))); + } + + [Fact] + public void AddRangeTest() + { + //Arrange + var tempDictionary = new ListedDictionary(); + tempDictionary.Add("item1", "value1"); + tempDictionary.Add("item2", "value2"); + tempDictionary.Add("item3", "value3"); + + var listDictionary = new ListedDictionary(); + + //Act + listDictionary.AddRange(tempDictionary); + + //Assert + Assert.True(listDictionary.Contains(new KeyValuePair("item2", "value2"))); + } + + [Fact] + public void ClearTest() + { + //Arrange + var listDictionary = new ListedDictionary(); + listDictionary.Add("item1", "value1"); + + //Act + listDictionary.Clear(); + + //Assert + Assert.Empty(listDictionary); + Assert.False(listDictionary.Contains(new KeyValuePair("item1", "value1"))); + } + + [Fact] + public void ContainsTest() + { + //Arrange + var listDictionary = new ListedDictionary(); + listDictionary.Add("item1", "value1"); + + //Assert + listDictionary.Contains(new KeyValuePair("item1", "value1")).Should().BeTrue(); + listDictionary.Contains(new KeyValuePair("item2", "value2")).Should().BeFalse(); + } + + [Fact] + public void CopyToTest() + { + //Arrange + var listDictionary = new ListedDictionary(); + listDictionary.Add("item1", "value1"); + listDictionary.Add("item2", "value2"); + listDictionary.Add("item3", "value3"); + var array = new KeyValuePair[3]; + //Act + listDictionary.CopyTo(array, 0); + //Assert + Assert.Equal(new KeyValuePair("item1", "value1"), array[0]); + Assert.Equal(new KeyValuePair("item2", "value2"), array[1]); + Assert.Equal(new KeyValuePair("item3", "value3"), array[2]); + } + + [Fact] + public void RemoveTest() + { + //Arrange + var listDictionary = new ListedDictionary(); + listDictionary.Add("item1", "value1"); + + //Act + listDictionary.Remove(new KeyValuePair("item1", "value1")); + + //Assert + Assert.Empty(listDictionary); + Assert.DoesNotContain(new KeyValuePair("item1", "value1"), listDictionary); + } + + [Fact] + public void ContainsKeyTest() + { + //Arrange + var listDictionary = new ListedDictionary(); + listDictionary.Add("item1", "value1"); + //Act + var resultT = listDictionary.ContainsKey("item1"); + var resultF = listDictionary.ContainsKey("item2"); + //Assert + resultT.Should().BeTrue(); + resultF.Should().BeFalse(); + } + + [Fact] + public void RemoveTest2() + { + //Arrange + var listDictionary = new ListedDictionary(); + listDictionary.Add("item1", "value1"); + //Act + var result = listDictionary.Remove("item1"); + //Assert + result.Should().BeTrue(); + listDictionary.ContainsKey("item1").Should().BeFalse(); + } + + [Fact] + public void IndexOfTest() + { + //Arrange + var listDictionary = new ListedDictionary(); + listDictionary.Add("item1", "value1"); + listDictionary.Add("item2", "value2"); + //Act + var result1 = listDictionary.IndexOf("item1"); + var result2 = listDictionary.IndexOf("item2"); + //Assert + result1.Should().Be(0); + result2.Should().Be(1); + } + + [Fact] + public void TryGetValueTest() + { + //Arrange + var listDictionary = new ListedDictionary(); + listDictionary.Add("item1", "value1"); + //Act + var resultT = listDictionary.TryGetValue("item1", out var value1); + var resultF = listDictionary.TryGetValue("item2", out var value2); + //Assert + resultT.Should().BeTrue(); + resultF.Should().BeFalse(); + value1.Should().Be("value1"); + } +} \ No newline at end of file diff --git a/tests/Nucs.Essentials.UnitTests/RollingWindowTest.cs b/tests/Nucs.Essentials.UnitTests/RollingWindowTest.cs new file mode 100644 index 0000000..e06cbfa --- /dev/null +++ b/tests/Nucs.Essentials.UnitTests/RollingWindowTest.cs @@ -0,0 +1,219 @@ +using System; +using System.Collections.Generic; +using System.Collections.Specialized; +using FluentAssertions; +using Nucs.Collections; +using Xunit; +using Xunit.Abstractions; +using Xunit.Sdk; + +namespace Nucs.Essentials.UnitTests; + +public class RollingWindowTest +{ + //TODO: try to do more tests with args cuz its awseome + [Theory] + [InlineData(1, 2, 0)] + [InlineData(2, 2, 1)] + [InlineData(3, 2, 2)] + [InlineData(1, 3, 0)] + [InlineData(3, 3, 2)] + [InlineData(4, 3, 3)] + public void MySpecialTestNewest(int numberOfPushes, int windowSize, int newestExpectedToBe) + { + var window = new RollingWindow(windowSize); + for (int i = 0; i < numberOfPushes; i++) + { + window.Push(i); + } + + window.Newest.Should().Be(newestExpectedToBe); + } + + [Fact] + public void LatestTest() + { + //Arrange + var window = new RollingWindow(5); + window.Push(1); + var latest = window.Latest; + + //Assert + latest.Should().Be(1); + + window.Push(2); + latest = window.Latest; + latest.Should().Be(1); + + window.Push(3); + latest = window.Latest; + latest.Should().Be(1); + + //When empty + var ex = Assert.Throws(() => {; + window.Reset(); + latest = window.Latest; + }); + + //Arrange + for(int i = 0; i < 8; i++) + window.Push(i); + //Assert + latest = window.Latest; + latest.Should().Be(3); + } + + [Theory] + [InlineData(1)] + [InlineData(2)] + [InlineData(-1)] + [InlineData(0)] + [InlineData(3)] + public void NewestTest(int number) + { + //Arrange + var window = new RollingWindow(3); + + //When Empty + var ex = Assert.Throws(() => {; + var newest = window.Newest; + }); + //Act + window.Push(number); + var newest = window.Newest; + //Assert + newest.Should().Be(number); + } + + [Fact] + public void MostRecentlyRemovedTest() + { + //Arrange + var window = new RollingWindow(3); + //When Nothing was Removed + window.Push(1); + Assert.Throws(() => {; + var exception = window.MostRecentlyRemoved; + }); + //Act + window.Push(2); + window.Push(3); + window.Push(4); + //Assert + window.MostRecentlyRemoved.Should().Be(1); + + //Arrange + window.Reset(); + window.Push(10); + //When Nothing was Removed + Assert.Throws(() => {; + var exception = window.MostRecentlyRemoved; + }); + //Act + window.Push(20); + window.Push(30); + window.Push(40); + //Assert + window.MostRecentlyRemoved.Should().Be(10); + + } + + [Fact] + public void IsReadyTest() + { + //Arrange + var window = new RollingWindow(3); + //Assert + window.IsReady.Should().BeFalse(); + + //Arrange + window.Push(1); + window.Push(2); + + //Assert + window.IsReady.Should().BeFalse(); + + //Arrange + window.Push(3); + + //Assert + window.IsReady.Should().BeTrue(); + + //Arrange + window.Push(4); + + //Assert + window.IsReady.Should().BeTrue(); + } + + [Fact] + public void ThisTest() + { + //Arrange + var window = new RollingWindow(5); + //Out of Range (No value) + Assert.Throws(() => {; + _ = window[2]; + }); + //Act + window.Push(1); + window.Push(2); + window.Push(3); + window.Push(4); + window.Push(5); + //Assert + window[0].Should().Be(5); + window[3].Should().Be(2); + //Out of Range + Assert.Throws(() => {; + _ = window[15]; + }); + } + + [Fact] + public void PushTest() + { + //Arrange + var window = new RollingWindow(3); + //Act + window.Push(1); + window.Push(2); + //Assert + window.Newest.Should().Be(2); + window.Latest.Should().Be(1); + //Arrange + var window2 = new RollingWindow(3); + //Act + window2.Push("One"); + window2.Push("Two"); + //Assert + window2[0].Should().Be("Two"); + //Act + window2.Push("Three"); + window2.Push("Four"); + //Assert + window2.Latest.Should().Be("Two"); + } + + [Fact] + public void ResetTest() + { + //Arrange + var window = new RollingWindow(3); + //Act + window.Push(1); + window.Push(2); + window.Push(3); + window.Reset(); + //Assert + window.Count.Should().Be(0); + window.IsReady.Should().BeFalse(); + //Act + window.Push(2); + window.Push(5); + window.Reset(); + window.Push(1); + //Assert + window.Count.Should().Be(1); + } +} \ No newline at end of file diff --git a/tests/Nucs.Essentials.UnitTests/ValueStringBuilderTest.cs b/tests/Nucs.Essentials.UnitTests/ValueStringBuilderTest.cs new file mode 100644 index 0000000..59fa885 --- /dev/null +++ b/tests/Nucs.Essentials.UnitTests/ValueStringBuilderTest.cs @@ -0,0 +1,176 @@ +using System; +using System.Collections.Generic; +using System.Collections.Specialized; +using FluentAssertions; +using Nucs.Collections; +using Nucs.Text; +using Xunit; +using Xunit.Abstractions; +using Xunit.Sdk; + +namespace Nucs.Essentials.UnitTests.Resources; + +public class ValueStringBuilderTest +{ + [Fact] + public void InsertTest() + { + //Arrange + var sb = new ValueStringBuilder(); + //Act + sb.Append("Absolute Kek"); + sb.Insert(8, " Top"); + //Assert + sb.ToString().Should().Be("Absolute Top Kek"); + } + + [Fact] + public void AppendTest() + { + //Arrange + var sb = new ValueStringBuilder(); + + sb.Append("This is "); + sb.Append("Absolute Kek"); + //Assert + sb.ToString().Should().Be("This is Absolute Kek"); + + //Arrange + var sb2 = new ValueStringBuilder(100); + //Assert + sb2.Append("This is "); + sb2.Length.Should().Be(8); + sb2.Append('!', 5); + sb2.Length.Should().Be(13); + sb2.AppendSpan(5); + sb2.Length.Should().Be(18); + sb2.Append("a long space"); + sb2.Length.Should().Be(30); + sb2.Insert(15, "HA"); + sb2[15].ToString().Should().Be("H"); + sb2[13] = ' '; + sb2[14] = ' '; + sb2[17] = ' '; + sb2[18] = ' '; + sb2[19] = ' '; + sb2.ToString().Should().Be("This is !!!!! HA a long space"); + } + + [Fact] + public void AppendTest5() + { + //Arrange + var sb = new ValueStringBuilder(); + + //Assert + sb.Append("This is "); + sb.ToString().Should().Be("This is "); + sb.Insert(0, "A"); + sb.ToString().Should().Be("AThis is "); + sb.Insert(sb.Length, "B"); + sb.ToString().Should().Be("AThis is B"); + sb.Insert(1, "C"); + sb.ToString().Should().Be("ACThis is B"); + + new Action(AppendTest25).Should().Throw(); + void AppendTest25() + { + var sb = new ValueStringBuilder(); + sb.Append("This is "); + sb.Insert(-1, "A"); //throws exception + } + } + + [Fact] + public void TryCopyToTest() + { + //Arrange + var sb = new ValueStringBuilder(); + sb.Append("Some text in a nice string builder"); + + Span destination = new char[34]; + Span destination2 = new char[20]; + Span destination3 = new char[100]; + + //Assert + sb.TryCopyTo(destination, out int charsWritten).Should().BeTrue(); + charsWritten.Should().Be(34); + //Assert + sb.Append("This is another text after the first one got demolished"); + sb.TryCopyTo(destination2, out int charsWritten2).Should().BeFalse(); + charsWritten2.Should().Be(0); + //Assert + sb.TryCopyTo(destination3, out int charsWritten3).Should().BeTrue(); + charsWritten3.Should().Be(0); + } + + [Theory] + [InlineData("Hello World", 11, 16)] + [InlineData("Hello World, This is a longer sentence", 38, 64)] + [InlineData("Shorter, but longer than 1st", 28, 32)] + public void AppendCapacityTest(string input, int lengthOutput, int capacityOutput) + { + //Arrange + var sb = new ValueStringBuilder(); + //Act + sb.Append(input); + //Assert + sb.Length.Should().Be(lengthOutput); + sb.Capacity.Should().Be(capacityOutput); + } + + + [Theory] + [InlineData(8, 16)] + [InlineData(17, 32)] + [InlineData(33, 64)] + [InlineData(65, 128)] + public void EnsureTotalCapacityTest(int required, int expected) + { + //Arrange + var sb = new ValueStringBuilder(); + sb.Append("Heyyyyy"); + //Act + sb.EnsureTotalCapacity(required); + //Assert + sb.Capacity.Should().Be(expected); + } + + [Fact] + public void RemoveStartTest() + { + //Arrange + var sb = new ValueStringBuilder(); + sb.Append("Hello World"); + //Act + sb.RemoveStart(6); + //Assert + sb.ToString().Should().Be("World"); + //Act + sb.RemoveStart(5); + //Assert + sb.ToString().Should().Be(""); + //Act + sb.Append("Hello"); + sb.RemoveStart(10); + //Assert + sb.ToString().Should().Be(""); + } + + [Theory] + [InlineData("Hello World", 5)] + [InlineData("Hello World, This is a longer sentence", 26)] + [InlineData("Shorter, but longer than 1st", 4)] + [InlineData("Capacity can be 16, 32, 64 and so on. This one is 64", 12)] + [InlineData("This string is 11 chars long, therefore the capacity is 128 and buffer is 52", 52)] + public void GetBufferTest(string input, int bufferLength) + { + //Arrange + var sb = new ValueStringBuilder(); + sb.Append(input); + //Act + var buffer = sb.GetBuffer(); + //Assert + buffer.Length.Should().Be(bufferLength); + } +} \ No newline at end of file From 9d5f06999a12535cac278922bf0fb78689f4dd87 Mon Sep 17 00:00:00 2001 From: GCollins994 <44612657+GCollins994@users.noreply.github.com> Date: Wed, 8 May 2024 00:16:12 -0400 Subject: [PATCH 2/3] tests/Removed-clutter Cleaned up code --- tests/Nucs.Essentials.UnitTests/ListDictionaryTest.cs | 2 -- tests/Nucs.Essentials.UnitTests/RollingWindowTest.cs | 1 - 2 files changed, 3 deletions(-) diff --git a/tests/Nucs.Essentials.UnitTests/ListDictionaryTest.cs b/tests/Nucs.Essentials.UnitTests/ListDictionaryTest.cs index a793050..efb6a5a 100644 --- a/tests/Nucs.Essentials.UnitTests/ListDictionaryTest.cs +++ b/tests/Nucs.Essentials.UnitTests/ListDictionaryTest.cs @@ -8,8 +8,6 @@ namespace Nucs.Essentials.UnitTests; -//Keys: Ctrl + E : check what func requires, Ctrl + W : select word, Ctrl + Shift + W : unselect word -//TODO: Read about generics, warning: its a deepdive so just get the general idea and dive back to tests public class ListDictionaryTest { [Fact] diff --git a/tests/Nucs.Essentials.UnitTests/RollingWindowTest.cs b/tests/Nucs.Essentials.UnitTests/RollingWindowTest.cs index e06cbfa..b1e483c 100644 --- a/tests/Nucs.Essentials.UnitTests/RollingWindowTest.cs +++ b/tests/Nucs.Essentials.UnitTests/RollingWindowTest.cs @@ -11,7 +11,6 @@ namespace Nucs.Essentials.UnitTests; public class RollingWindowTest { - //TODO: try to do more tests with args cuz its awseome [Theory] [InlineData(1, 2, 0)] [InlineData(2, 2, 1)] From 5e96cd723aa25391baedb8ee9c42f982667237ea Mon Sep 17 00:00:00 2001 From: GCollins994 <44612657+GCollins994@users.noreply.github.com> Date: Sat, 11 May 2024 10:56:11 -0400 Subject: [PATCH 3/3] update/new-unit-tests A few updates and corrections: Corrected function names NewestTestLoopPush, NewestTestSinglePush. Replaced redundant variables with discard operator '_'. --- .../Nucs.Essentials.UnitTests/RollingWindowTest.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/Nucs.Essentials.UnitTests/RollingWindowTest.cs b/tests/Nucs.Essentials.UnitTests/RollingWindowTest.cs index b1e483c..d318ba4 100644 --- a/tests/Nucs.Essentials.UnitTests/RollingWindowTest.cs +++ b/tests/Nucs.Essentials.UnitTests/RollingWindowTest.cs @@ -18,7 +18,7 @@ public class RollingWindowTest [InlineData(1, 3, 0)] [InlineData(3, 3, 2)] [InlineData(4, 3, 3)] - public void MySpecialTestNewest(int numberOfPushes, int windowSize, int newestExpectedToBe) + public void NewestTestLoopPush(int numberOfPushes, int windowSize, int newestExpectedToBe) { var window = new RollingWindow(windowSize); for (int i = 0; i < numberOfPushes; i++) @@ -49,7 +49,7 @@ public void LatestTest() latest.Should().Be(1); //When empty - var ex = Assert.Throws(() => {; + Assert.Throws(() => {; window.Reset(); latest = window.Latest; }); @@ -68,14 +68,14 @@ public void LatestTest() [InlineData(-1)] [InlineData(0)] [InlineData(3)] - public void NewestTest(int number) + public void NewestTestSinglePush(int number) { //Arrange var window = new RollingWindow(3); //When Empty - var ex = Assert.Throws(() => {; - var newest = window.Newest; + Assert.Throws(() => {; + _ = window.Newest; }); //Act window.Push(number); @@ -92,7 +92,7 @@ public void MostRecentlyRemovedTest() //When Nothing was Removed window.Push(1); Assert.Throws(() => {; - var exception = window.MostRecentlyRemoved; + _ = window.MostRecentlyRemoved; }); //Act window.Push(2); @@ -106,7 +106,7 @@ public void MostRecentlyRemovedTest() window.Push(10); //When Nothing was Removed Assert.Throws(() => {; - var exception = window.MostRecentlyRemoved; + _ = window.MostRecentlyRemoved; }); //Act window.Push(20);