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
491 changes: 245 additions & 246 deletions DataStructures.Tests/AATreeTests.cs

Large diffs are not rendered by default.

637 changes: 318 additions & 319 deletions DataStructures.Tests/AVLTreeTests.cs

Large diffs are not rendered by default.

549 changes: 274 additions & 275 deletions DataStructures.Tests/BinarySearchTreeTests.cs

Large diffs are not rendered by default.

821 changes: 410 additions & 411 deletions DataStructures.Tests/BitArrayTests.cs

Large diffs are not rendered by default.

140 changes: 69 additions & 71 deletions DataStructures.Tests/Cache/LfuCacheTests.cs
Original file line number Diff line number Diff line change
@@ -1,78 +1,76 @@
using System;
using DataStructures.Cache;
using NUnit.Framework;
using FluentAssertions;
using NUnit.Framework;

namespace DataStructures.Tests.Cache
namespace DataStructures.Tests.Cache;

public static class LfuCacheTests
{
public static class LfuCacheTests
[Test]
public static void TestPutGet()
{
var cache = new LfuCache<int, string>();
cache.Put(1, "one");

cache.Contains(1).Should().BeTrue();
cache.Get(1).Should().Be("one");
}

[Test]
public static void TestCacheMiss()
{
[Test]
public static void TestPutGet()
{
var cache = new LfuCache<int, string>();
cache.Put(1, "one");

cache.Contains(1).Should().BeTrue();
cache.Get(1).Should().Be("one");
}

[Test]
public static void TestCacheMiss()
{
var cache = new LfuCache<int, string>();
cache.Put(1, "one");

cache.Contains(5).Should().BeFalse();
cache.Get(5).Should().BeNull();
}

[Test]
public static void Evict_ItemWasNotUsed()
{
var cache = new LfuCache<int, string>(capacity: 1);
cache.Put(1, "one");

// Add to the full cache, 1 will be removed
cache.Put(2, "two");

cache.Get(1).Should().BeNull();
cache.Get(2).Should().Be("two");
}

[Test]
public static void Evict_OneItemWasUsed()
{
var cache = new LfuCache<int, string>(capacity: 2);
cache.Put(1, "one");
cache.Put(2, "two");

cache.Put(1, "ONE");

// Add to the full cache, 2 will be removed
cache.Put(3, "three");

cache.Get(1).Should().Be("ONE");
cache.Get(2).Should().BeNull();
cache.Get(3).Should().Be("three");
}

[Test]
public static void Evict_LruOrder()
{
var cache = new LfuCache<int, string>(capacity: 2);
cache.Put(1, "one");
cache.Put(2, "two");

cache.Put(1, "ONE");
cache.Put(2, "TWO");

// Add to the full cache, 1 will be removed
cache.Put(3, "three");

cache.Get(1).Should().BeNull();
cache.Get(2).Should().Be("TWO");
cache.Get(3).Should().Be("three");
}
var cache = new LfuCache<int, string>();
cache.Put(1, "one");

cache.Contains(5).Should().BeFalse();
cache.Get(5).Should().BeNull();
}

[Test]
public static void Evict_ItemWasNotUsed()
{
var cache = new LfuCache<int, string>(capacity: 1);
cache.Put(1, "one");

// Add to the full cache, 1 will be removed
cache.Put(2, "two");

cache.Get(1).Should().BeNull();
cache.Get(2).Should().Be("two");
}

[Test]
public static void Evict_OneItemWasUsed()
{
var cache = new LfuCache<int, string>(capacity: 2);
cache.Put(1, "one");
cache.Put(2, "two");

cache.Put(1, "ONE");

// Add to the full cache, 2 will be removed
cache.Put(3, "three");

cache.Get(1).Should().Be("ONE");
cache.Get(2).Should().BeNull();
cache.Get(3).Should().Be("three");
}

[Test]
public static void Evict_LruOrder()
{
var cache = new LfuCache<int, string>(capacity: 2);
cache.Put(1, "one");
cache.Put(2, "two");

cache.Put(1, "ONE");
cache.Put(2, "TWO");

// Add to the full cache, 1 will be removed
cache.Put(3, "three");

cache.Get(1).Should().BeNull();
cache.Get(2).Should().Be("TWO");
cache.Get(3).Should().Be("three");
}
}
106 changes: 52 additions & 54 deletions DataStructures.Tests/Cache/LruCacheTests.cs
Original file line number Diff line number Diff line change
@@ -1,71 +1,69 @@
using System;
using DataStructures.Cache;
using NUnit.Framework;
using FluentAssertions;
using NUnit.Framework;

