Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

View File

@ -0,0 +1,320 @@
// ASCIIEncodingTest - NUnit Test Cases for the System.Text.ASCIIEncoding class
//
// Author: Mike Kestner <mkestner@speakeasy.net>
//
// <c> 2002 Mike Kestner
using System;
using System.Text;
using NUnit.Framework;
using NUnit.Framework.Constraints;
#if !MOBILE
using NUnit.Framework.SyntaxHelpers;
#endif
namespace MonoTests.System.Text
{
[TestFixture]
public class ASCIIEncodingTest
{
private char[] testchars;
private byte[] testbytes;
[SetUp]
public void SetUp ()
{
testchars = new char[4];
testchars[0] = 'T';
testchars[1] = 'e';
testchars[2] = 's';
testchars[3] = 't';
testbytes = new byte[4];
testbytes[0] = (byte) 'T';
testbytes[1] = (byte) 'e';
testbytes[2] = (byte) 's';
testbytes[3] = (byte) 't';
}
[Test]
public void IsBrowserDisplay ()
{
Assert.IsFalse (Encoding.ASCII.IsBrowserDisplay);
}
[Test]
public void IsBrowserSave ()
{
Assert.IsFalse (Encoding.ASCII.IsBrowserSave);
}
[Test]
public void IsMailNewsDisplay ()
{
Assert.IsTrue (Encoding.ASCII.IsMailNewsDisplay);
}
[Test]
public void IsMailNewsSave ()
{
Assert.IsTrue (Encoding.ASCII.IsMailNewsSave);
}
[Test] // Test GetBytes(char[])
public void TestGetBytes1 ()
{
Encoding ascii_encoding = Encoding.ASCII;
byte[] bytes = ascii_encoding.GetBytes(testchars);
for (int i = 0; i < testchars.Length; i++)
Assert.AreEqual (testchars[i], (char) bytes[i]);
}
[Test] // Test GetBytes(char[], int, int)
public void TestGetBytes2 ()
{
Encoding ascii_encoding = Encoding.ASCII;
byte[] bytes = ascii_encoding.GetBytes(testchars, 1, 1);
Assert.AreEqual (1, bytes.Length, "#1");
Assert.AreEqual (testchars [1], (char) bytes [0], "#2");
}
[Test] // Test non-ASCII char in char[]
public void TestGetBytes3 ()
{
Encoding ascii_encoding = Encoding.ASCII;
testchars[2] = (char) 0x80;
byte[] bytes = ascii_encoding.GetBytes(testchars);
Assert.AreEqual ('T', (char) bytes [0], "#1");
Assert.AreEqual ('e', (char) bytes [1], "#2");
Assert.AreEqual ('?', (char) bytes [2], "#3");
Assert.AreEqual ('t', (char) bytes [3], "#4");
}
[Test] // Test GetBytes(char[], int, int, byte[], int)
public void TestGetBytes4 ()
{
Encoding ascii_encoding = Encoding.ASCII;
byte[] bytes = new Byte[1];
int cnt = ascii_encoding.GetBytes(testchars, 1, 1, bytes, 0);
Assert.AreEqual (1, cnt, "#1");
Assert.AreEqual (testchars [1], (char) bytes [0], "#2");
}
[Test] // Test GetBytes(string, int, int, byte[], int)
public void TestGetBytes5 ()
{
Encoding ascii_encoding = Encoding.ASCII;
byte[] bytes = new Byte[1];
int cnt = ascii_encoding.GetBytes("Test", 1, 1, bytes, 0);
Assert.AreEqual ('e', (char) bytes [0], "#1");
}
[Test] // Test GetBytes(string)
public void TestGetBytes6 ()
{
Encoding ascii_encoding = Encoding.ASCII;
byte[] bytes = ascii_encoding.GetBytes("Test");
for (int i = 0; i < testchars.Length; i++)
Assert.AreEqual (testchars [i], (char) bytes [i]);
}
[Test] // Test GetChars(byte[])
public void TestGetChars1 ()
{
Encoding ascii_encoding = Encoding.ASCII;
char[] chars = ascii_encoding.GetChars(testbytes);
for (int i = 0; i < testbytes.Length; i++)
Assert.AreEqual (testbytes[i], (byte) chars[i]);
}
[Test] // Test GetChars(byte[], int, int)
public void TestGetChars2 ()
{
Encoding ascii_encoding = Encoding.ASCII;
char[] chars = ascii_encoding.GetChars(testbytes, 1, 1);
Assert.AreEqual (1, chars.Length, "#1");
Assert.AreEqual (testbytes [1], (byte) chars [0], "#2");
}
[Test] // Test non-ASCII char in byte[]
public void TestGetChars3 ()
{
Encoding ascii_encoding = Encoding.ASCII;
testbytes[2] = 0x80;
char[] chars = ascii_encoding.GetChars(testbytes);
Assert.AreEqual ('T', chars [0], "#1");
Assert.AreEqual ('e', chars [1], "#2");
Assert.AreEqual ('?', chars [2], "#3");
Assert.AreEqual ('t', chars [3], "#4");
}
[Test] // Test GetChars(byte[], int, int, char[], int)
public void TestGetChars4 ()
{
Encoding ascii_encoding = Encoding.ASCII;
char[] chars = new char[1];
int cnt = ascii_encoding.GetChars(testbytes, 1, 1, chars, 0);
Assert.AreEqual (1, cnt, "#1");
Assert.AreEqual (testbytes [1], (byte) chars [0], "#2");
}
[Test] // Test GetString(char[])
public void TestGetString1 ()
{
Encoding ascii_encoding = Encoding.ASCII;
string str = ascii_encoding.GetString(testbytes);
Assert.AreEqual ("Test", str);
}
[Test] // Test GetString(char[], int, int)
public void TestGetString2 ()
{
Encoding ascii_encoding = Encoding.ASCII;
string str = ascii_encoding.GetString(testbytes, 1, 2);
Assert.AreEqual ("es", str);
}
[Test] // Test invalid byte handling
public void TestGetString3 ()
{
Encoding encoding = Encoding.ASCII;
byte [] bytes = new byte [] {0x61, 0xE1, 0xE2};
string s = encoding.GetString (bytes, 0, 3);
#if NET_2_0
Assert.AreEqual ("a??", s);
#else
Assert.AreEqual ("aab", s);
#endif
}
[Test] // Test Decoder
public void TestDecoder ()
{
Encoding ascii_encoding = Encoding.ASCII;
char[] chars = new char[1];
int cnt = ascii_encoding.GetDecoder().GetChars(testbytes, 1, 1, chars, 0);
Assert.AreEqual (1, cnt, "#1");
Assert.AreEqual (testbytes [1], (byte) chars [0], "#2");
}
[Test] // Test Decoder
public void TestEncoder ()
{
Encoding ascii_encoding = Encoding.ASCII;
byte[] bytes = new Byte[1];
int cnt = ascii_encoding.GetEncoder().GetBytes(testchars, 1, 1, bytes, 0, false);
Assert.AreEqual (1, cnt, "#1");
Assert.AreEqual (testchars [1], (char) bytes [0], "#2");
}
[Test]
public void TestZero ()
{
Encoding encoding = Encoding.ASCII;
Assert.AreEqual (string.Empty, encoding.GetString (new byte [0]), "#1");
Assert.AreEqual (string.Empty, encoding.GetString (new byte [0], 0, 0), "#2");
}
[Test]
[ExpectedException (typeof (DecoderFallbackException))]
public void DecoderFallback ()
{
Encoding e = Encoding.ASCII.Clone () as Encoding;
e.DecoderFallback = new DecoderExceptionFallback ();
e.GetChars (new byte [] {0x80});
}
[Test]
// [ExpectedException (typeof (ArgumentException))]
public void DecoderFallback2 ()
{
var bytes = new byte[] {
0x30, 0xa0, 0x31, 0xa8
};
var enc = (ASCIIEncoding)Encoding.ASCII.Clone ();
enc.DecoderFallback = new TestFallbackDecoder ();
var chars = new char [7];
var ret = enc.GetChars (bytes, 0, bytes.Length, chars, 0);
Console.WriteLine (ret);
for (int i = 0; i < chars.Length; i++) {
Console.Write ("{0:x2} ", (int)chars [i]);
}
Console.WriteLine ();
}
[Test]
public void DecoderFallback3 ()
{
var bytes = new byte[] {
0x30, 0xa0, 0x31, 0xa8
};
var enc = (ASCIIEncoding)Encoding.ASCII.Clone ();
enc.DecoderFallback = new TestFallbackDecoder ();
var chars = new char[] { '9', '8', '7', '6', '5' };
var ret = enc.GetChars (bytes, 0, bytes.Length, chars, 0);
Assert.That (ret, Is.EqualTo (4), "ret"); // FIXME: Wrong it should be 2
Assert.That (chars [0], Is.EqualTo ('0'), "chars[0]");
Assert.That (chars [1], Is.EqualTo ('1'), "chars[1]");
Assert.That (chars [2], Is.EqualTo ('7'), "chars[2]");
Assert.That (chars [3], Is.EqualTo ('6'), "chars[3]");
Assert.That (chars [4], Is.EqualTo ('5'), "chars[4]");
}
class TestFallbackDecoder : DecoderFallback {
const int count = 2;
public override int MaxCharCount {
get { return count; }
}
public override DecoderFallbackBuffer CreateFallbackBuffer ()
{
return new Buffer ();
}
class Buffer : DecoderFallbackBuffer {
char[] queue;
int index;
public override int Remaining {
get {
return queue.Length - index;
}
}
public override char GetNextChar ()
{
return index < queue.Length ? queue [index++] : '\0';
}
public override bool Fallback (byte[] bytes, int unused)
{
queue = new char[bytes.Length * count];
index = 0;
for (int i = 0; i < bytes.Length; i++) {
for (int j = 0; j < count; j++)
queue [index++] = (char)(bytes [i]+j);
}
return true;
}
public override bool MovePrevious ()
{
throw new NotImplementedException ();
}
public override void Reset ()
{
base.Reset ();
}
}
}
}
}

