mirror of
https://github.com/CommunityToolkit/dotnet.git
synced 2026-10-21 14:52:21 +08:00
9d7ac2ee94
* Initial guard APIs added * Added EqualTo and NotEqualTo APIs * Added more Guard APIs * New interval Guard APIs added * Added Guard APIs for IEnumerable<T> values * Renamed APIs * Added Guard APIs for Stream values * Added Guard APIs for ReadOnlySpan<T> values * Added Guard APIs for ReadOnlyMemory<T> values * Code refactoring * Minor code refactoring * Added Guard.IsBitwiseEqualTo<T> API * Added Guard.IsNull API * Code refactoring and optimizations * Code refactoring * Added Guard.HasSizeNotEqualTo<T> API * Added Guard size API overloads for the string type * Added Guard size API overloads for T[] arrays * Code refactoring * Removed unnecessary using directives * Added Guard reference check APIs * Added new Guaard APIs for string values * Improved Guard.IsBitwiseEqualTo<T> API * Improved Guard.IsNull APIs * Added new size APIs for copy operations * Added new type test APIs, minor code tweaks * Minor bug fixes * Added Guard APIs for Task values * Minor code tweak * Added new Guard.IsEmpty APIs * Refactored code to remove unsafe requirement * Added Guard.IsInRange<T> APIs * Added missing type checks in Guard.IsBitwiseEqualTo API * Added ValueTypeExtensions.ToHexString API * Code refactoring * Added ValueTypeExtensions tests * Added general Guard tests * Bug fixes in the Guard class * Minor speed improvements * Code refactoring * Removed unnecessary using directives * More speed improvements and API refactoring * Refactored/fixed some Guard APIs * More bug fixes * Added Guard tests for array APIs * Fixed the Guard.IsNotEmpty<T> array API * Moved exception throwers to separate class * Moved general Guard throwers to separate class * Disabled warning for XML overload resolution * Moved array Guard throwers to separate class * Moved stream Guard throwers to separate class * Fixed some XML docs * Moved enumerable Guard throwers to separate class * Moved task Guard throwers to separate class * Added new Guard APIs for tasks * Moved string Guard throwers to separate class * Moved ReadOnlySpan<T> Guard throwers to separate class * Added Span<T> APIs to the Guard class * Moved ReadOnlyMemory<T> Guard throwers to separate class * Added Memory<T> APIs to the Guard class * Minor code refactoring * Update file headers * Removed unnecessary methods * Added TypeExtensions.ToTypeString extension * Improved error messages for general Guard APIs * Improved error messages for Enumerable Guard APIs * Improved error messages for stream Guard APIs * Improved error messages for array Guard APIs * Improved error messages for string Guard APIs * Improved error messages for task Guard APIs * Improved error messages for span Guard APIs * Improved error messages for memory Guard APIs * Improved type string for nullable types * Improved type string for value tuple types * Minor performance improvement * Added numeric comparison T4 file * Updated .gitignore to skip generated files * Moved comparison Guard APIs to separate file * Renamed template file, fixed incorrect XML doc * Fixed missing blank line * Removed unnecessary type parameters * Moved enumerable Guard APIs to separate file * Code refactoring in the enumerable T4 template * Renamed a file * Added empty template for ThrowHelper.Collection * Code refactoring in the T4 templates * Switched collection throw helpers to T4 generation * Code refactoring * Removed incorrect file header * Added new Guard.IsInRangeFor API * Updated .gitignore, added generated files to source control * Added tests for Guard.IsInRangeFor * Improved formatting of values in error messages * Improved error messages * More improvements to the error messages * Minor code tweaks * Added Guard.IsNotOfType APIs * Added Guard.IsNotAssignableToType APIs * Added Guard.IsDefault<T> APIs * Fixed some XML docs * Added missing [Pure] attribute * Fixed typo in a comment * Removed unnecessary comment after code changes * Added XML comment for the T4 service * Added more comments to ToHexString * Suppressed an incorrect code style warning * Added more tests for Guard.IsInRange * Added Guard.IsNotInRangeFor APIs * Added readme file for the T4 templates * Changed generated files extension to .g.cs to avoid conflicts * Added Guard.IsCloseTo and Guard.IsNotCloseTo APIs * Added tests for IsCloseTo * Updated ".g.cs" extension for generated files Co-Authored-By: Michael Hawker MSFT (XAML Llama) <michael.hawker@outlook.com> * Removed extra space (typo) Co-Authored-By: Michael Hawker MSFT (XAML Llama) <michael.hawker@outlook.com> * More tweakes to the Guard.md file * Excluded TypeInfo.g.cs file from .ttinclude from checkout * Added more info about target ranges for some APIs * Added IsCloseTo overloads for long type * Minor code tweaks * Added comment to describe (uint) cast range check trick * Added a nore about the .g.cs files being checked in * Added .NET Standard 2.1 target, adjusted imported packages * Added [NotNull] attributes when needed * Added flow attributes to bool Guard APIs * Added missing Guard APIs for the string type * Added shared main unit test project * Removed duplicate files from UWP unit test project * Refactored UWP unit tests project (can't be in same folder as .shproj) * Moved markdown tests back into UWP project * Added UnitTests.NetCore project * Fixed some styling issues in test projects * Added .editorconfig file to UniTests.NetCore project too * Moved .editorconfig file to Shared project folder * Removed NetCore Tests compilation for Native Configuration. * Update Windows Community Toolkit.sln * Updated multiline comments style * Renamed some Guard APIs * Fixed collections tests, moved to shared project * Removed unnecessary [SuppressMessage] attributes * Added internal [NotNull] and [DoesNotReturnIf] attributes * Removed build warnings * Removed leftover compiler directives * Added Guard.IsTrue/False overloads with custom message * Fixed test file location after merge * Fixed Span<T> and Unsafe package references Co-authored-by: Michael Hawker MSFT (XAML Llama) <michael.hawker@outlook.com> Co-authored-by: Alexandre Zollinger Chohfi <alzollin@microsoft.com> Co-authored-by: Alexandre Zollinger Chohfi <chohfi@outlook.com> Co-authored-by: Michael Hawker MSFT (XAML Llama) <24302614+michael-hawker@users.noreply.github.com>
322 lines
9.5 KiB
C#
322 lines
9.5 KiB
C#
// Licensed to the .NET Foundation under one or more agreements.
|
|
// The .NET Foundation licenses this file to you under the MIT license.
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
using System;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using Microsoft.Toolkit.Extensions;
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
namespace UnitTests.Extensions
|
|
{
|
|
[TestClass]
|
|
public class Test_ArrayExtensions
|
|
{
|
|
[TestCategory("ArrayExtensions")]
|
|
[TestMethod]
|
|
public void Test_ArrayExtensions_FillArrayMid()
|
|
{
|
|
bool[,] test = new bool[4, 5];
|
|
|
|
test.Fill(true, 1, 1, 3, 2);
|
|
|
|
var expected = new bool[,]
|
|
{
|
|
{ false, false, false, false, false },
|
|
{ false, true, true, true, false },
|
|
{ false, true, true, true, false },
|
|
{ false, false, false, false, false },
|
|
};
|
|
|
|
CollectionAssert.AreEqual(
|
|
expected,
|
|
test,
|
|
"Fill failed. Expected:\n{0}.\nActual:\n{1}",
|
|
expected.ToArrayString(),
|
|
test.ToArrayString());
|
|
}
|
|
|
|
[TestCategory("ArrayExtensions")]
|
|
[TestMethod]
|
|
public void Test_ArrayExtensions_FillArrayTwice()
|
|
{
|
|
bool[,] test = new bool[4, 5];
|
|
|
|
test.Fill(true, 0, 0, 1, 2);
|
|
test.Fill(true, 1, 3, 2, 2);
|
|
|
|
var expected = new bool[,]
|
|
{
|
|
{ true, false, false, false, false },
|
|
{ true, false, false, true, true },
|
|
{ false, false, false, true, true },
|
|
{ false, false, false, false, false },
|
|
};
|
|
|
|
CollectionAssert.AreEqual(
|
|
expected,
|
|
test,
|
|
"Fill failed. Expected:\n{0}.\nActual:\n{1}",
|
|
expected.ToArrayString(),
|
|
test.ToArrayString());
|
|
}
|
|
|
|
[TestCategory("ArrayExtensions")]
|
|
[TestMethod]
|
|
public void Test_ArrayExtensions_FillArrayNegativeSize()
|
|
{
|
|
bool[,] test = new bool[4, 5];
|
|
|
|
test.Fill(true, 3, 4, -3, -2);
|
|
|
|
// TODO: We may want to think about this pattern in the future:
|
|
/*var expected = new bool[,]
|
|
{
|
|
{ false, false, false, false, false },
|
|
{ false, false, false, false, false },
|
|
{ false, false, true, true, true },
|
|
{ false, false, true, true, true },
|
|
};*/
|
|
|
|
var expected = new bool[,]
|
|
{
|
|
{ false, false, false, false, false },
|
|
{ false, false, false, false, false },
|
|
{ false, false, false, false, false },
|
|
{ false, false, false, false, false },
|
|
};
|
|
|
|
CollectionAssert.AreEqual(
|
|
expected,
|
|
test,
|
|
"Fill failed. Expected:\n{0}.\nActual:\n{1}",
|
|
expected.ToArrayString(),
|
|
test.ToArrayString());
|
|
}
|
|
|
|
[TestCategory("ArrayExtensions")]
|
|
[TestMethod]
|
|
public void Test_ArrayExtensions_FillArrayBottomEdgeBoundary()
|
|
{
|
|
bool[,] test = new bool[4, 5];
|
|
|
|
test.Fill(true, 1, 2, 2, 4);
|
|
|
|
var expected = new bool[,]
|
|
{
|
|
{ false, false, false, false, false },
|
|
{ false, false, true, true, false },
|
|
{ false, false, true, true, false },
|
|
{ false, false, true, true, false },
|
|
};
|
|
|
|
CollectionAssert.AreEqual(
|
|
expected,
|
|
test,
|
|
"Fill failed. Expected:\n{0}.\nActual:\n{1}",
|
|
expected.ToArrayString(),
|
|
test.ToArrayString());
|
|
}
|
|
|
|
[TestCategory("ArrayExtensions")]
|
|
[TestMethod]
|
|
public void Test_ArrayExtensions_FillArrayTopLeftCornerNegativeBoundary()
|
|
{
|
|
bool[,] test = new bool[4, 5];
|
|
|
|
test.Fill(true, -1, -1, 3, 3);
|
|
|
|
var expected = new bool[,]
|
|
{
|
|
{ true, true, false, false, false },
|
|
{ true, true, false, false, false },
|
|
{ false, false, false, false, false },
|
|
{ false, false, false, false, false },
|
|
};
|
|
|
|
CollectionAssert.AreEqual(
|
|
expected,
|
|
test,
|
|
"Fill failed. Expected:\n{0}.\nActual:\n{1}",
|
|
expected.ToArrayString(),
|
|
test.ToArrayString());
|
|
}
|
|
|
|
[TestCategory("ArrayExtensions")]
|
|
[TestMethod]
|
|
public void Test_ArrayExtensions_FillArrayBottomRightCornerBoundary()
|
|
{
|
|
bool[,] test = new bool[5, 4];
|
|
|
|
test.Fill(true, 3, 2, 3, 3);
|
|
|
|
var expected = new bool[,]
|
|
{
|
|
{ false, false, false, false },
|
|
{ false, false, false, false },
|
|
{ false, false, false, false },
|
|
{ false, false, true, true },
|
|
{ false, false, true, true },
|
|
};
|
|
|
|
CollectionAssert.AreEqual(
|
|
expected,
|
|
test,
|
|
"Fill failed. Expected:\n{0}.\nActual:\n{1}",
|
|
expected.ToArrayString(),
|
|
test.ToArrayString());
|
|
}
|
|
|
|
[TestCategory("ArrayExtensions")]
|
|
[TestMethod]
|
|
public void Test_ArrayExtensions_Jagged_GetColumn()
|
|
{
|
|
int[][] array =
|
|
{
|
|
new int[] { 5, 2, 4 },
|
|
new int[] { 6, 3 },
|
|
new int[] { 7 }
|
|
};
|
|
|
|
var col = array.GetColumn(1).ToArray();
|
|
|
|
CollectionAssert.AreEquivalent(new int[] { 2, 3, 0 }, col);
|
|
}
|
|
|
|
[TestCategory("ArrayExtensions")]
|
|
[TestMethod]
|
|
public void Test_ArrayExtensions_Jagged_GetColumn_Exception()
|
|
{
|
|
int[][] array =
|
|
{
|
|
new int[] { 5, 2, 4 },
|
|
new int[] { 6, 3 },
|
|
new int[] { 7 }
|
|
};
|
|
|
|
Assert.ThrowsException<ArgumentOutOfRangeException>(() =>
|
|
{
|
|
array.GetColumn(-1).ToArray();
|
|
});
|
|
|
|
Assert.ThrowsException<ArgumentOutOfRangeException>(() =>
|
|
{
|
|
array.GetColumn(3).ToArray();
|
|
});
|
|
}
|
|
|
|
[TestCategory("ArrayExtensions")]
|
|
[TestMethod]
|
|
public void Test_ArrayExtensions_Rectangular_GetColumn()
|
|
{
|
|
int[,] array =
|
|
{
|
|
{ 5, 2, 4 },
|
|
{ 6, 3, 9 },
|
|
{ 7, -1, 0 }
|
|
};
|
|
|
|
var col = array.GetColumn(1).ToArray();
|
|
|
|
CollectionAssert.AreEquivalent(new int[] { 2, 3, -1 }, col);
|
|
}
|
|
|
|
[TestCategory("ArrayExtensions")]
|
|
[TestMethod]
|
|
public void Test_ArrayExtensions_Rectangular_GetColumn_Exception()
|
|
{
|
|
int[,] array =
|
|
{
|
|
{ 5, 2, 4 },
|
|
{ 6, 3, 0 },
|
|
{ 7, 0, 0 }
|
|
};
|
|
|
|
Assert.ThrowsException<ArgumentOutOfRangeException>(() =>
|
|
{
|
|
array.GetColumn(-1).ToArray();
|
|
});
|
|
|
|
Assert.ThrowsException<ArgumentOutOfRangeException>(() =>
|
|
{
|
|
array.GetColumn(3).ToArray();
|
|
});
|
|
}
|
|
|
|
[TestCategory("ArrayExtensions")]
|
|
[TestMethod]
|
|
public void Test_ArrayExtensions_Rectangular_GetRow()
|
|
{
|
|
int[,] array =
|
|
{
|
|
{ 5, 2, 4 },
|
|
{ 6, 3, 9 },
|
|
{ 7, -1, 0 }
|
|
};
|
|
|
|
var col = array.GetRow(1).ToArray();
|
|
|
|
CollectionAssert.AreEquivalent(new int[] { 6, 3, 9 }, col);
|
|
}
|
|
|
|
[TestCategory("ArrayExtensions")]
|
|
[TestMethod]
|
|
public void Test_ArrayExtensions_Rectangular_GetRow_Exception()
|
|
{
|
|
int[,] array =
|
|
{
|
|
{ 5, 2, 4 },
|
|
{ 6, 3, 0 },
|
|
{ 7, 0, 0 }
|
|
};
|
|
|
|
Assert.ThrowsException<ArgumentOutOfRangeException>(() =>
|
|
{
|
|
array.GetRow(-1).ToArray();
|
|
});
|
|
|
|
Assert.ThrowsException<ArgumentOutOfRangeException>(() =>
|
|
{
|
|
array.GetRow(3).ToArray();
|
|
});
|
|
}
|
|
|
|
[TestCategory("ArrayExtensions")]
|
|
[TestMethod]
|
|
public void Test_ArrayExtensions_Rectangular_ToString()
|
|
{
|
|
int[,] array =
|
|
{
|
|
{ 5, 2, 4 },
|
|
{ 6, 3, -1 },
|
|
{ 7, 0, 9 }
|
|
};
|
|
|
|
string value = array.ToArrayString();
|
|
|
|
Debug.WriteLine(value);
|
|
|
|
Assert.AreEqual("[[5,\t2,\t4]," + Environment.NewLine + " [6,\t3,\t-1]," + Environment.NewLine + " [7,\t0,\t9]]", value);
|
|
}
|
|
|
|
[TestCategory("ArrayExtensions")]
|
|
[TestMethod]
|
|
public void Test_ArrayExtensions_Jagged_ToString()
|
|
{
|
|
int[][] array =
|
|
{
|
|
new int[] { 5, 2 },
|
|
new int[] { 6, 3, -1, 2 },
|
|
new int[] { 7, 0, 9 }
|
|
};
|
|
|
|
string value = array.ToArrayString();
|
|
|
|
Debug.WriteLine(value);
|
|
|
|
Assert.AreEqual("[[5,\t2]," + Environment.NewLine + " [6,\t3,\t-1,\t2]," + Environment.NewLine + " [7,\t0,\t9]]", value);
|
|
}
|
|
}
|
|
}
|