-
Notifications
You must be signed in to change notification settings - Fork 60
Added unit tests to TechnitiumLibrary.IO #32
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.io
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
e7ca89c
Added unit tests
zbalkan 02f825d
Fixed workflow
zbalkan 3c6a6f6
Updated workflow file
zbalkan a0a256c
Formatting
zbalkan e1b6f64
Removed unnecessary helpers
zbalkan ee59f56
Removed unnecessary helper
zbalkan 06ac8e1
Removed unnecessary helpers
zbalkan 0721a67
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
Some comments aren't visible on the classic Files Changed page.
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)] |
146 changes: 146 additions & 0 deletions
146
TechnitiumLibrary.UnitTests/TechnitiumLibrary.IO/BinaryReaderExtensionsTests.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,146 @@ | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using System; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using TechnitiumLibrary.IO; | ||
|
|
||
| namespace TechnitiumLibrary.UnitTests.TechnitiumLibrary.IO | ||
| { | ||
| [TestClass] | ||
| public sealed class BinaryReaderExtensionsTests | ||
| { | ||
| private static BinaryReader ReaderOf(params byte[] bytes) | ||
| { | ||
| return new BinaryReader(new MemoryStream(bytes)); | ||
| } | ||
|
|
||
| // ----------------------------------------------- | ||
| // ReadLength() | ||
| // ----------------------------------------------- | ||
|
|
||
| [TestMethod] | ||
| public void ReadLength_ShouldReadSingleByteLengths() | ||
| { | ||
| // GIVEN | ||
| BinaryReader reader = ReaderOf(0x05); | ||
|
|
||
| // WHEN | ||
| int length = reader.ReadLength(); | ||
|
|
||
| // THEN | ||
| Assert.AreEqual(5, length); | ||
| Assert.AreEqual(1, reader.BaseStream.Position); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ReadLength_ShouldReadMultiByteBigEndianLengths() | ||
| { | ||
| // GIVEN | ||
| // 0x82 => 2-byte length follows → value = 0x01 0x2C → 300 decimal | ||
| BinaryReader reader = ReaderOf(0x82, 0x01, 0x2C); | ||
|
|
||
| // WHEN | ||
| int length = reader.ReadLength(); | ||
|
|
||
| // THEN | ||
| Assert.AreEqual(300, length); | ||
| Assert.AreEqual(3, reader.BaseStream.Position); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ReadLength_ShouldThrow_WhenLengthPrefixTooLarge() | ||
| { | ||
| // GIVEN | ||
| // lower 7 bits = 0x05, meaning "next 5 bytes", exceeding allowed 4 | ||
| BinaryReader reader = ReaderOf(0x85); | ||
|
|
||
| // WHEN-THEN | ||
| Assert.ThrowsExactly<IOException>(() => reader.ReadLength()); | ||
| } | ||
|
|
||
| // ----------------------------------------------- | ||
| // ReadBuffer() | ||
| // ----------------------------------------------- | ||
|
|
||
| [TestMethod] | ||
| public void ReadBuffer_ShouldReturnBytes_WhenLengthPrefixed() | ||
| { | ||
| // GIVEN | ||
| // length=3, then bytes 0xAA, 0xBB, 0xCC | ||
| BinaryReader reader = ReaderOf(0x03, 0xAA, 0xBB, 0xCC); | ||
|
|
||
| // WHEN | ||
| byte[] data = reader.ReadBuffer(); | ||
|
|
||
| // THEN | ||
| Assert.HasCount(3, data); | ||
| CollectionAssert.AreEqual(new byte[] { 0xAA, 0xBB, 0xCC }, data); | ||
| } | ||
|
|
||
| // ----------------------------------------------- | ||
| // ReadShortString() | ||
| // ----------------------------------------------- | ||
|
|
||
| [TestMethod] | ||
| public void ReadShortString_ShouldDecodeUtf8StringCorrectly() | ||
| { | ||
| // GIVEN | ||
| string text = "Hello"; | ||
| byte[] encoded = Encoding.UTF8.GetBytes(text); | ||
|
|
||
| byte[] bytes = new byte[] { (byte)encoded.Length }.Concat(encoded).ToArray(); | ||
| BinaryReader reader = ReaderOf(bytes); | ||
|
|
||
| // WHEN | ||
| string result = reader.ReadShortString(); | ||
|
|
||
| // THEN | ||
| Assert.AreEqual(text, result); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ReadShortString_ShouldUseSpecifiedEncoding() | ||
| { | ||
| // GIVEN | ||
| string text = "Å"; | ||
| Encoding encoding = Encoding.UTF32; | ||
| byte[] encoded = encoding.GetBytes(text); | ||
|
|
||
| byte[] bytes = new byte[] { (byte)encoded.Length }.Concat(encoded).ToArray(); | ||
| BinaryReader reader = ReaderOf(bytes); | ||
|
|
||
| // WHEN | ||
| string result = reader.ReadShortString(encoding); | ||
|
|
||
| // THEN | ||
| Assert.AreEqual(text, result); | ||
| } | ||
|
|
||
| // ----------------------------------------------- | ||
| // ReadDateTime() | ||
| // ----------------------------------------------- | ||
|
|
||
| [TestMethod] | ||
| public void ReadDateTime_ShouldConvertEpochMilliseconds() | ||
| { | ||
| // GIVEN | ||
| DateTime expected = new DateTime(2024, 01, 01, 12, 00, 00, DateTimeKind.Utc); | ||
| long millis = (long)(expected - DateTime.UnixEpoch).TotalMilliseconds; | ||
|
|
||
| byte[] encoded = BitConverter.GetBytes(millis); | ||
|
|
||
| // Normalize to little-endian, which BinaryReader expects | ||
| if (!BitConverter.IsLittleEndian) | ||
| Array.Reverse(encoded); | ||
|
|
||
| BinaryReader reader = ReaderOf(encoded); | ||
|
|
||
| // WHEN | ||
| DateTime result = reader.ReadDateTime(); | ||
|
|
||
| // THEN | ||
| Assert.AreEqual(expected, result); | ||
| } | ||
| } | ||
| } |
174 changes: 174 additions & 0 deletions
174
TechnitiumLibrary.UnitTests/TechnitiumLibrary.IO/BinaryWriterExtensionsTests.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,174 @@ | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using System; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using TechnitiumLibrary.IO; | ||
|
|
||
| namespace TechnitiumLibrary.UnitTests.TechnitiumLibrary.IO | ||
| { | ||
| [TestClass] | ||
| public sealed class BinaryWriterExtensionsTests | ||
| { | ||
| private static (BinaryWriter writer, MemoryStream stream) CreateWriter() | ||
| { | ||
| MemoryStream ms = new MemoryStream(); | ||
|
zbalkan marked this conversation as resolved.
Outdated
|
||
| BinaryWriter bw = new BinaryWriter(ms); | ||
|
zbalkan marked this conversation as resolved.
Outdated
|
||
| return (bw, ms); | ||
| } | ||
|
|
||
| private static byte[] WrittenBytes(MemoryStream ms) => | ||
| ms.ToArray(); | ||
|
|
||
| // --------------------------------------- | ||
| // WriteLength() tests | ||
| // --------------------------------------- | ||
|
|
||
| [TestMethod] | ||
| public void WriteLength_ShouldEncodeSingleByte_WhenLessThan128() | ||
| { | ||
| // GIVEN | ||
| (BinaryWriter bw, MemoryStream ms) = CreateWriter(); | ||
|
|
||
| // WHEN | ||
| bw.WriteLength(42); | ||
|
|
||
| // THEN | ||
| CollectionAssert.AreEqual(new byte[] { 42 }, WrittenBytes(ms)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void WriteLength_ShouldEncodeMultiByte_BigEndianForm() | ||
| { | ||
| // GIVEN | ||
| (BinaryWriter bw, MemoryStream ms) = CreateWriter(); | ||
|
|
||
| // WHEN | ||
| // length = 0x0000012C (300 decimal) | ||
| bw.WriteLength(300); | ||
|
|
||
| // THEN | ||
| // Prefix = 0x82 (2 bytes follow) | ||
| // Then big-endian 01 2C | ||
| CollectionAssert.AreEqual( | ||
| new byte[] { 0x82, 0x01, 0x2C }, | ||
| WrittenBytes(ms) | ||
| ); | ||
| } | ||
|
|
||
| // --------------------------------------- | ||
| // WriteBuffer() | ||
| // --------------------------------------- | ||
|
|
||
| [TestMethod] | ||
| public void WriteBuffer_ShouldPrefixLength_AndWriteBytes() | ||
| { | ||
| // GIVEN | ||
| (BinaryWriter bw, MemoryStream ms) = CreateWriter(); | ||
| byte[] data = new byte[] { 0xAA, 0xBB, 0xCC }; | ||
|
|
||
| // WHEN | ||
| bw.WriteBuffer(data); | ||
|
|
||
| // THEN | ||
| CollectionAssert.AreEqual( | ||
| new byte[] { 0x03, 0xAA, 0xBB, 0xCC }, | ||
| WrittenBytes(ms) | ||
| ); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void WriteBuffer_WithOffset_ShouldWriteExpectedSegment() | ||
| { | ||
| // GIVEN | ||
| (BinaryWriter bw, MemoryStream ms) = CreateWriter(); | ||
| byte[] data = new byte[] { 1, 2, 3, 4, 5 }; | ||
|
|
||
| // WHEN | ||
| bw.WriteBuffer(data, offset: 1, count: 3); | ||
|
|
||
| // THEN | ||
| CollectionAssert.AreEqual( | ||
| new byte[] { 0x03, 2, 3, 4 }, | ||
| WrittenBytes(ms) | ||
| ); | ||
| } | ||
|
|
||
| // --------------------------------------- | ||
| // WriteShortString() | ||
| // --------------------------------------- | ||
|
|
||
| [TestMethod] | ||
| public void WriteShortString_ShouldWriteUtf8EncodedWithLength() | ||
| { | ||
| // GIVEN | ||
| (BinaryWriter bw, MemoryStream ms) = CreateWriter(); | ||
| string text = "Hello"; | ||
| byte[] utf8 = Encoding.UTF8.GetBytes(text); | ||
|
|
||
| // WHEN | ||
| bw.WriteShortString(text); | ||
|
|
||
| // THEN | ||
| byte[] expected = new byte[] { (byte)utf8.Length } | ||
| .Concat(utf8) | ||
| .ToArray(); | ||
|
|
||
| CollectionAssert.AreEqual(expected, WrittenBytes(ms)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void WriteShortString_ShouldUseSpecifiedEncoding() | ||
| { | ||
| // GIVEN | ||
| (BinaryWriter bw, MemoryStream ms) = CreateWriter(); | ||
| string text = "Å"; | ||
| Encoding enc = Encoding.UTF32; | ||
| byte[] bytes = enc.GetBytes(text); | ||
|
|
||
| // WHEN | ||
| bw.WriteShortString(text, enc); | ||
|
|
||
| // THEN | ||
| byte[] expected = new byte[] { (byte)bytes.Length } | ||
| .Concat(bytes) | ||
| .ToArray(); | ||
|
|
||
| CollectionAssert.AreEqual(expected, WrittenBytes(ms)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void WriteShortString_ShouldThrow_WhenStringTooLong() | ||
| { | ||
| // GIVEN | ||
| (BinaryWriter bw, MemoryStream _) = CreateWriter(); | ||
| string input = new string('x', 256); // UTF-8 => 256 bytes | ||
|
|
||
| // WHEN–THEN | ||
| Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => | ||
| bw.WriteShortString(input) | ||
| ); | ||
| } | ||
|
|
||
| // --------------------------------------- | ||
| // Write(DateTime) | ||
| // --------------------------------------- | ||
|
|
||
| [TestMethod] | ||
| public void WriteDate_ShouldEncodeMillisecondsFromUnixEpoch() | ||
| { | ||
| // GIVEN | ||
| DateTime expected = new DateTime(2024, 1, 2, 12, 00, 00, DateTimeKind.Utc); | ||
| long millis = (long)(expected - DateTime.UnixEpoch).TotalMilliseconds; | ||
|
|
||
| byte[] bytes = BitConverter.GetBytes(millis); | ||
| (BinaryWriter bw, MemoryStream ms) = CreateWriter(); | ||
|
|
||
| // WHEN | ||
| bw.Write(expected); | ||
|
|
||
| // THEN | ||
| CollectionAssert.AreEqual(bytes, WrittenBytes(ms)); | ||
| } | ||
| } | ||
| } | ||
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.