You've already forked linux-packaging-mono
Imported Upstream version 3.12.0
Former-commit-id: cf92446697332992ec36726e78eb8703e1f259d7
This commit is contained in:
116
mcs/class/corlib/Test/System.Text/EncodingTester.cs
Normal file
116
mcs/class/corlib/Test/System.Text/EncodingTester.cs
Normal file
@ -0,0 +1,116 @@
|
||||
//
|
||||
// EncodingTester.cs
|
||||
//
|
||||
// Author:
|
||||
// Marcos Henrich <marcos.henrich@xamarin.com>
|
||||
//
|
||||
// (C) 2014 Xamarin, Inc.
|
||||
//
|
||||
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace MonoTests.System.Text
|
||||
{
|
||||
class EncodingTester
|
||||
{
|
||||
class DecoderTestFallbackBuffer : DecoderFallbackBuffer
|
||||
{
|
||||
DecoderFallbackBuffer buffer;
|
||||
private FallbackDelegate fallbackAction;
|
||||
|
||||
public DecoderTestFallbackBuffer (DecoderReplacementFallback fallback, FallbackDelegate fallbackAction)
|
||||
{
|
||||
this.fallbackAction = fallbackAction;
|
||||
buffer = new DecoderReplacementFallbackBuffer (fallback);
|
||||
}
|
||||
|
||||
public override bool Fallback (byte [] bytesUnknown, int index)
|
||||
{
|
||||
fallbackAction (bytesUnknown, index);
|
||||
return buffer.Fallback (bytesUnknown, index);
|
||||
}
|
||||
|
||||
public override char GetNextChar ()
|
||||
{
|
||||
return buffer.GetNextChar ();
|
||||
}
|
||||
|
||||
public override bool MovePrevious ()
|
||||
{
|
||||
return buffer.MovePrevious ();
|
||||
}
|
||||
|
||||
public override int Remaining
|
||||
{
|
||||
get { return buffer.Remaining; }
|
||||
}
|
||||
|
||||
public override void Reset ()
|
||||
{
|
||||
buffer.Reset ();
|
||||
}
|
||||
}
|
||||
|
||||
class DecoderTestFallback : DecoderFallback
|
||||
{
|
||||
private DecoderReplacementFallback fallback;
|
||||
private FallbackDelegate fallbackAction;
|
||||
|
||||
public DecoderTestFallback (FallbackDelegate fallbackAction)
|
||||
{
|
||||
this.fallbackAction = fallbackAction;
|
||||
}
|
||||
|
||||
public override DecoderFallbackBuffer CreateFallbackBuffer ()
|
||||
{
|
||||
fallback = new DecoderReplacementFallback ();
|
||||
return new DecoderTestFallbackBuffer (fallback, fallbackAction);
|
||||
}
|
||||
|
||||
public override int MaxCharCount
|
||||
{
|
||||
get { return fallback.MaxCharCount; }
|
||||
}
|
||||
}
|
||||
|
||||
public delegate void FallbackDelegate (byte [] bytesUnknown, int index);
|
||||
|
||||
Encoding encoding;
|
||||
|
||||
byte [][] expectedUnknownBytes;
|
||||
int expectedUnknownBytesIndex;
|
||||
|
||||
public EncodingTester (string encodingName)
|
||||
{
|
||||
var decoderFallback = new DecoderTestFallback (this.DecoderFallback);
|
||||
encoding = Encoding.GetEncoding (encodingName, new EncoderReplacementFallback(), decoderFallback);
|
||||
}
|
||||
|
||||
private void DecoderFallback (byte [] bytesUnknown, int index)
|
||||
{
|
||||
if (expectedUnknownBytesIndex == expectedUnknownBytes.Length)
|
||||
expectedUnknownBytesIndex = 0;
|
||||
|
||||
var expectedBytes = expectedUnknownBytes [expectedUnknownBytesIndex++];
|
||||
Assert.AreEqual (expectedBytes, bytesUnknown);
|
||||
}
|
||||
|
||||
public void TestDecoderFallback (byte [] data, string expectedString, params byte [][] expectedUnknownBytes)
|
||||
{
|
||||
lock (this)
|
||||
{
|
||||
this.expectedUnknownBytes = expectedUnknownBytes;
|
||||
this.expectedUnknownBytesIndex = 0;
|
||||
|
||||
Assert.AreEqual (expectedString.Length, encoding.GetCharCount (data));
|
||||
Assert.AreEqual (expectedUnknownBytesIndex, expectedUnknownBytes.Length);
|
||||
|
||||
Assert.AreEqual (expectedString, encoding.GetString (data));
|
||||
Assert.AreEqual (expectedUnknownBytesIndex, expectedUnknownBytes.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1198,5 +1198,88 @@ namespace MonoTests.System.Text
|
||||
int charactersWritten = Encoding.UTF8.GetDecoder ().GetChars (bytes, 0, 0, chars, 10, false);
|
||||
Assert.AreEqual (0, charactersWritten, "#3");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EncodingFallback ()
|
||||
{
|
||||
/* Legal UTF-8 Byte Sequences
|
||||
* 1st 2nd 3rd 4th
|
||||
* 00..7F
|
||||
* C2..DF 80..BF
|
||||
* E0 A0..BF 80..BF
|
||||
* E1..EF 80..BF 80..BF
|
||||
* F0 90..BF 80..BF 80..BF
|
||||
* F1..F3 80..BF 80..BF 80..BF
|
||||
* F4 80..8F 80..BF 80..BF
|
||||
*/
|
||||
|
||||
var t = new EncodingTester ("utf-8");
|
||||
byte [] data;
|
||||
|
||||
// Invalid 1st byte
|
||||
for (byte b = 0x80; b <= 0xC1; b++) {
|
||||
data = new byte [] { b };
|
||||
t.TestDecoderFallback (data, "?", new byte [] { b });
|
||||
}
|
||||
|
||||
///Invalid 2nd byte
|
||||
// C2..DF 80..BF
|
||||
for (byte b = 0xC2; b <= 0xDF; b++) {
|
||||
data = new byte [] { b, 0x61 };
|
||||
t.TestDecoderFallback (data, "?a", new byte [] { b });
|
||||
}
|
||||
|
||||
// E0 A0..BF
|
||||
data = new byte [] { 0xE0, 0x99};
|
||||
t.TestDecoderFallback (data, "?", new byte [] { 0xE0, 0x99});
|
||||
|
||||
// E1..EF 80..BF
|
||||
for (byte b = 0xE1; b <= 0xEF; b++) {
|
||||
data = new byte [] { b, 0x61 };
|
||||
t.TestDecoderFallback (data, "?a", new byte [] { b });
|
||||
}
|
||||
|
||||
// F0 90..BF
|
||||
data = new byte [] { 0xF0, 0x8F};
|
||||
t.TestDecoderFallback (data, "?", new byte [] { 0xF0, 0x8F });
|
||||
|
||||
// F1..F4 80..XX
|
||||
for (byte b = 0xF1; b <= 0xF4; b++) {
|
||||
data = new byte [] { b, 0x61 };
|
||||
t.TestDecoderFallback (data, "?a", new byte [] { b });
|
||||
}
|
||||
|
||||
// C2..F3 XX..BF
|
||||
for (byte b = 0xC2; b <= 0xF3; b++) {
|
||||
data = new byte [] { b, 0xC0 };
|
||||
t.TestDecoderFallback (data, "??", new byte [] { b }, new byte [] { 0xC0 });
|
||||
}
|
||||
|
||||
// Invalid 3rd byte
|
||||
// E0..F3 90..BF 80..BF
|
||||
for (byte b = 0xE0; b <= 0xF3; b++) {
|
||||
data = new byte [] { b, 0xB0, 0x61 };
|
||||
t.TestDecoderFallback (data, "?a", new byte [] { b, 0xB0 });
|
||||
data = new byte [] { b, 0xB0, 0xC0 };
|
||||
t.TestDecoderFallback (data, "??", new byte [] { b, 0xB0 }, new byte [] { 0xC0 });
|
||||
}
|
||||
|
||||
// F4 80..8F 80..BF
|
||||
data = new byte [] { 0xF4, 0x8F, 0xC0 };
|
||||
t.TestDecoderFallback (data, "??", new byte [] { 0xF4, 0x8F }, new byte [] { 0xC0 });
|
||||
|
||||
// Invalid 4th byte
|
||||
// F0..F3 90..BF 80..BF 80..BF
|
||||
for (byte b = 0xF0; b <= 0xF3; b++) {
|
||||
data = new byte [] { b, 0xB0, 0xB0, 0x61 };
|
||||
t.TestDecoderFallback (data, "?a", new byte [] { b, 0xB0, 0xB0 });
|
||||
data = new byte [] { b, 0xB0, 0xB0, 0xC0 };
|
||||
t.TestDecoderFallback (data, "??", new byte [] { b, 0xB0, 0xB0 }, new byte [] { 0xC0 });
|
||||
}
|
||||
|
||||
// F4 80..8F 80..BF 80..BF
|
||||
data = new byte [] { 0xF4, 0x8F, 0xB0, 0xC0 };
|
||||
t.TestDecoderFallback (data, "??", new byte [] { 0xF4, 0x8F, 0xB0 }, new byte [] { 0xC0 });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -164,34 +164,18 @@ namespace MonoTests.System.Text
|
||||
}
|
||||
|
||||
[Test]
|
||||
#if NET_2_0
|
||||
[Category ("NotWorking")]
|
||||
#endif
|
||||
public void TestMaxCharCount()
|
||||
{
|
||||
UnicodeEncoding UnicodeEnc = new UnicodeEncoding ();
|
||||
#if NET_2_0
|
||||
// where is this extra 1 coming from?
|
||||
Assert.AreEqual (26, UnicodeEnc.GetMaxCharCount(50), "UTF #1");
|
||||
Assert.AreEqual (27, UnicodeEnc.GetMaxCharCount(51), "UTF #2");
|
||||
#else
|
||||
Assert.AreEqual (25, UnicodeEnc.GetMaxCharCount(50), "UTF #1");
|
||||
#endif
|
||||
}
|
||||
|
||||
[Test]
|
||||
#if NET_2_0
|
||||
[Category ("NotWorking")]
|
||||
#endif
|
||||
public void TestMaxByteCount()
|
||||
{
|
||||
UnicodeEncoding UnicodeEnc = new UnicodeEncoding ();
|
||||
#if NET_2_0
|
||||
// is this extra 2 BOM?
|
||||
Assert.AreEqual (102, UnicodeEnc.GetMaxByteCount(50), "UTF #1");
|
||||
#else
|
||||
Assert.AreEqual (100, UnicodeEnc.GetMaxByteCount(50), "UTF #1");
|
||||
#endif
|
||||
}
|
||||
|
||||
[Test]
|
||||
@ -257,5 +241,27 @@ namespace MonoTests.System.Text
|
||||
Assert.AreEqual (2, s.Length, "Length");
|
||||
Assert.AreEqual (65533, (int) s [1], "1");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetMaxByteCountIncludesBOM ()
|
||||
{
|
||||
Assert.AreEqual (2, Encoding.Unicode.GetMaxByteCount (0), "#1");
|
||||
Assert.AreEqual (4, Encoding.Unicode.GetMaxByteCount (1), "#2");
|
||||
Assert.AreEqual (6, Encoding.Unicode.GetMaxByteCount (2), "#3");
|
||||
Assert.AreEqual (10, Encoding.Unicode.GetMaxByteCount (4), "#4");
|
||||
Assert.AreEqual (20, Encoding.Unicode.GetMaxByteCount (9), "#5");
|
||||
Assert.AreEqual (22, Encoding.Unicode.GetMaxByteCount (10), "#6");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetMaxCharCountRoundsCorrectly ()
|
||||
{
|
||||
Assert.AreEqual (1, Encoding.Unicode.GetMaxCharCount (0), "#1");
|
||||
Assert.AreEqual (2, Encoding.Unicode.GetMaxCharCount (1), "#2");
|
||||
Assert.AreEqual (2, Encoding.Unicode.GetMaxCharCount (2), "#3");
|
||||
Assert.AreEqual (3, Encoding.Unicode.GetMaxCharCount (4), "#4");
|
||||
Assert.AreEqual (6, Encoding.Unicode.GetMaxCharCount (9), "#5");
|
||||
Assert.AreEqual (6, Encoding.Unicode.GetMaxCharCount (10), "#6");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user