-
Notifications
You must be signed in to change notification settings - Fork 62
Added unit tests to TechnitiumLibrary.ByteTree #31
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
Open
zbalkan
wants to merge
8
commits into
TechnitiumSoftware:master
Choose a base branch
from
zbalkan:feat/unit-test/technitiumlibrary.bytetree
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c96266b
Created ByteTree tests
zbalkan 831618d
Added AddOrUpdate_ShouldThrow_WhenNullKey
zbalkan d06d78d
Added TryUpdate_ShouldThrow_WhenNullKey
zbalkan a6866d6
Fixed workflow path
zbalkan a6a0f2f
Added TryUpdate_ShouldReturnFalse_WhenKeyMissing
zbalkan 63238cf
Added GetOrAdd test cases
zbalkan 8588ab8
Updated workflow file
zbalkan 1b7c5ce
Added copyright
zbalkan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| name: Unit testing (Windows / MSBuild) | ||
|
|
||
| on: | ||
| workflow_dispatch: | ||
| push: | ||
| branches: ["master"] | ||
| pull_request: | ||
| branches: ["master"] | ||
| schedule: | ||
| - cron: "0 0 * * 0" # weekly, Sunday 00:00 UTC | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| test: | ||
| runs-on: windows-latest | ||
|
|
||
| env: | ||
| SOLUTION_NAME: TechnitiumLibrary.sln | ||
| BUILD_CONFIGURATION: Debug | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@v4 | ||
|
|
||
| - name: Install .NET 9 SDK | ||
| uses: actions/setup-dotnet@v4 | ||
| with: | ||
| dotnet-version: 9.0.x | ||
|
|
||
| - name: Add MSBuild to PATH | ||
| uses: microsoft/setup-msbuild@v1 | ||
|
|
||
| - name: Restore | ||
| run: msbuild ${{ env.SOLUTION_NAME }} /t:Restore | ||
|
|
||
| - name: Build | ||
| run: msbuild ${{ env.SOLUTION_NAME }} /m /p:Configuration=${{ env.BUILD_CONFIGURATION }} | ||
|
|
||
| - name: Test (msbuild) | ||
| run: msbuild TechnitiumLibrary.Tests\TechnitiumLibrary.Tests.csproj /t:Test /p:Configuration=${{ env.BUILD_CONFIGURATION }} | ||
This file contains hidden or 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,2 +1,6 @@ | ||
| # TechnitiumLibrary | ||
| A library for .net based applications. | ||
|
|
||
| ## Quality Assurance | ||
|
|
||
| [](https://github.com/TechnitiumSoftware/TechnitiumLibrary/actions/workflows/unit-testing.yml) |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| [assembly: Parallelize(Scope = ExecutionScope.MethodLevel)] |
315 changes: 315 additions & 0 deletions
315
TechnitiumLibrary.UnitTests/TechnitiumLibrary.ByteTree/ByteTreeTests.cs
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,315 @@ | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using TechnitiumLibrary.ByteTree; | ||
|
|
||
| namespace TechnitiumLibrary.UnitTests.TechnitiumLibrary.ByteTree | ||
| { | ||
| [TestClass] | ||
| public sealed class ByteTreeTests | ||
| { | ||
| private static byte[] Key(params byte[] b) => b; | ||
|
|
||
| // --------------------------- | ||
| // ADD + GET | ||
| // --------------------------- | ||
| [TestMethod] | ||
| public void Add_ShouldInsertValue_WhenKeyDoesNotExist() | ||
| { | ||
| // GIVEN | ||
| ByteTree<string> tree = new ByteTree<string>(); | ||
|
|
||
| // WHEN | ||
| tree.Add(Key(1, 2, 3), "value"); | ||
|
|
||
| // THEN | ||
| Assert.AreEqual("value", tree[Key(1, 2, 3)]); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Add_ShouldThrow_WhenKeyExists() | ||
| { | ||
| // GIVEN | ||
| ByteTree<string> tree = new ByteTree<string>(); | ||
| tree.Add(Key(4), "first"); | ||
|
|
||
| // WHEN – THEN | ||
|
zbalkan marked this conversation as resolved.
|
||
| Assert.ThrowsExactly<ArgumentException>(() => | ||
|
zbalkan marked this conversation as resolved.
|
||
| tree.Add(Key(4), "duplicate")); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Add_ShouldThrow_WhenKeyNull() | ||
| { | ||
| ByteTree<string> tree = new ByteTree<string>(); | ||
| Assert.ThrowsExactly<ArgumentNullException>(() => tree.Add(null, "x")); | ||
|
zbalkan marked this conversation as resolved.
|
||
| } | ||
|
|
||
| // --------------------------- | ||
| // TryAdd | ||
| // --------------------------- | ||
| [TestMethod] | ||
| public void TryAdd_ShouldReturnTrue_WhenKeyAdded() | ||
| { | ||
| ByteTree<string> tree = new ByteTree<string>(); | ||
| bool result = tree.TryAdd(Key(1), "v"); | ||
| Assert.IsTrue(result); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void TryAdd_ShouldReturnFalse_WhenKeyExists() | ||
| { | ||
| ByteTree<string> tree = new ByteTree<string>(); | ||
| tree.Add(Key(5), "initial"); | ||
|
|
||
| bool result = tree.TryAdd(Key(5), "other"); | ||
|
|
||
| Assert.IsFalse(result); | ||
| Assert.AreEqual("initial", tree[Key(5)]); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void TryAdd_ShouldThrow_WhenKeyNull() | ||
| { | ||
| ByteTree<string> tree = new ByteTree<string>(); | ||
| Assert.ThrowsExactly<ArgumentNullException>(() => tree.TryAdd(null, "x")); | ||
|
zbalkan marked this conversation as resolved.
|
||
| } | ||
|
|
||
| // --------------------------- | ||
| // GET operations | ||
| // --------------------------- | ||
| [TestMethod] | ||
| public void TryGet_ShouldReturnTrue_WhenKeyExists() | ||
| { | ||
| ByteTree<string> tree = new ByteTree<string>(); | ||
| tree.Add(Key(1, 2), "data"); | ||
|
|
||
| bool found = tree.TryGet(Key(1, 2), out string? value); | ||
|
|
||
| Assert.IsTrue(found); | ||
| Assert.AreEqual("data", value); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void TryGet_ShouldReturnFalse_WhenMissing() | ||
| { | ||
| ByteTree<string> tree = new ByteTree<string>(); | ||
|
|
||
| bool result = tree.TryGet(Key(9), out string? value); | ||
|
|
||
| Assert.IsFalse(result); | ||
| Assert.IsNull(value); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void TryGet_ShouldThrow_WhenNull() | ||
| { | ||
| ByteTree<string> tree = new ByteTree<string>(); | ||
| Assert.ThrowsExactly<ArgumentNullException>(() => tree.TryGet(null, out _)); | ||
|
zbalkan marked this conversation as resolved.
|
||
| } | ||
|
|
||
| // --------------------------- | ||
| // ContainsKey | ||
| // --------------------------- | ||
| [TestMethod] | ||
| public void ContainsKey_ShouldReturnTrue_WhenKeyPresent() | ||
| { | ||
| ByteTree<string> tree = new ByteTree<string>(); | ||
| tree.Add(Key(3, 3), "v"); | ||
|
|
||
| Assert.IsTrue(tree.ContainsKey(Key(3, 3))); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ContainsKey_ShouldReturnFalse_WhenKeyMissing() | ||
| { | ||
| ByteTree<string> tree = new ByteTree<string>(); | ||
| Assert.IsFalse(tree.ContainsKey(Key(3, 100))); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ContainsKey_ShouldThrow_WhenNull() | ||
| { | ||
| ByteTree<string> tree = new ByteTree<string>(); | ||
| Assert.ThrowsExactly<ArgumentNullException>(() => tree.ContainsKey(null)); | ||
|
zbalkan marked this conversation as resolved.
|
||
| } | ||
|
|
||
| // --------------------------- | ||
| // Remove | ||
| // --------------------------- | ||
| [TestMethod] | ||
| public void TryRemove_ShouldReturnTrue_WhenKeyExists() | ||
| { | ||
| ByteTree<string> tree = new ByteTree<string>(); | ||
| tree.Add(Key(10), "v"); | ||
|
|
||
| bool result = tree.TryRemove(Key(10), out string? removed); | ||
|
|
||
| Assert.IsTrue(result); | ||
| Assert.AreEqual("v", removed); | ||
| Assert.IsFalse(tree.ContainsKey(Key(10))); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void TryRemove_ShouldReturnFalse_WhenMissing() | ||
| { | ||
| ByteTree<string> tree = new ByteTree<string>(); | ||
| bool result = tree.TryRemove(Key(11), out string? removed); | ||
|
|
||
| Assert.IsFalse(result); | ||
| Assert.IsNull(removed); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void TryRemove_ShouldThrow_WhenNull() | ||
| { | ||
| ByteTree<string> tree = new ByteTree<string>(); | ||
| Assert.ThrowsExactly<ArgumentNullException>(() => tree.TryRemove(null, out _)); | ||
|
zbalkan marked this conversation as resolved.
|
||
| } | ||
|
|
||
| // --------------------------- | ||
| // TryUpdate | ||
| // --------------------------- | ||
| [TestMethod] | ||
| public void TryUpdate_ShouldReplaceValue_WhenComparisonMatches() | ||
| { | ||
| ByteTree<string> tree = new ByteTree<string>(); | ||
| tree.Add(Key(5), "old"); | ||
|
|
||
| bool updated = tree.TryUpdate(Key(5), "new", "old"); | ||
|
|
||
| Assert.IsTrue(updated); | ||
| Assert.AreEqual("new", tree[Key(5)]); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void TryUpdate_ShouldReturnFalse_WhenComparisonDoesNotMatch() | ||
| { | ||
| ByteTree<string> tree = new ByteTree<string>(); | ||
| tree.Add(Key(7), "original"); | ||
|
|
||
| bool updated = tree.TryUpdate(Key(7), "attempt", "different"); | ||
|
|
||
| Assert.IsFalse(updated); | ||
| Assert.AreEqual("original", tree[Key(7)]); | ||
| } | ||
|
zbalkan marked this conversation as resolved.
zbalkan marked this conversation as resolved.
|
||
|
|
||
| // --------------------------- | ||
| // AddOrUpdate | ||
| // --------------------------- | ||
| [TestMethod] | ||
| public void AddOrUpdate_ShouldInsert_WhenMissing() | ||
| { | ||
| ByteTree<string> tree = new ByteTree<string>(); | ||
|
|
||
| string val = tree.AddOrUpdate( | ||
| Key(1, 1), | ||
| _ => "create", | ||
| (_, old) => old + "update"); | ||
|
|
||
| Assert.AreEqual("create", val); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void AddOrUpdate_ShouldModify_WhenExists() | ||
| { | ||
| ByteTree<string> tree = new ByteTree<string>(); | ||
| tree.Add(Key(1, 2), "first"); | ||
|
|
||
| string updated = tree.AddOrUpdate( | ||
| Key(1, 2), | ||
| _ => "ignored", | ||
| (_, old) => old + "_changed"); | ||
|
|
||
| Assert.AreEqual("first_changed", updated); | ||
| } | ||
|
zbalkan marked this conversation as resolved.
|
||
|
|
||
| // --------------------------- | ||
| // Indexer get/set | ||
| // --------------------------- | ||
| [TestMethod] | ||
| public void Indexer_Get_ShouldReturnExactValue() | ||
| { | ||
| ByteTree<string> tree = new ByteTree<string>(); | ||
| tree.Add(Key(99), "stored"); | ||
|
|
||
| Assert.AreEqual("stored", tree[Key(99)]); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Indexer_Set_ShouldOverwriteFormerValue() | ||
| { | ||
| ByteTree<string> tree = new ByteTree<string>(); | ||
| tree[Key(5, 5)] = "initial"; | ||
|
|
||
| tree[Key(5, 5)] = "updated"; | ||
|
|
||
| Assert.AreEqual("updated", tree[Key(5, 5)]); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Indexer_Get_ShouldThrow_WhenMissingKey() | ||
| { | ||
| ByteTree<string> tree = new ByteTree<string>(); | ||
| Assert.ThrowsExactly<KeyNotFoundException>(() => | ||
|
zbalkan marked this conversation as resolved.
|
||
| _ = tree[Key(8, 8)]); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Indexer_ShouldThrow_WhenNullKey() | ||
| { | ||
| ByteTree<string> tree = new ByteTree<string>(); | ||
| Assert.ThrowsExactly<ArgumentNullException>(() => tree[null] = "x"); | ||
|
zbalkan marked this conversation as resolved.
|
||
| } | ||
|
zbalkan marked this conversation as resolved.
|
||
|
|
||
| // --------------------------- | ||
| // Enumeration | ||
| // --------------------------- | ||
| [TestMethod] | ||
| public void Enumerator_ShouldYieldExistingValues() | ||
| { | ||
| ByteTree<string> tree = new ByteTree<string>(); | ||
| tree.Add(Key(1), "x"); | ||
| tree.Add(Key(2), "y"); | ||
| tree.Add(Key(3), "z"); | ||
|
|
||
| List<string> values = tree.ToList(); | ||
|
|
||
| Assert.HasCount(3, values); | ||
|
zbalkan marked this conversation as resolved.
zbalkan marked this conversation as resolved.
|
||
| CollectionAssert.AreEquivalent(new[] { "x", "y", "z" }, values); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ReverseEnumerable_ShouldYieldInReverseOrder() | ||
| { | ||
| ByteTree<string> tree = new ByteTree<string>(); | ||
| tree.Add(Key(0), "a"); | ||
| tree.Add(Key(1), "b"); | ||
| tree.Add(Key(255), "c"); | ||
|
|
||
| List<string> result = tree.GetReverseEnumerable().ToList(); | ||
|
|
||
| Assert.HasCount(3, result); | ||
|
zbalkan marked this conversation as resolved.
zbalkan marked this conversation as resolved.
|
||
| Assert.AreEqual("c", result[0]); // last sorted key | ||
| Assert.AreEqual("b", result[1]); | ||
| Assert.AreEqual("a", result[2]); | ||
| } | ||
|
|
||
| // --------------------------- | ||
| // Clear | ||
| // --------------------------- | ||
| [TestMethod] | ||
| public void Clear_ShouldEraseAllData() | ||
| { | ||
| ByteTree<string> tree = new ByteTree<string>(); | ||
| tree.Add(Key(1), "x"); | ||
| tree.Add(Key(2), "y"); | ||
|
|
||
| tree.Clear(); | ||
|
|
||
| Assert.IsTrue(tree.IsEmpty); | ||
| Assert.IsFalse(tree.ContainsKey(Key(1))); | ||
| } | ||
| } | ||
| } | ||
|
zbalkan marked this conversation as resolved.
|
||
15 changes: 15 additions & 0 deletions
15
TechnitiumLibrary.UnitTests/TechnitiumLibrary.UnitTests.csproj
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <Project Sdk="MSTest.Sdk/4.0.1"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net9.0</TargetFramework> | ||
| <LangVersion>latest</LangVersion> | ||
| <ImplicitUsings>disable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| <UseVSTest>true</UseVSTest> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\TechnitiumLibrary.ByteTree\TechnitiumLibrary.ByteTree.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.