forked from TheAlgorithms/C-Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to file-scoped namespaces (TheAlgorithms#436)
- Loading branch information
Showing
41 changed files
with
5,716 additions
and
5,763 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.