Merge branch 'master' of https://github.com/mono/mono
[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 = Encoding.UTF8.GetDecoder ();
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
60                 [Test]
61                 public void CustomEncodingGetDecoder ()
62                 {
63                         var encoding = new CustomEncoding ();
64                         var decoder = encoding.GetDecoder ();
65                         Assert.IsNotNull (decoder);
66                 }
67
68                 class CustomEncoding : Encoding {
69
70                         public override int GetByteCount (char [] chars, int index, int count)
71                         {
72                                 throw new NotSupportedException ();
73                         }
74
75                         public override int GetBytes (char [] chars, int charIndex, int charCount, byte [] bytes, int byteIndex)
76                         {
77                                 throw new NotSupportedException ();
78                         }
79
80                         public override int GetCharCount (byte [] bytes, int index, int count)
81                         {
82                                 throw new NotSupportedException ();
83                         }
84
85                         public override int GetChars (byte [] bytes, int byteIndex, int byteCount, char [] chars, int charIndex)
86                         {
87                                 throw new NotSupportedException ();
88                         }
89
90                         public override int GetMaxByteCount (int charCount)
91                         {
92                                 throw new NotSupportedException ();
93                         }
94
95                         public override int GetMaxCharCount (int byteCount)
96                         {
97                                 throw new NotSupportedException ();
98                         }
99                 }
100 #endif
101
102                 [Test]
103                 public void Bug10789 ()
104                 {
105                         byte[] bytes = new byte[100];
106                         char[] chars = new char[100];  
107
108                         Decoder conv = Encoding.UTF8.GetDecoder ();
109                         int charsUsed, bytesUsed;
110                         bool completed;
111                         
112                         conv.Convert (bytes, 0, 0, chars, 100, 0, false, out bytesUsed, out charsUsed, out completed);
113
114                         Assert.IsTrue (completed, "#1");
115                         Assert.AreEqual (0, charsUsed, "#2");
116                         Assert.AreEqual (0, bytesUsed, "#3");
117                 }
118         }
119 }