예제 #1
0
        public void SortKeyMiscTest()
        {
            CompareInfo ci = new CultureInfo("en-US").CompareInfo;
            string      s1 = "abc";
            string      s2 = "ABC";

            SortKey sk1 = ci.GetSortKey(s1);
            SortKey sk2 = ci.GetSortKey(s1);

            SortKey sk3 = ci.GetSortKey(s2);
            SortKey sk4 = ci.GetSortKey(s2, CompareOptions.IgnoreCase);
            SortKey sk5 = ci.GetSortKey(s1, CompareOptions.IgnoreCase);

            Assert.Equal(sk2, sk1);
            Assert.Equal(sk2.GetHashCode(), sk1.GetHashCode());
            Assert.Equal(sk2.KeyData, sk1.KeyData);

            Assert.NotEqual(sk3, sk1);
            Assert.NotEqual(sk3.GetHashCode(), sk1.GetHashCode());
            Assert.NotEqual(sk3.KeyData, sk1.KeyData);

            Assert.NotEqual(sk4, sk3);
            Assert.NotEqual(sk4.GetHashCode(), sk3.GetHashCode());
            Assert.NotEqual(sk4.KeyData, sk3.KeyData);

            Assert.Equal(sk4, sk5);
            Assert.Equal(sk4.GetHashCode(), sk5.GetHashCode());
            Assert.Equal(sk4.KeyData, sk5.KeyData);

            Assert.False(sk1.Equals(null));
            Assert.True(sk1.Equals(sk1));

            AssertExtensions.Throws <ArgumentNullException>("source", () => ci.GetSortKey(null));
            AssertExtensions.Throws <ArgumentException>("options", () => ci.GetSortKey(s1, CompareOptions.Ordinal));
        }
예제 #2
0
        public void SortKeyTest(CompareInfo compareInfo, string string1, string string2, CompareOptions options, int expectedSign)
        {
            SortKey sk1 = compareInfo.GetSortKey(string1, options);
            SortKey sk2 = compareInfo.GetSortKey(string2, options);

            Assert.Equal(expectedSign, Math.Sign(SortKey.Compare(sk1, sk2)));
            Assert.Equal(expectedSign == 0, sk1.Equals(sk2));

            if (!WindowsVersionHasTheCompareStringRegression)
            {
                Assert.Equal(Math.Sign(compareInfo.Compare(string1, string2, options)), Math.Sign(SortKey.Compare(sk1, sk2)));
            }

            Assert.Equal(compareInfo.GetHashCode(string1, options), sk1.GetHashCode());
            Assert.Equal(compareInfo.GetHashCode(string2, options), sk2.GetHashCode());

            Assert.Equal(string1, sk1.OriginalString);
            Assert.Equal(string2, sk2.OriginalString);

            // Now try the span-based versions - use BoundedMemory to detect buffer overruns

            RunSpanSortKeyTest(compareInfo, string1, options, sk1.KeyData);
            RunSpanSortKeyTest(compareInfo, string2, options, sk2.KeyData);

            unsafe static void RunSpanSortKeyTest(CompareInfo compareInfo, ReadOnlySpan <char> source, CompareOptions options, byte[] expectedSortKey)
예제 #3
0
    public static void Main()
    {
        // Creates two identical en-US cultures and one de-DE culture.
        CompareInfo myComp_enUS1 = new CultureInfo("en-US", false).CompareInfo;
        CompareInfo myComp_enUS2 = new CultureInfo("en-US", false).CompareInfo;
        CompareInfo myComp_deDE  = new CultureInfo("de-DE", false).CompareInfo;

        // Creates the base SortKey to compare with all the others.
        SortKey mySK1 = myComp_enUS1.GetSortKey("cant", CompareOptions.StringSort);
        // Creates a SortKey that is derived exactly the same way as the base SortKey.
        SortKey mySK2 = myComp_enUS1.GetSortKey("cant", CompareOptions.StringSort);
        // Creates a SortKey that uses word sort, which is the default sort.
        SortKey mySK3 = myComp_enUS1.GetSortKey("cant");
        // Creates a SortKey for a different string.
        SortKey mySK4 = myComp_enUS1.GetSortKey("can't", CompareOptions.StringSort);
        // Creates a SortKey from a different CompareInfo with the same culture.
        SortKey mySK5 = myComp_enUS2.GetSortKey("cant", CompareOptions.StringSort);
        // Creates a SortKey from a different CompareInfo with a different culture.
        SortKey mySK6 = myComp_deDE.GetSortKey("cant", CompareOptions.StringSort);

        // Compares the base SortKey with itself.
        Console.WriteLine("Comparing the base SortKey with itself: {0}", mySK1.Equals(mySK1));
        Console.WriteLine();

        // Prints the header for the table.
        Console.WriteLine("CompareInfo   Culture      OriginalString   CompareOptions   Equals()");

        // Compares the base SortKey with a SortKey that is
        //    created from the same CompareInfo with the same string and the same CompareOptions.
        Console.WriteLine("same          same         same             same             {0}", mySK1.Equals(mySK2));

        // Compares the base SortKey with a SortKey that is
        //    created from the same CompareInfo with the same string but with different CompareOptions.
        Console.WriteLine("same          same         same             different        {0}", mySK1.Equals(mySK3));

        // Compares the base SortKey with a SortKey that is
        //    created from the same CompareInfo with the different string
        //    but with the same CompareOptions.
        Console.WriteLine("same          same         different        same             {0}", mySK1.Equals(mySK4));

        // Compares the base SortKey with a SortKey that is
        //    created from a different CompareInfo (same culture)
        //    with the same string and the same CompareOptions.
        Console.WriteLine("different     same         same             same             {0}", mySK1.Equals(mySK5));

        // Compares the base SortKey with a SortKey that is
        //    created from a different CompareInfo (different culture)
        //    with the same string and the same CompareOptions.
        Console.WriteLine("different     different    same             same             {0}", mySK1.Equals(mySK6));
    }
예제 #4
0
        public void SortKeyTest(CompareInfo compareInfo, string string1, string string2, CompareOptions options, int expectedSign)
        {
            SortKey sk1 = compareInfo.GetSortKey(string1, options);
            SortKey sk2 = compareInfo.GetSortKey(string2, options);

            Assert.Equal(expectedSign, Math.Sign(SortKey.Compare(sk1, sk2)));
            Assert.Equal(expectedSign == 0, sk1.Equals(sk2));
            Assert.Equal(Math.Sign(compareInfo.Compare(string1, string2, options)), Math.Sign(SortKey.Compare(sk1, sk2)));

            Assert.Equal(compareInfo.GetHashCode(string1, options), sk1.GetHashCode());
            Assert.Equal(compareInfo.GetHashCode(string2, options), sk2.GetHashCode());

            Assert.Equal(string1, sk1.OriginalString);
            Assert.Equal(string2, sk2.OriginalString);
        }
예제 #5
0
 public override Boolean Equals(ANotification other)
 => SortKey.Equals(other.SortKey);