Merge pull request #439 from mono-soc-2012/garyb/iconfix
[mono.git] / mcs / class / corlib / Test / System.Text / EncoderTest.cs
1 //
2 // EncoderTest.cs
3 //
4 // Author:
5 //      Atsushi Enomoto  <atsushi@ximian.com>
6 //
7 // (C) 2006 Novell, Inc.
8 // 
9 using NUnit.Framework;
10 using System;
11 using System.Text;
12
13 namespace MonoTests.System.Text
14 {
15         [TestFixture]
16         public class EncoderTest
17         {
18 #if NET_2_0
19                 [Test]
20                 [ExpectedException (typeof (ArgumentNullException))]
21                 public void ConvertNullChars ()
22                 {
23                         int bytesUsed, charsUsed;
24                         bool done;
25                         Encoding.UTF8.GetEncoder ().Convert (
26                                 null, 0, 100, new byte [100], 0, 100, false,
27                                 out bytesUsed, out charsUsed, out done);
28                 }
29
30                 [Test]
31                 [ExpectedException (typeof (ArgumentNullException))]
32                 public void ConvertNullBytes ()
33                 {
34                         int bytesUsed, charsUsed;
35                         bool done;
36                         Encoding.UTF8.GetEncoder ().Convert (
37                                 new char [100], 0, 100, null, 0, 100, false,
38                                 out bytesUsed, out charsUsed, out done);
39                 }
40
41                 [Test]
42                 public void ConvertLimitedDestination ()
43                 {
44                         byte [] bytes = new byte [10000];
45                         char [] chars = new char [10000];
46
47                         Encoder conv = Encoding.UTF8.GetEncoder ();
48                         int bytesUsed, charsUsed;
49                         bool done;
50
51                         conv.Convert (chars, 0, 10000, bytes, 0, 1000, true,
52                                       out bytesUsed, out charsUsed, out done);
53
54                         Assert.IsFalse (done, "#1");
55                         Assert.AreEqual (625, bytesUsed, "#2");
56                         Assert.AreEqual (625, charsUsed, "#3");
57                 }
58
59                 [Test]
60                 public void CustomEncodingGetEncoder ()
61                 {
62                         var encoding = new CustomEncoding ();
63                         var encoder = encoding.GetEncoder ();
64                         Assert.IsNotNull (encoder);
65                 }
66
67                 [Test]
68                 public void ConvertZeroCharacters ()
69                 {
70                         int charsUsed, bytesUsed;
71                         bool completed;
72                         byte [] bytes = new byte [0];
73
74                         Encoding.UTF8.GetEncoder ().Convert (
75                                 new char[0], 0, 0, bytes, 0, bytes.Length, true,
76                                 out charsUsed, out bytesUsed, out completed);
77
78                         Assert.IsTrue (completed, "#1");
79                         Assert.AreEqual (0, charsUsed, "#2");
80                         Assert.AreEqual (0, bytesUsed, "#3");
81                 }
82
83                 class CustomEncoding : Encoding {
84
85                         public override int GetByteCount (char [] chars, int index, int count)
86                         {
87                                 throw new NotSupportedException ();
88                         }
89
90                         public override int GetBytes (char [] chars, int charIndex, int charCount, byte [] bytes, int byteIndex)
91                         {
92                                 throw new NotSupportedException ();
93                         }
94
95                         public override int GetCharCount (byte [] bytes, int index, int count)
96                         {
97                                 throw new NotSupportedException ();
98                         }
99
100                         public override int GetChars (byte [] bytes, int byteIndex, int byteCount, char [] chars, int charIndex)
101                         {
102                                 throw new NotSupportedException ();
103                         }
104
105                         public override int GetMaxByteCount (int charCount)
106                         {
107                                 throw new NotSupportedException ();
108                         }
109
110                         public override int GetMaxCharCount (int byteCount)
111                         {
112                                 throw new NotSupportedException ();
113                         }
114                 }
115 #endif
116         }
117 }