Skip to content

Commit

Permalink
Add tests for frozen collections (dotnet#80395)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Taillefer <[email protected]>
  • Loading branch information
geeknoid and Martin Taillefer authored Jan 12, 2023
1 parent 2038514 commit 6e1ca64
Show file tree
Hide file tree
Showing 5 changed files with 529 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,8 @@ The System.Collections.Immutable library is built-in as part of the shared frame
<Compile Include="System\Collections\Frozen\SmallFrozenSet.cs" />
<Compile Include="System\Collections\Frozen\ValueTypeDefaultComparerFrozenDictionary.cs" />
<Compile Include="System\Collections\Frozen\ValueTypeDefaultComparerFrozenSet.cs" />

<Compile Include="System\Collections\Frozen\Integer\IntegerFrozenDictionary.cs" Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))" />
<Compile Include="System\Collections\Frozen\Integer\IntegerFrozenSet.cs" Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))" />
<Compile Include="System\Collections\Frozen\Integer\SmallIntegerFrozenDictionary.cs" Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))" />
<Compile Include="System\Collections\Frozen\Integer\SmallIntegerFrozenSet.cs" Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))" />
<Compile Include="System\Collections\Frozen\Integer\SparseRangeIntegerFrozenSet.cs" Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))" />

<Compile Include="System\Collections\Frozen\Int32\Int32FrozenDictionary.cs" />
<Compile Include="System\Collections\Frozen\Int32\Int32FrozenSet.cs" />
<Compile Include="System\Collections\Frozen\Int32\SmallInt32FrozenDictionary.cs" Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))" />
<Compile Include="System\Collections\Frozen\Int32\SmallInt32FrozenSet.cs" Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))" />
<Compile Include="System\Collections\Frozen\Int32\SparseRangeInt32FrozenSet.cs" Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))" />

<Compile Include="System\Collections\Frozen\StringComparers\ComparerPicker.cs" />
<Compile Include="System\Collections\Frozen\StringComparers\FullCaseInsensitiveAsciiStringComparer.cs" />
<Compile Include="System\Collections\Frozen\StringComparers\FullCaseInsensitiveStringComparer.cs" />
Expand Down Expand Up @@ -143,6 +132,20 @@ The System.Collections.Immutable library is built-in as part of the shared frame
<None Include="Interfaces.cd" />
</ItemGroup>

<ItemGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))" >
<Compile Include="System\Collections\Frozen\Integer\IntegerFrozenDictionary.cs" />
<Compile Include="System\Collections\Frozen\Integer\IntegerFrozenSet.cs" />
<Compile Include="System\Collections\Frozen\Integer\SmallIntegerFrozenDictionary.cs" />
<Compile Include="System\Collections\Frozen\Integer\SmallIntegerFrozenSet.cs" />
<Compile Include="System\Collections\Frozen\Integer\SparseRangeIntegerFrozenSet.cs" />
</ItemGroup>

<ItemGroup Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))" >
<Compile Include="System\Collections\Frozen\Int32\SmallInt32FrozenDictionary.cs" />
<Compile Include="System\Collections\Frozen\Int32\SmallInt32FrozenSet.cs" />
<Compile Include="System\Collections\Frozen\Int32\SparseRangeInt32FrozenSet.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == '$(NetCoreAppCurrent)'">
<Reference Include="System.Collections" />
<Reference Include="System.Linq" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using Xunit;

