Merge pull request #1304 from slluis/mac-proxy-autoconfig
[mono.git] / mcs / class / corlib / Test / System.Text / DecoderTest.cs
1 //
2 // DecoderTest.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 DecoderTest
17         {
18 #if NET_2_0
19                 [Test]
20                 [ExpectedException (typeof (ArgumentNullException))]
21                 public void ConvertNullChars ()
22                 {
23                         int charsUsed, bytesUsed;
24                         bool done;
25                         Encoding.UTF8.GetDecoder ().Convert (
26                                 null, 0, 100, new char [100], 0, 100, false,
27                                 out charsUsed, out bytesUsed, out done);
28                 }
29
30                 [Test]
31                 [ExpectedException (typeof (ArgumentNullException))]
32                 public void ConvertNullBytes ()
33                 {
34                         int charsUsed, bytesUsed;
35                         bool done;
36                         Encoding.UTF8.GetDecoder ().Convert (
37                                 new byte [100], 0, 100, null, 0, 100, false,
38                                 out charsUsed, out bytesUsed, out done);
39                 }
40
41                 [Test]
42                 public void ConvertLimitedDestination ()
43                 {
44                         char [] chars = new char [10000];
45                         byte [] bytes = new byte [10000];
46
47                         Decoder conv = new ExposedDecoder ();
48                         int charsUsed, bytesUsed;
49                         bool done;
50
51                         conv.Convert (bytes, 0, 10000, chars, 0, 1000, true,
52                                       out charsUsed, out bytesUsed, out done);
53
54                         Assert.IsFalse (done, "#1");
55                         Assert.AreEqual (625, charsUsed, "#2");
56                         Assert.AreEqual (625, bytesUsed, "#3");
57                 }
58
59                 [Test]
60                 public void ConvertLimitedDestinationUTF8 ()
61                 {
62                         char [] chars = new char [10000];
63                         byte [] bytes = new byte [10000];
64
65                         Decoder conv = Encoding.UTF8.GetDecoder ();
66                         int charsUsed, bytesUsed;
67                         bool done;
68
69                         conv.Convert (bytes, 0, 10000, chars, 0, 1000, true,
70                                       out charsUsed, out bytesUsed, out done);
71
72                         Assert.IsFalse (done, "#1");
73                         Assert.AreEqual (1000, charsUsed, "#2");
74                         Assert.AreEqual (1000, bytesUsed, "#3");
75                 }
76
77
78                 [Test]
79                 public void CustomEncodingGetDecoder ()
80                 {
81                         var encoding = new CustomEncoding ();
82                         var decoder = encoding.GetDecoder ();
83                         Assert.IsNotNull (decoder);
84                 }
85
86                 class ExposedDecoder : Decoder {
87                         public override int GetCharCount (byte [] bytes, int index, int count)
88                         {
89                                 return Encoding.UTF8.GetDecoder ().GetCharCount (bytes, index, count);
90                         }
91
92                         public override int GetChars (byte [] bytes, int byteIndex, int byteCount, char [] chars, int charIndex)
93                         {
94                                 return Encoding.UTF8.GetDecoder ().GetChars (bytes, byteIndex, byteCount, chars, charIndex);
95                         }
96                 }
97
98                 class CustomEncoding : Encoding {
99
100                         public override int GetByteCount (char [] chars, int index, int count)
101                         {
102                                 throw new NotSupportedException ();
103                         }
104
105                         public override int GetBytes (char [] chars, int charIndex, int charCount, byte [] bytes, int byteIndex)
106                         {
107                                 throw new NotSupportedException ();
108                         }
109
110                         public override int GetCharCount (byte [] bytes, int index, int count)
111                         {
112                                 throw new NotSupportedException ();
113                         }
114
115                         public override int GetChars (byte [] bytes, int byteIndex, int byteCount, char [] chars, int charIndex)
116                         {
117                                 throw new NotSupportedException ();
118                         }
119
120                         public override int GetMaxByteCount (int charCount)
121                         {
122                                 throw new NotSupportedException ();
123                         }
124
125                         public override int GetMaxCharCount (int byteCount)
126                         {
127                                 throw new NotSupportedException ();
128                         }
129                 }
130 #endif
131
132                 [Test]
133                 public void Bug10789 ()
134                 {
135                         byte[] bytes = new byte[100];
136                         char[] chars = new char[100];  
137
138                         Decoder conv = Encoding.UTF8.GetDecoder ();
139                         int charsUsed, bytesUsed;
140                         bool completed;
141                         
142                         conv.Convert (bytes, 0, 0, chars, 100, 0, false, out bytesUsed, out charsUsed, out completed);
143
144                         Assert.IsTrue (completed, "#1");
145                         Assert.AreEqual (0, charsUsed, "#2");
146                         Assert.AreEqual (0, bytesUsed, "#3");
147                 }
148         }
149 }