5 // Marcos Henrich <marcos.henrich@xamarin.com>
7 // (C) 2014 Xamarin, Inc.
10 using NUnit.Framework;
12 using System.Collections.Generic;
15 namespace MonoTests.System.Text
19 class DecoderTestFallbackBuffer : DecoderFallbackBuffer
21 DecoderFallbackBuffer buffer;
22 private FallbackDelegate fallbackAction;
24 public DecoderTestFallbackBuffer (DecoderReplacementFallback fallback, FallbackDelegate fallbackAction)
26 this.fallbackAction = fallbackAction;
27 buffer = new DecoderReplacementFallbackBuffer (fallback);
30 public override bool Fallback (byte [] bytesUnknown, int index)
32 fallbackAction (bytesUnknown, index);
33 return buffer.Fallback (bytesUnknown, index);
36 public override char GetNextChar ()
38 return buffer.GetNextChar ();
41 public override bool MovePrevious ()
43 return buffer.MovePrevious ();
46 public override int Remaining
48 get { return buffer.Remaining; }
51 public override void Reset ()
57 class DecoderTestFallback : DecoderFallback
59 private DecoderReplacementFallback fallback;
60 private FallbackDelegate fallbackAction;
62 public DecoderTestFallback (FallbackDelegate fallbackAction)
64 this.fallbackAction = fallbackAction;
67 public override DecoderFallbackBuffer CreateFallbackBuffer ()
69 fallback = new DecoderReplacementFallback ();
70 return new DecoderTestFallbackBuffer (fallback, fallbackAction);
73 public override int MaxCharCount
75 get { return fallback.MaxCharCount; }
79 public delegate void FallbackDelegate (byte [] bytesUnknown, int index);
83 byte [][] expectedUnknownBytes;
84 int expectedUnknownBytesIndex;
86 public EncodingTester (string encodingName)
88 var decoderFallback = new DecoderTestFallback (this.DecoderFallback);
89 encoding = Encoding.GetEncoding (encodingName, new EncoderReplacementFallback(), decoderFallback);
92 private void DecoderFallback (byte [] bytesUnknown, int index)
94 if (expectedUnknownBytesIndex == expectedUnknownBytes.Length)
95 expectedUnknownBytesIndex = 0;
97 var expectedBytes = expectedUnknownBytes [expectedUnknownBytesIndex++];
98 Assert.AreEqual (expectedBytes, bytesUnknown);
101 public void TestDecoderFallback (byte [] data, string expectedString, params byte [][] expectedUnknownBytes)
105 this.expectedUnknownBytes = expectedUnknownBytes;
106 this.expectedUnknownBytesIndex = 0;
108 Assert.AreEqual (expectedString.Length, encoding.GetCharCount (data));
109 Assert.AreEqual (expectedUnknownBytesIndex, expectedUnknownBytes.Length);
111 Assert.AreEqual (expectedString, encoding.GetString (data));
112 Assert.AreEqual (expectedUnknownBytesIndex, expectedUnknownBytes.Length);