namespace DataStructures.Tests.Cache;

namespace DataStructures.Tests.Cache
public static class LruCacheTests
{
public static class LruCacheTests
[Test]
public static void TestPutGet()
{
[Test]
public static void TestPutGet()
{
var cache = new LruCache<int, string>();
cache.Put(1, "one");
var cache = new LruCache<int, string>();
cache.Put(1, "one");

cache.Contains(1).Should().BeTrue();
cache.Get(1).Should().Be("one");
}
cache.Contains(1).Should().BeTrue();
cache.Get(1).Should().Be("one");
}

[Test]
public static void TestCacheMiss()
{
var cache = new LruCache<int, string>();
cache.Put(1, "one");
[Test]
public static void TestCacheMiss()
{
var cache = new LruCache<int, string>();
cache.Put(1, "one");

cache.Contains(5).Should().BeFalse();
cache.Get(5).Should().BeNull();
}
cache.Contains(5).Should().BeFalse();
cache.Get(5).Should().BeNull();
}

[Test]
public static void TestCacheUpdate()
{
var cache = new LruCache<int, string>();
cache.Put(1, "one");
cache.Put(1, "ONE");
[Test]
public static void TestCacheUpdate()
{
var cache = new LruCache<int, string>();
cache.Put(1, "one");
cache.Put(1, "ONE");

cache.Get(1).Should().Be("ONE");
}
cache.Get(1).Should().Be("ONE");
}

[Test]
public static void RemoveOldestItem_ItemWasNotUsed()
{
var cache = new LruCache<int, string>(capacity: 2);
cache.Put(1, "one");
cache.Put(2, "two");
[Test]
public static void RemoveOldestItem_ItemWasNotUsed()
{
var cache = new LruCache<int, string>(capacity: 2);
cache.Put(1, "one");
cache.Put(2, "two");

// Add to the full cache, 1 will be removed
cache.Put(3, "three");
// Add to the full cache, 1 will be removed
cache.Put(3, "three");

cache.Get(1).Should().BeNull();
cache.Get(2).Should().Be("two");
cache.Get(3).Should().Be("three");
}
cache.Get(1).Should().BeNull();
cache.Get(2).Should().Be("two");
cache.Get(3).Should().Be("three");
}

[Test]
public static void RemoveOldestItem_ItemWasRecentlyUsed()
{
var cache = new LruCache<int, string>(capacity: 2);
cache.Put(1, "one");
cache.Put(2, "two");
cache.Get(1);
[Test]
public static void RemoveOldestItem_ItemWasRecentlyUsed()
{
var cache = new LruCache<int, string>(capacity: 2);
cache.Put(1, "one");
cache.Put(2, "two");
cache.Get(1);

// Add to the full cache, 1 was used, 2 should be removed
cache.Put(3, "three");
// Add to the full cache, 1 was used, 2 should be removed
cache.Put(3, "three");

cache.Get(1).Should().Be("one");
cache.Get(2).Should().BeNull();
cache.Get(3).Should().Be("three");
}
cache.Get(1).Should().Be("one");
cache.Get(2).Should().BeNull();
cache.Get(3).Should().Be("three");
}
}
}
51 changes: 25 additions & 26 deletions DataStructures.Tests/DisjointSet/DisjointSetTests.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
using DataStructures.DisjointSet;
using DataStructures.DisjointSet;
using FluentAssertions;
using NUnit.Framework;

