Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 921ca44

Browse files
dotnet-botvenkat-raman251
authored andcommitted
updated changes for system.globalization
1 parent fc250c1 commit 921ca44

File tree

143 files changed

+7917
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

143 files changed

+7917
-0
lines changed
Binary file not shown.
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.Text;
6+
using System.Globalization;
7+
using Xunit;
8+
9+
namespace System.Globalization.Tests
10+
{
11+
public class CompareInfoCompare2
12+
{
13+
public static string[] InterestingStrings = new string[] { null, "", "a", "1", "-", "A", "!", "abc", "aBc", "a\u0400Bc", "I", "i", "\u0130", "\u0131", "A", "\uFF21", "\uFE57" };
14+
15+
// PosTest1: Compare interesting strings ordinally
16+
[Fact]
17+
public void CompareInterestingStrings()
18+
{
19+
foreach (string s in InterestingStrings)
20+
{
21+
foreach (string r in InterestingStrings)
22+
{
23+
TestStrings(s, r);
24+
}
25+
}
26+
}
27+
28+
// PosTest2: Compare many characters
29+
[Fact]
30+
public void CompareManyCharacters()
31+
{
32+
for (int i = 0; i < 40; i++)
33+
{
34+
char c = TestLibrary.Generator.GetChar(-55);
35+
Assert.Equal(0, CultureInfo.CurrentCulture.CompareInfo.Compare(new string(new char[] { c }), new string(new char[] { c }), CompareOptions.Ordinal));
36+
for (int j = 0; j < (int)c; j++)
37+
{
38+
int compareResult = CultureInfo.CurrentCulture.CompareInfo.Compare(new string(new char[] { c }), new string(new char[] { (char)j }), CompareOptions.Ordinal);
39+
if (compareResult != 0) compareResult = compareResult / Math.Abs(compareResult);
40+
Assert.Equal(1, compareResult);
41+
}
42+
}
43+
}
44+
45+
// PosTest3: Compare many strings
46+
[Fact]
47+
public void CompareManyStrings()
48+
{
49+
for (int i = 0; i < 1000; i++)
50+
{
51+
string str1 = TestLibrary.Generator.GetString(-55, false, 5, 20);
52+
string str2 = TestLibrary.Generator.GetString(-55, false, 5, 20);
53+
Assert.Equal(0, CultureInfo.CurrentCulture.CompareInfo.Compare(str1, str1, CompareOptions.Ordinal));
54+
Assert.Equal(0, CultureInfo.CurrentCulture.CompareInfo.Compare(str2, str2, CompareOptions.Ordinal));
55+
TestStrings(str1, str2);
56+
}
57+
}
58+
59+
// PosTest4: Test Hungarian Culture
60+
[Fact]
61+
public void TestHungarianCulture()
62+
{
63+
CultureInfo oldCi = CultureInfo.CurrentCulture;
64+
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("hu-HU");
65+
TestStrings("dzsdzs", "ddzs");
66+
CultureInfo.DefaultThreadCurrentCulture = oldCi;
67+
TestStrings("\u00C0nimal", "A\u0300nimal");
68+
}
69+
70+
private void TestStrings(string str1, string str2)
71+
{
72+
int expectValue = PredictValue(str1, str2);
73+
int actualValue = CultureInfo.CurrentCulture.CompareInfo.Compare(str1, str2, CompareOptions.Ordinal);
74+
if (expectValue != 0) expectValue = expectValue / Math.Abs(expectValue);
75+
if (actualValue != 0) actualValue = actualValue / Math.Abs(actualValue);
76+
77+
Assert.Equal(expectValue, actualValue);
78+
}
79+
80+
private int PredictValue(string str1, string str2)
81+
{
82+
if (str1 == null)
83+
{
84+
if (str2 == null) return 0;
85+
else return -1;
86+
}
87+
if (str2 == null) return 1;
88+
89+
for (int i = 0; i < str1.Length; i++)
90+
{
91+
if (i >= str2.Length) return 1;
92+
if ((int)str1[i] > (int)str2[i]) return 1;
93+
if ((int)str1[i] < (int)str2[i]) return -1;
94+
}
95+
96+
if (str2.Length > str1.Length) return -1;
97+
98+
return 0;
99+
}
100+
}
101+
}
Binary file not shown.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.Text;
6+
using System.Globalization;
7+
using Xunit;
8+
9+
namespace System.Globalization.Tests
10+
{
11+
public class CompareInfoIndexOf2
12+
{
13+
public static string[] InterestingStrings = new string[] { "", "a", "1", "-", "A", "!", "abc", "aBc", "a\u0400Bc", "I", "i", "\u0130", "\u0131", "A", "\uFF21", "\uFE57" };
14+
15+
// PosTest1: Compare interesting strings ordinally
16+
[Fact]
17+
public void CompareInterestingStrings()
18+
{
19+
foreach (string s in InterestingStrings)
20+
{
21+
foreach (string r in InterestingStrings)
22+
{
23+
TestStrings(s, r);
24+
}
25+
}
26+
}
27+
28+
// PosTest2: Compare many strings
29+
[Fact]
30+
public void CompareManyStrings()
31+
{
32+
for (int i = 0; i < 1000; i++)
33+
{
34+
string str1 = TestLibrary.Generator.GetString(-55, false, 5, 20);
35+
string str2 = TestLibrary.Generator.GetString(-55, false, 5, 20);
36+
Assert.Equal(0, CultureInfo.CurrentCulture.CompareInfo.IndexOf(str1, str1, CompareOptions.Ordinal));
37+
Assert.Equal(0, CultureInfo.CurrentCulture.CompareInfo.IndexOf(str2, str2, CompareOptions.Ordinal));
38+
TestStrings(str1, str2);
39+
TestStrings(str1 + str2, str2);
40+
}
41+
}
42+
43+
// PosTest3: Specific regression cases
44+
[Fact]
45+
public void RegressionTests()
46+
{
47+
CultureInfo oldCi = CultureInfo.CurrentCulture;
48+
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("hu-HU");
49+
TestStrings("Foodzsdzsbar", "ddzs");
50+
CultureInfo.DefaultThreadCurrentCulture = oldCi;
51+
TestStrings("\u00C0nimal", "A\u0300");
52+
}
53+
54+
private void TestStrings(string str1, string str2)
55+
{
56+
int expectValue = PredictValue(str1, str2);
57+
int actualValue = CultureInfo.CurrentCulture.CompareInfo.IndexOf(str1, str2, CompareOptions.Ordinal);
58+
Assert.Equal(expectValue, actualValue);
59+
}
60+
61+
private int PredictValue(string str1, string str2)
62+
{
63+
if (str1 == null)
64+
{
65+
if (str2 == null) return 0;
66+
else return -1;
67+
}
68+
if (str2 == null) return -1;
69+
70+
if (str2.Length > str1.Length) return -1;
71+
72+
for (int i = 0; i <= str1.Length - str2.Length; i++)
73+
{
74+
bool match = true;
75+
for (int j = 0; j < str2.Length; j++)
76+
{
77+
if ((int)str1[i + j] != (int)str2[j])
78+
{
79+
match = false;
80+
break;
81+
}
82+
}
83+
if (match) return i;
84+
}
85+
return -1;
86+
}
87+
}
88+
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.Globalization;
6+
using Xunit;
7+
8+
namespace System.Globalization.Tests
9+
{
10+
public class CompareInfoTest
11+
{
12+
[Fact]
13+
public void TestCompareInfo()
14+
{
15+
CompareInfo ciENG = CompareInfo.GetCompareInfo("en-US");
16+
CompareInfo ciFR = CompareInfo.GetCompareInfo("fr-FR");
17+
18+
Assert.True(ciENG.Name.Equals("en-US", StringComparison.CurrentCultureIgnoreCase));
19+
Assert.NotEqual(ciENG.GetHashCode(), ciFR.GetHashCode());
20+
Assert.NotEqual(ciENG, ciFR);
21+
}
22+
23+
[Fact]
24+
public void CompareInfoIndexTest1()
25+
{
26+
// Creates CompareInfo for the InvariantCulture.
27+
CompareInfo myComp = CultureInfo.InvariantCulture.CompareInfo;
28+
29+
// iS is the starting index of the substring.
30+
int iS = 8;
31+
// iL is the length of the substring.
32+
int iL = 18;
33+
34+
// Searches for the ligature Æ.
35+
String myStr = "Is AE or ae the same as \u00C6 or \u00E6?";
36+
37+
Assert.Equal(myComp.IndexOf(myStr, "AE", iS, iL), 24);
38+
Assert.Equal(myComp.LastIndexOf(myStr, "AE", iS + iL - 1, iL), 24);
39+
40+
Assert.Equal(myComp.IndexOf(myStr, "ae", iS, iL), 9);
41+
Assert.Equal(myComp.LastIndexOf(myStr, "ae", iS + iL - 1, iL), 9);
42+
43+
Assert.Equal(myComp.IndexOf(myStr, '\u00C6', iS, iL), 24);
44+
Assert.Equal(myComp.LastIndexOf(myStr, '\u00C6', iS + iL - 1, iL), 24);
45+
46+
Assert.Equal(myComp.IndexOf(myStr, '\u00E6', iS, iL), 9);
47+
Assert.Equal(myComp.LastIndexOf(myStr, '\u00E6', iS + iL - 1, iL), 9);
48+
49+
Assert.Equal(myComp.IndexOf(myStr, "AE", iS, iL, CompareOptions.Ordinal), -1);
50+
Assert.Equal(myComp.LastIndexOf(myStr, "AE", iS + iL - 1, iL, CompareOptions.Ordinal), -1);
51+
52+
Assert.Equal(myComp.IndexOf(myStr, "ae", iS, iL, CompareOptions.Ordinal), 9);
53+
Assert.Equal(myComp.LastIndexOf(myStr, "ae", iS + iL - 1, iL, CompareOptions.Ordinal), 9);
54+
55+
Assert.Equal(myComp.IndexOf(myStr, '\u00C6', iS, iL, CompareOptions.Ordinal), 24);
56+
Assert.Equal(myComp.LastIndexOf(myStr, '\u00C6', iS + iL - 1, iL, CompareOptions.Ordinal), 24);
57+
58+
Assert.Equal(myComp.IndexOf(myStr, '\u00E6', iS, iL, CompareOptions.Ordinal), -1);
59+
Assert.Equal(myComp.LastIndexOf(myStr, '\u00E6', iS + iL - 1, iL, CompareOptions.Ordinal), -1);
60+
61+
Assert.Equal(myComp.IndexOf(myStr, "AE", iS, iL, CompareOptions.IgnoreCase), 9);
62+
Assert.Equal(myComp.LastIndexOf(myStr, "AE", iS + iL - 1, iL, CompareOptions.IgnoreCase), 24);
63+
64+
Assert.Equal(myComp.IndexOf(myStr, "ae", iS, iL, CompareOptions.IgnoreCase), 9);
65+
Assert.Equal(myComp.LastIndexOf(myStr, "ae", iS + iL - 1, iL, CompareOptions.IgnoreCase), 24);
66+
67+
Assert.Equal(myComp.IndexOf(myStr, '\u00C6', iS, iL, CompareOptions.IgnoreCase), 9);
68+
Assert.Equal(myComp.LastIndexOf(myStr, '\u00C6', iS + iL - 1, iL, CompareOptions.IgnoreCase), 24);
69+
70+
Assert.Equal(myComp.IndexOf(myStr, '\u00E6', iS, iL, CompareOptions.IgnoreCase), 9);
71+
Assert.Equal(myComp.LastIndexOf(myStr, '\u00E6', iS + iL - 1, iL, CompareOptions.IgnoreCase), 24);
72+
}
73+
74+
[Fact]
75+
public void CompareInfoIndexTest2()
76+
{
77+
// Creates CompareInfo for the InvariantCulture.
78+
CompareInfo myComp = CultureInfo.InvariantCulture.CompareInfo;
79+
80+
// iS is the starting index of the substring.
81+
int iS = 8;
82+
// iL is the length of the substring.
83+
int iL = 18;
84+
85+
// Searches for the combining character sequence Latin capital letter U with diaeresis or Latin small letter u with diaeresis.
86+
string myStr = "Is \u0055\u0308 or \u0075\u0308 the same as \u00DC or \u00FC?";
87+
88+
Assert.Equal(myComp.IndexOf(myStr, "U\u0308", iS, iL), 24);
89+
Assert.Equal(myComp.LastIndexOf(myStr, "U\u0308", iS + iL - 1, iL), 24);
90+
91+
Assert.Equal(myComp.IndexOf(myStr, "u\u0308", iS, iL), 9);
92+
Assert.Equal(myComp.LastIndexOf(myStr, "u\u0308", iS + iL - 1, iL), 9);
93+
94+
Assert.Equal(myComp.IndexOf(myStr, '\u00DC', iS, iL), 24);
95+
Assert.Equal(myComp.LastIndexOf(myStr, '\u00DC', iS + iL - 1, iL), 24);
96+
97+
Assert.Equal(myComp.IndexOf(myStr, '\u00FC', iS, iL), 9);
98+
Assert.Equal(myComp.LastIndexOf(myStr, '\u00FC', iS + iL - 1, iL), 9);
99+
100+
Assert.Equal(myComp.IndexOf(myStr, "U\u0308", iS, iL, CompareOptions.Ordinal), -1);
101+
Assert.Equal(myComp.LastIndexOf(myStr, "U\u0308", iS + iL - 1, iL, CompareOptions.Ordinal), -1);
102+
103+
Assert.Equal(myComp.IndexOf(myStr, "u\u0308", iS, iL, CompareOptions.Ordinal), 9);
104+
Assert.Equal(myComp.LastIndexOf(myStr, "u\u0308", iS + iL - 1, iL, CompareOptions.Ordinal), 9);
105+
106+
Assert.Equal(myComp.IndexOf(myStr, '\u00DC', iS, iL, CompareOptions.Ordinal), 24);
107+
Assert.Equal(myComp.LastIndexOf(myStr, '\u00DC', iS + iL - 1, iL, CompareOptions.Ordinal), 24);
108+
109+
Assert.Equal(myComp.IndexOf(myStr, '\u00FC', iS, iL, CompareOptions.Ordinal), -1);
110+
Assert.Equal(myComp.LastIndexOf(myStr, '\u00FC', iS + iL - 1, iL, CompareOptions.Ordinal), -1);
111+
112+
Assert.Equal(myComp.IndexOf(myStr, "U\u0308", iS, iL, CompareOptions.IgnoreCase), 9);
113+
Assert.Equal(myComp.LastIndexOf(myStr, "U\u0308", iS + iL - 1, iL, CompareOptions.IgnoreCase), 24);
114+
115+
Assert.Equal(myComp.IndexOf(myStr, "u\u0308", iS, iL, CompareOptions.IgnoreCase), 9);
116+
Assert.Equal(myComp.LastIndexOf(myStr, "u\u0308", iS + iL - 1, iL, CompareOptions.IgnoreCase), 24);
117+
118+
Assert.Equal(myComp.IndexOf(myStr, '\u00DC', iS, iL, CompareOptions.IgnoreCase), 9);
119+
Assert.Equal(myComp.LastIndexOf(myStr, '\u00DC', iS + iL - 1, iL, CompareOptions.IgnoreCase), 24);
120+
121+
Assert.Equal(myComp.IndexOf(myStr, '\u00FC', iS, iL, CompareOptions.IgnoreCase), 9);
122+
Assert.Equal(myComp.LastIndexOf(myStr, '\u00FC', iS + iL - 1, iL, CompareOptions.IgnoreCase), 24);
123+
}
124+
}
125+
}

0 commit comments

Comments
 (0)