Merge pull request #439 from mono-soc-2012/garyb/iconfix
[mono.git] / mcs / class / corlib / Test / System.Text / EncodingInfoTest.cs
1 //
2 // EncodingInfoTest.cs
3 //
4 // Author:
5 //      Atsushi Enomoto  <atsushi@ximian.com>
6 //
7 // (C) 2006 Novell, Inc.
8 // 
9
10 #if NET_2_0
11
12 using NUnit.Framework;
13 using System;
14 using System.Collections.Generic;
15 using System.IO;
16 using System.Reflection;
17 using System.Text;
18
19 namespace MonoTests.System.Text
20 {
21         [TestFixture]
22         public class EncodingInfoTest
23         {
24                 [Test]
25                 // The purpose of this test is to make sure that
26                 // new encodings added to I18N are also listed in the
27                 // returned array from Encoding.GetEncodings() so that
28                 // we can make sure to put additional encodings into
29                 // Encoding.GetEncodings() code.
30                 public void EncodingGetEncodingsReturnsAll ()
31                 {
32                         // Make sure that those I18N assemblies are loaded.
33                         string basePath = Assembly.GetAssembly (typeof (int)).CodeBase;
34                         basePath = basePath.Substring (0, basePath.LastIndexOf ('/'));
35                         Assert.IsNotNull (Assembly.LoadFrom (basePath + "/I18N.West.dll"), "West");
36                         Assert.IsNotNull (Assembly.LoadFrom (basePath + "/I18N.CJK.dll"), "CJK");
37                         Assert.IsNotNull (Assembly.LoadFrom (basePath + "/I18N.MidEast.dll"), "MidEast");
38                         Assert.IsNotNull (Assembly.LoadFrom (basePath + "/I18N.Rare.dll"), "Rare");
39                         Assert.IsNotNull (Assembly.LoadFrom (basePath + "/I18N.Other.dll"), "Other");
40
41                         List<int> list = new List<int> ();
42                         for (int i = 1; i < 0x10000; i++) {
43                                 // Do this in a method to work around #5432
44                                 GetEncoding (i, list);
45                         }
46                         int [] reference = list.ToArray ();
47
48                         EncodingInfo [] infos = Encoding.GetEncodings ();
49                         int [] actual = new int [infos.Length];
50
51                         for (int i = 0; i < infos.Length; i++)
52                                 actual [i] = infos [i].CodePage;
53
54                         Assert.AreEqual (reference, actual);
55                 }
56
57                 [Test]
58                 public void GetEncodingForAllInfo ()
59                 {
60                         foreach (EncodingInfo i in Encoding.GetEncodings ())
61                                 Assert.IsNotNull (i.GetEncoding (), "codepage " + i);
62                 }
63
64                 void GetEncoding (int id, List<int> list) {
65                         try {
66                                 Encoding.GetEncoding (id);
67                                 list.Add (id);
68                         } catch {
69                         }
70                 }
71         }
72 }
73
74 #endif