Skip to content

Commit

Permalink
Add unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
bgrainger committed Oct 2, 2019
1 parent c959283 commit fcdc54c
Show file tree
Hide file tree
Showing 6 changed files with 398 additions and 0 deletions.
6 changes: 6 additions & 0 deletions IndexRange.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ VisualStudioVersion = 16.0.29326.143
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IndexRange", "src\IndexRange\IndexRange.csproj", "{10D3FAED-35C8-4AD2-AF58-9546A5BB4AB7}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IndexRange.UnitTests", "tests\IndexRange.UnitTests\IndexRange.UnitTests.csproj", "{6671EA3C-BFA4-4423-925B-BB753F04EBC4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,6 +17,10 @@ Global
{10D3FAED-35C8-4AD2-AF58-9546A5BB4AB7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{10D3FAED-35C8-4AD2-AF58-9546A5BB4AB7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{10D3FAED-35C8-4AD2-AF58-9546A5BB4AB7}.Release|Any CPU.Build.0 = Release|Any CPU
{6671EA3C-BFA4-4423-925B-BB753F04EBC4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6671EA3C-BFA4-4423-925B-BB753F04EBC4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6671EA3C-BFA4-4423-925B-BB753F04EBC4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6671EA3C-BFA4-4423-925B-BB753F04EBC4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
21 changes: 21 additions & 0 deletions tests/IndexRange.UnitTests/AssertExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 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 Xunit;

namespace System
{
public static class AssertExtensions
{
public static T Throws<T>(string expectedParamName, Func<object> testCode)
where T : ArgumentException
{
T exception = Assert.Throws<T>(testCode);

Assert.Equal(expectedParamName, exception.ParamName);

return exception;
}
}
}
21 changes: 21 additions & 0 deletions tests/IndexRange.UnitTests/IndexRange.UnitTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp3.0;net48</TargetFrameworks>
<LangVersion>8.0</LangVersion>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Nullable>Enable</Nullable>
</PropertyGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net48' ">
<ProjectReference Include="..\..\src\IndexRange\IndexRange.csproj" />
<PackageReference Include="System.Memory" Version="4.5.3" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.3.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
</ItemGroup>

</Project>
136 changes: 136 additions & 0 deletions tests/IndexRange.UnitTests/IndexTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// 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.Collections.Generic;
using Xunit;

namespace System.Tests
{
public static class IndexTests
{
[Fact]
public static void CreationTest()
{
Index index = new Index(1, fromEnd: false);
Assert.Equal(1, index.Value);
Assert.False(index.IsFromEnd);

index = new Index(11, fromEnd: true);
Assert.Equal(11, index.Value);
Assert.True(index.IsFromEnd);

index = Index.Start;
Assert.Equal(0, index.Value);
Assert.False(index.IsFromEnd);

index = Index.End;
Assert.Equal(0, index.Value);
Assert.True(index.IsFromEnd);

index = Index.FromStart(3);
Assert.Equal(3, index.Value);
Assert.False(index.IsFromEnd);

index = Index.FromEnd(10);
Assert.Equal(10, index.Value);
Assert.True(index.IsFromEnd);

AssertExtensions.Throws<ArgumentOutOfRangeException>("value", () => new Index(-1, fromEnd: false));
AssertExtensions.Throws<ArgumentOutOfRangeException>("value", () => Index.FromStart(-3));
AssertExtensions.Throws<ArgumentOutOfRangeException>("value", () => Index.FromEnd(-1));
}

[Fact]
public static void GetOffsetTest()
{
Index index = Index.FromStart(3);
Assert.Equal(3, index.GetOffset(3));
Assert.Equal(3, index.GetOffset(10));
Assert.Equal(3, index.GetOffset(20));

// we don't validate the length in the GetOffset so passing short length will just return the regular calculation according to the length value.
Assert.Equal(3, index.GetOffset(2));

index = Index.FromEnd(3);
Assert.Equal(0, index.GetOffset(3));
Assert.Equal(7, index.GetOffset(10));
Assert.Equal(17, index.GetOffset(20));

// we don't validate the length in the GetOffset so passing short length will just return the regular calculation according to the length value.
Assert.Equal(-1, index.GetOffset(2));
}

[Fact]
public static void ImplicitCastTest()
{
Index index = 10;
Assert.Equal(10, index.Value);
Assert.False(index.IsFromEnd);

AssertExtensions.Throws<ArgumentOutOfRangeException>("value", () => index = -10);
}

[Fact]
public static void EqualityTest()
{
Index index1 = 10;
Index index2 = 10;
Assert.True(index1.Equals(index2));
Assert.True(index1.Equals((object) index2));

index2 = new Index(10, fromEnd: true);
Assert.False(index1.Equals(index2));
Assert.False(index1.Equals((object) index2));

index2 = new Index(9, fromEnd: false);
Assert.False(index1.Equals(index2));
Assert.False(index1.Equals((object) index2));
}

[Fact]
public static void HashCodeTest()
{
Index index1 = 10;
Index index2 = 10;
Assert.Equal(index1.GetHashCode(), index2.GetHashCode());

index2 = new Index(10, fromEnd: true);
Assert.NotEqual(index1.GetHashCode(), index2.GetHashCode());

index2 = new Index(99999, fromEnd: false);
Assert.NotEqual(index1.GetHashCode(), index2.GetHashCode());
}

[Fact]
public static void ToStringTest()
{
Index index1 = 100;
Assert.Equal(100.ToString(), index1.ToString());

index1 = new Index(50, fromEnd: true);
Assert.Equal("^" + 50.ToString(), index1.ToString());
}

[Fact]
public static void CollectionTest()
{
int[] array = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
List<int> list = new List<int>(array);

for (int i = 0; i < list.Count; i++)
{
Assert.Equal(i, list[Index.FromStart(i)]);
Assert.Equal(list.Count - i - 1, list[^(i + 1)]);

Assert.Equal(i, array[Index.FromStart(i)]);
Assert.Equal(list.Count - i - 1, array[^(i + 1)]);

#if !NET48
Assert.Equal(array.AsSpan().Slice(i, array.Length - i).ToArray(), array[i..]);
#endif
}
}
}
}
44 changes: 44 additions & 0 deletions tests/IndexRange.UnitTests/IndexTests2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using Xunit;

namespace IndexRange.UnitTests
{
public class IndexTests2
{
[Theory]
[InlineData(0)]
[InlineData(1)]
[InlineData(int.MaxValue)]
public void ConstructFromBeginning(int value)
{
Index index = value;
Assert.Equal(value, index.Value);
Assert.False(index.IsFromEnd);
}

[Theory]
[InlineData(0)]
[InlineData(1)]
[InlineData(int.MaxValue)]
public void ConstructFromEnd(int value)
{
Index index = ^value;
Assert.Equal(value, index.Value);
Assert.True(index.IsFromEnd);
}

[Fact]
public void ToStringFromBeginning()
{
Assert.Equal("0", ((Index) 0).ToString());
Assert.Equal("100", ((Index) 100).ToString());
}

[Fact]
public void ToStringFromEnd()
{
Assert.Equal("^0", (^0).ToString());
Assert.Equal("^100", (^100).ToString());
}
}
}
Loading

0 comments on commit fcdc54c

Please sign in to comment.