namespace DataStructures.Tests.DisjointSet
namespace DataStructures.Tests.DisjointSet;

[TestFixture]
public class DisjointSetTests
{
[TestFixture]
public class DisjointSetTests
[Test]
public static void MakeSetDataInitializationTest()
{
DisjointSet<int> ds = new();
var one = ds.MakeSet(1);
var two = ds.MakeSet(2);
one.Data.Should().Be(1);
two.Data.Should().Be(2);
}
[Test]
public static void UnionTest()
{
[Test]
public static void MakeSetDataInitializationTest()
{
DisjointSet<int> ds = new();
var one = ds.MakeSet(1);
var two = ds.MakeSet(2);
one.Data.Should().Be(1);
two.Data.Should().Be(2);
}
[Test]
public static void UnionTest()
{
DisjointSet<int> ds = new();
var one = ds.MakeSet(1);
var two = ds.MakeSet(2);
var three = ds.MakeSet(3);
ds.UnionSet(one, two);
ds.FindSet(one).Should().Be(ds.FindSet(two));
ds.UnionSet(one, three);
ds.FindSet(two).Should().Be(ds.FindSet(three));
(one.Rank + two.Rank + three.Rank).Should().Be(1);
}
DisjointSet<int> ds = new();
var one = ds.MakeSet(1);
var two = ds.MakeSet(2);
var three = ds.MakeSet(3);
ds.UnionSet(one, two);
ds.FindSet(one).Should().Be(ds.FindSet(two));
ds.UnionSet(one, three);
ds.FindSet(two).Should().Be(ds.FindSet(three));
(one.Rank + two.Rank + three.Rank).Should().Be(1);
}
}
48 changes: 23 additions & 25 deletions DataStructures.Tests/Fenwick/BinaryIndexedTreeTests.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,35 @@
using DataStructures.Fenwick;
using NUnit.Framework;
using FluentAssertions;
using System;
using NUnit.Framework;

namespace DataStructures.Tests.Fenwick;

namespace DataStructures.Tests.Fenwick
[TestFixture]
internal class BinaryIndexedTreeTests
{
[TestFixture]
internal class BinaryIndexedTreeTests
[Test]
public void GetSum_CreateBITAndRequestSum_ReturnCorrect()
{
[Test]
public void GetSum_CreateBITAndRequestSum_ReturnCorrect()
{
int[] array = { 2, 1, 1, 3, 2, 3, 4, 5, 6, 7, 8, 9 };
var tree = new BinaryIndexedTree(array);
var expectedSum = 12;
int[] array = { 2, 1, 1, 3, 2, 3, 4, 5, 6, 7, 8, 9 };
var tree = new BinaryIndexedTree(array);
var expectedSum = 12;

var resultedSum = tree.GetSum(5);
var resultedSum = tree.GetSum(5);

resultedSum.Should().Be(expectedSum);
}
resultedSum.Should().Be(expectedSum);
}

[Test]
public void UpdateTree_UpdateTreeAndRequestSum_GetSum()
{
int[] array = { 2, 1, 1, 3, 2, 3, 4, 5, 6, 7, 8, 9 };
var tree = new BinaryIndexedTree(array);
var expectedSum = 18;
[Test]
public void UpdateTree_UpdateTreeAndRequestSum_GetSum()
{
int[] array = { 2, 1, 1, 3, 2, 3, 4, 5, 6, 7, 8, 9 };
var tree = new BinaryIndexedTree(array);
var expectedSum = 18;

array[3] += 6;
tree.UpdateTree(3, 6);
array[3] += 6;
tree.UpdateTree(3, 6);

var resultedSum = tree.GetSum(5);
resultedSum.Should().Be(expectedSum);
}
var resultedSum = tree.GetSum(5);
resultedSum.Should().Be(expectedSum);
}
}
Loading