Renamed UnicodeCharacterRange to UnicodeCodePointRange, and added a few unit tests.
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Release</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{04E97F21-EF04-441F-83CF-2E71F3AAB089}</ProjectGuid>
|
||||
<OutputType>WinExe</OutputType>
|
||||
|
||||
@@ -10,7 +10,7 @@ namespace System.Unicode.Builder
|
||||
{
|
||||
public sealed class UnicodeCharacterDataBuilder
|
||||
{
|
||||
private readonly UnicodeCharacterRange codePointRange;
|
||||
private readonly UnicodeCodePointRange codePointRange;
|
||||
private string name;
|
||||
private UnicodeCategory category = UnicodeCategory.OtherNotAssigned;
|
||||
private CanonicalCombiningClass canonicalCombiningClass;
|
||||
@@ -29,7 +29,7 @@ namespace System.Unicode.Builder
|
||||
private readonly List<UnicodeNameAlias> nameAliases = new List<UnicodeNameAlias>();
|
||||
private readonly List<int> crossRerefences = new List<int>();
|
||||
|
||||
public UnicodeCharacterRange CodePointRange { get { return codePointRange; } }
|
||||
public UnicodeCodePointRange CodePointRange { get { return codePointRange; } }
|
||||
|
||||
public string Name
|
||||
{
|
||||
@@ -127,11 +127,11 @@ namespace System.Unicode.Builder
|
||||
public IList<int> CrossRerefences { get { return crossRerefences; } }
|
||||
|
||||
public UnicodeCharacterDataBuilder(int codePoint)
|
||||
: this(new UnicodeCharacterRange(codePoint))
|
||||
: this(new UnicodeCodePointRange(codePoint))
|
||||
{
|
||||
}
|
||||
|
||||
public UnicodeCharacterDataBuilder(UnicodeCharacterRange codePointRange)
|
||||
public UnicodeCharacterDataBuilder(UnicodeCodePointRange codePointRange)
|
||||
{
|
||||
this.codePointRange = codePointRange;
|
||||
this.category = UnicodeCategory.OtherNotAssigned;
|
||||
|
||||
@@ -61,7 +61,7 @@ namespace System.Unicode.Builder
|
||||
|
||||
while (reader.MoveToNextLine())
|
||||
{
|
||||
var codePoint = new UnicodeCharacterRange(int.Parse(reader.ReadField(), NumberStyles.HexNumber));
|
||||
var codePoint = new UnicodeCodePointRange(int.Parse(reader.ReadField(), NumberStyles.HexNumber));
|
||||
|
||||
string name = reader.ReadField();
|
||||
|
||||
@@ -79,7 +79,7 @@ namespace System.Unicode.Builder
|
||||
{
|
||||
if (rangeStartCodePoint < 0) throw new InvalidDataException("Invalid range data in UnicodeData.txt.");
|
||||
|
||||
codePoint = new UnicodeCharacterRange(rangeStartCodePoint, codePoint.LastCodePoint);
|
||||
codePoint = new UnicodeCodePointRange(rangeStartCodePoint, codePoint.LastCodePoint);
|
||||
|
||||
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)
|
||||
|
||||
@@ -178,7 +178,7 @@ namespace System.Unicode.Builder
|
||||
{
|
||||
ContributoryProperties property;
|
||||
|
||||
var range = UnicodeCharacterRange.Parse(reader.ReadTrimmedField());
|
||||
var range = UnicodeCodePointRange.Parse(reader.ReadTrimmedField());
|
||||
if (EnumHelper<ContributoryProperties>.TryGetNamedValue(reader.ReadTrimmedField(), out property))
|
||||
{
|
||||
builder.SetProperties(property, range);
|
||||
@@ -195,7 +195,7 @@ namespace System.Unicode.Builder
|
||||
{
|
||||
CoreProperties property;
|
||||
|
||||
var range = UnicodeCharacterRange.Parse(reader.ReadTrimmedField());
|
||||
var range = UnicodeCodePointRange.Parse(reader.ReadTrimmedField());
|
||||
if (EnumHelper<CoreProperties>.TryGetNamedValue(reader.ReadTrimmedField(), out property))
|
||||
{
|
||||
builder.SetProperties(property, range);
|
||||
@@ -364,7 +364,7 @@ namespace System.Unicode.Builder
|
||||
{
|
||||
while (reader.MoveToNextLine())
|
||||
{
|
||||
builder.AddBlockEntry(new UnicodeBlock(UnicodeCharacterRange.Parse(reader.ReadField()), reader.ReadTrimmedField()));
|
||||
builder.AddBlockEntry(new UnicodeBlock(UnicodeCodePointRange.Parse(reader.ReadField()), reader.ReadTrimmedField()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -179,7 +179,7 @@ namespace System.Unicode.Builder
|
||||
}
|
||||
}
|
||||
|
||||
public void SetProperties(ContributoryProperties property, UnicodeCharacterRange codePointRange)
|
||||
public void SetProperties(ContributoryProperties property, UnicodeCodePointRange codePointRange)
|
||||
{
|
||||
int firstIndex = FindUcdCodePoint(codePointRange.FirstCodePoint);
|
||||
int lastIndex = FindUcdCodePoint(codePointRange.LastCodePoint);
|
||||
@@ -210,7 +210,7 @@ namespace System.Unicode.Builder
|
||||
}
|
||||
}
|
||||
|
||||
public void SetProperties(CoreProperties property, UnicodeCharacterRange codePointRange)
|
||||
public void SetProperties(CoreProperties property, UnicodeCodePointRange codePointRange)
|
||||
{
|
||||
int firstIndex = FindUcdCodePoint(codePointRange.FirstCodePoint);
|
||||
int lastIndex = FindUcdCodePoint(codePointRange.LastCodePoint);
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Release</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{8DFDEE6C-4F0D-4DE1-B346-574CB56D2B8B}</ProjectGuid>
|
||||
<OutputType>Exe</OutputType>
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace UnicodeInformation.Tests
|
||||
{
|
||||
public static class AssertEx
|
||||
{
|
||||
public static void ThrowsExactly<TException>(Action action, string methodName)
|
||||
public static void ThrowsExactly<TException>(Action action, string methodName = null, string message = null)
|
||||
where TException : Exception
|
||||
{
|
||||
try
|
||||
@@ -19,12 +19,12 @@ namespace UnicodeInformation.Tests
|
||||
catch (TException ex)
|
||||
{
|
||||
if (ex.GetType() != typeof(TException))
|
||||
Assert.Fail("The " + methodName + " method should throw an exception of type " + typeof(TException).Name + " but got " + ex.GetType().Name + ".");
|
||||
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 + ".");
|
||||
else
|
||||
return;
|
||||
}
|
||||
|
||||
Assert.Fail("The " + methodName + " method should throw an exception of type " + typeof(TException).Name + ".");
|
||||
Assert.Fail(message ?? (methodName != null ? "The " + methodName + " method should throw an exception of type " : "Expected an exception of type ") + typeof(TException).Name + ".");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -148,7 +148,8 @@ namespace System.Unicode.Tests
|
||||
{
|
||||
var data = (await UnicodeDataProcessor.BuildDataAsync(ucdSource, unihanSource));
|
||||
|
||||
using (var stream = new DeflateStream(File.Create("ucd.dat"), CompressionLevel.Optimal, false))
|
||||
//using (var stream = new DeflateStream(File.Create("ucd.dat"), CompressionLevel.Optimal, false))
|
||||
using (var stream = File.Create("ucd.dat"))
|
||||
{
|
||||
data.WriteToStream(stream);
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ namespace UnicodeInformation.Tests
|
||||
{
|
||||
var info = UnicodeInfo.GetCharInfo(codePoint);
|
||||
Assert.AreEqual(codePoint, info.CodePoint);
|
||||
Assert.AreEqual(category, info.Category);
|
||||
Assert.AreEqual(category, info.Category);
|
||||
Assert.AreEqual(name, info.Name);
|
||||
Assert.AreEqual(block, UnicodeInfo.GetBlockName(codePoint));
|
||||
Assert.AreEqual(block, info.Block);
|
||||
@@ -192,7 +192,64 @@ namespace UnicodeInformation.Tests
|
||||
|
||||
AssertEx.ThrowsExactly<IndexOutOfRangeException>(() => UnicodeInfo.GetCjkRadicalInfo(0), nameof(UnicodeInfo.GetCjkRadicalInfo));
|
||||
AssertEx.ThrowsExactly<IndexOutOfRangeException>(() => UnicodeInfo.GetCjkRadicalInfo(215), nameof(UnicodeInfo.GetCjkRadicalInfo));
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CodePointRangeTest()
|
||||
{
|
||||
var fullRange = new UnicodeCodePointRange(0, 0x10FFFF);
|
||||
|
||||
Assert.AreEqual(0, fullRange.FirstCodePoint);
|
||||
Assert.AreEqual(0x10FFFF, fullRange.LastCodePoint);
|
||||
Assert.AreEqual(false, fullRange.IsSingleCodePoint);
|
||||
|
||||
var letterA = new UnicodeCodePointRange('A');
|
||||
|
||||
Assert.AreEqual('A', letterA.FirstCodePoint);
|
||||
Assert.AreEqual('A', letterA.LastCodePoint);
|
||||
Assert.AreEqual(true, letterA.IsSingleCodePoint);
|
||||
|
||||
AssertEx.ThrowsExactly<ArgumentOutOfRangeException>(() => new UnicodeCodePointRange(-1));
|
||||
AssertEx.ThrowsExactly<ArgumentOutOfRangeException>(() => new UnicodeCodePointRange(0x110000));
|
||||
AssertEx.ThrowsExactly<ArgumentOutOfRangeException>(() => new UnicodeCodePointRange(int.MaxValue));
|
||||
AssertEx.ThrowsExactly<ArgumentOutOfRangeException>(() => new UnicodeCodePointRange(-1, 10));
|
||||
AssertEx.ThrowsExactly<ArgumentOutOfRangeException>(() => new UnicodeCodePointRange(10, 0x110000));
|
||||
AssertEx.ThrowsExactly<ArgumentOutOfRangeException>(() => new UnicodeCodePointRange(-1, 0x110000));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CodePointRangeEnumerationTest()
|
||||
{
|
||||
const int start = 0xA3F;
|
||||
const int end = 0x105F;
|
||||
|
||||
// Generic test
|
||||
{
|
||||
int i = start;
|
||||
|
||||
foreach (int n in new UnicodeCodePointRange(start, end))
|
||||
{
|
||||
Assert.AreEqual(i++, n);
|
||||
}
|
||||
}
|
||||
|
||||
// Nongeneric test
|
||||
{
|
||||
int i = start;
|
||||
|
||||
var enumerator = (IEnumerator)new UnicodeCodePointRange(start, end).GetEnumerator();
|
||||
|
||||
while (enumerator.MoveNext())
|
||||
{
|
||||
Assert.AreEqual(i++, enumerator.Current);
|
||||
}
|
||||
|
||||
enumerator.Reset();
|
||||
|
||||
Assert.AreEqual(true, enumerator.MoveNext());
|
||||
Assert.AreEqual(start, enumerator.Current);
|
||||
}
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
[TestMethod]
|
||||
@@ -209,41 +266,15 @@ namespace UnicodeInformation.Tests
|
||||
for (int i = 0x2F800; i < 0x30000; ++i)
|
||||
Assert.AreEqual(i, UnihanCharacterData.UnpackCodePoint(UnihanCharacterData.PackCodePoint(i)));
|
||||
|
||||
try
|
||||
{
|
||||
UnihanCharacterData.PackCodePoint(0xA000);
|
||||
Assert.Fail("The PackCodePoint method should fail for code points outside of the valid range.");
|
||||
}
|
||||
catch (ArgumentOutOfRangeException)
|
||||
{
|
||||
}
|
||||
const string packCodePointErrorMessage = "The PackCodePoint method should fail for code points outside of the valid range.";
|
||||
const string unpackCodePointErrorMessage = "The PackCodePoint method should fail for values outside of the valid range.";
|
||||
|
||||
try
|
||||
{
|
||||
UnihanCharacterData.PackCodePoint(0xFB00);
|
||||
Assert.Fail("The PackCodePoint method should fail for code points outside of the valid range.");
|
||||
}
|
||||
catch (ArgumentOutOfRangeException)
|
||||
{
|
||||
}
|
||||
AssertEx.ThrowsExactly<ArgumentOutOfRangeException>(() => UnihanCharacterData.PackCodePoint(0xA000), nameof(UnihanCharacterData.PackCodePoint), packCodePointErrorMessage);
|
||||
AssertEx.ThrowsExactly<ArgumentOutOfRangeException>(() => UnihanCharacterData.PackCodePoint(0xFB00), nameof(UnihanCharacterData.PackCodePoint), packCodePointErrorMessage);
|
||||
AssertEx.ThrowsExactly<ArgumentOutOfRangeException>(() => UnihanCharacterData.PackCodePoint(0x30000), nameof(UnihanCharacterData.PackCodePoint), packCodePointErrorMessage);
|
||||
|
||||
try
|
||||
{
|
||||
UnihanCharacterData.PackCodePoint(0x30000);
|
||||
Assert.Fail("The PackCodePoint method should fail for code points outside of the valid range.");
|
||||
}
|
||||
catch (ArgumentOutOfRangeException)
|
||||
{
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
UnihanCharacterData.UnpackCodePoint(0x20000);
|
||||
Assert.Fail("The UnpackCodePoint method should fail for code points outside of the valid range.");
|
||||
}
|
||||
catch (ArgumentOutOfRangeException)
|
||||
{
|
||||
}
|
||||
AssertEx.ThrowsExactly<ArgumentOutOfRangeException>(() => UnihanCharacterData.UnpackCodePoint(-1), nameof(UnihanCharacterData.UnpackCodePoint), unpackCodePointErrorMessage);
|
||||
AssertEx.ThrowsExactly<ArgumentOutOfRangeException>(() => UnihanCharacterData.UnpackCodePoint(0x20000), nameof(UnihanCharacterData.UnpackCodePoint), unpackCodePointErrorMessage);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Release</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{50337426-E884-4394-9E1A-F6F7A555F5D9}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
|
||||
@@ -8,10 +8,10 @@ namespace System.Unicode
|
||||
{
|
||||
public struct UnicodeBlock
|
||||
{
|
||||
public readonly UnicodeCharacterRange CodePointRange;
|
||||
public readonly UnicodeCodePointRange CodePointRange;
|
||||
public readonly string Name;
|
||||
|
||||
internal UnicodeBlock(UnicodeCharacterRange codePointRange, string name)
|
||||
internal UnicodeBlock(UnicodeCodePointRange codePointRange, string name)
|
||||
{
|
||||
this.CodePointRange = codePointRange;
|
||||
this.Name = name;
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace System.Unicode
|
||||
{
|
||||
internal sealed class UnicodeCharacterData
|
||||
{
|
||||
public readonly UnicodeCharacterRange CodePointRange;
|
||||
public readonly UnicodeCodePointRange CodePointRange;
|
||||
public readonly string Name;
|
||||
public readonly UnicodeNameAlias[] NameAliases;
|
||||
public readonly UnicodeCategory Category;
|
||||
@@ -31,7 +31,7 @@ namespace System.Unicode
|
||||
|
||||
internal UnicodeCharacterData
|
||||
(
|
||||
UnicodeCharacterRange codePointRange,
|
||||
UnicodeCodePointRange codePointRange,
|
||||
string name,
|
||||
UnicodeNameAlias[] nameAliases,
|
||||
UnicodeCategory category,
|
||||
|
||||
+5
-5
@@ -8,7 +8,7 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace System.Unicode
|
||||
{
|
||||
public struct UnicodeCharacterRange : IEnumerable<int>
|
||||
public struct UnicodeCodePointRange : IEnumerable<int>
|
||||
{
|
||||
public struct Enumerator : IEnumerator<int>
|
||||
{
|
||||
@@ -39,7 +39,7 @@ namespace System.Unicode
|
||||
|
||||
public bool IsSingleCodePoint { get { return FirstCodePoint == LastCodePoint; } }
|
||||
|
||||
public UnicodeCharacterRange(int codePoint)
|
||||
public UnicodeCodePointRange(int codePoint)
|
||||
{
|
||||
if (codePoint < 0 || codePoint > 0x10FFFF) throw new ArgumentOutOfRangeException(nameof(codePoint));
|
||||
|
||||
@@ -47,7 +47,7 @@ namespace System.Unicode
|
||||
LastCodePoint = codePoint;
|
||||
}
|
||||
|
||||
public UnicodeCharacterRange(int firstCodePoint, int lastCodePoint)
|
||||
public UnicodeCodePointRange(int firstCodePoint, int lastCodePoint)
|
||||
{
|
||||
if (firstCodePoint < 0 || firstCodePoint > 0x10FFFF) throw new ArgumentOutOfRangeException(nameof(firstCodePoint));
|
||||
if (lastCodePoint < firstCodePoint || lastCodePoint > 0x10FFFF) throw new ArgumentOutOfRangeException(nameof(lastCodePoint));
|
||||
@@ -71,7 +71,7 @@ namespace System.Unicode
|
||||
return FirstCodePoint == LastCodePoint ? FirstCodePoint.ToString("X4") : FirstCodePoint.ToString("X4") + ".." + LastCodePoint.ToString("X4");
|
||||
}
|
||||
|
||||
public static UnicodeCharacterRange Parse(string s)
|
||||
public static UnicodeCodePointRange Parse(string s)
|
||||
{
|
||||
int start, end;
|
||||
|
||||
@@ -88,7 +88,7 @@ namespace System.Unicode
|
||||
end = int.Parse(s.Substring(rangeSeparatorOffset + 2), NumberStyles.HexNumber);
|
||||
}
|
||||
|
||||
return new UnicodeCharacterRange(start, end);
|
||||
return new UnicodeCodePointRange(start, end);
|
||||
}
|
||||
|
||||
public Enumerator GetEnumerator() { return new Enumerator(FirstCodePoint, LastCodePoint); }
|
||||
@@ -100,7 +100,7 @@ namespace System.Unicode
|
||||
{
|
||||
var fields = (UcdFields)reader.ReadUInt16();
|
||||
|
||||
var codePointRange = (fields & UcdFields.CodePointRange) != 0 ? new UnicodeCharacterRange(ReadCodePoint(reader), ReadCodePoint(reader)) : new UnicodeCharacterRange(ReadCodePoint(reader));
|
||||
var codePointRange = (fields & UcdFields.CodePointRange) != 0 ? new UnicodeCodePointRange(ReadCodePoint(reader), ReadCodePoint(reader)) : new UnicodeCodePointRange(ReadCodePoint(reader));
|
||||
|
||||
string name = null;
|
||||
UnicodeNameAlias[] nameAliases = UnicodeNameAlias.EmptyArray;
|
||||
@@ -249,7 +249,7 @@ namespace System.Unicode
|
||||
|
||||
private static UnicodeBlock ReadBlockEntry(BinaryReader reader)
|
||||
{
|
||||
return new UnicodeBlock(new UnicodeCharacterRange(ReadCodePoint(reader), ReadCodePoint(reader)), reader.ReadString());
|
||||
return new UnicodeBlock(new UnicodeCodePointRange(ReadCodePoint(reader), ReadCodePoint(reader)), reader.ReadString());
|
||||
}
|
||||
|
||||
private static int ReadInt24(BinaryReader reader)
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<MinimumVisualStudioVersion>11.0</MinimumVisualStudioVersion>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Release</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{CB722958-A1C4-4121-804B-7D5A671491B1}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
@@ -83,7 +83,7 @@
|
||||
<Compile Include="UnicodeCategoryExtensions.cs" />
|
||||
<Compile Include="UnicodeCategoryInfo.cs" />
|
||||
<Compile Include="UnicodeCharacterData.cs" />
|
||||
<Compile Include="UnicodeCharacterRange.cs" />
|
||||
<Compile Include="UnicodeCodePointRange.cs" />
|
||||
<Compile Include="UnicodeCharInfo.cs" />
|
||||
<Compile Include="UnicodeInfo.cs" />
|
||||
<Compile Include="UnicodeNameAlias.cs" />
|
||||
|
||||
Reference in New Issue
Block a user