Debian Codegraph @

mcs/class/I18N/MidEast/Test/I18N.MidEast.Test.cs

raw symbols SCIP
1 //
2 // I18N.MidEast.Test.cs
3 //
4 // Author:
5 // Mikko Korkalo <[email protected]>
6 // Based on I18N.CJK.Test.cs by Atsushi Enomoto
7 //
8 // Copyright (C) 2005 Novell, Inc. http://www.novell.com
9 // Copyright (C) 2013 Mikko Korkalo
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 //MidEast codepages
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 //Manager does not support EncoderFallback, and changing it afterwards seems not possible.
47 //Encoding enc = Manager.GetEncoding(codepage);
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 // simple string case
60 //Assert.AreEqual (encoded.Length,
61 // enc.GetByteCount (decoded),
62 // "GetByteCount(string)");
63 actual = enc.GetBytes (decoded);
64 Assert.AreEqual (encoded, actual,
65 "GetBytes(string)");
66
67 // simple char[] case
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 }