Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: Add Unit Tests #1

Merged
merged 3 commits into from
May 11, 2024
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
160 changes: 160 additions & 0 deletions tests/Nucs.Essentials.UnitTests/ListDictionaryTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using FluentAssertions;
using Nucs.Collections;
using Xunit;
using Xunit.Abstractions;

namespace Nucs.Essentials.UnitTests;

public class ListDictionaryTest
{
[Fact]
public void AddTest()
{
//Arrange
var listDictionary = new ListedDictionary<string, string>();

//Act
listDictionary.Add("item1", "value1");

//Assert
Assert.True(listDictionary.Contains(new KeyValuePair<string, string>("item1", "value1")));
Assert.False(listDictionary.Contains(new KeyValuePair<string, string>("item2", "value2")));
}

[Fact]
public void AddRangeTest()
{
//Arrange
var tempDictionary = new ListedDictionary<string, string>();
tempDictionary.Add("item1", "value1");
tempDictionary.Add("item2", "value2");
tempDictionary.Add("item3", "value3");

var listDictionary = new ListedDictionary<string, string>();

//Act
listDictionary.AddRange(tempDictionary);

//Assert
Assert.True(listDictionary.Contains(new KeyValuePair<string, string>("item2", "value2")));
}

[Fact]
public void ClearTest()
{
//Arrange
var listDictionary = new ListedDictionary<string, string>();
listDictionary.Add("item1", "value1");

//Act
listDictionary.Clear();

//Assert
Assert.Empty(listDictionary);
Assert.False(listDictionary.Contains(new KeyValuePair<string, string>("item1", "value1")));
}

[Fact]
public void ContainsTest()
{
//Arrange
var listDictionary = new ListedDictionary<string, string>();
listDictionary.Add("item1", "value1");

//Assert
listDictionary.Contains(new KeyValuePair<string, string>("item1", "value1")).Should().BeTrue();
listDictionary.Contains(new KeyValuePair<string, string>("item2", "value2")).Should().BeFalse();
}

[Fact]
public void CopyToTest()
{
//Arrange
var listDictionary = new ListedDictionary<string, string>();
listDictionary.Add("item1", "value1");
listDictionary.Add("item2", "value2");
listDictionary.Add("item3", "value3");
var array = new KeyValuePair<string, string>[3];
//Act
listDictionary.CopyTo(array, 0);
//Assert
Assert.Equal(new KeyValuePair<string, string>("item1", "value1"), array[0]);
Assert.Equal(new KeyValuePair<string, string>("item2", "value2"), array[1]);
Assert.Equal(new KeyValuePair<string, string>("item3", "value3"), array[2]);
}

[Fact]
public void RemoveTest()
{
//Arrange
var listDictionary = new ListedDictionary<string, string>();
listDictionary.Add("item1", "value1");

//Act
listDictionary.Remove(new KeyValuePair<string, string>("item1", "value1"));

//Assert
Assert.Empty(listDictionary);
Assert.DoesNotContain(new KeyValuePair<string, string>("item1", "value1"), listDictionary);
}

[Fact]
public void ContainsKeyTest()
{
//Arrange
var listDictionary = new ListedDictionary<string, string>();
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<string, string>();
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<string, string>();
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<string, string>();
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");
}
}
218 changes: 218 additions & 0 deletions tests/Nucs.Essentials.UnitTests/RollingWindowTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
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
{
[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 NewestTestLoopPush(int numberOfPushes, int windowSize, int newestExpectedToBe)
{
var window = new RollingWindow<int>(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<int>(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
Assert.Throws<DivideByZeroException>(() => {;
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 NewestTestSinglePush(int number)
{
//Arrange
var window = new RollingWindow<int>(3);

//When Empty
Assert.Throws<DivideByZeroException>(() => {;
_ = window.Newest;
});
//Act
window.Push(number);
var newest = window.Newest;
//Assert
newest.Should().Be(number);
}

[Fact]
public void MostRecentlyRemovedTest()
{
//Arrange
var window = new RollingWindow<int>(3);
//When Nothing was Removed
window.Push(1);
Assert.Throws<InvalidOperationException>(() => {;
_ = 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<InvalidOperationException>(() => {;
_ = 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<int>(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<int>(5);
//Out of Range (No value)
Assert.Throws<ArgumentOutOfRangeException>(() => {;
_ = 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<ArgumentOutOfRangeException>(() => {;
_ = window[15];
});
}

[Fact]
public void PushTest()
{
//Arrange
var window = new RollingWindow<int>(3);
//Act
window.Push(1);
window.Push(2);
//Assert
window.Newest.Should().Be(2);
window.Latest.Should().Be(1);
//Arrange
var window2 = new RollingWindow<string>(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<int>(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);
}
}
Loading