| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
using System; |
| 13 |
using System.IO; |
| 14 |
using System.Text; |
| 15 |
using NUnit.Framework; |
| 16 |
|
| 17 |
using MonoTests.Helpers; |
| 18 |
|
| 19 |
namespace MonoTests.I18N.MidEast |
| 20 |
{ |
| 21 |
[TestFixture] |
| 22 |
public class TestMidEast |
| 23 |
{ |
| 24 |
private global::I18N.Common.Manager Manager = global::I18N.Common.Manager.PrimaryManager; |
| 25 |
|
| 26 |
|
| 27 |
int[] cps = { 1254, 28599, 1254, 1255, 28598, 28596, 38598, 1256 }; |
| 28 |
|
| 29 |
void AssertEncode (string utf8file, string decfile, int codepage) |
| 30 |
{ |
| 31 |
AssertEncode(utf8file, decfile, codepage, null); |
| 32 |
} |
| 33 |
void AssertEncode (string utf8file, string decfile, int codepage, |
| 34 |
string fallbackString) |
| 35 |
{ |
| 36 |
string decoded = null; |
| 37 |
byte [] encoded = null; |
| 38 |
using (StreamReader sr = new StreamReader (utf8file, |
| 39 |
Encoding.UTF8)) { |
| 40 |
decoded = sr.ReadToEnd (); |
| 41 |
} |
| 42 |
using (FileStream fs = File.OpenRead (decfile)) { |
| 43 |
encoded = new byte [fs.Length]; |
| 44 |
fs.Read (encoded, 0, (int) fs.Length); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
Encoding enc; |
| 49 |
if (fallbackString != null) { |
| 50 |
enc = Encoding.GetEncoding(codepage, |
| 51 |
new System.Text.EncoderReplacementFallback(fallbackString), |
| 52 |
new System.Text.DecoderReplacementFallback("irrelevant") |
| 53 |
); |
| 54 |
} else { |
| 55 |
enc = Encoding.GetEncoding(codepage); |
| 56 |
} |
| 57 |
byte [] actual; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
actual = enc.GetBytes (decoded); |
| 64 |
Assert.AreEqual (encoded, actual, |
| 65 |
"GetBytes(string)"); |
| 66 |
|
| 67 |
|
| 68 |
Assert.AreEqual (encoded.Length, |
| 69 |
enc.GetByteCount (decoded.ToCharArray (), 0, decoded.Length), |
| 70 |
"GetByteCount(char[], 0, len)"); |
| 71 |
actual = enc.GetBytes (decoded.ToCharArray (), 0, decoded.Length); |
| 72 |
Assert.AreEqual (encoded, actual, |
| 73 |
"GetBytes(char[], 0, len)"); |
| 74 |
} |
| 75 |
|
| 76 |
void AssertDecode (string utf8file, string decfile, int codepage) |
| 77 |
{ |
| 78 |
string decoded = null; |
| 79 |
byte [] encoded = null; |
| 80 |
using (StreamReader sr = new StreamReader (utf8file, |
| 81 |
Encoding.UTF8)) { |
| 82 |
decoded = sr.ReadToEnd (); |
| 83 |
} |
| 84 |
using (FileStream fs = File.OpenRead (decfile)) { |
| 85 |
encoded = new byte [fs.Length]; |
| 86 |
fs.Read (encoded, 0, (int) fs.Length); |
| 87 |
} |
| 88 |
Encoding enc = Manager.GetEncoding (codepage); |
| 89 |
char [] actual; |
| 90 |
|
| 91 |
Assert.AreEqual (decoded.Length, |
| 92 |
enc.GetCharCount (encoded, 0, encoded.Length), |
| 93 |
"GetCharCount(byte[], 0, len)"); |
| 94 |
actual = enc.GetChars (encoded, 0, encoded.Length); |
| 95 |
Assert.AreEqual (decoded.ToCharArray (), actual, |
| 96 |
"GetChars(byte[], 0, len)"); |
| 97 |
} |
| 98 |
|
| 99 |
public void HandleFallback_Encode_Generic(int cp) |
| 100 |
{ |
| 101 |
AssertEncode (TestResourceHelper.GetFullPathOfResource ("Test/texts/encoder-handlefallback-generic-utf8.txt"), TestResourceHelper.GetFullPathOfResource ("Test/texts/encoder-handlefallback-generic-output.txt"), cp, "error_foobar"); |
| 102 |
} |
| 103 |
|
| 104 |
[Test] |
| 105 |
public void HandleFallback_Encode_All() |
| 106 |
{ |
| 107 |
foreach (int cp in cps) |
| 108 |
HandleFallback_Encode_Generic(cp); |
| 109 |
} |
| 110 |
[Test] |
| 111 |
public void Ascii_Test_All() |
| 112 |
{ |
| 113 |
foreach (int cp in cps) |
| 114 |
AssertEncode(TestResourceHelper.GetFullPathOfResource ("Test/texts/ascii-test.txt"), TestResourceHelper.GetFullPathOfResource ("Test/texts/ascii-test.txt"), cp); |
| 115 |
} |
| 116 |
|
| 117 |
[Test] |
| 118 |
public void CP1254_Encode () |
| 119 |
{ |
| 120 |
AssertEncode (TestResourceHelper.GetFullPathOfResource ("Test/texts/turkish-utf8.txt"), TestResourceHelper.GetFullPathOfResource ("Test/texts/turkish-1254.txt"), 1254); |
| 121 |
} |
| 122 |
} |
| 123 |
} |