2005-09-27 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / corlib / Mono.Globalization.Unicode / create-normalization-tests.cs
1 using System;
2 using System.Collections;
3 using System.Globalization;
4 using System.IO;
5 using System.Text;
6
7 namespace Mono.Globalization.Unicode
8 {
9         public class NormalizationTestGenerator
10         {
11                 public static void Main ()
12                 {
13                         new NormalizationTestGenerator ().Run ();
14                 }
15
16                 int line;
17                 int type;
18                 ArrayList tests = new ArrayList ();
19
20                 void Run ()
21                 {
22                         try {
23                                 using (StreamReader sr = new StreamReader ("downloaded/NormalizationTest.txt")) {
24                                         for (line = 1; sr.Peek () >= 0; line++) {
25                                                 ProcessLine (sr.ReadLine ());
26                                         }
27                                 }
28                         } catch (Exception) {
29                                 Console.Error.WriteLine ("Error at line {0}", line);
30                                 throw;
31                         }
32
33                         TextWriter Output = new StringWriter ();
34                         foreach (Testcase test in tests) {
35                                 Output.Write ("tests.Add (new Testcase (");
36                                 foreach (string data in test.Data) {
37                                         Output.Write ("\"");
38                                         foreach (char c in data)
39                                                 Output.Write ("\\u{0:X04}", (int) c);
40                                         Output.Write ("\", ");
41                                 }
42                                 Output.WriteLine ("{0}));", test.TestType);
43                         }
44
45                         StreamReader template = new StreamReader ("StringNormalizationTestSource.cs");
46                         string ret = template.ReadToEnd ();
47                         ret = ret.Replace ("@@@@@@ Replace Here @@@@@@", Output.ToString ());
48                         Console.WriteLine (ret);
49                 }
50
51                 class Testcase
52                 {
53                         public int TestType;
54                         public string [] Data;
55
56                         public Testcase (int type, string [] data)
57                         {
58                                 this.TestType = type;
59                                 this.Data = data;
60                         }
61                 }
62
63                 void ProcessLine (string s)
64                 {
65                         int idx = s.IndexOf ('#');
66                         if (idx >= 0)
67                                 s = s.Substring (0, idx);
68                         if (s.Length == 0)
69                                 return;
70                         if (s [0] == '@') { // @Part0-3
71                                 type++;
72                                 return;
73                         }
74
75                         string [] parts = s.Split (';');
76                         string [] data = new string [5];
77                         bool skip = false;
78                         for (int form = 0; form < 5; form++) {
79                                 string [] values = parts [form].Split (' ');
80                                 char [] raw = new char [values.Length];
81                                 for (int i = 0; i < raw.Length; i++) {
82                                         int x = int.Parse (values [i].Trim (),
83                                                 NumberStyles.HexNumber);
84                                         if (x > char.MaxValue) {
85                                                 Console.Error.WriteLine ("at line {0} test contains character {1:X} that is larger than char.MaxValue. Ignored.", line, x);
86                                                 skip = true;
87                                                 break;
88                                         }
89                                         raw [i] = (char) x;
90                                 }
91                                 if (skip)
92                                         break;
93                                 data [form] = new string (raw);
94                         }
95                         if (skip)
96                                 return;
97                         tests.Add (new Testcase (type, data));
98                 }
99         }
100 }
101