Merge pull request #1266 from esdrubal/datetimenewformat
[mono.git] / mcs / class / corlib / Test / System.Text / EncodingTester.cs
1 //
2 // EncodingTester.cs
3 //
4 // Author:
5 //      Marcos Henrich  <marcos.henrich@xamarin.com>
6 //
7 // (C) 2014 Xamarin, Inc.
8 // 
9
10 using NUnit.Framework;
11 using System;
12 using System.Collections.Generic;
13 using System.Text;
14
15 namespace MonoTests.System.Text
16 {
17         class EncodingTester
18         {
19                 class DecoderTestFallbackBuffer : DecoderFallbackBuffer
20                 {
21                         DecoderFallbackBuffer buffer;
22                         private FallbackDelegate fallbackAction;
23
24                         public DecoderTestFallbackBuffer (DecoderReplacementFallback fallback, FallbackDelegate fallbackAction)
25                         {
26                                 this.fallbackAction = fallbackAction;
27                                 buffer = new DecoderReplacementFallbackBuffer (fallback);
28                         }
29
30                         public override bool Fallback (byte [] bytesUnknown, int index)
31                         {
32                                 fallbackAction (bytesUnknown, index);
33                                 return buffer.Fallback (bytesUnknown, index);
34                         }
35
36                         public override char GetNextChar ()
37                         {
38                                 return buffer.GetNextChar ();
39                         }
40
41                         public override bool MovePrevious ()
42                         {
43                                 return buffer.MovePrevious ();
44                         }
45
46                         public override int Remaining
47                         {
48                                 get { return buffer.Remaining; }
49                         }
50
51                         public override void Reset ()
52                         {
53                                 buffer.Reset ();
54                         }
55                 }
56
57                 class DecoderTestFallback : DecoderFallback
58                 {
59                         private DecoderReplacementFallback fallback;
60                         private FallbackDelegate fallbackAction;
61
62                         public DecoderTestFallback (FallbackDelegate fallbackAction)
63                         {
64                                 this.fallbackAction = fallbackAction;
65                         }
66
67                         public override DecoderFallbackBuffer CreateFallbackBuffer ()
68                         {
69                                 fallback = new DecoderReplacementFallback ();
70                                 return new DecoderTestFallbackBuffer (fallback, fallbackAction);
71                         }
72
73                         public override int MaxCharCount
74                         {
75                                 get { return fallback.MaxCharCount; }
76                         }
77                 }
78
79                 public delegate void FallbackDelegate (byte [] bytesUnknown, int index);
80
81                 Encoding encoding;
82
83                 byte [][] expectedUnknownBytes;
84                 int expectedUnknownBytesIndex;
85
86                 public EncodingTester (string encodingName)
87                 {
88                         var decoderFallback = new DecoderTestFallback (this.DecoderFallback);
89                         encoding = Encoding.GetEncoding (encodingName, new EncoderReplacementFallback(), decoderFallback);
90                 }
91
92                 private void DecoderFallback (byte [] bytesUnknown, int index)
93                 {
94                         if (expectedUnknownBytesIndex == expectedUnknownBytes.Length)
95                                 expectedUnknownBytesIndex = 0;
96
97                         var expectedBytes = expectedUnknownBytes [expectedUnknownBytesIndex++];
98                         Assert.AreEqual (expectedBytes, bytesUnknown);
99                 }
100
101                 public void TestDecoderFallback (byte [] data, string expectedString,  params byte [][] expectedUnknownBytes)
102                 {
103                         lock (this)
104                         {
105                                 this.expectedUnknownBytes = expectedUnknownBytes;
106                                 this.expectedUnknownBytesIndex = 0;
107
108                                 Assert.AreEqual (expectedString.Length, encoding.GetCharCount (data));
109                                 Assert.AreEqual (expectedUnknownBytesIndex, expectedUnknownBytes.Length);
110
111                                 Assert.AreEqual (expectedString, encoding.GetString (data));
112                                 Assert.AreEqual (expectedUnknownBytesIndex, expectedUnknownBytes.Length);
113                         }
114                 }
115         }
116 }