From 1944df9e2d2f7ee1841d6b4dcc56eca30657515c Mon Sep 17 00:00:00 2001
From: Stanislav Muhametsin <346799+stazz@users.noreply.github.com>
Date: Thu, 27 Dec 2018 23:15:23 +0200
Subject: [PATCH] Adding a bunch of tests for UtilPack, with the goal of
catching error and upping code coverage. Already discovered and fixed few
bugs.
---
.../UtilPack.Configuration.csproj | 2 -
.../UtilPack.Documentation.csproj | 3 -
.../Code/UtilPack.JSON/UtilPack.JSON.csproj | 2 -
.../UtilPack.Logging/UtilPack.Logging.csproj | 3 -
.../UtilPack.MSBuild.AsyncExec.csproj | 2 -
.../UtilPack.TabularData.csproj | 2 -
Source/Code/UtilPack/ArgumentValidator.cs | 23 +-
Source/Code/UtilPack/Asynchrony.cs | 191 ++++++--
Source/Code/UtilPack/BinaryExtensions.cs | 80 +++-
Source/Code/UtilPack/BinaryStringPool.cs | 21 +-
Source/Code/UtilPack/CollectionExtensions.cs | 4 +-
Source/Code/UtilPack/Empty.cs | 2 -
Source/Code/UtilPack/Loading.cs | 434 ------------------
Source/Code/UtilPack/NullableComparer.cs | 18 +-
Source/Code/UtilPack/ResizableArray.cs | 18 +-
Source/Code/UtilPack/StreamRelated.cs | 56 ++-
Source/Code/UtilPack/TimeComparers.cs | 18 +-
Source/Code/UtilPack/UtilPack.csproj | 8 +-
.../Tests.UtilPack/AbstractDisposable.cs | 54 +++
.../Tests/Tests.UtilPack/ArgumentValidator.cs | 136 ++++++
Source/Tests/Tests.UtilPack/Asynchrony.cs | 70 +++
.../Tests/Tests.UtilPack/BinaryExtensions.cs | 296 ++++++++++++
.../Tests/Tests.UtilPack/BinaryStringPool.cs | 70 +++
.../{Log10Test.cs => BinaryUtils.cs} | 27 +-
Source/Tests/Tests.UtilPack/Empty.cs | 37 ++
Source/Tests/Tests.UtilPack/ResizableArray.cs | 46 ++
26 files changed, 1038 insertions(+), 585 deletions(-)
delete mode 100644 Source/Code/UtilPack/Loading.cs
create mode 100644 Source/Tests/Tests.UtilPack/AbstractDisposable.cs
create mode 100644 Source/Tests/Tests.UtilPack/ArgumentValidator.cs
create mode 100644 Source/Tests/Tests.UtilPack/Asynchrony.cs
create mode 100644 Source/Tests/Tests.UtilPack/BinaryExtensions.cs
create mode 100644 Source/Tests/Tests.UtilPack/BinaryStringPool.cs
rename Source/Tests/Tests.UtilPack/{Log10Test.cs => BinaryUtils.cs} (67%)
create mode 100644 Source/Tests/Tests.UtilPack/Empty.cs
create mode 100644 Source/Tests/Tests.UtilPack/ResizableArray.cs
diff --git a/Source/Code/UtilPack.Configuration/UtilPack.Configuration.csproj b/Source/Code/UtilPack.Configuration/UtilPack.Configuration.csproj
index 3f4b339..76c0c65 100644
--- a/Source/Code/UtilPack.Configuration/UtilPack.Configuration.csproj
+++ b/Source/Code/UtilPack.Configuration/UtilPack.Configuration.csproj
@@ -6,8 +6,6 @@
True
- True
-
diff --git a/Source/Code/UtilPack.Documentation/UtilPack.Documentation.csproj b/Source/Code/UtilPack.Documentation/UtilPack.Documentation.csproj
index dfbb7c0..69eb3f9 100644
--- a/Source/Code/UtilPack.Documentation/UtilPack.Documentation.csproj
+++ b/Source/Code/UtilPack.Documentation/UtilPack.Documentation.csproj
@@ -5,10 +5,7 @@
- True
-
True
- latest
diff --git a/Source/Code/UtilPack.JSON/UtilPack.JSON.csproj b/Source/Code/UtilPack.JSON/UtilPack.JSON.csproj
index a25bc7c..da1ee8e 100644
--- a/Source/Code/UtilPack.JSON/UtilPack.JSON.csproj
+++ b/Source/Code/UtilPack.JSON/UtilPack.JSON.csproj
@@ -5,8 +5,6 @@
- True
-
True
diff --git a/Source/Code/UtilPack.Logging/UtilPack.Logging.csproj b/Source/Code/UtilPack.Logging/UtilPack.Logging.csproj
index 9ca5a7b..6cb0201 100644
--- a/Source/Code/UtilPack.Logging/UtilPack.Logging.csproj
+++ b/Source/Code/UtilPack.Logging/UtilPack.Logging.csproj
@@ -6,9 +6,6 @@
True
- True
-
- latest
$(DefineConstants);INTERNALIZE
diff --git a/Source/Code/UtilPack.MSBuild.AsyncExec/UtilPack.MSBuild.AsyncExec.csproj b/Source/Code/UtilPack.MSBuild.AsyncExec/UtilPack.MSBuild.AsyncExec.csproj
index c484a8d..61d67a9 100644
--- a/Source/Code/UtilPack.MSBuild.AsyncExec/UtilPack.MSBuild.AsyncExec.csproj
+++ b/Source/Code/UtilPack.MSBuild.AsyncExec/UtilPack.MSBuild.AsyncExec.csproj
@@ -5,8 +5,6 @@
- True
-
True
diff --git a/Source/Code/UtilPack.TabularData/UtilPack.TabularData.csproj b/Source/Code/UtilPack.TabularData/UtilPack.TabularData.csproj
index 069e501..fc13efa 100644
--- a/Source/Code/UtilPack.TabularData/UtilPack.TabularData.csproj
+++ b/Source/Code/UtilPack.TabularData/UtilPack.TabularData.csproj
@@ -6,8 +6,6 @@
True
- True
-
diff --git a/Source/Code/UtilPack/ArgumentValidator.cs b/Source/Code/UtilPack/ArgumentValidator.cs
index c6e06a2..6b72765 100644
--- a/Source/Code/UtilPack/ArgumentValidator.cs
+++ b/Source/Code/UtilPack/ArgumentValidator.cs
@@ -35,6 +35,21 @@ namespace UtilPack
#endif
static class ArgumentValidator
{
+ ///
+ /// This message will be the error message of exception thrown by .
+ ///
+ public const String NULLREF_MESSAGE = "Extension method 'this' parameter is null.";
+
+ ///
+ /// The parameter name will be suffixed by this string when throwing an error inside .
+ ///
+ public const String EMPTY_STRING_SUFFIX = " was empty string.";
+
+ ///
+ /// The parameter name will be suffixed by this string when throwing an error inside or .
+ ///
+ public const String EMPTY_SUFFIX = " was empty.";
+
///
/// Checks whether a method parameter is null.
///
@@ -75,7 +90,7 @@ public static T ValidateNotNullReference( T value )
{
if ( value == null )
{
- throw new NullReferenceException( "Extension method 'this' parameter is null." );
+ throw new NullReferenceException( NULLREF_MESSAGE );
}
return value;
}
@@ -97,7 +112,7 @@ public static IEnumerable ValidateNotEmpty( String parameterName, IEnumera
ValidateNotNull( parameterName, value );
if ( !value.Any() )
{
- throw new ArgumentException( parameterName + " was empty." );
+ throw new ArgumentException( parameterName + EMPTY_SUFFIX );
}
return value;
}
@@ -119,7 +134,7 @@ public static T[] ValidateNotEmpty( String parameterName, T[] value )
ValidateNotNull( parameterName, value );
if ( value.Length <= 0 )
{
- throw new ArgumentException( parameterName + " was empty." );
+ throw new ArgumentException( parameterName + EMPTY_SUFFIX );
}
return value;
}
@@ -140,7 +155,7 @@ public static String ValidateNotEmpty( String parameterName, String value )
ValidateNotNull( parameterName, value );
if ( value.Length == 0 )
{
- throw new ArgumentException( parameterName + " was empty string." );
+ throw new ArgumentException( parameterName + EMPTY_STRING_SUFFIX );
}
return value;
}
diff --git a/Source/Code/UtilPack/Asynchrony.cs b/Source/Code/UtilPack/Asynchrony.cs
index fb0e71e..fa5f732 100644
--- a/Source/Code/UtilPack/Asynchrony.cs
+++ b/Source/Code/UtilPack/Asynchrony.cs
@@ -22,6 +22,14 @@
using System.Threading.Tasks;
using UtilPack;
+using TTaskExt = System.Threading.Tasks.
+#if NET40
+ TaskEx
+#else
+ Task
+#endif
+ ;
+
namespace UtilPack
{
///
@@ -64,7 +72,7 @@ static class TaskUtils
///
/// Gets the task which is completed.
///
- public static System.Threading.Tasks.Task CompletedTask { get; }
+ public static Task CompletedTask { get; }
///
/// Gets the task which is completed to a boolean value true.
@@ -80,25 +88,13 @@ static class TaskUtils
static TaskUtils()
{
- var src = new System.Threading.Tasks.TaskCompletionSource
-
+
diff --git a/Source/Tests/Tests.UtilPack/AbstractDisposable.cs b/Source/Tests/Tests.UtilPack/AbstractDisposable.cs
new file mode 100644
index 0000000..af086ad
--- /dev/null
+++ b/Source/Tests/Tests.UtilPack/AbstractDisposable.cs
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2018 Stanislav Muhametsin. All rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.
+ *
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using System;
+using System.Collections.Generic;
+using System.Text;
+using UtilPack;
+
+namespace Tests.UtilPack
+{
+ [TestClass]
+ public class AbstractDisposableTests
+ {
+ [TestMethod]
+ public void TestThatDisposeGetsCalled()
+ {
+ var disposable = new TestDisposable();
+ using ( disposable )
+ {
+ Assert.IsFalse( disposable.Disposed );
+ Assert.IsFalse( disposable.DisposeCalled );
+ }
+
+ Assert.IsTrue( disposable.Disposed );
+ Assert.IsTrue( disposable.DisposeCalled );
+ }
+
+ private sealed class TestDisposable : AbstractDisposable
+ {
+ protected override void Dispose( Boolean disposing )
+ {
+ this.DisposeCalled = true;
+ }
+
+ public Boolean DisposeCalled { get; private set; }
+ }
+
+ }
+}
diff --git a/Source/Tests/Tests.UtilPack/ArgumentValidator.cs b/Source/Tests/Tests.UtilPack/ArgumentValidator.cs
new file mode 100644
index 0000000..1c4bbe6
--- /dev/null
+++ b/Source/Tests/Tests.UtilPack/ArgumentValidator.cs
@@ -0,0 +1,136 @@
+/*
+ * Copyright 2018 Stanislav Muhametsin. All rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.
+ *
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using UtilPack;
+
+namespace Tests.UtilPack
+{
+ [TestClass]
+ public class ArgumentValidatorTests
+ {
+ [TestMethod]
+ public void TestValidateNotNullReference()
+ {
+ var obj = new Object();
+ Assert.IsTrue( ReferenceEquals( obj, ArgumentValidator.ValidateNotNullReference( obj ) ) );
+ }
+
+ [TestMethod,
+ ExpectedException( typeof( NullReferenceException ), ArgumentValidator.NULLREF_MESSAGE, AllowDerivedTypes = false )]
+ public void TestValidateNotNullReferenceWithNull()
+ {
+ ArgumentValidator.ValidateNotNullReference( null );
+ }
+
+ [TestMethod]
+ public void TestValidateNotNull()
+ {
+ var obj = new Object();
+ Assert.IsTrue( ReferenceEquals( obj, ArgumentValidator.ValidateNotNull( nameof( obj ), obj ) ) );
+ }
+
+ private const String OBJ = "obj";
+
+ [TestMethod,
+ ExpectedException( typeof( ArgumentNullException ), OBJ, AllowDerivedTypes = false )]
+ public void TestValidateNotNullWithNull()
+ {
+ ArgumentValidator.ValidateNotNull( OBJ, null );
+ }
+
+ [TestMethod]
+ public void TestValidateNotEmptyString()
+ {
+ ArgumentValidator.ValidateNotEmpty( OBJ, "nonempty" );
+ }
+
+ [TestMethod,
+ ExpectedException( typeof( ArgumentException ), OBJ + ArgumentValidator.EMPTY_STRING_SUFFIX, AllowDerivedTypes = false )
+ ]
+ public void TestValidateNotEmptyStringWithEmptyString()
+ {
+ ArgumentValidator.ValidateNotEmpty( OBJ, "" );
+ }
+
+ [TestMethod,
+ ExpectedException( typeof( ArgumentNullException ), OBJ, AllowDerivedTypes = false )]
+ public void TestValidateNotEmptyStringWithNullString()
+ {
+ ArgumentValidator.ValidateNotEmpty( OBJ, null );
+ }
+
+ [TestMethod]
+ public void TestValidateNonEmptyEnumerable()
+ {
+ ArgumentValidator.ValidateNotEmpty( OBJ, Enumerable.Repeat( new Object(), 1 ) );
+ }
+
+ [TestMethod,
+ ExpectedException( typeof( ArgumentException ), OBJ + ArgumentValidator.EMPTY_SUFFIX, AllowDerivedTypes = false )]
+ public void TestValidateNonEmptyEnumerableWithEmptyEnumerable()
+ {
+ ArgumentValidator.ValidateNotEmpty( OBJ, Enumerable.Empty() );
+ }
+
+ [TestMethod,
+ ExpectedException( typeof( ArgumentNullException ), OBJ, AllowDerivedTypes = false )]
+ public void TestValidateNonEmptyEnumerableWithNull()
+ {
+ ArgumentValidator.ValidateNotEmpty( OBJ, null as IEnumerable );
+ }
+
+ [TestMethod]
+ public void TestValidateNonEmptyArray()
+ {
+ ArgumentValidator.ValidateNotEmpty( OBJ, new[] { new Object() } );
+ }
+
+ [TestMethod,
+ ExpectedException( typeof( ArgumentException ), OBJ + ArgumentValidator.EMPTY_SUFFIX, AllowDerivedTypes = false )]
+ public void TestValidateNonEmptyEnumerableWithEmptyArray()
+ {
+ ArgumentValidator.ValidateNotEmpty( OBJ, new Object[] { } );
+ }
+
+ [TestMethod,
+ ExpectedException( typeof( ArgumentNullException ), OBJ, AllowDerivedTypes = false )]
+ public void TestValidateNonEmptyArrayWithNull()
+ {
+ ArgumentValidator.ValidateNotEmpty( OBJ, null as Object[] );
+ }
+
+ [TestMethod]
+ public void TestValidateAllNotNull()
+ {
+ ArgumentValidator.ValidateAllNotNull( OBJ, new Object[] { } );
+ ArgumentValidator.ValidateAllNotNull( OBJ, new[] { new Object() } );
+ }
+
+ [TestMethod,
+ ExpectedException( typeof( ArgumentNullException ), AllowDerivedTypes = false )]
+ public void TestValidateAllNotNullWithNull()
+ {
+ ArgumentValidator.ValidateAllNotNull( OBJ, new Object[] { null } );
+ }
+ }
+
+}
diff --git a/Source/Tests/Tests.UtilPack/Asynchrony.cs b/Source/Tests/Tests.UtilPack/Asynchrony.cs
new file mode 100644
index 0000000..129a2e6
--- /dev/null
+++ b/Source/Tests/Tests.UtilPack/Asynchrony.cs
@@ -0,0 +1,70 @@
+/*
+ * Copyright 2018 Stanislav Muhametsin. All rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.
+ *
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.Threading.Tasks;
+using UtilPack;
+
+namespace Tests.UtilPack
+{
+ [TestClass]
+ public class AsynchronyTests
+ {
+ [TestMethod, Timeout( 1500 )]
+ public async Task TestTimeoutAfter()
+ {
+ await Task.Delay( TimeSpan.FromMilliseconds( 1000 ) )
+ .TimeoutAfter( TimeSpan.FromMilliseconds( 2000 ), default );
+ }
+
+ [TestMethod,
+ Timeout( 1500 ),
+ ExpectedException( typeof( TimeoutException ), AllowDerivedTypes = false )
+ ]
+ public async Task TestTimeoutAfterWithExpectedException()
+ {
+ await Task.Delay( TimeSpan.FromMilliseconds( 1000 ) )
+ .TimeoutAfter( TimeSpan.FromMilliseconds( 500 ), default );
+ }
+
+ [TestMethod, Timeout( 1500 )]
+ public async Task TestTimeoutAfterWithResult()
+ {
+ await this.DelayWithResult( TimeSpan.FromMilliseconds( 1000 ), new Object() )
+ .TimeoutAfter( TimeSpan.FromMilliseconds( 2000 ), default );
+ }
+
+ [TestMethod,
+ Timeout( 1500 ),
+ ExpectedException( typeof( TimeoutException ), AllowDerivedTypes = false )
+ ]
+ public async Task TestTimeoutAfterWithresultAndExpectedException()
+ {
+ await this.DelayWithResult( TimeSpan.FromMilliseconds( 1000 ), new Object() )
+ .TimeoutAfter( TimeSpan.FromMilliseconds( 500 ), default );
+ }
+
+ private async Task DelayWithResult( TimeSpan delay, T result )
+ {
+ await Task.Delay( delay );
+ return result;
+ }
+ }
+}
diff --git a/Source/Tests/Tests.UtilPack/BinaryExtensions.cs b/Source/Tests/Tests.UtilPack/BinaryExtensions.cs
new file mode 100644
index 0000000..79c1611
--- /dev/null
+++ b/Source/Tests/Tests.UtilPack/BinaryExtensions.cs
@@ -0,0 +1,296 @@
+/*
+ * Copyright 2018 Stanislav Muhametsin. All rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.
+ *
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Runtime.InteropServices;
+using System.Security.Cryptography;
+using System.Text;
+using Tests.UtilPack;
+using UtilPack;
+
+namespace Tests.UtilPack
+{
+ [TestClass]
+ public class BinaryExtensions
+ {
+ [TestMethod]
+ public void TestStreamExtensions()
+ {
+ using ( var stream = new MemoryStream( new Byte[20] ) )
+ {
+ Assert.AreEqual( 0, stream.Position );
+ stream.SeekFromBegin( 10 );
+ Assert.AreEqual( 10, stream.Position );
+ stream.SeekFromCurrent( 10 );
+ Assert.AreEqual( 20, stream.Position );
+ stream.SeekFromCurrent( -10 );
+ Assert.AreEqual( 10, stream.Position );
+ stream.SeekFromBegin( 0 );
+ Assert.AreEqual( 0, stream.Position );
+ }
+ }
+ }
+
+ [TestClass]
+ public class ByteArraySerializationFuzzyTests
+ {
+ private static ByteArraySerializationFuzzyTestPerformer _performer = new ByteArraySerializationFuzzyTestPerformer();
+
+ [TestMethod]
+ public void TestSByte()
+ {
+ _performer.PerformTest(
+ val => unchecked((SByte) val),
+ UtilPackExtensions.WriteSByteToBytes,
+ UtilPackExtensions.ReadSByteFromBytes,
+ UtilPackExtensions.WriteSByteToBytesNoRef,
+ UtilPackExtensions.ReadSByteFromBytesNoRef
+ );
+ }
+
+ [TestMethod]
+ public void TestByte()
+ {
+ _performer.PerformTest(
+ val => unchecked((Byte) val),
+ UtilPackExtensions.WriteByteToBytes,
+ UtilPackExtensions.ReadByteFromBytes,
+ UtilPackExtensions.WriteByteToBytesNoRef,
+ UtilPackExtensions.ReadByteFromBytesNoRef
+ );
+ }
+
+ [TestMethod]
+ public void TestInt16()
+ {
+ _performer.PerformTest(
+ val => unchecked((Int16) val),
+ UtilPackExtensions.WriteInt16LEToBytes,
+ UtilPackExtensions.ReadInt16LEFromBytes,
+ UtilPackExtensions.WriteInt16LEToBytesNoRef,
+ UtilPackExtensions.ReadInt16LEFromBytesNoRef,
+ UtilPackExtensions.WriteInt16BEToBytes,
+ UtilPackExtensions.ReadInt16BEFromBytes,
+ UtilPackExtensions.WriteInt16BEToBytesNoRef,
+ UtilPackExtensions.ReadInt16BEFromBytesNoRef
+ );
+ }
+
+ [TestMethod]
+ public void TestUInt16()
+ {
+ _performer.PerformTest(
+ val => unchecked((UInt16) val),
+ UtilPackExtensions.WriteUInt16LEToBytes,
+ UtilPackExtensions.ReadUInt16LEFromBytes,
+ UtilPackExtensions.WriteUInt16LEToBytesNoRef,
+ UtilPackExtensions.ReadUInt16LEFromBytesNoRef,
+ UtilPackExtensions.WriteUInt16BEToBytes,
+ UtilPackExtensions.ReadUInt16BEFromBytes,
+ UtilPackExtensions.WriteUInt16BEToBytesNoRef,
+ UtilPackExtensions.ReadUInt16BEFromBytesNoRef
+ );
+ }
+
+ [TestMethod]
+ public void TestInt32()
+ {
+ _performer.PerformTest(
+ val => unchecked((Int32) val),
+ UtilPackExtensions.WriteInt32LEToBytes,
+ UtilPackExtensions.ReadInt32LEFromBytes,
+ UtilPackExtensions.WriteInt32LEToBytesNoRef,
+ UtilPackExtensions.ReadInt32LEFromBytesNoRef,
+ UtilPackExtensions.WriteInt32BEToBytes,
+ UtilPackExtensions.ReadInt32BEFromBytes,
+ UtilPackExtensions.WriteInt32BEToBytesNoRef,
+ UtilPackExtensions.ReadInt32BEFromBytesNoRef
+ );
+ }
+
+ [TestMethod]
+ public void TestUInt32()
+ {
+ _performer.PerformTest(
+ val => unchecked((UInt32) val),
+ UtilPackExtensions.WriteUInt32LEToBytes,
+ UtilPackExtensions.ReadUInt32LEFromBytes,
+ UtilPackExtensions.WriteUInt32LEToBytesNoRef,
+ UtilPackExtensions.ReadUInt32LEFromBytesNoRef,
+ UtilPackExtensions.WriteUInt32BEToBytes,
+ UtilPackExtensions.ReadUInt32BEFromBytes,
+ UtilPackExtensions.WriteUInt32BEToBytesNoRef,
+ UtilPackExtensions.ReadUInt32BEFromBytesNoRef
+ );
+ }
+
+ [TestMethod]
+ public void TestInt64()
+ {
+ _performer.PerformTest(
+ val => val,
+ UtilPackExtensions.WriteInt64LEToBytes,
+ UtilPackExtensions.ReadInt64LEFromBytes,
+ UtilPackExtensions.WriteInt64LEToBytesNoRef,
+ UtilPackExtensions.ReadInt64LEFromBytesNoRef,
+ UtilPackExtensions.WriteInt64BEToBytes,
+ UtilPackExtensions.ReadInt64BEFromBytes,
+ UtilPackExtensions.WriteInt64BEToBytesNoRef,
+ UtilPackExtensions.ReadInt64BEFromBytesNoRef
+ );
+ }
+
+ [TestMethod]
+ public void TestUInt64()
+ {
+ _performer.PerformTest(
+ val => unchecked((UInt64) val),
+ UtilPackExtensions.WriteUInt64LEToBytes,
+ UtilPackExtensions.ReadUInt64LEFromBytes,
+ UtilPackExtensions.WriteUInt64LEToBytesNoRef,
+ UtilPackExtensions.ReadUInt64LEFromBytesNoRef,
+ UtilPackExtensions.WriteUInt64BEToBytes,
+ UtilPackExtensions.ReadUInt64BEFromBytes,
+ UtilPackExtensions.WriteUInt64BEToBytesNoRef,
+ UtilPackExtensions.ReadUInt64BEFromBytesNoRef
+ );
+ }
+
+ [TestMethod]
+ public void TestDouble()
+ {
+ _performer.PerformTest(
+ val => BitConverter.Int64BitsToDouble( val ),
+ UtilPackExtensions.WriteDoubleLEToBytes,
+ UtilPackExtensions.ReadDoubleLEFromBytes,
+ UtilPackExtensions.WriteDoubleLEToBytesNoRef,
+ UtilPackExtensions.ReadDoubleLEFromBytesNoRef,
+ UtilPackExtensions.WriteDoubleBEToBytes,
+ UtilPackExtensions.ReadDoubleBEFromBytes,
+ UtilPackExtensions.WriteDoubleBEToBytesNoRef,
+ UtilPackExtensions.ReadDoubleBEFromBytesNoRef
+ );
+ }
+
+ [TestMethod]
+ public void TestSingle()
+ {
+ _performer.PerformTest(
+ val => BitConverter.Int32BitsToSingle( unchecked((Int32) val) ),
+ UtilPackExtensions.WriteSingleLEToBytes,
+ UtilPackExtensions.ReadSingleLEFromBytes,
+ UtilPackExtensions.WriteSingleLEToBytesNoRef,
+ UtilPackExtensions.ReadSingleLEFromBytesNoRef,
+ UtilPackExtensions.WriteSingleBEToBytes,
+ UtilPackExtensions.ReadSingleBEFromBytes,
+ UtilPackExtensions.WriteSingleBEToBytesNoRef,
+ UtilPackExtensions.ReadSingleBEFromBytesNoRef
+ );
+ }
+ }
+
+ public delegate Byte[] WriteWithRef( Byte[] array, ref Int32 index, T value );
+
+ public delegate T ReadWithRef( Byte[] array, ref Int32 index );
+
+ public delegate Byte[] WriteWithoutRef( Byte[] array, Int32 index, T value );
+
+ public delegate T ReadWithoutRef( Byte[] array, Int32 index );
+
+
+ public sealed class ByteArraySerializationFuzzyTestPerformer
+ {
+ public ByteArraySerializationFuzzyTestPerformer(
+ )
+ {
+ var array = new Byte[sizeof( Int64 )];
+
+ RandomNumberGenerator.Fill( array );
+ this.Value = BitConverter.ToInt64( array );
+ }
+
+ public Int64 Value { get; }
+
+ public void PerformTest(
+ Func convert,
+ WriteWithRef write,
+ ReadWithRef read,
+ WriteWithoutRef writeNoRef,
+ ReadWithoutRef readNoRef
+ )
+ {
+ var val = convert( this.Value );
+ PerformTestWithRef( val, write, read );
+ PerformTestWithoutRef( val, writeNoRef, readNoRef );
+ }
+
+ private static void PerformTestWithRef(
+ T val,
+ WriteWithRef write,
+ ReadWithRef read
+ )
+ {
+ var size = Marshal.SizeOf();
+ var array = new Byte[size];
+ var idx = 0;
+ write( array, ref idx, val );
+ Assert.AreEqual( size, idx );
+ idx = 0;
+ var deserialized = read( array, ref idx );
+ Assert.AreEqual( size, idx );
+ Assert.AreEqual( deserialized, val, "Value was different for methods: " + write.Method + ", " + read.Method );
+ }
+
+ private static void PerformTestWithoutRef(
+ T val,
+ WriteWithoutRef write,
+ ReadWithoutRef read
+ )
+ {
+ var size = Marshal.SizeOf();
+ var array = new Byte[size];
+ var idx = 0;
+ write( array, idx, val );
+ var deserialized = read( array, idx );
+ Assert.AreEqual( deserialized, val );
+ }
+
+ }
+}
+
+public static partial class E_UtilPack
+{
+ public static void PerformTest(
+ this ByteArraySerializationFuzzyTestPerformer performer,
+ Func convert,
+ WriteWithRef writeLE,
+ ReadWithRef readLE,
+ WriteWithoutRef writeNoRefLE,
+ ReadWithoutRef readNoRefLE,
+ WriteWithRef writeBE,
+ ReadWithRef readBE,
+ WriteWithoutRef writeNoRefBE,
+ ReadWithoutRef readNoRefBE
+ )
+ {
+ performer.PerformTest( convert, writeLE, readLE, writeNoRefLE, readNoRefLE );
+ performer.PerformTest( convert, writeBE, readBE, writeNoRefBE, readNoRefBE );
+ }
+}
\ No newline at end of file
diff --git a/Source/Tests/Tests.UtilPack/BinaryStringPool.cs b/Source/Tests/Tests.UtilPack/BinaryStringPool.cs
new file mode 100644
index 0000000..3a0705f
--- /dev/null
+++ b/Source/Tests/Tests.UtilPack/BinaryStringPool.cs
@@ -0,0 +1,70 @@
+/*
+ * Copyright 2018 Stanislav Muhametsin. All rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.
+ *
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using System;
+using System.Collections.Generic;
+using System.Text;
+using UtilPack;
+
+namespace Tests.UtilPack
+{
+ [TestClass]
+ public class BinaryStringPoolTests
+ {
+ [TestMethod]
+ public void TestBinaryStringPoolCaching()
+ {
+ var encoding = new UTF8Encoding( false, false );
+ var pool = BinaryStringPoolFactory.NewNotConcurrentBinaryStringPool( encoding );
+ var str1 = "Test";
+ var str1Pooled = pool.GetString( encoding.GetBytes( str1 ) );
+ Assert.AreEqual( str1, str1Pooled );
+ Assert.AreNotSame( str1, str1Pooled );
+ var str1PooledAgain = pool.GetString( encoding.GetBytes( str1 ) );
+ Assert.AreSame( str1Pooled, str1PooledAgain );
+ }
+
+ [TestMethod]
+ public void TestBinaryStringPoolDifferentStrings()
+ {
+ var encoding = new UTF8Encoding( false, false );
+ var pool = BinaryStringPoolFactory.NewNotConcurrentBinaryStringPool( encoding );
+ var str1 = "Test";
+ var str1Pooled = pool.GetString( encoding.GetBytes( str1 ) );
+ Assert.AreEqual( str1, str1Pooled );
+ Assert.AreNotSame( str1, str1Pooled );
+ var str2 = "AnotherTest";
+ var str2Pooled = pool.GetString( encoding.GetBytes( str2 ) );
+ Assert.AreEqual( str2, str2Pooled );
+ Assert.AreNotSame( str2, str2Pooled );
+ }
+
+ [TestMethod]
+ public void TestBinaryStringPoolArrayModification()
+ {
+ var encoding = new UTF8Encoding( false, false );
+ var pool = BinaryStringPoolFactory.NewNotConcurrentBinaryStringPool( encoding );
+ var str = "Test";
+ var strBytes = encoding.GetBytes( str );
+ var strPooled = pool.GetString( strBytes );
+ strBytes[1] = 1;
+ var strPooled2 = pool.GetString( encoding.GetBytes( str ) );
+ Assert.AreSame( strPooled, strPooled2 );
+ }
+ }
+}
diff --git a/Source/Tests/Tests.UtilPack/Log10Test.cs b/Source/Tests/Tests.UtilPack/BinaryUtils.cs
similarity index 67%
rename from Source/Tests/Tests.UtilPack/Log10Test.cs
rename to Source/Tests/Tests.UtilPack/BinaryUtils.cs
index 018d3d9..5b46674 100644
--- a/Source/Tests/Tests.UtilPack/Log10Test.cs
+++ b/Source/Tests/Tests.UtilPack/BinaryUtils.cs
@@ -18,13 +18,14 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
+using System.Linq;
using System.Text;
using UtilPack;
namespace Tests.UtilPack
{
[TestClass]
- public class NumericTest
+ public class BinaryUtilsTest
{
[TestMethod]
@@ -48,5 +49,29 @@ public void TestLog10()
Assert.AreEqual( 19, BinaryUtils.Log10( 10000000000000000000UL ) );
Assert.AreEqual( 19, BinaryUtils.Log10( 18446744073709551615UL ) ); // 2^64
}
+
+ [TestMethod]
+ public void TestLog2()
+ {
+ Assert.AreEqual( -1, BinaryUtils.Log2( 0 ) );
+
+ foreach ( var i in Enumerable.Range( 1, 31 ) )
+ {
+ var number = unchecked((UInt32) 1 << i);
+ Assert.AreEqual( i, BinaryUtils.Log2( number ) );
+ Assert.AreEqual( i - 1, BinaryUtils.Log2( number - 1 ) );
+ }
+ Assert.AreEqual( 31, BinaryUtils.Log2( UInt32.MaxValue ) );
+
+ Assert.AreEqual( -1, BinaryUtils.Log2( 0UL ) );
+
+ foreach ( var i in Enumerable.Range( 1, 31 ) )
+ {
+ var number = unchecked((UInt64) 1L << i);
+ Assert.AreEqual( i, BinaryUtils.Log2( number ) );
+ Assert.AreEqual( i - 1, BinaryUtils.Log2( number - 1 ) );
+ }
+ Assert.AreEqual( 63, BinaryUtils.Log2( UInt64.MaxValue ) );
+ }
}
}
diff --git a/Source/Tests/Tests.UtilPack/Empty.cs b/Source/Tests/Tests.UtilPack/Empty.cs
new file mode 100644
index 0000000..1143e4b
--- /dev/null
+++ b/Source/Tests/Tests.UtilPack/Empty.cs
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2018 Stanislav Muhametsin. All rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.
+ *
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using UtilPack;
+
+namespace Tests.UtilPack
+{
+ [TestClass]
+ public class EmptyTests
+ {
+ [TestMethod]
+ public void TestEmpty()
+ {
+ Assert.AreEqual( 0, Empty.Array.Length );
+ Assert.IsFalse( Empty.Enumerable.Any() );
+ }
+ }
+}
diff --git a/Source/Tests/Tests.UtilPack/ResizableArray.cs b/Source/Tests/Tests.UtilPack/ResizableArray.cs
new file mode 100644
index 0000000..c6693d0
--- /dev/null
+++ b/Source/Tests/Tests.UtilPack/ResizableArray.cs
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2018 Stanislav Muhametsin. All rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.
+ *
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using System;
+using System.Collections.Generic;
+using System.Text;
+using UtilPack;
+
+namespace Tests.UtilPack
+{
+ [TestClass]
+ public class ResizableArrayTests
+ {
+
+ [TestMethod]
+ public void TestResizableArrayUsage()
+ {
+ var resizable = new ResizableArray();
+ Assert.AreEqual( -1, resizable.MaximumSize );
+ Assert.IsTrue( resizable.ExponentialResize );
+
+ Assert.AreEqual( 0, resizable.CurrentMaxCapacity );
+ Assert.AreEqual( 0, resizable.Array.Length );
+
+ resizable.CurrentMaxCapacity = 10;
+ Assert.AreEqual( 10, resizable.CurrentMaxCapacity );
+ Assert.AreEqual( 16, resizable.Array.Length );
+ }
+
+ }
+}