Skip to content

Commit 84e1a40

Browse files
committed
Renamed UnicodeCharacterRange to UnicodeCodePointRange, and added a few unit tests.
1 parent 78baae7 commit 84e1a40

14 files changed

+98
-66
lines changed

UnicodeCharacterInspector/UnicodeCharacterInspector.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
44
<PropertyGroup>
5-
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Release</Configuration>
66
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
77
<ProjectGuid>{04E97F21-EF04-441F-83CF-2E71F3AAB089}</ProjectGuid>
88
<OutputType>WinExe</OutputType>

UnicodeInformation.Builder/UnicodeCharacterDataBuilder.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace System.Unicode.Builder
1010
{
1111
public sealed class UnicodeCharacterDataBuilder
1212
{
13-
private readonly UnicodeCharacterRange codePointRange;
13+
private readonly UnicodeCodePointRange codePointRange;
1414
private string name;
1515
private UnicodeCategory category = UnicodeCategory.OtherNotAssigned;
1616
private CanonicalCombiningClass canonicalCombiningClass;
@@ -29,7 +29,7 @@ public sealed class UnicodeCharacterDataBuilder
2929
private readonly List<UnicodeNameAlias> nameAliases = new List<UnicodeNameAlias>();
3030
private readonly List<int> crossRerefences = new List<int>();
3131

32-
public UnicodeCharacterRange CodePointRange { get { return codePointRange; } }
32+
public UnicodeCodePointRange CodePointRange { get { return codePointRange; } }
3333

3434
public string Name
3535
{
@@ -127,11 +127,11 @@ public CoreProperties CoreProperties
127127
public IList<int> CrossRerefences { get { return crossRerefences; } }
128128

129129
public UnicodeCharacterDataBuilder(int codePoint)
130-
: this(new UnicodeCharacterRange(codePoint))
130+
: this(new UnicodeCodePointRange(codePoint))
131131
{
132132
}
133133

134-
public UnicodeCharacterDataBuilder(UnicodeCharacterRange codePointRange)
134+
public UnicodeCharacterDataBuilder(UnicodeCodePointRange codePointRange)
135135
{
136136
this.codePointRange = codePointRange;
137137
this.category = UnicodeCategory.OtherNotAssigned;

UnicodeInformation.Builder/UnicodeDataProcessor.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ private static async Task ProcessUnicodeDataFile(IDataSource ucdSource, UnicodeI
6161

6262
while (reader.MoveToNextLine())
6363
{
64-
var codePoint = new UnicodeCharacterRange(int.Parse(reader.ReadField(), NumberStyles.HexNumber));
64+
var codePoint = new UnicodeCodePointRange(int.Parse(reader.ReadField(), NumberStyles.HexNumber));
6565

6666
string name = reader.ReadField();
6767

@@ -79,7 +79,7 @@ private static async Task ProcessUnicodeDataFile(IDataSource ucdSource, UnicodeI
7979
{
8080
if (rangeStartCodePoint < 0) throw new InvalidDataException("Invalid range data in UnicodeData.txt.");
8181

82-
codePoint = new UnicodeCharacterRange(rangeStartCodePoint, codePoint.LastCodePoint);
82+
codePoint = new UnicodeCodePointRange(rangeStartCodePoint, codePoint.LastCodePoint);
8383

8484
name = name.Substring(1, name.Length - 8).ToUpperInvariant(); // Upper-case the name in order to respect unicode naming scheme. (Spec says all names are uppercase ASCII)
8585

@@ -178,7 +178,7 @@ private static async Task ProcessPropListFile(IDataSource ucdSource, UnicodeInfo
178178
{
179179
ContributoryProperties property;
180180

181-
var range = UnicodeCharacterRange.Parse(reader.ReadTrimmedField());
181+
var range = UnicodeCodePointRange.Parse(reader.ReadTrimmedField());
182182
if (EnumHelper<ContributoryProperties>.TryGetNamedValue(reader.ReadTrimmedField(), out property))
183183
{
184184
builder.SetProperties(property, range);
@@ -195,7 +195,7 @@ private static async Task ProcessDerivedCorePropertiesFile(IDataSource ucdSource
195195
{
196196
CoreProperties property;
197197

198-
var range = UnicodeCharacterRange.Parse(reader.ReadTrimmedField());
198+
var range = UnicodeCodePointRange.Parse(reader.ReadTrimmedField());
199199
if (EnumHelper<CoreProperties>.TryGetNamedValue(reader.ReadTrimmedField(), out property))
200200
{
201201
builder.SetProperties(property, range);
@@ -364,7 +364,7 @@ private static async Task ProcessBlocksFile(IDataSource ucdSource, UnicodeInfoBu
364364
{
365365
while (reader.MoveToNextLine())
366366
{
367-
builder.AddBlockEntry(new UnicodeBlock(UnicodeCharacterRange.Parse(reader.ReadField()), reader.ReadTrimmedField()));
367+
builder.AddBlockEntry(new UnicodeBlock(UnicodeCodePointRange.Parse(reader.ReadField()), reader.ReadTrimmedField()));
368368
}
369369
}
370370
}

UnicodeInformation.Builder/UnicodeInfoBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public UnihanCharacterDataBuilder GetUnihan(int codePoint)
179179
}
180180
}
181181

182-
public void SetProperties(ContributoryProperties property, UnicodeCharacterRange codePointRange)
182+
public void SetProperties(ContributoryProperties property, UnicodeCodePointRange codePointRange)
183183
{
184184
int firstIndex = FindUcdCodePoint(codePointRange.FirstCodePoint);
185185
int lastIndex = FindUcdCodePoint(codePointRange.LastCodePoint);
@@ -210,7 +210,7 @@ public void SetProperties(ContributoryProperties property, UnicodeCharacterRange
210210
}
211211
}
212212

213-
public void SetProperties(CoreProperties property, UnicodeCharacterRange codePointRange)
213+
public void SetProperties(CoreProperties property, UnicodeCodePointRange codePointRange)
214214
{
215215
int firstIndex = FindUcdCodePoint(codePointRange.FirstCodePoint);
216216
int lastIndex = FindUcdCodePoint(codePointRange.LastCodePoint);

UnicodeInformation.Builder/UnicodeInformation.Builder.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
44
<PropertyGroup>
5-
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Release</Configuration>
66
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
77
<ProjectGuid>{8DFDEE6C-4F0D-4DE1-B346-574CB56D2B8B}</ProjectGuid>
88
<OutputType>Exe</OutputType>

UnicodeInformation.Tests/AssertEx.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace UnicodeInformation.Tests
99
{
1010
public static class AssertEx
1111
{
12-
public static void ThrowsExactly<TException>(Action action, string methodName)
12+
public static void ThrowsExactly<TException>(Action action, string methodName = null, string message = null)
1313
where TException : Exception
1414
{
1515
try
@@ -19,12 +19,12 @@ public static void ThrowsExactly<TException>(Action action, string methodName)
1919
catch (TException ex)
2020
{
2121
if (ex.GetType() != typeof(TException))
22-
Assert.Fail("The " + methodName + " method should throw an exception of type " + typeof(TException).Name + " but got " + ex.GetType().Name + ".");
22+
Assert.Fail(message ?? (methodName != null ? "The " + methodName + " method should throw an exception of type " : "Expected an exception of type ") + typeof(TException).Name + " but got " + ex.GetType().Name + ".");
2323
else
2424
return;
2525
}
2626

27-
Assert.Fail("The " + methodName + " method should throw an exception of type " + typeof(TException).Name + ".");
27+
Assert.Fail(message ?? (methodName != null ? "The " + methodName + " method should throw an exception of type " : "Expected an exception of type ") + typeof(TException).Name + ".");
2828
}
2929
}
3030
}

UnicodeInformation.Tests/UnicodeInfoBuilderTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ public async Task BuildAndWriteDataAsync()
148148
{
149149
var data = (await UnicodeDataProcessor.BuildDataAsync(ucdSource, unihanSource));
150150

151-
using (var stream = new DeflateStream(File.Create("ucd.dat"), CompressionLevel.Optimal, false))
151+
//using (var stream = new DeflateStream(File.Create("ucd.dat"), CompressionLevel.Optimal, false))
152+
using (var stream = File.Create("ucd.dat"))
152153
{
153154
data.WriteToStream(stream);
154155
}

UnicodeInformation.Tests/UnicodeInfoTests.cs

Lines changed: 66 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private static void AssertChar(int codePoint, UnicodeCategory category, string n
6868
{
6969
var info = UnicodeInfo.GetCharInfo(codePoint);
7070
Assert.AreEqual(codePoint, info.CodePoint);
71-
Assert.AreEqual(category, info.Category);
71+
Assert.AreEqual(category, info.Category);
7272
Assert.AreEqual(name, info.Name);
7373
Assert.AreEqual(block, UnicodeInfo.GetBlockName(codePoint));
7474
Assert.AreEqual(block, info.Block);
@@ -192,7 +192,64 @@ public void RadicalInfoTest()
192192

193193
AssertEx.ThrowsExactly<IndexOutOfRangeException>(() => UnicodeInfo.GetCjkRadicalInfo(0), nameof(UnicodeInfo.GetCjkRadicalInfo));
194194
AssertEx.ThrowsExactly<IndexOutOfRangeException>(() => UnicodeInfo.GetCjkRadicalInfo(215), nameof(UnicodeInfo.GetCjkRadicalInfo));
195-
}
195+
}
196+
197+
[TestMethod]
198+
public void CodePointRangeTest()
199+
{
200+
var fullRange = new UnicodeCodePointRange(0, 0x10FFFF);
201+
202+
Assert.AreEqual(0, fullRange.FirstCodePoint);
203+
Assert.AreEqual(0x10FFFF, fullRange.LastCodePoint);
204+
Assert.AreEqual(false, fullRange.IsSingleCodePoint);
205+
206+
var letterA = new UnicodeCodePointRange('A');
207+
208+
Assert.AreEqual('A', letterA.FirstCodePoint);
209+
Assert.AreEqual('A', letterA.LastCodePoint);
210+
Assert.AreEqual(true, letterA.IsSingleCodePoint);
211+
212+
AssertEx.ThrowsExactly<ArgumentOutOfRangeException>(() => new UnicodeCodePointRange(-1));
213+
AssertEx.ThrowsExactly<ArgumentOutOfRangeException>(() => new UnicodeCodePointRange(0x110000));
214+
AssertEx.ThrowsExactly<ArgumentOutOfRangeException>(() => new UnicodeCodePointRange(int.MaxValue));
215+
AssertEx.ThrowsExactly<ArgumentOutOfRangeException>(() => new UnicodeCodePointRange(-1, 10));
216+
AssertEx.ThrowsExactly<ArgumentOutOfRangeException>(() => new UnicodeCodePointRange(10, 0x110000));
217+
AssertEx.ThrowsExactly<ArgumentOutOfRangeException>(() => new UnicodeCodePointRange(-1, 0x110000));
218+
}
219+
220+
[TestMethod]
221+
public void CodePointRangeEnumerationTest()
222+
{
223+
const int start = 0xA3F;
224+
const int end = 0x105F;
225+
226+
// Generic test
227+
{
228+
int i = start;
229+
230+
foreach (int n in new UnicodeCodePointRange(start, end))
231+
{
232+
Assert.AreEqual(i++, n);
233+
}
234+
}
235+
236+
// Nongeneric test
237+
{
238+
int i = start;
239+
240+
var enumerator = (IEnumerator)new UnicodeCodePointRange(start, end).GetEnumerator();
241+
242+
while (enumerator.MoveNext())
243+
{
244+
Assert.AreEqual(i++, enumerator.Current);
245+
}
246+
247+
enumerator.Reset();
248+
249+
Assert.AreEqual(true, enumerator.MoveNext());
250+
Assert.AreEqual(start, enumerator.Current);
251+
}
252+
}
196253

197254
#if DEBUG
198255
[TestMethod]
@@ -209,41 +266,15 @@ public void UnihanCodePointPackingTest()
209266
for (int i = 0x2F800; i < 0x30000; ++i)
210267
Assert.AreEqual(i, UnihanCharacterData.UnpackCodePoint(UnihanCharacterData.PackCodePoint(i)));
211268

212-
try
213-
{
214-
UnihanCharacterData.PackCodePoint(0xA000);
215-
Assert.Fail("The PackCodePoint method should fail for code points outside of the valid range.");
216-
}
217-
catch (ArgumentOutOfRangeException)
218-
{
219-
}
220-
221-
try
222-
{
223-
UnihanCharacterData.PackCodePoint(0xFB00);
224-
Assert.Fail("The PackCodePoint method should fail for code points outside of the valid range.");
225-
}
226-
catch (ArgumentOutOfRangeException)
227-
{
228-
}
269+
const string packCodePointErrorMessage = "The PackCodePoint method should fail for code points outside of the valid range.";
270+
const string unpackCodePointErrorMessage = "The PackCodePoint method should fail for values outside of the valid range.";
229271

230-
try
231-
{
232-
UnihanCharacterData.PackCodePoint(0x30000);
233-
Assert.Fail("The PackCodePoint method should fail for code points outside of the valid range.");
234-
}
235-
catch (ArgumentOutOfRangeException)
236-
{
237-
}
272+
AssertEx.ThrowsExactly<ArgumentOutOfRangeException>(() => UnihanCharacterData.PackCodePoint(0xA000), nameof(UnihanCharacterData.PackCodePoint), packCodePointErrorMessage);
273+
AssertEx.ThrowsExactly<ArgumentOutOfRangeException>(() => UnihanCharacterData.PackCodePoint(0xFB00), nameof(UnihanCharacterData.PackCodePoint), packCodePointErrorMessage);
274+
AssertEx.ThrowsExactly<ArgumentOutOfRangeException>(() => UnihanCharacterData.PackCodePoint(0x30000), nameof(UnihanCharacterData.PackCodePoint), packCodePointErrorMessage);
238275

239-
try
240-
{
241-
UnihanCharacterData.UnpackCodePoint(0x20000);
242-
Assert.Fail("The UnpackCodePoint method should fail for code points outside of the valid range.");
243-
}
244-
catch (ArgumentOutOfRangeException)
245-
{
246-
}
276+
AssertEx.ThrowsExactly<ArgumentOutOfRangeException>(() => UnihanCharacterData.UnpackCodePoint(-1), nameof(UnihanCharacterData.UnpackCodePoint), unpackCodePointErrorMessage);
277+
AssertEx.ThrowsExactly<ArgumentOutOfRangeException>(() => UnihanCharacterData.UnpackCodePoint(0x20000), nameof(UnihanCharacterData.UnpackCodePoint), unpackCodePointErrorMessage);
247278
}
248279
#endif
249280
}

UnicodeInformation.Tests/UnicodeInformation.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<PropertyGroup>
4-
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Release</Configuration>
55
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
66
<ProjectGuid>{50337426-E884-4394-9E1A-F6F7A555F5D9}</ProjectGuid>
77
<OutputType>Library</OutputType>

UnicodeInformation/UnicodeBlock.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ namespace System.Unicode
88
{
99
public struct UnicodeBlock
1010
{
11-
public readonly UnicodeCharacterRange CodePointRange;
11+
public readonly UnicodeCodePointRange CodePointRange;
1212
public readonly string Name;
1313

14-
internal UnicodeBlock(UnicodeCharacterRange codePointRange, string name)
14+
internal UnicodeBlock(UnicodeCodePointRange codePointRange, string name)
1515
{
1616
this.CodePointRange = codePointRange;
1717
this.Name = name;

0 commit comments

Comments
 (0)