namespace System.Collections.Frozen.Tests
{
public static class ComparerPickerTests
{
private static StringComparerBase NewPicker(string[] values, bool ignoreCase)
{
var c = ComparerPicker.Pick(values, ignoreCase, out int minLen, out int maxLenDiff);

foreach (var s in values)
{
Assert.True(s.Length >= minLen);
Assert.True(s.Length <= minLen + maxLenDiff);
}

return c;
}

[Fact]
public static void LeftHand()
{
var c = NewPicker(new[] { "K0", "K20", "K300" }, false);
Assert.IsType<LeftJustifiedSingleCharComparer>(c);
Assert.Equal(1, ((SubstringComparerBase)c).Index);
Assert.Equal(1, ((SubstringComparerBase)c).Count);

c = NewPicker(new[] { "S1" }, false);
Assert.IsType<LeftJustifiedSingleCharComparer>(c);
Assert.Equal(0, ((SubstringComparerBase)c).Index);
Assert.Equal(1, ((SubstringComparerBase)c).Count);

c = NewPicker(new[] { "S1", "T1" }, false);
Assert.IsType<LeftJustifiedSingleCharComparer>(c);
Assert.Equal(0, ((SubstringComparerBase)c).Index);
Assert.Equal(1, ((SubstringComparerBase)c).Count);

c = NewPicker(new[] { "SA1", "TA1", "SB1" }, false);
Assert.IsType<LeftJustifiedSubstringComparer>(c);
Assert.Equal(0, ((SubstringComparerBase)c).Index);
Assert.Equal(2, ((SubstringComparerBase)c).Count);
}

[Fact]
public static void LeftHandCaseInsensitive()
{
var c = NewPicker(new[] { "É1" }, true);
Assert.IsType<LeftJustifiedCaseInsensitiveSubstringComparer>(c);
Assert.Equal(0, ((SubstringComparerBase)c).Index);
Assert.Equal(1, ((SubstringComparerBase)c).Count);

c = NewPicker(new[] { "É1", "T1" }, true);
Assert.IsType<LeftJustifiedCaseInsensitiveSubstringComparer>(c);
Assert.Equal(0, ((SubstringComparerBase)c).Index);
Assert.Equal(1, ((SubstringComparerBase)c).Count);

c = NewPicker(new[] { "ÉA1", "TA1", "ÉB1" }, true);
Assert.IsType<LeftJustifiedCaseInsensitiveSubstringComparer>(c);
Assert.Equal(0, ((SubstringComparerBase)c).Index);
Assert.Equal(2, ((SubstringComparerBase)c).Count);

c = NewPicker(new[] { "ABCDEÉ1ABCDEF", "ABCDETA1ABCDEF", "ABCDESB1ABCDEF" }, true);
Assert.IsType<LeftJustifiedCaseInsensitiveSubstringComparer>(c);
Assert.Equal(5, ((SubstringComparerBase)c).Index);
Assert.Equal(1, ((SubstringComparerBase)c).Count);

c = NewPicker(new[] { "ABCDEFÉ1ABCDEF", "ABCDEFTA1ABCDEF", "ABCDEFSB1ABCDEF" }, true);
Assert.IsType<LeftJustifiedCaseInsensitiveSubstringComparer>(c);
Assert.Equal(6, ((SubstringComparerBase)c).Index);
Assert.Equal(1, ((SubstringComparerBase)c).Count);

c = NewPicker(new[] { "ABCÉDEFÉ1ABCDEF", "ABCÉDEFTA1ABCDEF", "ABCÉDEFSB1ABCDEF" }, true);
Assert.IsType<LeftJustifiedCaseInsensitiveSubstringComparer>(c);
Assert.Equal(7, ((SubstringComparerBase)c).Index);
Assert.Equal(1, ((SubstringComparerBase)c).Count);
}

[Fact]
public static void LeftHandCaseInsensitiveAscii()
{
var c = NewPicker(new[] { "S1" }, true);
Assert.IsType<LeftJustifiedCaseInsensitiveAsciiSubstringComparer>(c);
Assert.Equal(0, ((SubstringComparerBase)c).Index);
Assert.Equal(1, ((SubstringComparerBase)c).Count);

c = NewPicker(new[] { "S1", "T1" }, true);
Assert.IsType<LeftJustifiedCaseInsensitiveAsciiSubstringComparer>(c);
Assert.Equal(0, ((SubstringComparerBase)c).Index);
Assert.Equal(1, ((SubstringComparerBase)c).Count);

c = NewPicker(new[] { "SA1", "TA1", "SB1" }, true);
Assert.IsType<LeftJustifiedCaseInsensitiveAsciiSubstringComparer>(c);
Assert.Equal(0, ((SubstringComparerBase)c).Index);
Assert.Equal(2, ((SubstringComparerBase)c).Count);
}

[Fact]
public static void RightHand()
{
var c = NewPicker(new[] { "1S", "1T" }, false);
Assert.IsType<RightJustifiedSingleCharComparer>(c);
Assert.Equal(-1, ((SubstringComparerBase)c).Index);
Assert.Equal(1, ((SubstringComparerBase)c).Count);

c = NewPicker(new[] { "1AS", "1AT", "1BS" }, false);
Assert.IsType<RightJustifiedSubstringComparer>(c);
Assert.Equal(-2, ((SubstringComparerBase)c).Index);
Assert.Equal(2, ((SubstringComparerBase)c).Count);
}

[Fact]
public static void RightHandCaseInsensitive()
{
var c = NewPicker(new[] { "1É", "1T" }, true);
Assert.IsType<RightJustifiedCaseInsensitiveSubstringComparer>(c);
Assert.Equal(-1, ((SubstringComparerBase)c).Index);
Assert.Equal(1, ((SubstringComparerBase)c).Count);

c = NewPicker(new[] { "1AÉ", "1AT", "1BÉ" }, true);
Assert.IsType<RightJustifiedCaseInsensitiveSubstringComparer>(c);
Assert.Equal(-2, ((SubstringComparerBase)c).Index);
Assert.Equal(2, ((SubstringComparerBase)c).Count);
}

[Fact]
public static void RightHandCaseInsensitiveAscii()
{
var c = NewPicker(new[] { "1S", "1T" }, true);
Assert.IsType<RightJustifiedCaseInsensitiveAsciiSubstringComparer>(c);
Assert.Equal(-1, ((SubstringComparerBase)c).Index);
Assert.Equal(1, ((SubstringComparerBase)c).Count);

c = NewPicker(new[] { "1AS", "1AT", "1BS" }, true);
Assert.IsType<RightJustifiedCaseInsensitiveAsciiSubstringComparer>(c);
Assert.Equal(-2, ((SubstringComparerBase)c).Index);
Assert.Equal(2, ((SubstringComparerBase)c).Count);
}

[Fact]
public static void Full()
{
var c = NewPicker(new[] { "ABC", "DBC", "ADC", "ABD", "ABDABD" }, false);
Assert.IsType<FullStringComparer>(c);
}

[Fact]
public static void FullCaseInsensitive()
{
var c = NewPicker(new[] { "æbc", "DBC", "æDC", "æbd", "æbdæbd" }, true);
Assert.IsType<FullCaseInsensitiveStringComparer>(c);
}

[Fact]
public static void FullCaseInsensitiveAscii()
{
var c = NewPicker(new[] { "abc", "DBC", "aDC", "abd", "abdabd" }, true);
Assert.IsType<FullCaseInsensitiveAsciiStringComparer>(c);
}

[Fact]
public static void IsAllAscii()
{
Assert.True(ComparerPicker.IsAllAscii("abc".AsSpan()));
Assert.True(ComparerPicker.IsAllAscii("abcdefghij".AsSpan()));
Assert.False(ComparerPicker.IsAllAscii("abcdéfghij".AsSpan()));
}
}
}
Loading

0 comments on commit 6e1ca64

Please sign in to comment.