示例#1
0
        public void IsWellKnownOrdinalComparer_TestCases()
        {
            CompareInfo ci_enUS = CompareInfo.GetCompareInfo("en-US");

            // First, instantiate and test the comparers directly

            RunTest(null, false, false);
            RunTest(EqualityComparer <string> .Default, true, false);  // EC<string>.Default is Ordinal-equivalent
            RunTest(EqualityComparer <object> .Default, false, false); // EC<object>.Default isn't a string comparer
            RunTest(StringComparer.Ordinal, true, false);
            RunTest(StringComparer.OrdinalIgnoreCase, true, true);
            RunTest(StringComparer.InvariantCulture, false, false);                         // not ordinal
            RunTest(StringComparer.InvariantCultureIgnoreCase, false, false);               // not ordinal
            RunTest(GetNonRandomizedComparer("WrappedAroundDefaultComparer"), true, false); // EC<string>.Default is Ordinal-equivalent
            RunTest(GetNonRandomizedComparer("WrappedAroundStringComparerOrdinal"), true, false);
            RunTest(GetNonRandomizedComparer("WrappedAroundStringComparerOrdinalIgnoreCase"), true, true);
            RunTest(new CustomStringComparer(), false, false);                     // not an inbox comparer
            RunTest(ci_enUS.GetStringComparer(CompareOptions.None), false, false); // linguistic
            RunTest(ci_enUS.GetStringComparer(CompareOptions.Ordinal), true, false);
            RunTest(ci_enUS.GetStringComparer(CompareOptions.OrdinalIgnoreCase), true, true);

            // Then, make sure that this API works with common collection types

            RunTest(new Dictionary <string, object>().Comparer, true, false);
            RunTest(new Dictionary <string, object>(StringComparer.Ordinal).Comparer, true, false);
            RunTest(new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase).Comparer, true, true);
            RunTest(new Dictionary <string, object>(StringComparer.InvariantCulture).Comparer, false, false);
            RunTest(new Dictionary <string, object>(StringComparer.InvariantCultureIgnoreCase).Comparer, false, false);

            RunTest(new HashSet <string>().Comparer, true, false);
            RunTest(new HashSet <string>(StringComparer.Ordinal).Comparer, true, false);
            RunTest(new HashSet <string>(StringComparer.OrdinalIgnoreCase).Comparer, true, true);
            RunTest(new HashSet <string>(StringComparer.InvariantCulture).Comparer, false, false);
            RunTest(new HashSet <string>(StringComparer.InvariantCultureIgnoreCase).Comparer, false, false);
        public static void CompareInfoThrows()
        {
            Assert.Throws <ArgumentNullException>("compareInfo", () => { CompareInfo info = null; info.GetStringComparer(CompareOptions.None); });

            Assert.Throws <ArgumentException>("options", () => new CultureInfo("tr-TR").CompareInfo.GetStringComparer((CompareOptions)0xFFFF));
            Assert.Throws <ArgumentException>("options", () => new CultureInfo("tr-TR").CompareInfo.GetStringComparer(CompareOptions.Ordinal | CompareOptions.IgnoreCase));
            Assert.Throws <ArgumentException>("options", () => new CultureInfo("tr-TR").CompareInfo.GetStringComparer(CompareOptions.OrdinalIgnoreCase | CompareOptions.IgnoreCase));

            Assert.Throws <ArgumentNullException>("obj", () => new CultureInfo("tr-TR").CompareInfo.GetStringComparer(CompareOptions.None).GetHashCode(null));
        }