Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
41 changes: 41 additions & 0 deletions .github/workflows/unit-testing.yml
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 }}
Comment thread
zbalkan marked this conversation as resolved.
Outdated
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
# TechnitiumLibrary
A library for .net based applications.

## Quality Assurance

[![Unit testing (Windows / MSBuild)](https://github.com/TechnitiumSoftware/TechnitiumLibrary/actions/workflows/unit-testing.yml/badge.svg)](https://github.com/TechnitiumSoftware/TechnitiumLibrary/actions/workflows/unit-testing.yml)
3 changes: 3 additions & 0 deletions TechnitiumLibrary.UnitTests/MSTestSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

[assembly: Parallelize(Scope = ExecutionScope.MethodLevel)]
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
Comment thread
zbalkan marked this conversation as resolved.
Assert.ThrowsExactly<ArgumentException>(() =>
Comment thread
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"));
Comment thread
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"));
Comment thread
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 _));
Comment thread
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));
Comment thread
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 _));
Comment thread
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)]);
}
Comment thread
zbalkan marked this conversation as resolved.
Comment thread
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);
}
Comment thread
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>(() =>
Comment thread
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");
Comment thread
zbalkan marked this conversation as resolved.
}
Comment thread
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);
Comment thread
zbalkan marked this conversation as resolved.
Comment thread
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);
Comment thread
zbalkan marked this conversation as resolved.
Comment thread
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)));
}
}
}
Comment thread
zbalkan marked this conversation as resolved.
15 changes: 15 additions & 0 deletions TechnitiumLibrary.UnitTests/TechnitiumLibrary.UnitTests.csproj
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>
Loading
Loading