From 6e1ca642370eefbe49c61a0b3f518772875fbe24 Mon Sep 17 00:00:00 2001 From: Martin Taillefer Date: Thu, 12 Jan 2023 10:53:20 -0800 Subject: [PATCH] Add tests for frozen collections (#80395) Co-authored-by: Martin Taillefer --- .../src/System.Collections.Immutable.csproj | 25 +- .../tests/Frozen/ComparerPickerTests.cs | 172 +++++++++ .../tests/Frozen/ComparerTests.cs | 340 ++++++++++++++++++ ...ionaryTest.cs => FrozenDictionaryTests.cs} | 0 .../System.Collections.Immutable.Tests.csproj | 4 +- 5 files changed, 529 insertions(+), 12 deletions(-) create mode 100644 src/libraries/System.Collections.Immutable/tests/Frozen/ComparerPickerTests.cs create mode 100644 src/libraries/System.Collections.Immutable/tests/Frozen/ComparerTests.cs rename src/libraries/System.Collections.Immutable/tests/Frozen/{FrozenDictionaryTest.cs => FrozenDictionaryTests.cs} (100%) diff --git a/src/libraries/System.Collections.Immutable/src/System.Collections.Immutable.csproj b/src/libraries/System.Collections.Immutable/src/System.Collections.Immutable.csproj index d57ea73eafddf..bfa1b92f4e4e1 100644 --- a/src/libraries/System.Collections.Immutable/src/System.Collections.Immutable.csproj +++ b/src/libraries/System.Collections.Immutable/src/System.Collections.Immutable.csproj @@ -35,19 +35,8 @@ The System.Collections.Immutable library is built-in as part of the shared frame - - - - - - - - - - - @@ -143,6 +132,20 @@ The System.Collections.Immutable library is built-in as part of the shared frame + + + + + + + + + + + + + + diff --git a/src/libraries/System.Collections.Immutable/tests/Frozen/ComparerPickerTests.cs b/src/libraries/System.Collections.Immutable/tests/Frozen/ComparerPickerTests.cs new file mode 100644 index 0000000000000..821bebbec3242 --- /dev/null +++ b/src/libraries/System.Collections.Immutable/tests/Frozen/ComparerPickerTests.cs @@ -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(c); + Assert.Equal(1, ((SubstringComparerBase)c).Index); + Assert.Equal(1, ((SubstringComparerBase)c).Count); + + c = NewPicker(new[] { "S1" }, false); + Assert.IsType(c); + Assert.Equal(0, ((SubstringComparerBase)c).Index); + Assert.Equal(1, ((SubstringComparerBase)c).Count); + + c = NewPicker(new[] { "S1", "T1" }, false); + Assert.IsType(c); + Assert.Equal(0, ((SubstringComparerBase)c).Index); + Assert.Equal(1, ((SubstringComparerBase)c).Count); + + c = NewPicker(new[] { "SA1", "TA1", "SB1" }, false); + Assert.IsType(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(c); + Assert.Equal(0, ((SubstringComparerBase)c).Index); + Assert.Equal(1, ((SubstringComparerBase)c).Count); + + c = NewPicker(new[] { "É1", "T1" }, true); + Assert.IsType(c); + Assert.Equal(0, ((SubstringComparerBase)c).Index); + Assert.Equal(1, ((SubstringComparerBase)c).Count); + + c = NewPicker(new[] { "ÉA1", "TA1", "ÉB1" }, true); + Assert.IsType(c); + Assert.Equal(0, ((SubstringComparerBase)c).Index); + Assert.Equal(2, ((SubstringComparerBase)c).Count); + + c = NewPicker(new[] { "ABCDEÉ1ABCDEF", "ABCDETA1ABCDEF", "ABCDESB1ABCDEF" }, true); + Assert.IsType(c); + Assert.Equal(5, ((SubstringComparerBase)c).Index); + Assert.Equal(1, ((SubstringComparerBase)c).Count); + + c = NewPicker(new[] { "ABCDEFÉ1ABCDEF", "ABCDEFTA1ABCDEF", "ABCDEFSB1ABCDEF" }, true); + Assert.IsType(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(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(c); + Assert.Equal(0, ((SubstringComparerBase)c).Index); + Assert.Equal(1, ((SubstringComparerBase)c).Count); + + c = NewPicker(new[] { "S1", "T1" }, true); + Assert.IsType(c); + Assert.Equal(0, ((SubstringComparerBase)c).Index); + Assert.Equal(1, ((SubstringComparerBase)c).Count); + + c = NewPicker(new[] { "SA1", "TA1", "SB1" }, true); + Assert.IsType(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(c); + Assert.Equal(-1, ((SubstringComparerBase)c).Index); + Assert.Equal(1, ((SubstringComparerBase)c).Count); + + c = NewPicker(new[] { "1AS", "1AT", "1BS" }, false); + Assert.IsType(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(c); + Assert.Equal(-1, ((SubstringComparerBase)c).Index); + Assert.Equal(1, ((SubstringComparerBase)c).Count); + + c = NewPicker(new[] { "1AÉ", "1AT", "1BÉ" }, true); + Assert.IsType(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(c); + Assert.Equal(-1, ((SubstringComparerBase)c).Index); + Assert.Equal(1, ((SubstringComparerBase)c).Count); + + c = NewPicker(new[] { "1AS", "1AT", "1BS" }, true); + Assert.IsType(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(c); + } + + [Fact] + public static void FullCaseInsensitive() + { + var c = NewPicker(new[] { "æbc", "DBC", "æDC", "æbd", "æbdæbd" }, true); + Assert.IsType(c); + } + + [Fact] + public static void FullCaseInsensitiveAscii() + { + var c = NewPicker(new[] { "abc", "DBC", "aDC", "abd", "abdabd" }, true); + Assert.IsType(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())); + } + } +} diff --git a/src/libraries/System.Collections.Immutable/tests/Frozen/ComparerTests.cs b/src/libraries/System.Collections.Immutable/tests/Frozen/ComparerTests.cs new file mode 100644 index 0000000000000..be1074bf532e1 --- /dev/null +++ b/src/libraries/System.Collections.Immutable/tests/Frozen/ComparerTests.cs @@ -0,0 +1,340 @@ +// 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 ComparerTests + { + private static void Equal(SubstringComparerBase c, string a, string b, bool fullEqual) + { + Assert.True(c.EqualsPartial(a, b)); + Assert.Equal(c.GetHashCode(a), c.GetHashCode(b)); + Assert.Equal(fullEqual, c.Equals(a, b)); + } + + private static void Equal(StringComparerBase c, string a, string b, bool fullEqual) + { + Assert.Equal(c.GetHashCode(a), c.GetHashCode(b)); + Assert.Equal(fullEqual, c.Equals(a, b)); + } + + private static void NotEqual(SubstringComparerBase c, string a, string b) + { + Assert.False(c.EqualsPartial(a, b)); + Assert.False(c.Equals(a, b)); + Assert.NotEqual(c.GetHashCode(a), c.GetHashCode(b)); + } + + private static void NotEqual(StringComparerBase c, string a, string b) + { + Assert.False(c.Equals(a, b)); + Assert.NotEqual(c.GetHashCode(a), c.GetHashCode(b)); + } + + [Fact] + public static void LeftHand() + { + var c = new LeftJustifiedSubstringComparer + { + Index = 0, + Count = 1 + }; + + Equal(c, "a", "a", true); + Equal(c, "a", "aa", false); + Equal(c, "a", "ab", false); + NotEqual(c, "a", "A"); + NotEqual(c, "a", "b"); + + c.Index = 1; + c.Count = 1; + Equal(c, "Aa", "Ba", false); + Equal(c, "Aa", "Baa", false); + Equal(c, "aa", "Bab", false); + Equal(c, "Aa", "Aa", true); + Equal(c, "Aab", "Aab", true); + NotEqual(c, "Aa", "BA"); + NotEqual(c, "Aa", "Bb"); + + c.Index = 1; + c.Count = 2; + Equal(c, "Aaa", "Baa", false); + Equal(c, "Aaa", "Baaa", false); + Equal(c, "aaa", "Baab", false); + Equal(c, "Aaa", "Aaa", true); + Equal(c, "Aaab", "Aaab", true); + NotEqual(c, "Aaa", "BaA"); + NotEqual(c, "Aaa", "Bab"); + } + + [Fact] + public static void LeftHandSingleChar() + { + var c = new LeftJustifiedSingleCharComparer + { + Index = 0, + Count = 1 + }; + + Equal(c, "a", "a", true); + Equal(c, "a", "aa", false); + Equal(c, "a", "ab", false); + NotEqual(c, "a", "A"); + NotEqual(c, "a", "b"); + + c.Index = 1; + c.Count = 1; + Equal(c, "Aa", "Ba", false); + Equal(c, "Aa", "Baa", false); + Equal(c, "aa", "Bab", false); + Equal(c, "Aa", "Aa", true); + Equal(c, "Aab", "Aab", true); + NotEqual(c, "Aa", "BA"); + NotEqual(c, "Aa", "Bb"); + } + + [Fact] + public static void LeftHandCaseInsensitive() + { + var c = new LeftJustifiedCaseInsensitiveSubstringComparer + { + Index = 0, + Count = 1 + }; + + Equal(c, "a", "a", true); + Equal(c, "a", "A", true); + Equal(c, "a", "aa", false); + Equal(c, "a", "AA", false); + Equal(c, "a", "ab", false); + Equal(c, "a", "AB", false); + NotEqual(c, "a", "b"); + + c.Index = 1; + c.Count = 1; + Equal(c, "Xa", "Ya", false); + Equal(c, "Xa", "YA", false); + Equal(c, "Xa", "Xa", true); + Equal(c, "Xa", "XA", true); + Equal(c, "Xa", "Yaa", false); + Equal(c, "Xa", "YAA", false); + Equal(c, "Xa", "Yab", false); + Equal(c, "Xa", "YAB", false); + NotEqual(c, "Xa", "Yb"); + } + + [Fact] + public static void LeftHandCaseInsensitiveAscii() + { + var c = new LeftJustifiedCaseInsensitiveAsciiSubstringComparer + { + Index = 0, + Count = 1 + }; + + Equal(c, "a", "a", true); + Equal(c, "a", "A", true); + Equal(c, "a", "aa", false); + Equal(c, "a", "AA", false); + Equal(c, "a", "ab", false); + Equal(c, "a", "AB", false); + NotEqual(c, "a", "b"); + + c.Index = 1; + c.Count = 1; + Equal(c, "Xa", "Ya", false); + Equal(c, "Xa", "YA", false); + Equal(c, "Xa", "Xa", true); + Equal(c, "Xa", "XA", true); + Equal(c, "Xa", "Yaa", false); + Equal(c, "Xa", "YAA", false); + Equal(c, "Xa", "Yab", false); + Equal(c, "Xa", "YAB", false); + NotEqual(c, "Xa", "Yb"); + } + + [Fact] + public static void RightHand() + { + var c = new RightJustifiedSubstringComparer + { + Index = -1, + Count = 1 + }; + + Equal(c, "a", "a", true); + Equal(c, "a", "aa", false); + Equal(c, "a", "ba", false); + NotEqual(c, "a", "A"); + NotEqual(c, "a", "b"); + + c.Index = -2; + c.Count = 1; + Equal(c, "aX", "aY", false); + Equal(c, "XaX", "YaY", false); + Equal(c, "XaX", "YYaY", false); + Equal(c, "XXaX", "YaY", false); + NotEqual(c, "XXaX", "YYa"); + + c.Index = -2; + c.Count = 2; + Equal(c, "aa", "aa", true); + Equal(c, "aa", "aaa", false); + Equal(c, "aa", "baa", false); + NotEqual(c, "aa", "AA"); + NotEqual(c, "aa", "bb"); + } + + [Fact] + public static void RightHandSingleChar() + { + var c = new RightJustifiedSingleCharComparer + { + Index = -1, + Count = 1 + }; + + Equal(c, "a", "a", true); + Equal(c, "a", "aa", false); + Equal(c, "a", "ba", false); + NotEqual(c, "a", "A"); + NotEqual(c, "a", "b"); + + c.Index = -2; + c.Count = 1; + Equal(c, "aX", "aY", false); + Equal(c, "XaX", "YaY", false); + Equal(c, "XaX", "YYaY", false); + Equal(c, "XXaX", "YaY", false); + NotEqual(c, "XXaX", "YYa"); + } + + [Fact] + public static void RightHandCaseInsensitive() + { + var c = new RightJustifiedCaseInsensitiveSubstringComparer + { + Index = -1, + Count = 1 + }; + + Equal(c, "a", "a", true); + Equal(c, "a", "aa", false); + Equal(c, "a", "ba", false); + Equal(c, "a", "A", true); + Equal(c, "a", "AA", false); + Equal(c, "a", "BA", false); + NotEqual(c, "a", "b"); + + c.Index = -2; + c.Count = 1; + Equal(c, "aX", "aY", false); + Equal(c, "XaX", "YaY", false); + Equal(c, "XaX", "YYaY", false); + Equal(c, "XXaX", "YaY", false); + Equal(c, "aX", "AY", false); + Equal(c, "XaX", "YAY", false); + Equal(c, "XaX", "YYAY", false); + Equal(c, "XXaX", "YAY", false); + NotEqual(c, "XXaX", "YYa"); + + c.Index = -2; + c.Count = 2; + Equal(c, "aa", "aa", true); + Equal(c, "aa", "aaa", false); + Equal(c, "aa", "baa", false); + Equal(c, "aa", "AA", true); + Equal(c, "aa", "AAA", false); + Equal(c, "aa", "bAA", false); + NotEqual(c, "aa", "bb"); + } + + [Fact] + public static void RightHandCaseInsensitiveAscii() + { + var c = new RightJustifiedCaseInsensitiveAsciiSubstringComparer + { + Index = -1, + Count = 1 + }; + + Equal(c, "a", "a", true); + Equal(c, "a", "aa", false); + Equal(c, "a", "ba", false); + Equal(c, "a", "A", true); + Equal(c, "a", "AA", false); + Equal(c, "a", "BA", false); + NotEqual(c, "a", "b"); + + c.Index = -2; + c.Count = 1; + Equal(c, "aX", "aY", false); + Equal(c, "XaX", "YaY", false); + Equal(c, "XaX", "YYaY", false); + Equal(c, "XXaX", "YaY", false); + Equal(c, "aX", "AY", false); + Equal(c, "XaX", "YAY", false); + Equal(c, "XaX", "YYAY", false); + Equal(c, "XXaX", "YAY", false); + NotEqual(c, "XXaX", "YYa"); + + c.Index = -2; + c.Count = 2; + Equal(c, "aa", "aa", true); + Equal(c, "aa", "aaa", false); + Equal(c, "aa", "baa", false); + Equal(c, "aa", "AA", true); + Equal(c, "aa", "AAA", false); + Equal(c, "aa", "bAA", false); + NotEqual(c, "aa", "bb"); + } + + [Fact] + public static void Full() + { + var c = new FullStringComparer(); + + Equal(c, "", "", true); + Equal(c, "A", "A", true); + Equal(c, "AA", "AA", true); + + NotEqual(c, "A", "AA"); + NotEqual(c, "AA", "A"); + } + + [Fact] + public static void FullCaseInsensitive() + { + var c = new FullCaseInsensitiveStringComparer(); + + Equal(c, "", "", true); + Equal(c, "A", "A", true); + Equal(c, "A", "a", true); + Equal(c, "a", "A", true); + Equal(c, "AA", "aa", true); + Equal(c, "aa", "AA", true); + + NotEqual(c, "A", "AA"); + NotEqual(c, "AA", "A"); + } + + [Fact] + public static void FullCaseInsensitiveAscii() + { + var c = new FullCaseInsensitiveAsciiStringComparer(); + + Equal(c, "", "", true); + Equal(c, "A", "A", true); + Equal(c, "A", "a", true); + Equal(c, "a", "A", true); + Equal(c, "AA", "aa", true); + Equal(c, "aa", "AA", true); + + NotEqual(c, "A", "AA"); + NotEqual(c, "AA", "A"); + } + } +} diff --git a/src/libraries/System.Collections.Immutable/tests/Frozen/FrozenDictionaryTest.cs b/src/libraries/System.Collections.Immutable/tests/Frozen/FrozenDictionaryTests.cs similarity index 100% rename from src/libraries/System.Collections.Immutable/tests/Frozen/FrozenDictionaryTest.cs rename to src/libraries/System.Collections.Immutable/tests/Frozen/FrozenDictionaryTests.cs diff --git a/src/libraries/System.Collections.Immutable/tests/System.Collections.Immutable.Tests.csproj b/src/libraries/System.Collections.Immutable/tests/System.Collections.Immutable.Tests.csproj index dd07319971ec6..0a72d5d53eaf6 100644 --- a/src/libraries/System.Collections.Immutable/tests/System.Collections.Immutable.Tests.csproj +++ b/src/libraries/System.Collections.Immutable/tests/System.Collections.Immutable.Tests.csproj @@ -40,7 +40,9 @@ - + + +