Skip to content

Commit e24e67a

Browse files
[release/5.0-rc2] Fix the globalization test. (#42226)
* Fix the globalization test. The failure is because Windows regresion. The change here is to avoid having the test fail because of that. The regression is tracked by Windows team to fix it. * Add a comment Co-authored-by: Tarek Mahmoud Sayed <tarekms@microsoft.com>
1 parent 0bc5cd5 commit e24e67a

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/libraries/System.Globalization/tests/CompareInfo/CompareInfoTests.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Buffers;
55
using System.Collections.Generic;
6+
using System.Runtime.InteropServices;
67
using System.Reflection;
78
using System.Text;
89
using Xunit;
@@ -359,6 +360,16 @@ public void SortKeyKanaTest(CompareInfo compareInfo, string string1, string stri
359360
SortKeyTest(compareInfo, string1, string2, options, expected);
360361
}
361362

363+
364+
[DllImport("kernel32", CharSet = CharSet.Unicode)]
365+
private static extern int CompareStringEx(string lpLocaleName, uint dwCmpFlags, string lpString1, int cchCount1, string lpString2, int cchCount2, IntPtr lpVersionInformation, IntPtr lpReserved, int lParam);
366+
private const int NORM_LINGUISTIC_CASING = 0x08000000; // use linguistic rules for casing
367+
368+
// Windows has introduced a regression when comparing 2 strings containing zero sort weight characters (e.g. “” and “\u200C”) when using the NORM_LINGUISTIC_CASING flag.
369+
// This code can be deleted after Windows fix the regression.
370+
private static bool WindowsVersionHasTheCompareStringRegression =>
371+
PlatformDetection.IsNlsGlobalization && CompareStringEx("", NORM_LINGUISTIC_CASING, "", 0, "\u200C", 1, IntPtr.Zero, IntPtr.Zero, 0) != 2;
372+
362373
[Theory]
363374
[MemberData(nameof(SortKey_TestData))]
364375
public void SortKeyTest(CompareInfo compareInfo, string string1, string string2, CompareOptions options, int expectedSign)
@@ -368,7 +379,11 @@ public void SortKeyTest(CompareInfo compareInfo, string string1, string string2,
368379

369380
Assert.Equal(expectedSign, Math.Sign(SortKey.Compare(sk1, sk2)));
370381
Assert.Equal(expectedSign == 0, sk1.Equals(sk2));
371-
Assert.Equal(Math.Sign(compareInfo.Compare(string1, string2, options)), Math.Sign(SortKey.Compare(sk1, sk2)));
382+
383+
if (!WindowsVersionHasTheCompareStringRegression)
384+
{
385+
Assert.Equal(Math.Sign(compareInfo.Compare(string1, string2, options)), Math.Sign(SortKey.Compare(sk1, sk2)));
386+
}
372387

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

0 commit comments

Comments
 (0)