*** empty log message ***
[m17n/m17n-lib-cs.git] / textprop.cs
1 using System;
2 using System.Collections.Generic;
3 using M17N;
4 using M17N.Core;
5
6 public class Test
7 {
8   const int LENGTH = 10;
9   const int DEPTH = 10;
10   static MText mt = new MText ("0123456789");
11   static MSymbol key = new MSymbol ("k");
12   static MSymbol val0 = new MSymbol ("0");
13   static MSymbol val1 = new MSymbol ("1");
14   static MSymbol val2 = new MSymbol ("2");
15   static MTextProperty prop0 = new MTextProperty (key, val0);
16   static MTextProperty prop1 = new MTextProperty (key, val1);
17   static MTextProperty prop2 = new MTextProperty (key, val2);
18
19   static int[] nvals = new int[LENGTH];
20   static MSymbol[,] valtable = new MSymbol[LENGTH, DEPTH + 1];
21
22   static void TestPushProp (int from, int to, MSymbol key, MSymbol val)
23   {
24     for (int i = from; i < to; i++)
25       if (nvals[i] == DEPTH)
26         return;
27
28     for (int i = from; i < to; i++)
29       {
30         valtable[i, nvals[i]] = val;
31         nvals[i]++;
32       }
33     Console.WriteLine ("from {0}, to {1}, push {2}, {3}.\n",
34                        from, to, key, val);
35
36     mt.PushProp (from, to, key, val);
37   }
38
39   static void TestPushProp (int from, int to, MTextProperty prop)
40   {
41     for (int i = from; i < to; i++)
42       if (nvals[i] == DEPTH)
43         return;
44
45     for (int i = from; i < to; i++)
46       {
47         valtable[i, nvals[i]] = (MSymbol) prop.Val;
48         nvals[i]++;
49       }
50     Console.WriteLine ("from {0}, to {1}, push {2}.\n", from, to, prop);
51
52     mt.PushProp (from, to, prop);
53   }
54
55   static void TestPopProp (int from, int to, MSymbol key)
56   {
57     for (int i = from; i < to; i++)
58       if (nvals[i] > 0)
59         {
60           valtable[i, nvals[i] - 1] = null;
61           nvals[i]--;
62         }
63     Console.WriteLine ("from {0}, to {1}, pop.\n", from, to);
64
65     mt.PopProp (from, to, key);
66   }
67     
68   static void TestDelIns (int from, int to, int from2)
69   {
70     int i, j, l = to - from;
71     int[] nvals2 = new int[LENGTH];
72     MSymbol[,] valtable2 = new MSymbol[LENGTH, DEPTH + 1];
73
74     // copy
75     for (i = from; i < to; i++)
76       {
77         nvals2[i - from] = nvals[i];
78         for (j = 0; j < nvals[i]; j++)
79           valtable2[i - from, j] = valtable[i, j];
80       }
81
82     // delete
83     for (i = to; i < LENGTH; i++)
84       {
85         nvals[i - l] = nvals[i];
86         for (j = 0; j < nvals[i]; j++)
87           valtable[i - l, j] = valtable[i, j];
88       }
89
90     // move
91     for (i = LENGTH - 1; i >= from2 + l; i--)
92       {
93         nvals[i] = nvals[i - l];
94         for (j = 0; j < nvals[i]; j++)
95           valtable[i, j] = valtable[i - l, j];
96       }
97
98     // insert
99     for (i = from2; i < from2 + l; i++)
100       {
101         nvals[i] = nvals2[i - from2];
102         for (j = 0; j < nvals[i]; j++)
103           valtable[i, j] = valtable2[i - from2, j];
104       }
105     Console.WriteLine ("from {0}, to {1}, moveto {2}.\n", from, to, from2);
106
107     MText mt2 = mt.Dup ();
108     mt.Del (from, to);
109     mt.Ins (from2, mt2, from, to);
110   }
111
112   static bool Compare ()
113   {
114     for (int i = 0; i < LENGTH; i++)
115       {
116         MTextProperty[] array;
117         object val = mt.GetProp (i, key, out array);
118
119         if (array == null)
120           {
121             if (nvals[i] != 0)
122               {
123                 Console.WriteLine ("nvals[{0}] is {1}, array.Length is null.\n",
124                                    i, nvals[i]);
125                 return false;
126               }
127           }
128
129         else if (nvals[i] != array.Length)
130           {
131             Console.WriteLine ("nvals[{0}] is {1}, array.Length is {2}.\n",
132                                i, nvals[i], array.Length);
133             return false;
134           }
135
136         else
137           {
138             for (int j = 0; j < nvals[i]; j++)
139               if (valtable[i, nvals[i] - 1 - j] != (MSymbol) array[j].Val)
140                 {
141                   Console.WriteLine ("valtable[{0}, {1}] is {2}, array[{1}] is {3}.\n",
142                                      i, j, valtable[i, j], array[j]);
143                   return false;
144                 }
145           }
146
147         if (val == null)
148           {
149             if (nvals[i] != 0)
150               {
151                 Console.WriteLine ("GetProp returned null for index {0}.\n",
152                                    i);
153                 return false;
154               }
155           }
156
157         else
158           {
159             if (nvals[i] == 0)
160               {
161                 Console.WriteLine ("GetProp returned {0} for index {1}.\n",
162                                    val, i);
163                 return false;
164               }
165
166             else if (valtable[i, nvals[i] - 1] == null)
167               {
168                 Console.WriteLine ("GetProp returned {0} for index {1}.\n",
169                                    val, i);
170                 return false;
171               }
172
173             else if ((MSymbol) val != valtable[i, nvals[i] - 1])
174               {
175                 Console.WriteLine ("GetProp returned {0} for index {1}.\n",
176                                    val, i);
177                 return false;
178               }
179           }
180       }
181     return true;
182   }
183
184   static void Dump ()
185   {
186     for (int i = 0; i < LENGTH; i++)
187       {
188         Console.Write (i + " ");
189         for (int j = nvals[i] - 1; j >= 0; j--)
190           Console.Write (valtable[i,j] + " ");
191         Console.WriteLine (":");
192       }
193     Console.WriteLine ("");
194   }
195
196   public static void Main (string[] args)
197   {
198     Random r = new Random (int.Parse (args[0]));
199     int check = (args.Length > 1 ? int.Parse (args[1]) : 0xFFFFFFF);
200
201     for (int loop = 0; loop < 100000; loop++)
202       {
203         Console.WriteLine ("--- loop = {0} ---\n", loop);
204         if (loop >= check)
205           {
206             mt.DumpPropNested ();
207             Dump ();
208             M17n.debug = true;
209           }
210
211         int from = r.Next (LENGTH);
212         int to = r.Next (LENGTH + 1);
213         if (from > to)
214           {
215             int tmp = from;
216             from = to;
217             to = tmp;
218           }
219
220         MSymbol val;
221         //MSymbol val = r.Next (2) == 0 ? val0 : val1;
222         switch (r.Next (3))
223           {
224           case 0:
225             val = val0;
226             break;
227           case 1:
228             val = val1;
229             break;
230           default:
231             val = val2;
232             break;
233           }
234
235         MTextProperty prop;
236         //MTextProperty prop = r.Next (2) == 0 ? prop0 : prop1;
237         switch (r.Next (3))
238           {
239           case 0:
240             prop = prop0;
241             break;
242           case 1:
243             prop = prop1;
244             break;
245           default:
246             prop = prop2;
247             break;
248           }
249
250         switch (r.Next (4))
251           {
252           case 0:
253             TestPushProp (from, to, key, val);
254             break;
255           case 1:
256             TestPushProp (from, to, prop);
257             break;
258           case 2:
259             TestPopProp (from, to, key);
260             break;
261           case 3:
262             TestDelIns (from, to, r.Next (LENGTH - (to - from) + 1));
263             break;
264           }
265
266         if (Compare () == false)
267           {
268             mt.DumpPropNested ();
269             Console.WriteLine ("");
270             Dump ();
271             Console.WriteLine ("Failed.");
272             return;
273           }
274       }
275   }
276 }