*** empty log message ***
[m17n/m17n-lib-cs.git] / frontsensitive.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 = 9;
9   static MText mt = new MText ("012345678");
10   static MSymbol key = MSymbol.PropertyKey ("fse",
11                                             MProperty.Flags.FrontSensitive);
12   static MSymbol val0 = MSymbol.Of ("0");
13   static MSymbol val1 = MSymbol.Of ("1");
14   static MSymbol val2 = MSymbol.Of ("2");
15   static MProperty prop0 = new MProperty (key, val0);
16   static MProperty prop1 = new MProperty (key, val1);
17   static MProperty prop2 = new MProperty (key, val2);
18
19   // Each property is identified by its beginning point.
20   // The character at position i has the property that begins at prop[i].
21   // If the character has no property, prop[i] == -1.
22   static int[] prop = new int[LENGTH];
23
24   // The property beginning at position i ends at position end[i].
25   // If no property begins at position i, end[i] is undefined.
26   static int[] end = new int[LENGTH];
27
28   // The property beginning at position i has value value[i].
29   // If no property begings at positoin i, value[i] is undefined.
30   static MSymbol[] value = new MSymbol[LENGTH];
31
32   static void TestPushProp (int from, int to, MProperty newprop)
33   {
34     Console.WriteLine ("from {0}, to {1}, push {2}.\n", from, to, newprop.Val);
35
36     int i, n;
37
38     if (from > 0 && prop[from - 1] == prop[from] && prop[from] != -1)
39       {
40         n = end[prop[from]];
41         for (i = prop[from]; i < n; i++)
42           prop[i] = -1;
43       }
44     if (to < LENGTH && prop[to - 1] == prop[to] && prop[to] != -1)
45       {
46         n = end[prop[to]];
47         for (i = to; i < n; i++)
48           prop[i] = -1;
49       }
50     for (i = from; i < to; i++)
51       prop[i] = from;
52     end[from] = to;
53     value[from] = (MSymbol) newprop.Val;
54
55     mt.PushProp (from, to, newprop);
56   }
57
58   static void TestPopProp (int from, int to, MSymbol key)
59   {
60     Console.WriteLine ("from {0}, to {1}, pop.\n", from, to);
61
62     int i, n;
63
64     Console.WriteLine ("prop[from] = {0}", prop[from]);
65     if (from > 0 && prop[from - 1] == prop[from] && prop[from] != -1)
66       for (i = prop[from]; i < from; i++)
67           prop[i] = -1;
68
69     if (to < LENGTH && prop[to - 1] == prop[to] && prop[to] != -1)
70       {
71         n = end[prop[to]];
72         for (i = to; i < n; i++)
73           prop[i] = -1;
74       }
75
76     for (i = from; i < to; i++)
77       prop[i] = -1;
78
79     mt.PopProp (from, to, key);
80   }
81     
82   static void TestDelIns (int from, int to, int from2)
83   {
84     Console.WriteLine ("from {0}, to {1}, moveto {2}.\n", from, to, from2);
85
86     int i, n, l = to - from;
87     int[] prop2 = new int[LENGTH], end2 = new int[LENGTH];
88     MSymbol[] value2 = new MSymbol[LENGTH];
89
90     DebugDump (0);
91     // sensitivity for deletion
92     if (from > 0 && prop[from] != -1)
93       {
94         n = end[prop[from]];
95         for (i = prop[from - 1] == prop[from] ? prop[from] : from; i < n; i++)
96           prop[i] = -1;
97       }
98
99     if (to < LENGTH && prop[to] != -1)
100       {
101         n = end[prop[to]];
102         for (i = prop[to - 1] == prop[to] ? prop[to] : to; i < n; i++)
103           prop[i] = -1;
104       }
105
106     DebugDump (1);
107     // copy
108     for (i = from; i < to; i++)
109       {
110         if (prop[i] != -1)
111           {
112             prop2[i - from] = prop[i] - from + from2;
113             end2[prop2[i - from]] = end[prop[i]] - from + from2;
114             value2[prop2[i - from]] = value[prop[i]];
115           }
116         else
117           prop2[i - from] = -1;
118       }
119
120     DebugDump (2);
121     // delete
122     for (i = to; i < LENGTH; i++)
123       {
124         if (prop[i] != -1)
125           {
126             prop[i - l] = prop[i] - l;
127             end[prop[i - l]] = end[prop[i]] - l;
128             value[prop[i - l]] = value[prop[i]];
129           }
130         else
131           prop[i - l] = -1;
132       }
133     prop[LENGTH - l] = -1;
134
135     DebugDump (3);
136     // sensitivity for insertion
137     if (prop[from2] != -1)
138       {
139         n = end[prop[from2]];
140         if (from2 > 0 && prop[from2 - 1] == prop[from2])
141           i = prop[from2];
142         else
143           i = from2;
144         for (; i < n; i++)
145           prop[i] = -1;
146       }
147
148     DebugDump (4);
149     // move
150     for (i = LENGTH - 1; i >= from2 + l; i--)
151       {
152         if (prop[i - l] != -1)
153           {
154             prop[i] = prop[i - l] + l;
155             end[prop[i]] = end[prop[i - l]] + l;
156             value[prop[i]] = value[prop[i - l]];
157           }
158         else
159           prop[i] = -1;
160       }
161
162     DebugDump (5);
163     // insert
164     for (i = from2; i < from2 + l; i++)
165       {
166         if (prop2[i - from2] != -1)
167           {
168             prop[i] = prop2[i - from2];
169             end[prop[i]] = end2[prop2[i - from2]];
170             value[prop[i]] = value2[prop2[i - from2]];
171           }
172         else
173           prop[i] = -1;
174       }
175     DebugDump (6);
176
177     MText mt2 = mt.Dup ();
178     mt.Del (from, to);
179     mt.Ins (from2, mt2, from, to);
180   }
181
182   static bool Compare ()
183   {
184     for (int i = 0; i < LENGTH; i++)
185       {
186         object val = mt.GetProp (i, key);
187
188         if (prop[i] == -1 && (MSymbol) val != null)
189           {
190             Console.WriteLine ("value[{0}] is nil, GetProp returned {1}",
191                                i,
192                                val == null ? MSymbol.nil : val);
193             return false;
194           }
195
196         if (prop[i] != -1 && (value[prop[i]] != (MSymbol) val))
197           {
198             Console.WriteLine ("value[{0}] is {1}, GetProp returned {2}",
199                                i,
200                                value[prop[i]] == null ? MSymbol.nil : value[prop[i]],
201                                val == null ? MSymbol.nil : val);
202             return false;
203           }
204       }
205     return true;
206   }
207
208   static void Dump ()
209   {
210     for (int i = 0; i <= LENGTH; i++)
211       Console.Write ("{0} ", i);
212     Console.WriteLine ("\n-------------------");
213
214     if (prop[0] == -1)
215       Console.Write ("  ");
216     else
217       Console.Write ("{0} ", value[prop[0]]);
218
219     for (int i = 1; i < LENGTH; i++)
220       {
221         if (prop[i] == -1)
222           Console.Write ("  ");
223         else if (prop[i - 1] == prop[i])
224           Console.Write ("< ");
225         else
226           Console.Write ("{0} ", value[prop[i]]);
227       }
228     Console.Write ("\n");
229   }
230
231   static void DebugDump (int n)
232   {
233     /*
234     int i;
235
236     Console.Write ("\n#{0}\n  ", n);
237     for (i = 0; i <= LENGTH; i++)
238       Console.Write ("{0} ", i);
239     Console.Write ("\n----------------------\nP ");
240     for (i = 0; i < LENGTH; i++)
241       {
242         if (prop[i] != -1)
243           Console.Write ("{0} ", prop[i]);
244         else
245           Console.Write ("  ");
246       }
247     Console.Write ("\nE ");
248     if (prop[0] != -1)
249       Console.Write ("{0} ", end[0]);
250     else
251       Console.Write ("  ");
252     for (i = 1; i < LENGTH; i++)
253       {
254         if (prop[i - 1] != prop[i] && prop[i] != -1)
255           Console.Write ("{0} ", end[i]);
256         else
257           Console.Write ("  ");
258       }
259     Console.Write ("\nV ");
260     if (prop[0] != -1)
261       Console.Write ("{0} ", value[0]);
262     else
263       Console.Write ("  ");
264     for (i = 1; i < LENGTH; i++)
265       {
266         if (prop[i - 1] != prop[i] && prop[i] != -1)
267           Console.Write ("{0} ", value[i]);
268         else
269           Console.Write ("  ");
270       }
271     Console.Write ("\n");
272     */
273   }
274
275   public static void Main (string[] args)
276   {
277     Random r = new Random (int.Parse (args[0]));
278     int check = (args.Length > 1 ? int.Parse (args[1]) : 0xFFFFFFF);
279
280     for (int i = 0; i < LENGTH; i++)
281       prop[i] = -1;     
282
283     for (int loop = 0; loop < 1000000; loop++)
284       {
285         int from, to;
286
287         Console.WriteLine ("--- loop = {0} ---\n", loop);
288         if (loop >= check)
289           {
290             mt.DumpPropNested ();
291             Dump ();
292             M17n.debug = true;
293           }
294
295         do {
296           from = r.Next (LENGTH);
297           to = r.Next (LENGTH) + 1;
298         } while (from == to);
299         if (from > to)
300           {
301             int tmp = from;
302             from = to;
303             to = tmp;
304           }
305
306         MProperty prop;
307         switch (r.Next (3))
308           {
309           case 0:
310             prop = prop0;
311             break;
312           case 1:
313             prop = prop1;
314             break;
315           default:
316             prop = prop2;
317             break;
318           }
319
320         switch (r.Next (3))
321           {
322           case 0:
323             TestPushProp (from, to, prop);
324             break;
325           case 1:
326             TestPopProp (from, to, key);
327             break;
328           case 2:
329             TestDelIns (from, to, r.Next (LENGTH - (to - from) + 1));
330             break;
331           }
332
333         if (M17n.debug)
334           mt.DumpPropNested ();
335
336         if (Compare () == false)
337           {
338             Console.WriteLine ("");
339             Dump ();
340             Console.WriteLine ("Failed.");
341             return;
342           }
343       }
344   }
345 }