aboutsummaryrefslogtreecommitdiff
path: root/Utils/tests
diff options
context:
space:
mode:
Diffstat (limited to 'Utils/tests')
-rw-r--r--Utils/tests/ERRNOTest.cs48
-rw-r--r--Utils/tests/Memory/MemoryHandleTest.cs183
-rw-r--r--Utils/tests/Memory/MemoryTests.cs244
-rw-r--r--Utils/tests/Memory/VnTableTests.cs168
-rw-r--r--Utils/tests/README.md0
-rw-r--r--Utils/tests/VNLib.UtilsTests.csproj36
-rw-r--r--Utils/tests/VnEncodingTests.cs100
7 files changed, 779 insertions, 0 deletions
diff --git a/Utils/tests/ERRNOTest.cs b/Utils/tests/ERRNOTest.cs
new file mode 100644
index 0000000..bd14b50
--- /dev/null
+++ b/Utils/tests/ERRNOTest.cs
@@ -0,0 +1,48 @@
+/*
+* Copyright (c) 2022 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.UtilsTests
+* File: ERRNOTest.cs
+*
+* ERRNOTest.cs is part of VNLib.UtilsTests which is part of the larger
+* VNLib collection of libraries and utilities.
+*
+* VNLib.UtilsTests is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published
+* by the Free Software Foundation, either version 2 of the License,
+* or (at your option) any later version.
+*
+* VNLib.UtilsTests is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with VNLib.UtilsTests. If not, see http://www.gnu.org/licenses/.
+*/
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+using VNLib.Utils;
+
+namespace VNLib.Utils.Tests
+{
+ [TestClass]
+ public class ERRNOTest
+ {
+
+ [TestMethod]
+ public unsafe void ERRNOSizeTest()
+ {
+ Assert.IsTrue(sizeof(ERRNO) == sizeof(nint));
+ }
+
+ }
+}
diff --git a/Utils/tests/Memory/MemoryHandleTest.cs b/Utils/tests/Memory/MemoryHandleTest.cs
new file mode 100644
index 0000000..02ef1f1
--- /dev/null
+++ b/Utils/tests/Memory/MemoryHandleTest.cs
@@ -0,0 +1,183 @@
+/*
+* Copyright (c) 2022 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.UtilsTests
+* File: MemoryHandleTest.cs
+*
+* MemoryHandleTest.cs is part of VNLib.UtilsTests which is part of the larger
+* VNLib collection of libraries and utilities.
+*
+* VNLib.UtilsTests is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published
+* by the Free Software Foundation, either version 2 of the License,
+* or (at your option) any later version.
+*
+* VNLib.UtilsTests is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with VNLib.UtilsTests. If not, see http://www.gnu.org/licenses/.
+*/
+
+
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+using VNLib.Utils;
+using VNLib.Utils.Extensions;
+
+using static VNLib.Utils.Memory.Memory;
+
+namespace VNLib.Utils.Memory.Tests
+{
+ [TestClass]
+ public class MemoryHandleTest
+ {
+
+ [TestMethod]
+ public void MemoryHandleAllocLongExtensionTest()
+ {
+ //Check for negatives
+ Assert.ThrowsException<ArgumentOutOfRangeException>(() => Shared.Alloc<byte>(-1));
+
+ //Make sure over-alloc throws
+ Assert.ThrowsException<NativeMemoryOutOfMemoryException>(() => Shared.Alloc<byte>(ulong.MaxValue, false));
+ }
+#if TARGET_64_BIT
+ [TestMethod]
+ public unsafe void MemoryHandleBigAllocTest()
+ {
+ const long bigHandleSize = (long)uint.MaxValue + 1024;
+
+ using MemoryHandle<byte> handle = Shared.Alloc<byte>(bigHandleSize);
+
+ //verify size
+ Assert.AreEqual(handle.ByteLength, (ulong)bigHandleSize);
+ //Since handle is byte, should also match
+ Assert.AreEqual(handle.Length, (ulong)bigHandleSize);
+
+ //Should throw overflow
+ Assert.ThrowsException<OverflowException>(() => _ = handle.Span);
+ Assert.ThrowsException<OverflowException>(() => _ = handle.IntLength);
+
+ //Should get the remaining span
+ Span<byte> offsetTest = handle.GetOffsetSpan(int.MaxValue, 1024);
+
+ Assert.ThrowsException<ArgumentOutOfRangeException>(() => _ = handle.GetOffsetSpan((long)int.MaxValue + 1, 1024));
+
+ }
+#else
+
+#endif
+
+ [TestMethod]
+ public unsafe void BasicMemoryHandleTest()
+ {
+ using MemoryHandle<byte> handle = Shared.Alloc<byte>(128, true);
+
+ Assert.AreEqual(handle.IntLength, 128);
+
+ Assert.AreEqual(handle.Length, (ulong)128);
+
+ //Check span against base pointer deref
+
+ handle.Span[120] = 10;
+
+ Assert.AreEqual(*handle.GetOffset(120), 10);
+ }
+
+
+ [TestMethod]
+ public unsafe void MemoryHandleDisposedTest()
+ {
+ using MemoryHandle<byte> handle = Shared.Alloc<byte>(1024);
+
+ //Make sure handle is not invalid until disposed
+ Assert.IsFalse(handle.IsInvalid);
+ Assert.IsFalse(handle.IsClosed);
+ Assert.AreNotEqual(IntPtr.Zero, handle.BasePtr);
+
+ //Dispose the handle early and test
+ handle.Dispose();
+
+ Assert.IsTrue(handle.IsInvalid);
+ Assert.IsTrue(handle.IsClosed);
+
+ Assert.ThrowsException<ObjectDisposedException>(() => _ = handle.Span);
+ Assert.ThrowsException<ObjectDisposedException>(() => _ = handle.BasePtr);
+ Assert.ThrowsException<ObjectDisposedException>(() => _ = handle.Base);
+ Assert.ThrowsException<ObjectDisposedException>(() => handle.Resize(10));
+ Assert.ThrowsException<ObjectDisposedException>(() => _ = handle.GetOffset(10));
+ Assert.ThrowsException<ObjectDisposedException>(() => handle.ThrowIfClosed());
+ }
+
+ [TestMethod]
+ public unsafe void MemoryHandleCountDisposedTest()
+ {
+ using MemoryHandle<byte> handle = Shared.Alloc<byte>(1024);
+
+ //Make sure handle is not invalid until disposed
+ Assert.IsFalse(handle.IsInvalid);
+ Assert.IsFalse(handle.IsClosed);
+ Assert.AreNotEqual(IntPtr.Zero, handle.BasePtr);
+
+ bool test = false;
+ //Increase handle counter
+ handle.DangerousAddRef(ref test);
+ Assert.IsTrue(test);
+
+ //Dispose the handle early and test
+ handle.Dispose();
+
+ //Asser is valid still
+
+ //Make sure handle is not invalid until disposed
+ Assert.IsFalse(handle.IsInvalid);
+ Assert.IsFalse(handle.IsClosed);
+ Assert.AreNotEqual(IntPtr.Zero, handle.BasePtr);
+
+ //Dec handle count
+ handle.DangerousRelease();
+
+ //Now make sure the class is disposed
+
+ Assert.IsTrue(handle.IsInvalid);
+ Assert.IsTrue(handle.IsClosed);
+ Assert.ThrowsException<ObjectDisposedException>(() => _ = handle.Span);
+ }
+
+ [TestMethod]
+ public unsafe void MemoryHandleExtensionsTest()
+ {
+ using MemoryHandle<byte> handle = Shared.Alloc<byte>(1024);
+
+ Assert.AreEqual(handle.IntLength, 1024);
+
+ Assert.ThrowsException<ArgumentOutOfRangeException>(() => handle.Resize(-1));
+
+ //Resize the handle
+ handle.Resize(2048);
+
+ Assert.AreEqual(handle.IntLength, 2048);
+
+ Assert.IsTrue(handle.AsSpan(2048).IsEmpty);
+
+ Assert.ThrowsException<ArgumentOutOfRangeException>(() => _ = handle.AsSpan(2049));
+
+ Assert.ThrowsException<ArgumentOutOfRangeException>(() => _ = handle.GetOffset(2049));
+
+ Assert.ThrowsException<ArgumentOutOfRangeException>(() => _ = handle.GetOffset(-1));
+
+ //test resize
+ handle.ResizeIfSmaller(100);
+ //Handle should be unmodified
+ Assert.AreEqual(handle.IntLength, 2048);
+
+ //test working
+ handle.ResizeIfSmaller(4096);
+ Assert.AreEqual(handle.IntLength, 4096);
+ }
+ }
+}
diff --git a/Utils/tests/Memory/MemoryTests.cs b/Utils/tests/Memory/MemoryTests.cs
new file mode 100644
index 0000000..5b68cf5
--- /dev/null
+++ b/Utils/tests/Memory/MemoryTests.cs
@@ -0,0 +1,244 @@
+/*
+* Copyright (c) 2022 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.UtilsTests
+* File: MemoryTests.cs
+*
+* MemoryTests.cs is part of VNLib.UtilsTests which is part of the larger
+* VNLib collection of libraries and utilities.
+*
+* VNLib.UtilsTests is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published
+* by the Free Software Foundation, either version 2 of the License,
+* or (at your option) any later version.
+*
+* VNLib.UtilsTests is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with VNLib.UtilsTests. If not, see http://www.gnu.org/licenses/.
+*/
+
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using System.Runtime.InteropServices;
+
+using VNLib.Utils.Extensions;
+
+namespace VNLib.Utils.Memory.Tests
+{
+ [TestClass()]
+ public class MemoryTests
+ {
+ [TestMethod]
+ public void MemorySharedHeapLoadedTest()
+ {
+ Assert.IsNotNull(Memory.Shared);
+ }
+
+ [TestMethod()]
+ public void UnsafeAllocTest()
+ {
+ //test against negative number
+ Assert.ThrowsException<ArgumentException>(() => Memory.UnsafeAlloc<byte>(-1));
+
+ //Alloc large block test (100mb)
+ const int largTestSize = 100000 * 1024;
+ //Alloc super small block
+ const int smallTestSize = 5;
+
+ using (UnsafeMemoryHandle<byte> buffer = Memory.UnsafeAlloc<byte>(largTestSize, false))
+ {
+ Assert.AreEqual(largTestSize, buffer.IntLength);
+ Assert.AreEqual(largTestSize, buffer.Span.Length);
+
+ buffer.Span[0] = 254;
+ Assert.AreEqual(buffer.Span[0], 254);
+ }
+
+ using (UnsafeMemoryHandle<byte> buffer = Memory.UnsafeAlloc<byte>(smallTestSize, false))
+ {
+ Assert.AreEqual(smallTestSize, buffer.IntLength);
+ Assert.AreEqual(smallTestSize, buffer.Span.Length);
+
+ buffer.Span[0] = 254;
+ Assert.AreEqual(buffer.Span[0], 254);
+ }
+
+ //Different data type
+
+ using(UnsafeMemoryHandle<long> buffer = Memory.UnsafeAlloc<long>(largTestSize, false))
+ {
+ Assert.AreEqual(largTestSize, buffer.IntLength);
+ Assert.AreEqual(largTestSize, buffer.Span.Length);
+
+ buffer.Span[0] = long.MaxValue;
+ Assert.AreEqual(buffer.Span[0], long.MaxValue);
+ }
+
+ using (UnsafeMemoryHandle<long> buffer = Memory.UnsafeAlloc<long>(smallTestSize, false))
+ {
+ Assert.AreEqual(smallTestSize, buffer.IntLength);
+ Assert.AreEqual(smallTestSize, buffer.Span.Length);
+
+ buffer.Span[0] = long.MaxValue;
+ Assert.AreEqual(buffer.Span[0], long.MaxValue);
+ }
+ }
+
+ [TestMethod()]
+ public void UnsafeZeroMemoryAsSpanTest()
+ {
+ //Alloc test buffer
+ Span<byte> test = new byte[1024];
+ test.Fill(0);
+ //test other empty span
+ Span<byte> verify = new byte[1024];
+ verify.Fill(0);
+
+ //Fill test buffer with random values
+ Random.Shared.NextBytes(test);
+
+ //make sure buffers are not equal
+ Assert.IsFalse(test.SequenceEqual(verify));
+
+ //Zero buffer
+ Memory.UnsafeZeroMemory<byte>(test);
+
+ //Make sure buffers are equal
+ Assert.IsTrue(test.SequenceEqual(verify));
+ }
+
+ [TestMethod()]
+ public void UnsafeZeroMemoryAsMemoryTest()
+ {
+ //Alloc test buffer
+ Memory<byte> test = new byte[1024];
+ test.Span.Fill(0);
+ //test other empty span
+ Memory<byte> verify = new byte[1024];
+ verify.Span.Fill(0);
+
+ //Fill test buffer with random values
+ Random.Shared.NextBytes(test.Span);
+
+ //make sure buffers are not equal
+ Assert.IsFalse(test.Span.SequenceEqual(verify.Span));
+
+ //Zero buffer
+ Memory.UnsafeZeroMemory<byte>(test);
+
+ //Make sure buffers are equal
+ Assert.IsTrue(test.Span.SequenceEqual(verify.Span));
+ }
+
+ [TestMethod()]
+ public void InitializeBlockAsSpanTest()
+ {
+ //Alloc test buffer
+ Span<byte> test = new byte[1024];
+ test.Fill(0);
+ //test other empty span
+ Span<byte> verify = new byte[1024];
+ verify.Fill(0);
+
+ //Fill test buffer with random values
+ Random.Shared.NextBytes(test);
+
+ //make sure buffers are not equal
+ Assert.IsFalse(test.SequenceEqual(verify));
+
+ //Zero buffer
+ Memory.InitializeBlock(test);
+
+ //Make sure buffers are equal
+ Assert.IsTrue(test.SequenceEqual(verify));
+ }
+
+ [TestMethod()]
+ public void InitializeBlockMemoryTest()
+ {
+ //Alloc test buffer
+ Memory<byte> test = new byte[1024];
+ test.Span.Fill(0);
+ //test other empty span
+ Memory<byte> verify = new byte[1024];
+ verify.Span.Fill(0);
+
+ //Fill test buffer with random values
+ Random.Shared.NextBytes(test.Span);
+
+ //make sure buffers are not equal
+ Assert.IsFalse(test.Span.SequenceEqual(verify.Span));
+
+ //Zero buffer
+ Memory.InitializeBlock(test);
+
+ //Make sure buffers are equal
+ Assert.IsTrue(test.Span.SequenceEqual(verify.Span));
+ }
+
+ #region structmemory tests
+
+ [StructLayout(LayoutKind.Sequential)]
+ struct TestStruct
+ {
+ public int X;
+ public int Y;
+ }
+
+ [TestMethod()]
+ public unsafe void ZeroStructAsPointerTest()
+ {
+ TestStruct* s = Memory.Shared.StructAlloc<TestStruct>();
+ s->X = 10;
+ s->Y = 20;
+ Assert.AreEqual(10, s->X);
+ Assert.AreEqual(20, s->Y);
+ //zero struct
+ Memory.ZeroStruct(s);
+ //Verify data was zeroed
+ Assert.AreEqual(0, s->X);
+ Assert.AreEqual(0, s->Y);
+ //Free struct
+ Memory.Shared.StructFree(s);
+ }
+
+ [TestMethod()]
+ public unsafe void ZeroStructAsVoidPointerTest()
+ {
+ TestStruct* s = Memory.Shared.StructAlloc<TestStruct>();
+ s->X = 10;
+ s->Y = 20;
+ Assert.AreEqual(10, s->X);
+ Assert.AreEqual(20, s->Y);
+ //zero struct
+ Memory.ZeroStruct<TestStruct>((void*)s);
+ //Verify data was zeroed
+ Assert.AreEqual(0, s->X);
+ Assert.AreEqual(0, s->Y);
+ //Free struct
+ Memory.Shared.StructFree(s);
+ }
+
+ [TestMethod()]
+ public unsafe void ZeroStructAsIntPtrTest()
+ {
+ TestStruct* s = Memory.Shared.StructAlloc<TestStruct>();
+ s->X = 10;
+ s->Y = 20;
+ Assert.AreEqual(10, s->X);
+ Assert.AreEqual(20, s->Y);
+ //zero struct
+ Memory.ZeroStruct<TestStruct>((IntPtr)s);
+ //Verify data was zeroed
+ Assert.AreEqual(0, s->X);
+ Assert.AreEqual(0, s->Y);
+ //Free struct
+ Memory.Shared.StructFree(s);
+ }
+ #endregion
+ }
+} \ No newline at end of file
diff --git a/Utils/tests/Memory/VnTableTests.cs b/Utils/tests/Memory/VnTableTests.cs
new file mode 100644
index 0000000..11350d4
--- /dev/null
+++ b/Utils/tests/Memory/VnTableTests.cs
@@ -0,0 +1,168 @@
+/*
+* Copyright (c) 2022 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.UtilsTests
+* File: VnTableTests.cs
+*
+* VnTableTests.cs is part of VNLib.UtilsTests which is part of the larger
+* VNLib collection of libraries and utilities.
+*
+* VNLib.UtilsTests is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published
+* by the Free Software Foundation, either version 2 of the License,
+* or (at your option) any later version.
+*
+* VNLib.UtilsTests is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with VNLib.UtilsTests. If not, see http://www.gnu.org/licenses/.
+*/
+
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+
+namespace VNLib.Utils.Memory.Tests
+{
+ [TestClass()]
+ public class VnTableTests
+ {
+ [TestMethod()]
+ public void VnTableTest()
+ {
+ Assert.ThrowsException<ArgumentOutOfRangeException>(() =>
+ {
+ using VnTable<int> table = new(-1, 0);
+ });
+ Assert.ThrowsException<ArgumentOutOfRangeException>(() =>
+ {
+ using VnTable<int> table = new(0, -1);
+ });
+ Assert.ThrowsException<ArgumentOutOfRangeException>(() =>
+ {
+ using VnTable<int> table = new(-1, -1);
+ });
+
+ //Empty table
+ using (VnTable<int> empty = new(0, 0))
+ {
+ Assert.IsTrue(empty.Empty);
+ //Test 0 rows/cols
+ Assert.AreEqual(0, empty.Rows);
+ Assert.AreEqual(0, empty.Cols);
+ }
+
+ using (VnTable<int> table = new(40000, 10000))
+ {
+ Assert.IsFalse(table.Empty);
+
+ //Test table size
+ Assert.AreEqual(40000, table.Rows);
+ Assert.AreEqual(10000, table.Cols);
+ }
+
+
+ //Test oom, should be native
+ Assert.ThrowsException<NativeMemoryOutOfMemoryException>(() =>
+ {
+ using VnTable<int> table = new(int.MaxValue, 2);
+ });
+ }
+
+ [TestMethod()]
+ public void VnTableTest1()
+ {
+ //No throw if empty
+ using VnTable<int> table = new(null!,0, 0);
+
+ //Throw if table is not empty
+ Assert.ThrowsException<ArgumentNullException>(() =>
+ {
+ using VnTable<int> table = new(null!,1, 1);
+ });
+
+ }
+
+ [TestMethod()]
+ public void GetSetTest()
+ {
+ static void TestIndexAt(VnTable<int> table, int row, int col, int value)
+ {
+ table[row, col] = value;
+ Assert.AreEqual(value, table[row, col]);
+ Assert.AreEqual(value, table.Get(row, col));
+ }
+
+ static void TestSetAt(VnTable<int> table, int row, int col, int value)
+ {
+ table.Set(row, col, value);
+ Assert.AreEqual(value, table[row, col]);
+ Assert.AreEqual(value, table.Get(row, col));
+ }
+
+ static void TestSetDirectAccess(VnTable<int> table, int row, int col, int value)
+ {
+ int address = row * table.Cols + col;
+ table[(uint)address] = value;
+
+ //Get value using indexer
+ Assert.AreEqual(value, table[row, col]);
+ }
+
+ static void TestGetDirectAccess(VnTable<int> table, int row, int col, int value)
+ {
+ table[row, col] = value;
+
+ int address = row * table.Cols + col;
+
+ //Test direct access
+ Assert.AreEqual(value, table[(uint)address]);
+
+ //Get value using indexer
+ Assert.AreEqual(value, table[row, col]);
+ Assert.AreEqual(value, table.Get(row, col));
+ }
+
+
+ using (VnTable<int> table = new(11, 11))
+ {
+ //Test index at 10,10
+ TestIndexAt(table, 10, 10, 11);
+ //Test same index with different value using the .set() method
+ TestSetAt(table, 10, 10, 25);
+
+ //Test direct access
+ TestSetDirectAccess(table, 10, 10, 50);
+
+ TestGetDirectAccess(table, 10, 10, 37);
+
+ //Test index at 0,0
+ TestIndexAt(table, 0, 0, 13);
+ TestSetAt(table, 0, 0, 85);
+
+ //Test at 0,0
+ TestSetDirectAccess(table, 0, 0, 100);
+ TestGetDirectAccess(table, 0, 0, 86);
+ }
+ }
+
+ [TestMethod()]
+ public void DisposeTest()
+ {
+ //Alloc table
+ VnTable<int> table = new(10, 10);
+ //Dispose table
+ table.Dispose();
+
+ //Test that methods throw on access
+ Assert.ThrowsException<ObjectDisposedException>(() => table[10, 10] = 10);
+ Assert.ThrowsException<ObjectDisposedException>(() => table.Set(10, 10, 10));
+ Assert.ThrowsException<ObjectDisposedException>(() => table[10, 10] == 10);
+ Assert.ThrowsException<ObjectDisposedException>(() => table.Get(10, 10));
+ }
+
+ }
+} \ No newline at end of file
diff --git a/Utils/tests/README.md b/Utils/tests/README.md
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/Utils/tests/README.md
diff --git a/Utils/tests/VNLib.UtilsTests.csproj b/Utils/tests/VNLib.UtilsTests.csproj
new file mode 100644
index 0000000..89b3124
--- /dev/null
+++ b/Utils/tests/VNLib.UtilsTests.csproj
@@ -0,0 +1,36 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+ <PropertyGroup>
+ <TargetFramework>net6.0</TargetFramework>
+ <ImplicitUsings>enable</ImplicitUsings>
+ <Nullable>enable</Nullable>
+
+ <IsPackable>false</IsPackable>
+
+ <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
+
+ </PropertyGroup>
+
+ <ItemGroup>
+ <PackageReference Include="ErrorProne.NET.CoreAnalyzers" Version="0.1.2">
+ <PrivateAssets>all</PrivateAssets>
+ <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
+ </PackageReference>
+ <PackageReference Include="ErrorProne.NET.Structs" Version="0.1.2">
+ <PrivateAssets>all</PrivateAssets>
+ <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
+ </PackageReference>
+ <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
+ <PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
+ <PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
+ <PackageReference Include="coverlet.collector" Version="3.2.0">
+ <PrivateAssets>all</PrivateAssets>
+ <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
+ </PackageReference>
+ </ItemGroup>
+
+ <ItemGroup>
+ <ProjectReference Include="..\src\VNLib.Utils.csproj" />
+ </ItemGroup>
+
+</Project>
diff --git a/Utils/tests/VnEncodingTests.cs b/Utils/tests/VnEncodingTests.cs
new file mode 100644
index 0000000..a4e52f0
--- /dev/null
+++ b/Utils/tests/VnEncodingTests.cs
@@ -0,0 +1,100 @@
+/*
+* Copyright (c) 2022 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.UtilsTests
+* File: VnEncodingTests.cs
+*
+* VnEncodingTests.cs is part of VNLib.UtilsTests which is part of the larger
+* VNLib collection of libraries and utilities.
+*
+* VNLib.UtilsTests is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published
+* by the Free Software Foundation, either version 2 of the License,
+* or (at your option) any later version.
+*
+* VNLib.UtilsTests is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with VNLib.UtilsTests. If not, see http://www.gnu.org/licenses/.
+*/
+
+using System;
+using System.Buffers;
+using System.Buffers.Text;
+using System.Collections.Generic;
+using System.Linq;
+using System.Security.Cryptography;
+using System.Text;
+using System.Threading.Tasks;
+
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using VNLib.Utils;
+
+namespace VNLib.Utils.Tests
+{
+ [TestClass()]
+ public class VnEncodingTests
+ {
+
+ [TestMethod()]
+ public void Base64ToUrlSafeInPlaceTest()
+ {
+ //Get randomd data to encode
+ byte[] dataToEncode = RandomNumberGenerator.GetBytes(64);
+ //Calc buffer size
+ int base64Output = Base64.GetMaxEncodedToUtf8Length(64);
+
+ byte[] encodeBuffer = new byte[base64Output];
+ //Base64 encode
+ OperationStatus status = Base64.EncodeToUtf8(dataToEncode, encodeBuffer, out _, out int bytesEncoded, true);
+
+ Assert.IsTrue(status == OperationStatus.Done);
+
+ Span<byte> encodeSpan = encodeBuffer.AsSpan(0, bytesEncoded);
+
+ //Make sure some illegal characters are encoded
+ Assert.IsTrue(encodeSpan.Contains((byte)'+') || encodeSpan.Contains((byte)'/'));
+
+ //Convert to url safe
+ VnEncoding.Base64ToUrlSafeInPlace(encodeSpan);
+
+ //Make sure the illegal characters are gone
+ Assert.IsFalse(encodeSpan.Contains((byte)'+') || encodeSpan.Contains((byte)'/'));
+ }
+
+ [TestMethod()]
+ public void Base64FromUrlSafeInPlaceTest()
+ {
+ //url safe base64 with known encoded characters
+ const string base64UrlSafe = "lZUABUd8q2BS7p8giysuC7PpEabAFBnMqBPL-9A-qgfR1lbTHQ4tMm8E8nimm2YAd5NGDIQ0vxfU9i5l53tF_WXa_H4vkHfzlv0Df-lLADJV7z8sn-8sfUGdaAiIS8_4OmVGnnY4-TppLMsVR6ov2t07HdOHPPsFFhSpBMXa2pwRveRATcxBA2XxVe09FOWgahhssNS7lU9eC7fRw7icD4ZoJcLSRBbxrjRmeVXKhPIaXR-4mnQ5-vqYzAr9S99CthgbAtVn_WjmDcda6pUB9JW9lp7ylDa9e1r_z39cihTXMOGaUSjVURJaWrNF8CkfW56_x2ODCBmZPov1YyEhww==";
+
+ //Convert to utf8 binary
+ byte[] utf8 = Encoding.UTF8.GetBytes(base64UrlSafe);
+
+ //url decode
+ VnEncoding.Base64FromUrlSafeInPlace(utf8);
+
+ //Confirm illegal chars have been converted back to base64
+ Assert.IsFalse(utf8.Contains((byte)'_') || utf8.Contains((byte)'-'));
+
+ //Decode in place to confrim its valid
+ OperationStatus status = Base64.DecodeFromUtf8InPlace(utf8, out int bytesWritten);
+
+ Assert.IsFalse(status == OperationStatus.NeedMoreData);
+ Assert.IsFalse(status == OperationStatus.DestinationTooSmall);
+ Assert.IsFalse(status == OperationStatus.InvalidData);
+ }
+
+ [TestMethod()]
+ public void TryToBase64CharsTest()
+ {
+
+ }
+
+
+ }
+} \ No newline at end of file