View File

@ -0,0 +1,327 @@
2009-10-27 Sebastien Pouliot <sebastien@ximian.com>
* UnicodeEncodingTest.cs: Add test cases with an odd number of
bytes being used in GetString (it does not crash but it does not
work like MS FX either).
2009-09-12 Gonzalo Paniagua Javier <gonzalo@novell.com>
* StringBuilderTest.cs: new test.
2009-07-26 Gonzalo Paniagua Javier <gonzalo@novell.com>
* StringBuilderTest.cs: new overflow tests.
2009-06-26 Robert Jordan <robertj@gmx.net>
* UTF8EncodingTest.cs: Fix the NET_1_1 build.
2009-06-26 Zoltan Varga <vargaz@gmail.com>
* *.cs: Convert all tests to new-style nunit classes/methods. Add
[Test] attributes to some tests which were missing them.
2009-02-05 Gert Driesen <drieseng@users.sourceforge.net>
* UTF32EncodingTest.cs: Added tests for GetByteCount overloads.
2009-01-30 Atsushi Enomoto <atsushi@ximian.com>
* UTF8EncodingTest.cs : added test for bug #415628.
2009-01-13 Jb Evain <jbevain@novell.com>
* EncoderTest.cs: Make sure we can get an encoder for a custom
encoding.
* DecoderTest.cs: ditto.
2008-04-09 Atsushi Enomoto <atsushi@ximian.com>
* UTF7EncodingTest.cs : added test for broken GetCharCount() case.
2007-12-27 Atsushi Enomoto <atsushi@ximian.com>
* EncoderReplacementFallbackTest.cs,
EncoderReplacementFallbackBufferTest.cs : encoder test for
"\uFFFD" replacement.
2007-10-26 Atsushi Enomoto <atsushi@ximian.com>
* UTF8EncodingTest.cs : looks like the issue is not fixed in .NET 1.x
and hence it looked like a regression. (It wasn't.)
2007-10-25 Atsushi Enomoto <atsushi@ximian.com>
* UnicodeEncodingTest.cs,
UTF8EncodingTest.cs,
DecoderReplacementFallbackBufferTest.cs,
DecoderReplacementFallbackTest.cs : default replacement buffer fix.
Added test for Reset() for replacement buffer.
2007-10-16 Gert Driesen <drieseng@users.sourceforge.net>
* TestEncoding.cs: Also make class available on 1.0 profile.
* EncodingTest.cs: Added tests for Is* properties.
* UTF7EncodingTest.cs: Added tests for Is* properties.
* UnicodeEncodingTest.cs: Added tests for Is* properties. Fixed
line endings.
* UTF8EncodingTest.cs: Added tests for Is* properties. Spaces to
tabs. Numbered tests.
* ASCIIEncodingTest.cs: Added tests for Is* properties. No longer
derive from TestCase class. Spaces to tabs.
* UTF32EncodingTest.cs: Added tests for Is* properties. No longer
derive from deprecated Assertion class.
2007-07-06 Gert Driesen <drieseng@users.sourceforge.net>
* DecoderReplacementFallbackTest.cs: Fixed DontChangeReadOnlyCodePage-
DecoderFallback test to pass on systems where codepage 932 is valid.
Assert that exception is thrown by DecoderFallback property.
* EncoderReplacementFallbackTest.cs: Fixed DontChangeReadOnlyCodePage-
EncoderFallback test to pass on systems where codepage 932 is valid.
Assert that exception is thrown by EncoderFallback property.
2007-04-19 Marek Habersack <mhabersack@novell.com>
* DecoderReplacementFallbackTest.cs: expect the correct
exception.
2007-02-01 Gert Driesen <drieseng@users.sourceforge.net>
* EncodingTest.cs: Enabled test again.
2007-02-01 Atsushi Enomoto <atsushi@ximian.com>
* EncodingTest.cs :
reverted part of the related patch, thus added NotWorking.
2007-01-31 Gert Driesen <drieseng@users.sourceforge.net>
* EncodingTest.cs: Improved test for not-supported encoding.
2007-01-31 Gert Driesen <drieseng@users.sourceforge.net>
* EncodingTest.cs: Added tests for GetEncoding.
2006-07-18 Kornél Pál <kornelpal@gmail.com>
* ASCIIEncodingTest.cs: Added TestGetString3 test.
2006-07-11 Kornél Pál <kornelpal@gmail.com>
* StringBuilderTest.cs: Added SetLength test.
2006-06-24 Kornél Pál <kornelpal@gmail.com>
* UnicodeEncodingTest.cs: Added ByteOrderMark tests.
2006-05-25 Atsushi Enomoto <atsushi@ximian.com>
* EncodingInfoTest.cs : new TestFixture.
2006-04-13 Atsushi Enomoto <atsushi@ximian.com>
* ASCIIEncodingTest.cs : added DecoderFallback().
2006-03-30 Atsushi Enomoto <atsushi@ximian.com>
* EncoderTest.cs, DecoderTest.cs : new tests, for Convert().
2006-02-14 Atsushi Enomoto <atsushi@ximian.com>
* UTF8EncodingTest.cs : test for bug #77550.
2006-02-03 Atsushi Enomoto <atsushi@ximian.com>
* UTF8EncodingTest.cs : added test for insufficient bytes for
flush=true and leftOver!='\0' case.
2006-01-24 Atsushi Enomoto <atsushi@ximian.com>
* UTF7EncodingTest.cs : added test for bug #77315.
* UTF8EncodingTest.cs : added test for bug #77315. Also, now a
bunch of tests are working (while they are marked as NotDotNet
under 1.1 profile).
2006-01-20 Raja R Harinath <rharinath@novell.com>
* UnicodeEncodingTest.cs (TestMaxByteCount): Remove extra declaration.
2006-01-20 Atsushi Enomoto <atsushi@ximian.com>
* UTF8EncodingTest.cs : added CloneNotReadOnly().
2006-01-19 Atsushi Enomoto <atsushi@ximian.com>
* UnicodeEncodingTest.cs, DecoderReplacementFallbackBufferTest.cs,
EncoderReplacementFallbackBufferTest.cs : tests were fixed but
"NotWorking" were not marked correctly.
2006-01-19 Atsushi Enomoto <atsushi@ximian.com>
* UTF7EncodingTest.cs UTF8EncodingTest.cs UnicodeEncodingTest.cs :
due to the introduction of fallback, some of their internals have
changed. Thus marked some tests as NotWorking.
* DecoderReplacementFallbackBufferTest.cs,
EncoderReplacementFallbackBufferTest.cs :
some behaviors have changed since beta2, so marked old tests as
NotWorking.
2005-12-05 Sebastien Pouliot <sebastien@ximian.com>
* StringBuilderTest.cs: Test case MaxCapacity_Overflow3 doesn't work
under MS 1.1 SP1 (it should throw an ArgumentOutOfRangeException) but
it does work under Mono and MS 2.0 (final). Tagged as "NotWorking".
2005-11-22 Atsushi Enomoto <atsushi@ximian.com>
* EncoderReplacementFallbackTest.cs :
Added Latin1 replacement test.
Updated some test results changed from beta2 to RTM.
* DecoderReplacementFallbackTest.cs : the same updates.
2005-11-16 Atsushi Enomoto <atsushi@ximian.com>
* UTF8EncodingTest.cs : Simply replaced all ArgumentException with
switched alias (it is DecodefFallbackException in NET_2_0).
2005-11-15 Atsushi Enomoto <atsushi@ximian.com>
* EncoderReplacementFallbackBufferTest.cs : new test (it's weird, but
mostly identical to DecoderReplacementFallbackBufferTest...)
2005-11-15 Atsushi Enomoto <atsushi@ximian.com>
* DecoderReplacementFallbackBufferTest.cs : new test.
2005-11-15 Atsushi Enomoto <atsushi@ximian.com>
* TestEncoding.cs,
DecoderReplacementFallbackTest.cs,
EncoderReplacementFallbackTest.cs : new 2.0 tests.
2005-08-25 Atsushi Enomoto <atsushi@ximian.com>
* UTF8EncodingTest.cs : added GetCharsFEFF().
2005-06-21 Ben Maurer <bmaurer@ximian.com>
* StringBuilderTest.cs: Test replacing with a longer string.
2005-05-06 Ben Maurer <bmaurer@ximian.com>
* StringBuilderTest.cs (MaxCapacity_Overflow3): Test for #72244.
2005-01-21 Ben Maurer <bmaurer@ximian.com>
* StringBuilderTest.cs (CapacityFromString): This relies on impl
specific behavior. Rewrite the test.
2005-01-11 Gonzalo Paniagua Javier <gonzalo@ximian.com>
* System.Text/StringBuilderTest.cs: new tests for capacity when the
StringBuilder is created from a string.
2005-01-10 Gonzalo Paniagua Javier <gonzalo@ximian.com>
* StringBuilderTest.cs: patch to test for capacity being exceeded.
2004-06-23 Sebastien Pouliot <sebastien@ximian.com>
* UTF7EncodingTest.cs: Added 3 more test cases from RFC1642 to decode
and re-encode UTF7 from/to unicode.
2004-06-07 Atsushi Enomoto <atsushi@ximian.com>
* UTF8EncodingTest.cs : Added TestThrowOnInvalid().
2004-05-26 Sebastien Pouliot <sebastien@ximian.com>
* StringBuilderTest.cs: Added unit tests to be sure integer overflows
are catched.
2004-05-20 Sebastien Pouliot <sebastien@ximian.com>
* UTF7EncodingTest.cs, UnicodeEncodingTest.cs: Removed unused SetUp
and TearDown to facilitate searches.
2004-05-14 Sebastien Pouliot <sebastien@ximian.com>
* UTF8EncodingTest.cs: Test both characters in test 2.1.4.
2004-04-14 Sebastien Pouliot <sebastien@ximian.com>
* UTF8EncodingTest.cs: Ajusted test 3.3.6 and 3.3.7 because MS accept
invalid UTF8 (when it shouldn't) and Mono doesn't. Both case are now
accepted by the unit tests.
2004-04-14 Sebastien Pouliot <sebastien@ximian.com>
* UTF8EncodingTest.cs: Added 70 new unit tests from UTF-8-test.txt
(available from http://www.cl.cam.ac.uk/~mgk25/). Tests are adapted
so MS implementation pass them all.
2004-03-19 Dick Porter <dick@ximian.com>
* UnicodeEncodingTest.cs: Test for character counts on subranges
of arrays with a BOM at the start of the array.
2004-03-10 Juraj Skripsky <juraj@hotfeet.ch>
* StringBuilderTest.cs: added new test for bug in Insert.
2004-01-23 Gonzalo Paniagua Javier <gonzalo@ximian.com>
* StringBuilderTest.cs: added test for bug #53240.
2004-01-14 Gonzalo Paniagua Javier <gonzalo@ximian.com>
* StringBuilderTest.cs: added tests for the couple of bugs found in the
new StringBuilder implementation.
2003-05-14 Nick Drochak <ndrochak@gol.com>
* UTF7EncodingTest.cs:
* UTF8EncodingTest.cs: added from Patrick Kalkman <kalkman@cistron.nl>
2003-05-10 Gonzalo Paniagua Javier <gonzalo@ximian.com>
* StringBuilderTest.cs: added replace tests from bug #41397 by
<tom@acquist.com>>.
2003-03-05 Gonzalo Paniagua Javier <gonzalo@ximian.com>
* ASCIIEncodingTest.cs: added a couple of asserts fot zero-length
arrays.
2003-02-05 Nick Drochak <ndrochak@gol.com>
* StringBuilderTest.cs: Test for specific exception using Nunit verson
2 style Attribute.
2003-01-30 Zoltan Varga <vargaz@freemail.hu>
* StringBuilderTest.cs: added test for passing a negative capacity to
the constructor.
2002-12-21 Nick Drochak <ndrochak@gol.com>
* all: make tests build and run under nunit2
2002-12-12 Jackson Harper <jackson@latitudegeo.com>
* AllTests.cs: Comment out UTF8... test (it does not exist)
2002-03-17 Mike Kestner <mkestner@speakeasy.net>
* ASCIIEncodingTest.cs : New suite for ASCIIEncoding.
* AllTests.cs : Add the new suite.
2002-02-10 Nick Drochak <ndrochak@gol.com>
* AllTests.cs: Remove StringBuilderTest suite. There's a major failure
when it's included and the test errors aren't displayed.
2002-02-09 Nick Drochak <ndrochak@gol.com>
* StringBuilderTest.cs: Fixed tests that were failing against mscorlib.

View File

@ -0,0 +1,116 @@
//
// DecoderReplacementFallbackBuffer.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com>
//
// Copyright (C) 2005 Novell, Inc. http://www.novell.com
//
#if NET_2_0
using System;
using System.IO;
using System.Text;
using NUnit.Framework;
using Buffer = System.Text.DecoderReplacementFallbackBuffer;
namespace MonoTests.System.Text
{
[TestFixture]
public class DecoderReplacementFallbackBufferTest
{
private Buffer NewInstance ()
{
return new Buffer (new DecoderReplacementFallback ());
}
[Test]
public void FallbackEmptyDefault ()
{
Buffer b = NewInstance ();
Assert.IsTrue (b.Fallback (new byte [] {}, 0), "#0");
Assert.IsFalse (b.MovePrevious (), "#1");
Assert.AreEqual (1, b.Remaining, "#2");
Assert.AreEqual ('?', b.GetNextChar (), "#3");
Assert.AreEqual (0, b.Remaining, "#4");
// the string is already consumed.
Assert.AreEqual (char.MinValue, b.GetNextChar (), "#5");
}
[Test]
public void FallbackDefaultEncodingUTF8 ()
{
Buffer b = Encoding.UTF8.DecoderFallback.CreateFallbackBuffer () as Buffer;
Assert.IsTrue (b.Fallback (new byte [] {}, 0), "#1");
Assert.IsFalse (b.MovePrevious (), "#2");
Assert.AreEqual (1, b.Remaining, "#3");
Assert.AreEqual ('\uFFFD', b.GetNextChar (), "#4");
}
[Test]
public void FallbackEmptyForEncodingUTF8 ()
{
Buffer b = new DecoderReplacementFallbackBuffer (new DecoderReplacementFallback (String.Empty));
Assert.IsFalse (b.Fallback (new byte [] {}, 0), "#1");
Assert.IsFalse (b.MovePrevious (), "#2");
Assert.AreEqual (0, b.Remaining, "#3");
// the string does not exist.
Assert.AreEqual (char.MinValue, b.GetNextChar (), "#4");
}
[Test]
public void FallbackSequential ()
{
Buffer b = NewInstance ();
b.Fallback (new byte [] {}, 0);
b.GetNextChar ();
b.Fallback (new byte [] {}, 0);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void FallbackRecursiveError ()
{
Buffer b = NewInstance ();
b.Fallback (new byte [] {}, 0);
b.Fallback (new byte [] {}, 0);
}
[Test]
[Category ("NotWorking")]
public void Iterate ()
{
Assert.AreEqual ('\0', Encoding.UTF8.DecoderFallback
.CreateFallbackBuffer ().GetNextChar (), "#1");
Buffer b = NewInstance ();
// Assert.AreEqual (1, b.Remaining, "#2");
// Assert.AreEqual ('?', b.GetNextChar (), "#3");
Assert.AreEqual (0, b.Remaining, "#4");
Assert.AreEqual ('\0', b.GetNextChar (), "#5");
// Assert.IsTrue (b.MovePrevious (), "#6");
// Assert.AreEqual (1, b.Remaining, "#7");
Assert.IsFalse (b.MovePrevious (), "#8");
// Assert.AreEqual ('?', b.GetNextChar (), "#9");
}
[Test]
public void Reset ()
{
DecoderReplacementFallback f = new DecoderReplacementFallback ("X");
DecoderReplacementFallbackBuffer b = new DecoderReplacementFallbackBuffer (f);
b.Fallback (new byte [0], 0);
Assert.AreEqual (1, b.Remaining, "#1");
b.Reset ();
Assert.AreEqual (0, b.Remaining, "#2");
b.Fallback (new byte [0], 0); // do not raise an error
b.Reset ();
Assert.AreEqual (0, (int) b.GetNextChar (), "#3");
}
}
}
#endif

View File

@ -0,0 +1,107 @@
//
// DecoderReplacementFallback.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com>
//
// Copyright (C) 2005 Novell, Inc. http://www.novell.com
//
#if NET_2_0
using System;
using System.IO;
using System.Text;
using NUnit.Framework;
namespace MonoTests.System.Text
{
[TestFixture]
public class DecoderReplacementFallbackTest
{
[Test]
public void Defaults ()
{
DecoderReplacementFallback f =
new DecoderReplacementFallback ();
Assert.AreEqual ("?", f.DefaultString, "#1");
Assert.AreEqual (1, f.MaxCharCount, "#2");
f = new DecoderReplacementFallback (String.Empty);
Assert.AreEqual (String.Empty, f.DefaultString, "#3");
Assert.AreEqual (0, f.MaxCharCount, "#4");
f = Encoding.UTF8.DecoderFallback as DecoderReplacementFallback;
Assert.IsNotNull (f, "#5");
// This behavior was introduced as
// http://support.microsoft.com/kb/940521/
Assert.AreEqual ("\uFFFD", f.DefaultString, "#6");
Assert.AreEqual (1, f.MaxCharCount, "#7");
// after beta2 this test became invalid.
//f = new MyEncoding ().DecoderFallback as DecoderReplacementFallback;
//Assert.IsNotNull (f, "#8");
//Assert.AreEqual (String.Empty, f.DefaultString, "#9");
//Assert.AreEqual (0, f.MaxCharCount, "#10");
f = DecoderFallback.ReplacementFallback as DecoderReplacementFallback;
Assert.AreEqual ("?", f.DefaultString, "#11");
Assert.AreEqual (1, f.MaxCharCount, "#12");
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void DontChangeReadOnlyUTF8DecoderFallback ()
{
Encoding.UTF8.DecoderFallback =
new DecoderReplacementFallback ();
}
[Test]
public void DontChangeReadOnlyCodePageDecoderFallback ()
{
Encoding encoding = Encoding.GetEncoding (Encoding.Default.CodePage);
try {
encoding.DecoderFallback = new DecoderReplacementFallback ();
Assert.Fail ("#1");
} catch (InvalidOperationException ex) {
Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
}
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void CustomEncodingSetEncoderFallback ()
{
new MyEncoding ().DecoderFallback =
new DecoderReplacementFallback ();
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void EncodingSetNullDecoderFallback ()
{
Encoding.Default.DecoderFallback = null;
}
[Test]
// Don't throw an exception
public void SetDecoderFallback ()
{
Encoding.Default.GetDecoder ().Fallback =
new DecoderReplacementFallback ();
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void DecoderSetNullFallback ()
{
Encoding.Default.GetDecoder ().Fallback = null;
}
}
}
#endif

View File

@ -0,0 +1,119 @@
//
// DecoderTest.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com>
//
// (C) 2006 Novell, Inc.
//
using NUnit.Framework;
using System;
using System.Text;
namespace MonoTests.System.Text
{
[TestFixture]
public class DecoderTest
{
#if NET_2_0
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void ConvertNullChars ()
{
int charsUsed, bytesUsed;
bool done;
Encoding.UTF8.GetDecoder ().Convert (
null, 0, 100, new char [100], 0, 100, false,
out charsUsed, out bytesUsed, out done);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void ConvertNullBytes ()
{
int charsUsed, bytesUsed;
bool done;
Encoding.UTF8.GetDecoder ().Convert (
new byte [100], 0, 100, null, 0, 100, false,
out charsUsed, out bytesUsed, out done);
}
[Test]
public void ConvertLimitedDestination ()
{
char [] chars = new char [10000];
byte [] bytes = new byte [10000];
Decoder conv = Encoding.UTF8.GetDecoder ();
int charsUsed, bytesUsed;
bool done;
conv.Convert (bytes, 0, 10000, chars, 0, 1000, true,
out charsUsed, out bytesUsed, out done);
Assert.IsFalse (done, "#1");
Assert.AreEqual (625, charsUsed, "#2");
Assert.AreEqual (625, bytesUsed, "#3");
}
[Test]
public void CustomEncodingGetDecoder ()
{
var encoding = new CustomEncoding ();
var decoder = encoding.GetDecoder ();
Assert.IsNotNull (decoder);
}
class CustomEncoding : Encoding {
public override int GetByteCount (char [] chars, int index, int count)
{
throw new NotSupportedException ();
}
public override int GetBytes (char [] chars, int charIndex, int charCount, byte [] bytes, int byteIndex)
{
throw new NotSupportedException ();
}
public override int GetCharCount (byte [] bytes, int index, int count)
{
throw new NotSupportedException ();
}
public override int GetChars (byte [] bytes, int byteIndex, int byteCount, char [] chars, int charIndex)
{
throw new NotSupportedException ();
}
public override int GetMaxByteCount (int charCount)
{
throw new NotSupportedException ();
}
public override int GetMaxCharCount (int byteCount)
{
throw new NotSupportedException ();
}
}
#endif
[Test]
public void Bug10789 ()
{
byte[] bytes = new byte[100];
char[] chars = new char[100];
Decoder conv = Encoding.UTF8.GetDecoder ();
int charsUsed, bytesUsed;
bool completed;
conv.Convert (bytes, 0, 0, chars, 100, 0, false, out bytesUsed, out charsUsed, out completed);
Assert.IsTrue (completed, "#1");
Assert.AreEqual (0, charsUsed, "#2");
Assert.AreEqual (0, bytesUsed, "#3");
}
}
}

View File

@ -0,0 +1,101 @@
//
// EncoderReplacementFallbackBuffer.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com> <atsushi@xamarin.com>
//
// Copyright (C) 2005 Novell, Inc. http://www.novell.com
// Copyright (C) 2011 Xamain, Inc. http://xamarin.com
// Copyright 2011 Xamarin Inc (http://www.xamarin.com).
//
#if NET_2_0
using System;
using System.IO;
using System.Text;
using NUnit.Framework;
using Buffer = System.Text.EncoderReplacementFallbackBuffer;
namespace MonoTests.System.Text
{
[TestFixture]
public class EncoderReplacementFallbackBufferTest
{
private Buffer NewInstance ()
{
return new Buffer (new EncoderReplacementFallback ());
}
[Test]
public void FallbackEmptyDefault ()
{
Buffer b = NewInstance ();
Assert.IsTrue (b.Fallback ('X', 0), "#0");
Assert.IsFalse (b.MovePrevious (), "#1");
Assert.AreEqual (1, b.Remaining, "#2");
Assert.AreEqual ('?', b.GetNextChar (), "#3");
Assert.AreEqual (0, b.Remaining, "#4");
// the string is already consumed.
Assert.AreEqual (char.MinValue, b.GetNextChar (), "#5");
}
[Test]
public void FallbackEmptyForEncodingUTF8 ()
{
Buffer b = Encoding.UTF8.EncoderFallback.CreateFallbackBuffer () as Buffer;
Assert.IsTrue (b.Fallback ('X', 0), "#1");
Assert.IsFalse (b.MovePrevious (), "#2");
Assert.AreEqual (1, b.Remaining, "#3");
// the string does not exist.
Assert.AreEqual ('\uFFFD', b.GetNextChar (), "#4");
}
[Test]
public void FallbackSequential ()
{
Buffer b = NewInstance ();
b.Fallback ('X', 0);
b.GetNextChar ();
b.Fallback ('X', 0);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void FallbackRecursiveError ()
{
Buffer b = NewInstance ();
b.Fallback ('X', 0);
b.Fallback ('X', 0);
}
[Test]
[Category ("NotWorking")]
public void Iterate ()
{
Assert.AreEqual ('\0', Encoding.UTF8.EncoderFallback
.CreateFallbackBuffer ().GetNextChar (), "#1");
Buffer b = NewInstance ();
// Assert.AreEqual (1, b.Remaining, "#2");
// Assert.AreEqual ('?', b.GetNextChar (), "#3");
Assert.AreEqual (0, b.Remaining, "#4");
Assert.AreEqual ('\0', b.GetNextChar (), "#5");
// Assert.IsTrue (b.MovePrevious (), "#6");
// Assert.AreEqual (1, b.Remaining, "#7");
Assert.IsFalse (b.MovePrevious (), "#8");
// Assert.AreEqual ('?', b.GetNextChar (), "#9");
}
[Test]
public void Reset ()
{
// Xamarin bug #545
Encoding.UTF8.GetBytes ("\uDE7E\uDE7E");
}
}
}
#endif

View File

@ -0,0 +1,115 @@
//
// EncoderReplacementFallback.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com>
//
// Copyright (C) 2005 Novell, Inc. http://www.novell.com
//
#if NET_2_0
using System;
using System.IO;
using System.Text;
using NUnit.Framework;
namespace MonoTests.System.Text
{
[TestFixture]
public class EncoderReplacementFallbackTest
{
[Test]
public void Defaults ()
{
EncoderReplacementFallback f =
new EncoderReplacementFallback ();
Assert.AreEqual ("?", f.DefaultString, "#1");
Assert.AreEqual (1, f.MaxCharCount, "#2");
f = new EncoderReplacementFallback (String.Empty);
Assert.AreEqual (String.Empty, f.DefaultString, "#3");
Assert.AreEqual (0, f.MaxCharCount, "#4");
f = Encoding.UTF8.EncoderFallback as EncoderReplacementFallback;
Assert.IsNotNull (f, "#5");
Assert.AreEqual ("\uFFFD", f.DefaultString, "#6");
Assert.AreEqual (1, f.MaxCharCount, "#7");
// after beta2 this test became invalid.
//f = new MyEncoding ().EncoderFallback as EncoderReplacementFallback;
//Assert.IsNotNull (f, "#8");
//Assert.AreEqual (String.Empty, f.DefaultString, "#9");
//Assert.AreEqual (0, f.MaxCharCount, "#10");
f = EncoderFallback.ReplacementFallback as EncoderReplacementFallback;
Assert.AreEqual ("?", f.DefaultString, "#11");
Assert.AreEqual (1, f.MaxCharCount, "#12");
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void DontChangeReadOnlyUTF8EncoderFallback ()
{
Encoding.UTF8.EncoderFallback =
new EncoderReplacementFallback ();
}
[Test]
public void DontChangeReadOnlyCodePageEncoderFallback ()
{
Encoding encoding = Encoding.GetEncoding (Encoding.Default.CodePage);
try {
encoding.EncoderFallback = new EncoderReplacementFallback ();
Assert.Fail ("#1");
} catch (InvalidOperationException ex) {
Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
}
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void CustomEncodingSetEncoderFallback ()
{
new MyEncoding ().EncoderFallback =
new EncoderReplacementFallback ();
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void EncodingSetNullEncoderFallback ()
{
Encoding.Default.EncoderFallback = null;
}
[Test]
// Don't throw an exception
public void SetEncoderFallback ()
{
Encoding.Default.GetEncoder ().Fallback =
new EncoderReplacementFallback ();
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void EncoderSetNullFallback ()
{
Encoding.Default.GetEncoder ().Fallback = null;
}
[Test]
public void Latin1Replacement ()
// coz Latin1 is easy single byte encoding.
{
Encoding enc = Encoding.GetEncoding (28591); // Latin1
byte [] reference = new byte [] {0x58, 0x58, 0x3F, 0x3F, 0x3F, 0x5A, 0x5A};
byte [] bytes = enc.GetBytes ("XX\u3007\u4E00\u9780ZZ");
Assert.AreEqual (reference, bytes, "#1");
}
}
}
#endif

View File

@ -0,0 +1,117 @@
//
// EncoderTest.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com>
//
// (C) 2006 Novell, Inc.
//
using NUnit.Framework;
using System;
using System.Text;
namespace MonoTests.System.Text
{
[TestFixture]
public class EncoderTest
{
#if NET_2_0
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void ConvertNullChars ()
{
int bytesUsed, charsUsed;
bool done;
Encoding.UTF8.GetEncoder ().Convert (
null, 0, 100, new byte [100], 0, 100, false,
out bytesUsed, out charsUsed, out done);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void ConvertNullBytes ()
{
int bytesUsed, charsUsed;
bool done;
Encoding.UTF8.GetEncoder ().Convert (
new char [100], 0, 100, null, 0, 100, false,
out bytesUsed, out charsUsed, out done);
}
[Test]
public void ConvertLimitedDestination ()
{
byte [] bytes = new byte [10000];
char [] chars = new char [10000];
Encoder conv = Encoding.UTF8.GetEncoder ();
int bytesUsed, charsUsed;
bool done;
conv.Convert (chars, 0, 10000, bytes, 0, 1000, true,
out bytesUsed, out charsUsed, out done);
Assert.IsFalse (done, "#1");
Assert.AreEqual (625, bytesUsed, "#2");
Assert.AreEqual (625, charsUsed, "#3");
}
[Test]
public void CustomEncodingGetEncoder ()
{
var encoding = new CustomEncoding ();
var encoder = encoding.GetEncoder ();
Assert.IsNotNull (encoder);
}
[Test]
public void ConvertZeroCharacters ()
{
int charsUsed, bytesUsed;
bool completed;
byte [] bytes = new byte [0];
Encoding.UTF8.GetEncoder ().Convert (
new char[0], 0, 0, bytes, 0, bytes.Length, true,
out charsUsed, out bytesUsed, out completed);
Assert.IsTrue (completed, "#1");
Assert.AreEqual (0, charsUsed, "#2");
Assert.AreEqual (0, bytesUsed, "#3");
}
class CustomEncoding : Encoding {
public override int GetByteCount (char [] chars, int index, int count)
{
throw new NotSupportedException ();
}
public override int GetBytes (char [] chars, int charIndex, int charCount, byte [] bytes, int byteIndex)
{
throw new NotSupportedException ();
}
public override int GetCharCount (byte [] bytes, int index, int count)
{
throw new NotSupportedException ();
}
public override int GetChars (byte [] bytes, int byteIndex, int byteCount, char [] chars, int charIndex)
{
throw new NotSupportedException ();
}
public override int GetMaxByteCount (int charCount)
{
throw new NotSupportedException ();
}
public override int GetMaxCharCount (int byteCount)
{
throw new NotSupportedException ();
}
}
#endif
}
}

View File

@ -0,0 +1,71 @@
//
// EncodingInfoTest.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com>
//
// (C) 2006 Novell, Inc.
//
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
namespace MonoTests.System.Text
{
[TestFixture]
[Category ("MobileNotWorking")]
public class EncodingInfoTest
{
[Test]
// The purpose of this test is to make sure that
// new encodings added to I18N are also listed in the
// returned array from Encoding.GetEncodings() so that
// we can make sure to put additional encodings into
// Encoding.GetEncodings() code.
public void EncodingGetEncodingsReturnsAll ()
{
// Make sure that those I18N assemblies are loaded.
string basePath = Assembly.GetAssembly (typeof (int)).CodeBase;
basePath = basePath.Substring (0, basePath.LastIndexOf ('/'));
Assert.IsNotNull (Assembly.LoadFrom (basePath + "/I18N.West.dll"), "West");
Assert.IsNotNull (Assembly.LoadFrom (basePath + "/I18N.CJK.dll"), "CJK");
Assert.IsNotNull (Assembly.LoadFrom (basePath + "/I18N.MidEast.dll"), "MidEast");
Assert.IsNotNull (Assembly.LoadFrom (basePath + "/I18N.Rare.dll"), "Rare");
Assert.IsNotNull (Assembly.LoadFrom (basePath + "/I18N.Other.dll"), "Other");
List<int> list = new List<int> ();
for (int i = 1; i < 0x10000; i++) {
// Do this in a method to work around #5432
GetEncoding (i, list);
}
int [] reference = list.ToArray ();
EncodingInfo [] infos = Encoding.GetEncodings ();
int [] actual = new int [infos.Length];
for (int i = 0; i < infos.Length; i++)
actual [i] = infos [i].CodePage;
Assert.AreEqual (reference, actual);
}
[Test]
public void GetEncodingForAllInfo ()
{
foreach (EncodingInfo i in Encoding.GetEncodings ())
Assert.IsNotNull (i.GetEncoding (), "codepage " + i);
}
void GetEncoding (int id, List<int> list) {
try {
Encoding.GetEncoding (id);
list.Add (id);
} catch {
}
}
}
}

View File

@ -0,0 +1,130 @@
//
// EncodingTest.cs - Unit Tests for System.Text.Encoding
//
// Author:
// Gert Driesen (drieseng@users.sourceforge.net)
//
// Copyright (C) 2007 Gert Driesen
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Text;
using NUnit.Framework;
namespace MonoTests.System.Text
{
[TestFixture]
public class EncodingTest
{
[Test]
[Category ("NotWorking")]
[ExpectedException (typeof (NotSupportedException))]
public void IsBrowserDisplay ()
{
MyEncoding enc = new MyEncoding ();
Assert.IsFalse (enc.IsBrowserDisplay);
}
[Test]
[Category ("NotWorking")]
[ExpectedException (typeof (NotSupportedException))]
public void IsBrowserSave ()
{
MyEncoding enc = new MyEncoding ();
Assert.IsFalse (enc.IsBrowserSave);
}
[Test]
[Category ("NotWorking")]
[ExpectedException (typeof (NotSupportedException))]
public void IsMailNewsDisplay ()
{
MyEncoding enc = new MyEncoding ();
Assert.IsFalse (enc.IsMailNewsDisplay);
}
[Test]
[Category ("NotWorking")]
[ExpectedException (typeof (NotSupportedException))]
public void IsMailNewsSave ()
{
MyEncoding enc = new MyEncoding ();
Assert.IsFalse (enc.IsMailNewsSave);
}
[Test]
public void GetEncoding_CodePage_Default ()
{
Assert.AreEqual (Encoding.Default, Encoding.GetEncoding (0));
}
[Test]
public void GetEncoding_CodePage_Invalid ()
{
try {
Encoding.GetEncoding (-1);
Assert.Fail ("#A1");
} catch (ArgumentOutOfRangeException ex) {
Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
Assert.IsNull (ex.InnerException, "#A3");
Assert.IsNotNull (ex.Message, "#A4");
Assert.IsNotNull (ex.ParamName, "#A5");
Assert.AreEqual ("codepage", ex.ParamName, "#A6");
}
try {
Encoding.GetEncoding (65536);
Assert.Fail ("#B1");
} catch (ArgumentOutOfRangeException ex) {
Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
Assert.IsNull (ex.InnerException, "#B3");
Assert.IsNotNull (ex.Message, "#B4");
Assert.IsNotNull (ex.ParamName, "#B5");
Assert.AreEqual ("codepage", ex.ParamName, "#B6");
}
}
[Test]
public void GetEncoding_Name_NotSupported ()
{
try {
Encoding.GetEncoding ("doesnotexist");
Assert.Fail ("#1");
} catch (ArgumentException ex) {
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.IsTrue (ex.Message.IndexOf ("doesnotexist") != -1, "#5");
Assert.IsNotNull (ex.ParamName, "#6");
Assert.AreEqual ("name", ex.ParamName, "#7");
}
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void GetEncoding_Name_Null ()
{
Encoding.GetEncoding ((string) null);
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,61 @@
//
// TestEncoding.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com>
//
// Copyright (C) 2005 Novell, Inc. http://www.novell.com
//
// Used for testing custom encoding.
//
using System;
using System.IO;
using System.Text;
using NUnit.Framework;
namespace MonoTests.System.Text
{
internal class MyEncoding : Encoding
{
public MyEncoding ()
{
}
public MyEncoding (int codepage)
: base (codepage)
{
}
public override int GetByteCount (char [] chars, int index, int count)
{
return 0;
}
public override int GetBytes (char [] chars, int charIndex, int charCount, byte [] bytes, int byteIndex)
{
return 0;
}
public override int GetCharCount (byte [] bytes, int index, int count)
{
return 0;
}
public override int GetChars (byte [] bytes, int byteIndex, int byteCount, char [] chars, int charIndex)
{
return 0;
}
public override int GetMaxByteCount (int length)
{
return 0;
}
public override int GetMaxCharCount (int length)
{
return 0;
}
}
}

View File

@ -0,0 +1,280 @@
#if NET_2_0
using System;
using System.Text;
using NUnit.Framework;
namespace MonoTests.System.Text
{
[TestFixture]
public class UTF32EncodingTest
{
[Test] // GetByteCount (Char [])
[Category ("NotDotNet")] // A1/B1 return 24 on MS
public void GetByteCount1 ()
{
char [] chars = new char[] { 'z', 'a', '\u0306',
'\u01FD', '\u03B2', '\uD8FF', '\uDCFF' };
UTF32Encoding le = new UTF32Encoding (false, true);
Assert.AreEqual (28, le.GetByteCount (chars), "#A1");
Assert.AreEqual (0, le.GetByteCount (new char [0]), "#A2");
UTF32Encoding be = new UTF32Encoding (true, true);
Assert.AreEqual (28, be.GetByteCount (chars), "#B1");
Assert.AreEqual (0, be.GetByteCount (new char [0]), "#B2");
}
[Test] // GetByteCount (Char [])
public void GetByteCount1_Chars_Null ()
{
UTF32Encoding enc = new UTF32Encoding ();
try {
enc.GetByteCount ((Char []) null);
Assert.Fail ("#1");
} catch (ArgumentNullException ex) {
Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.AreEqual ("chars", ex.ParamName, "#5");
}
}
[Test] // GetByteCount (String)
[Category ("NotDotNet")] // A1/B1 return 24 on MS
public void GetByteCount2 ()
{
string s = "za\u0306\u01FD\u03B2\uD8FF\uDCFF";
UTF32Encoding le = new UTF32Encoding (false, true);
Assert.AreEqual (28, le.GetByteCount (s), "#A1");
Assert.AreEqual (0, le.GetByteCount (string.Empty), "#A2");
UTF32Encoding be = new UTF32Encoding (true, true);
Assert.AreEqual (28, be.GetByteCount (s), "#B1");
Assert.AreEqual (0, be.GetByteCount (string.Empty), "#B2");
}
[Test] // GetByteCount (String)
public void GetByteCount2_S_Null ()
{
UTF32Encoding enc = new UTF32Encoding ();
try {
enc.GetByteCount ((string) null);
Assert.Fail ("#1");
} catch (ArgumentNullException ex) {
Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.AreEqual ("s", ex.ParamName, "#5");
}
}
[Test] // GetByteCount (Char *)
public unsafe void GetByteCount3 ()
{
char [] chars = new char[] { 'z', 'a', '\u0306',
'\u01FD', '\u03B2', '\uD8FF', '\uDCFF' };
fixed (char* cp = chars) {
UTF32Encoding le = new UTF32Encoding (false, true);
Assert.AreEqual (12, le.GetByteCount (cp, 3), "#A1");
Assert.AreEqual (4, le.GetByteCount (cp, 1), "#A2");
Assert.AreEqual (0, le.GetByteCount (cp, 0), "#A3");
Assert.AreEqual (24, le.GetByteCount (cp, 6), "#A4");
//Assert.AreEqual (24, le.GetByteCount (cp, 7), "#A5");
UTF32Encoding be = new UTF32Encoding (true, true);
Assert.AreEqual (12, be.GetByteCount (cp, 3), "#B1");
Assert.AreEqual (4, be.GetByteCount (cp, 1), "#B2");
Assert.AreEqual (0, be.GetByteCount (cp, 0), "#B3");
Assert.AreEqual (24, be.GetByteCount (cp, 6), "#B4");
//Assert.AreEqual (24, be.GetByteCount (cp, 7), "#B5");
}
}
[Test] // GetByteCount (Char *)
public unsafe void GetByteCount3_Chars_Null ()
{
UTF32Encoding enc = new UTF32Encoding ();
try {
enc.GetByteCount ((char *) null, 1);
Assert.Fail ("#1");
} catch (ArgumentNullException ex) {
Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.AreEqual ("chars", ex.ParamName, "#5");
}
}
[Test] // GetByteCount (Char [], Int32, Int32)
public void GetByteCount4 ()
{
char [] chars = new char[] { 'z', 'a', '\u0306',
'\u01FD', '\u03B2', '\uD8FF', '\uDCFF' };
UTF32Encoding le = new UTF32Encoding (false, true);
Assert.AreEqual (12, le.GetByteCount (chars, 0, 3), "#A1");
Assert.AreEqual (16, le.GetByteCount (chars, 2, 4), "#A2");
Assert.AreEqual (4, le.GetByteCount (chars, 4, 1), "#A3");
Assert.AreEqual (4, le.GetByteCount (chars, 6, 1), "#A4");
Assert.AreEqual (0, le.GetByteCount (chars, 6, 0), "#A5");
Assert.AreEqual (24, le.GetByteCount (chars, 0, 6), "#A6");
//Assert.AreEqual (24, le.GetByteCount (chars, 0, 7), "#A7");
UTF32Encoding be = new UTF32Encoding (true, true);
Assert.AreEqual (12, be.GetByteCount (chars, 0, 3), "#B1");
Assert.AreEqual (16, be.GetByteCount (chars, 2, 4), "#B2");
Assert.AreEqual (4, be.GetByteCount (chars, 4, 1), "#B3");
Assert.AreEqual (4, be.GetByteCount (chars, 6, 1), "#B4");
Assert.AreEqual (0, be.GetByteCount (chars, 6, 0), "#B5");
Assert.AreEqual (24, be.GetByteCount (chars, 0, 6), "#B6");
//Assert.AreEqual (24, be.GetByteCount (chars, 0, 6), "#B7");
}
[Test] // GetByteCount (Char [], Int32, Int32)
public void GetByteCount4_Chars_Null ()
{
UTF32Encoding enc = new UTF32Encoding ();
try {
enc.GetByteCount ((Char []) null, 0, 1);
Assert.Fail ("#1");
} catch (ArgumentNullException ex) {
Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.AreEqual ("chars", ex.ParamName, "#5");
}
}
[Test] // GetByteCount (Char [], Int32, Int32)
public void GetByteCount4_Count_Negative ()
{
char [] chars = new char[] { 'z', 'a', '\u0306',
'\u01FD', '\u03B2', '\uD8FF', '\uDCFF' };
UTF32Encoding enc = new UTF32Encoding ();
try {
enc.GetByteCount (chars, 1, -1);
Assert.Fail ("#1");
} catch (ArgumentOutOfRangeException ex) {
// Non-negative number required
Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.AreEqual ("count", ex.ParamName, "#5");
}
}
[Test] // GetByteCount (Char [], Int32, Int32)
public void GetByteCount4_Count_Overflow ()
{
char [] chars = new char[] { 'z', 'a', '\u0306',
'\u01FD', '\u03B2', '\uD8FF', '\uDCFF' };
UTF32Encoding enc = new UTF32Encoding ();
try {
enc.GetByteCount (chars, 6, 2);
Assert.Fail ("#1");
} catch (ArgumentOutOfRangeException ex) {
// Index and count must refer to a location
// within the buffer
Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
//Assert.AreEqual ("chars", ex.ParamName, "#5");
}
}
[Test] // GetByteCount (Char [], Int32, Int32)
public void GetByteCount4_Index_Negative ()
{
char [] chars = new char[] { 'z', 'a', '\u0306',
'\u01FD', '\u03B2', '\uD8FF', '\uDCFF' };
UTF32Encoding enc = new UTF32Encoding ();
try {
enc.GetByteCount (chars, -1, 1);
Assert.Fail ("#1");
} catch (ArgumentOutOfRangeException ex) {
// Non-negative number required
Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.AreEqual ("index", ex.ParamName, "#5");
}
}
[Test] // GetByteCount (Char [], Int32, Int32)
public void GetByteCount4_Index_Overflow ()
{
char [] chars = new char[] { 'z', 'a', '\u0306',
'\u01FD', '\u03B2', '\uD8FF', '\uDCFF' };
UTF32Encoding enc = new UTF32Encoding ();
try {
enc.GetByteCount (chars, 7, 1);
Assert.Fail ("#1");
} catch (ArgumentOutOfRangeException ex) {
// Index and count must refer to a location
// within the buffer
Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
//Assert.AreEqual ("chars", ex.ParamName, "#5");
}
}
[Test]
public void GetPreamble ()
{
byte[] lePreamble = new UTF32Encoding(false, true).GetPreamble();
Assert.AreEqual (new byte [] { 0xff, 0xfe, 0, 0 }, lePreamble, "#1");
byte[] bePreamble = new UTF32Encoding(true, true).GetPreamble();
Assert.AreEqual (new byte [] { 0, 0, 0xfe, 0xff }, bePreamble, "#2");
}
[Test]
public void IsBrowserDisplay ()
{
UTF32Encoding le = new UTF32Encoding (false, true);
Assert.IsFalse (le.IsBrowserDisplay, "#1");
UTF32Encoding be = new UTF32Encoding (true, true);
Assert.IsFalse (be.IsBrowserDisplay, "#2");
}
[Test]
public void IsBrowserSave ()
{
UTF32Encoding le = new UTF32Encoding (false, true);
Assert.IsFalse (le.IsBrowserSave);
UTF32Encoding be = new UTF32Encoding (true, true);
Assert.IsFalse (be.IsBrowserSave, "#2");
}
[Test]
public void IsMailNewsDisplay ()
{
UTF32Encoding le = new UTF32Encoding (false, true);
Assert.IsFalse (le.IsMailNewsDisplay);
UTF32Encoding be = new UTF32Encoding (true, true);
Assert.IsFalse (be.IsMailNewsDisplay, "#2");
}
[Test]
public void IsMailNewsSave ()
{
UTF32Encoding le = new UTF32Encoding (false, true);
Assert.IsFalse (le.IsMailNewsSave);
UTF32Encoding be = new UTF32Encoding (true, true);
Assert.IsFalse (be.IsMailNewsSave, "#2");
}
}
}
#endif

View File

@ -0,0 +1,299 @@
//
// UTF7EncodingTest.cs - NUnit Test Cases for System.Text.UTF7Encoding
//
// Authors
// Patrick Kalkman kalkman@cistron.nl
// Sebastien Pouliot <sebastien@ximian.com>
//
// (C) 2003 Patrick Kalkman
// Copyright (C) 2004 Novell (http://www.novell.com)
//
using NUnit.Framework;
using System;
using System.Text;
using AssertType = NUnit.Framework.Assert;
namespace MonoTests.System.Text
{
[TestFixture]
public class UTF7EncodingTest
{
[Test]
public void IsBrowserDisplay ()
{
UTF7Encoding utf7 = new UTF7Encoding ();
Assert.IsTrue (!utf7.IsBrowserDisplay);
}
[Test]
public void IsBrowserSave ()
{
UTF7Encoding utf7 = new UTF7Encoding ();
Assert.IsTrue (!utf7.IsBrowserSave);
}
[Test]
public void IsMailNewsDisplay ()
{
UTF7Encoding utf7 = new UTF7Encoding ();
Assert.IsTrue (utf7.IsMailNewsDisplay);
}
[Test]
public void IsMailNewsSave ()
{
UTF7Encoding utf7 = new UTF7Encoding ();
Assert.IsTrue (utf7.IsMailNewsSave);
}
[Test]
public void TestDirectlyEncoded1()
{
// Unicode characters a-z, A-Z, 0-9 and '()_./:? are directly encoded.
string UniCodeString = "\u0061\u007A\u0041\u005A\u0030\u0039\u0027\u003F";
byte[] UTF7Bytes = null;
UTF7Encoding UTF7enc = new UTF7Encoding ();
UTF7Bytes = UTF7enc.GetBytes (UniCodeString);
Assert.AreEqual (0x61, UTF7Bytes [0], "UTF7 #1");
Assert.AreEqual (0x7A, UTF7Bytes [1], "UTF7 #2");
Assert.AreEqual (0x41, UTF7Bytes [2], "UTF7 #3");
Assert.AreEqual (0x5A, UTF7Bytes [3], "UTF7 #4");
Assert.AreEqual (0x30, UTF7Bytes [4], "UTF7 #5");
Assert.AreEqual (0x39, UTF7Bytes [5], "UTF7 #6");
Assert.AreEqual (0x27, UTF7Bytes [6], "UTF7 #7");
Assert.AreEqual (0x3F, UTF7Bytes [7], "UTF7 #8");
}
[Test]
public void TestDirectlyEncoded2()
{
// Unicode characters a-z, A-Z, 0-9 and '()_./:? are directly encoded.
string UniCodeString = "\u0061\u007A\u0041\u005A\u0030\u0039\u0027\u003F";
byte[] UTF7Bytes = new byte [8];
int Length = UniCodeString.Length;
UTF7Encoding UTF7enc = new UTF7Encoding ();
int Cnt = UTF7enc.GetBytes (UniCodeString.ToCharArray(), 0, Length, UTF7Bytes, 0);
Assert.AreEqual (0x61, UTF7Bytes [0], "UTF7 #1");
Assert.AreEqual (0x7A, UTF7Bytes [1], "UTF7 #2");
Assert.AreEqual (0x41, UTF7Bytes [2], "UTF7 #3");
Assert.AreEqual (0x5A, UTF7Bytes [3], "UTF7 #4");
Assert.AreEqual (0x30, UTF7Bytes [4], "UTF7 #5");
Assert.AreEqual (0x39, UTF7Bytes [5], "UTF7 #6");
Assert.AreEqual (0x27, UTF7Bytes [6], "UTF7 #7");
Assert.AreEqual (0x3F, UTF7Bytes [7], "UTF7 #8");
}
[Test]
public void TestEncodeOptionalEncoded()
{
string UniCodeString = "\u0021\u0026\u002A\u003B";
byte[] UTF7Bytes = null;
//Optional Characters are allowed.
UTF7Encoding UTF7enc = new UTF7Encoding (true);
UTF7Bytes = UTF7enc.GetBytes (UniCodeString);
Assert.AreEqual (0x21, UTF7Bytes [0], "UTF7 #1");
Assert.AreEqual (0x26, UTF7Bytes [1], "UTF7 #2");
Assert.AreEqual (0x2A, UTF7Bytes [2], "UTF7 #3");
Assert.AreEqual (0x3B, UTF7Bytes [3], "UTF7 #4");
//Optional characters are not allowed.
UTF7enc = new UTF7Encoding (false);
UTF7Bytes = UTF7enc.GetBytes (UniCodeString);
Assert.AreEqual (0x2B, UTF7Bytes [0], "UTF7 #5");
Assert.AreEqual (0x41, UTF7Bytes [1], "UTF7 #6");
Assert.AreEqual (0x43, UTF7Bytes [2], "UTF7 #7");
Assert.AreEqual (0x45, UTF7Bytes [3], "UTF7 #8");
Assert.AreEqual (0x41, UTF7Bytes [1], "UTF7 #6");
}
[Test]
public void TestEncodeUnicodeShifted1()
{
string UniCodeString = "\u0041\u2262\u0391\u002E";
byte[] UTF7Bytes = null;
UTF7Encoding UTF7enc = new UTF7Encoding();
UTF7Bytes = UTF7enc.GetBytes (UniCodeString);
//"A<NOT IDENTICAL TO><ALPHA>." is encoded as A+ImIDkQ-. see RFC 1642
Assert.AreEqual (0x41, UTF7Bytes [0], "UTF7 #1");
Assert.AreEqual (0x2B, UTF7Bytes [1], "UTF7 #2");
Assert.AreEqual (0x49, UTF7Bytes [2], "UTF7 #3");
Assert.AreEqual (0x6D, UTF7Bytes [3], "UTF7 #4");
Assert.AreEqual (0x49, UTF7Bytes [4], "UTF7 #5");
Assert.AreEqual (0x44, UTF7Bytes [5], "UTF7 #6");
Assert.AreEqual (0x6B, UTF7Bytes [6], "UTF7 #7");
Assert.AreEqual (0x51, UTF7Bytes [7], "UTF7 #8");
Assert.AreEqual (0x2D, UTF7Bytes [8], "UTF7 #9");
Assert.AreEqual (0x2E, UTF7Bytes [9], "UTF7 #10");
}
[Test]
public void TestEncodeUnicodeShifted2()
{
string UniCodeString = "\u0041\u2262\u0391\u002E";
byte[] UTF7Bytes = new byte [10];
int Length = UniCodeString.Length;
UTF7Encoding UTF7enc = new UTF7Encoding ();
int Cnt = UTF7enc.GetBytes (UniCodeString.ToCharArray(), 0, Length, UTF7Bytes, 0);
//"A<NOT IDENTICAL TO><ALPHA>." is encoded as A+ImIDkQ-. see RFC 1642
Assert.AreEqual (0x41, UTF7Bytes [0], "UTF7 #1");
Assert.AreEqual (0x2B, UTF7Bytes [1], "UTF7 #2");
Assert.AreEqual (0x49, UTF7Bytes [2], "UTF7 #3");
Assert.AreEqual (0x6D, UTF7Bytes [3], "UTF7 #4");
Assert.AreEqual (0x49, UTF7Bytes [4], "UTF7 #5");
Assert.AreEqual (0x44, UTF7Bytes [5], "UTF7 #6");
Assert.AreEqual (0x6B, UTF7Bytes [6], "UTF7 #7");
Assert.AreEqual (0x51, UTF7Bytes [7], "UTF7 #8");
Assert.AreEqual (0x2D, UTF7Bytes [8], "UTF7 #9");
Assert.AreEqual (0x2E, UTF7Bytes [9], "UTF7 #10");
}
[Test]
public void RFC1642_Example1 ()
{
string UniCodeString = "\u0041\u2262\u0391\u002E";
char[] expected = UniCodeString.ToCharArray ();
byte[] UTF7Bytes = new byte [] {0x41, 0x2B, 0x49, 0x6D, 0x49, 0x44, 0x6B, 0x51, 0x2D, 0x2E};
UTF7Encoding UTF7enc = new UTF7Encoding ();
char[] actual = UTF7enc.GetChars (UTF7Bytes);
// "A+ImIDkQ-." is decoded as "A<NOT IDENTICAL TO><ALPHA>." see RFC 1642
Assert.AreEqual (expected [0], actual [0], "UTF #1");
Assert.AreEqual (expected [1], actual [1], "UTF #2");
Assert.AreEqual (expected [2], actual [2], "UTF #3");
Assert.AreEqual (expected [3], actual [3], "UTF #4");
Assert.AreEqual (UniCodeString, UTF7enc.GetString (UTF7Bytes), "GetString");
}
[Test]
public void RFC1642_Example2 ()
{
string UniCodeString = "\u0048\u0069\u0020\u004D\u006F\u004D\u0020\u263A\u0021";
char[] expected = UniCodeString.ToCharArray ();
byte[] UTF7Bytes = new byte[] { 0x48, 0x69, 0x20, 0x4D, 0x6F, 0x4D, 0x20, 0x2B, 0x4A, 0x6A, 0x6F, 0x41, 0x49, 0x51, 0x2D };
UTF7Encoding UTF7enc = new UTF7Encoding ();
char[] actual = UTF7enc.GetChars (UTF7Bytes);
// "Hi Mom +Jjo-!" is decoded as "Hi Mom <WHITE SMILING FACE>!"
Assert.AreEqual (expected [0], actual [0], "UTF #1");
Assert.AreEqual (expected [1], actual [1], "UTF #2");
Assert.AreEqual (expected [2], actual [2], "UTF #3");
Assert.AreEqual (expected [3], actual [3], "UTF #4");
Assert.AreEqual (expected [4], actual [4], "UTF #5");
Assert.AreEqual (expected [5], actual [5], "UTF #6");
Assert.AreEqual (expected [6], actual [6], "UTF #7");
Assert.AreEqual (expected [7], actual [7], "UTF #8");
Assert.AreEqual (expected [8], actual [8], "UTF #9");
Assert.AreEqual (UniCodeString, UTF7enc.GetString (UTF7Bytes), "GetString");
}
[Test]
public void RFC1642_Example3 ()
{
string UniCodeString = "\u65E5\u672C\u8A9E";
char[] expected = UniCodeString.ToCharArray ();
byte[] UTF7Bytes = new byte[] { 0x2B, 0x5A, 0x65, 0x56, 0x6E, 0x4C, 0x49, 0x71, 0x65, 0x2D };
UTF7Encoding UTF7enc = new UTF7Encoding ();
char[] actual = UTF7enc.GetChars (UTF7Bytes);
// "+ZeVnLIqe-" is decoded as Japanese "nihongo"
Assert.AreEqual (expected [0], actual [0], "UTF #1");
Assert.AreEqual (expected [1], actual [1], "UTF #2");
Assert.AreEqual (expected [2], actual [2], "UTF #3");
Assert.AreEqual (UniCodeString, UTF7enc.GetString (UTF7Bytes), "GetString");
}
[Test]
public void RFC1642_Example4 ()
{
string UniCodeString = "\u0049\u0074\u0065\u006D\u0020\u0033\u0020\u0069\u0073\u0020\u00A3\u0031\u002E";
char[] expected = UniCodeString.ToCharArray ();
byte[] UTF7Bytes = new byte[] { 0x49, 0x74, 0x65, 0x6D, 0x20, 0x33, 0x20, 0x69, 0x73, 0x20, 0x2B, 0x41, 0x4B, 0x4D, 0x2D, 0x31, 0x2E };
UTF7Encoding UTF7enc = new UTF7Encoding ();
char[] actual = UTF7enc.GetChars (UTF7Bytes);
// "Item 3 is +AKM-1." is decoded as "Item 3 is <POUND SIGN>1."
Assert.AreEqual (expected [0], actual [0], "UTF #1");
Assert.AreEqual (expected [1], actual [1], "UTF #2");
Assert.AreEqual (expected [2], actual [2], "UTF #3");
Assert.AreEqual (expected [3], actual [3], "UTF #4");
Assert.AreEqual (expected [4], actual [4], "UTF #5");
Assert.AreEqual (expected [5], actual [5], "UTF #6");
Assert.AreEqual (expected [6], actual [6], "UTF #7");
Assert.AreEqual (expected [7], actual [7], "UTF #8");
Assert.AreEqual (expected [8], actual [8], "UTF #9");
Assert.AreEqual (expected [9], actual [9], "UTF #10");
Assert.AreEqual (expected [10], actual [10], "UTF #11");
Assert.AreEqual (expected [11], actual [11], "UTF #12");
Assert.AreEqual (expected [12], actual [12], "UTF #13");
Assert.AreEqual (UniCodeString, UTF7enc.GetString (UTF7Bytes), "GetString");
}
[Test]
public void TestMaxCharCount()
{
UTF7Encoding UTF7enc = new UTF7Encoding ();
Assert.AreEqual (50, UTF7enc.GetMaxCharCount(50), "UTF #1");
}
[Test]
#if NET_2_0
[Category ("NotWorking")]
#endif
public void TestMaxByteCount()
{
UTF7Encoding UTF7enc = new UTF7Encoding ();
#if NET_2_0
Assert.AreEqual (152, UTF7enc.GetMaxByteCount(50), "UTF #1");
#else
Assert.AreEqual (136, UTF7enc.GetMaxByteCount(50), "UTF #1");
#endif
}
[Test]
[ExpectedException (typeof (ArgumentException))]
[Category ("NotDotNet")] // MS bug
public void Bug77315 ()
{
string s = new UTF7Encoding ().GetString (
Encoding.ASCII.GetBytes ("+2AA-"));
}
[Test]
public void GetCharCount ()
{
string original = "*123456789*123456789*123456789*123456789*123456789*123456789*123456789*123456789";
byte [] bytes = Encoding.UTF7.GetBytes (original);
AssertType.AreEqual (112, bytes.Length, "#1");
AssertType.AreEqual (80, Encoding.UTF7.GetCharCount (bytes), "#2");
string decoded = Encoding.UTF7.GetString(Encoding.UTF7.GetBytes(original));
AssertType.AreEqual (original, decoded, "#3");
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,261 @@
//
// UnicodeEncodingTest.cs - NUnit Test Cases for System.Text.UnicodeEncoding
//
// Author:
// Patrick Kalkman kalkman@cistron.nl
//
// (C) 2003 Patrick Kalkman
//
using NUnit.Framework;
using System;
using System.Text;
namespace MonoTests.System.Text
{
[TestFixture]
public class UnicodeEncodingTest
{
[Test]
public void IsBrowserDisplay ()
{
UnicodeEncoding enc = new UnicodeEncoding ();
Assert.IsFalse (enc.IsBrowserDisplay);
}
[Test]
public void IsBrowserSave ()
{
UnicodeEncoding enc = new UnicodeEncoding ();
Assert.IsTrue (enc.IsBrowserSave);
}
[Test]
public void IsMailNewsDisplay ()
{
UnicodeEncoding enc = new UnicodeEncoding ();
Assert.IsFalse (enc.IsMailNewsDisplay);
}
[Test]
public void IsMailNewsSave ()
{
UnicodeEncoding enc = new UnicodeEncoding ();
Assert.IsFalse (enc.IsMailNewsSave);
}
[Test]
public void TestEncodingGetBytes1()
{
//pi and sigma in unicode
string Unicode = "\u03a0\u03a3";
byte[] UniBytes;
UnicodeEncoding UnicodeEnc = new UnicodeEncoding (); //little-endian
UniBytes = UnicodeEnc.GetBytes (Unicode);
Assert.AreEqual (0xA0, UniBytes [0], "Uni #1");
Assert.AreEqual (0x03, UniBytes [1], "Uni #2");
Assert.AreEqual (0xA3, UniBytes [2], "Uni #3");
Assert.AreEqual (0x03, UniBytes [3], "Uni #4");
}
[Test]
public void TestEncodingGetBytes2()
{
//pi and sigma in unicode
string Unicode = "\u03a0\u03a3";
byte[] UniBytes;
UnicodeEncoding UnicodeEnc = new UnicodeEncoding (true, true); //big-endian
UniBytes = UnicodeEnc.GetBytes (Unicode);
Assert.AreEqual (0x03, UniBytes [0], "Uni #1");
Assert.AreEqual (0xA0, UniBytes [1], "Uni #2");
Assert.AreEqual (0x03, UniBytes [2], "Uni #3");
Assert.AreEqual (0xA3, UniBytes [3], "Uni #4");
}
[Test]
public void TestEncodingGetBytes3()
{
//pi and sigma in unicode
string Unicode = "\u03a0\u03a3";
byte[] UniBytes = new byte [4];
UnicodeEncoding UnicodeEnc = new UnicodeEncoding (); //little-endian
int Cnt = UnicodeEnc.GetBytes (Unicode.ToCharArray(), 0, Unicode.Length, UniBytes, 0);
Assert.AreEqual (4, Cnt, "Uni #1");
Assert.AreEqual (0xA0, UniBytes [0], "Uni #2");
Assert.AreEqual (0x03, UniBytes [1], "Uni #3");
Assert.AreEqual (0xA3, UniBytes [2], "Uni #4");
Assert.AreEqual (0x03, UniBytes [3], "Uni #5");
}
[Test]
public void TestEncodingDecodingGetBytes1()
{
//pi and sigma in unicode
string Unicode = "\u03a0\u03a3";
UnicodeEncoding UnicodeEnc = new UnicodeEncoding (); //little-endian
//Encode the unicode string.
byte[] UniBytes = UnicodeEnc.GetBytes (Unicode);
//Decode the bytes to a unicode char array.
char[] UniChars = UnicodeEnc.GetChars (UniBytes);
string Result = new string(UniChars);
Assert.AreEqual (Unicode, Result, "Uni #1");
}
[Test]
public void TestEncodingDecodingGetBytes2()
{
//pi and sigma in unicode
string Unicode = "\u03a0\u03a3";
UnicodeEncoding UnicodeEnc = new UnicodeEncoding (true, true); //big-endian
//Encode the unicode string.
byte[] UniBytes = UnicodeEnc.GetBytes (Unicode);
//Decode the bytes to a unicode char array.
char[] UniChars = UnicodeEnc.GetChars (UniBytes);
string Result = new string(UniChars);
Assert.AreEqual (Unicode, Result, "Uni #1");
}
[Test]
public void TestEncodingGetCharCount ()
{
byte[] b = new byte[] {255, 254, 115, 0, 104, 0, 105, 0};
UnicodeEncoding encoding = new UnicodeEncoding ();
Assert.AreEqual (3, encoding.GetCharCount (b, 2, b.Length - 2),
"GetCharCount #1");
}
[Test]
public void TestPreamble1()
{
//litle-endian with byte order mark.
UnicodeEncoding UnicodeEnc = new UnicodeEncoding (false, true);
byte[] PreAmble = UnicodeEnc.GetPreamble();
Assert.AreEqual (0xFF, PreAmble [0], "Uni #1");
Assert.AreEqual (0xFE, PreAmble [1], "Uni #2");
}
[Test]
public void TestPreamble2()
{
//big-endian with byte order mark.
UnicodeEncoding UnicodeEnc = new UnicodeEncoding (true, true);
byte[] PreAmble = UnicodeEnc.GetPreamble();
Assert.AreEqual (0xFE, PreAmble [0], "Uni #1");
Assert.AreEqual (0xFF, PreAmble [1], "Uni #2");
}
[Test]
public void TestPreamble3()
{
//little-endian without byte order mark.
UnicodeEncoding UnicodeEnc = new UnicodeEncoding (false, false);
byte[] PreAmble = UnicodeEnc.GetPreamble();
Assert.AreEqual (0, PreAmble.Length, "Uni #1");
}
[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]
public void ZeroLengthArrays ()
{
UnicodeEncoding encoding = new UnicodeEncoding ();
encoding.GetCharCount (new byte [0]);
encoding.GetChars (new byte [0]);
encoding.GetCharCount (new byte [0], 0, 0);
encoding.GetChars (new byte [0], 0, 0);
encoding.GetChars (new byte [0], 0, 0, new char [0], 0);
encoding.GetByteCount (new char [0]);
encoding.GetBytes (new char [0]);
encoding.GetByteCount (new char [0], 0, 0);
encoding.GetBytes (new char [0], 0, 0);
encoding.GetBytes (new char [0], 0, 0, new byte [0], 0);
encoding.GetByteCount ("");
encoding.GetBytes ("");
}
[Test]
public void ByteOrderMark ()
{
string littleEndianString = "\ufeff\u0042\u004f\u004d";
string bigEndianString = "\ufffe\u4200\u4f00\u4d00";
byte [] littleEndianBytes = new byte [] {0xff, 0xfe, 0x42, 0x00, 0x4f, 0x00, 0x4d, 0x00};
byte [] bigEndianBytes = new byte [] {0xfe, 0xff, 0x00, 0x42, 0x00, 0x4f, 0x00, 0x4d};
UnicodeEncoding encoding;
encoding = new UnicodeEncoding (false, true);
Assert.AreEqual (encoding.GetBytes (littleEndianString), littleEndianBytes, "BOM #1");
Assert.AreEqual (encoding.GetBytes (bigEndianString), bigEndianBytes, "BOM #2");
Assert.AreEqual (encoding.GetString (littleEndianBytes), littleEndianString, "BOM #3");
Assert.AreEqual (encoding.GetString (bigEndianBytes), bigEndianString, "BOM #4");
encoding = new UnicodeEncoding (true, true);
Assert.AreEqual (encoding.GetBytes (littleEndianString), bigEndianBytes, "BOM #5");
Assert.AreEqual (encoding.GetBytes (bigEndianString), littleEndianBytes, "BOM #6");
Assert.AreEqual (encoding.GetString (littleEndianBytes), bigEndianString, "BOM #7");
Assert.AreEqual (encoding.GetString (bigEndianBytes), littleEndianString, "BOM #8");
}
[Test]
[Category ("NotWorking")]
public void GetString_Odd_Count_0 ()
{
byte [] array = new byte [3];
string s = Encoding.Unicode.GetString (array, 0, 3);
Assert.AreEqual (0, (int) s [0], "0");
Assert.AreEqual (2, s.Length, "Length");
Assert.AreEqual (65533, (int) s [1], "1");
}
[Test]
[Category ("NotWorking")]
public void GetString_Odd_Count_ff ()
{
byte [] array = new byte [3] { 0xff, 0xff, 0xff };
string s = Encoding.Unicode.GetString (array, 0, 3);
Assert.AreEqual (65535, (int) s [0], "0");
Assert.AreEqual (2, s.Length, "Length");
Assert.AreEqual (65533, (int) s [1], "1");
}
}
}