*** empty log message ***
[m17n/m17n-lib-cs.git] / rearsensitive.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 ("rse",
11                                             MProperty.Flags.RearSensitive);
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     if (from > 0 && prop[from - 1] == prop[from] && prop[from] != -1)
65       for (i = prop[from]; i < from; i++)
66           prop[i] = -1;
67
68     if (to < LENGTH && prop[to - 1] == prop[to] && prop[to] != -1)
69       {
70         n = end[prop[to]];
71         for (i = to; i < n; i++)
72           prop[i] = -1;
73       }
74
75     for (i = from; i < to; i++)
76       prop[i] = -1;
77
78     mt.PopProp (from, to, key);
79   }
80     
81   static void TestDelIns (int from, int to, int from2)
82   {
83     Console.WriteLine ("from {0}, to {1}, moveto {2}.\n", from, to, from2);
84
85     int i, j, n, l = to - from;
86     int[] prop2 = new int[LENGTH], end2 = new int[LENGTH];
87     MSymbol[] value2 = new MSymbol[LENGTH];
88
89     // sensitivity for deletion
90     if (from > 0 && prop[from - 1] != -1)
91       {
92         n = prop[from - 1] == prop[from] ? end[prop[from]] : from;
93         for (i = prop[from - 1]; i < n; i++)
94           prop[i] = -1;
95       }
96
97     if (to < LENGTH && prop[to - 1] != -1)
98       {
99         n = prop[to - 1] == prop[to] ? end[prop[to]] : to;
100         for (i = prop[to - 1]; i < n; i++)
101           prop[i] = -1;
102       }
103
104     // copy
105     for (i = from, j = from2; i < to; i++, j++)
106       {
107         if (prop[i] != -1)
108           {
109             prop2[j] = prop[i] - from + from2;
110             end2[j] = end[i] - from + from2;
111             value2[j] = value[i];
112           }
113         else
114           prop2[j] = -1;
115       }
116
117     /*
118     Console.Write ("\n  ");
119     for (j = from2; j < from2 + l; j++)
120       Console.Write ("{0} ", j);
121     Console.Write ("\n--------------------\nP ");
122     for (j = from2; j < from2 + l; j++)
123       {
124         if (prop2[j] != -1)
125           Console.Write ("{0} ", prop2[j]);
126         else
127           Console.Write ("  ");
128       }
129     Console.Write ("\nE ");
130     for (j = from2; j < from2 + l; j++)
131       {
132         if (prop2[j] != -1)
133           Console.Write ("{0} ", end2[j]);
134         else
135           Console.Write ("  ");
136       }
137     Console.Write ("\nV ");
138     for (j = from2; j < from2 + l; j++)
139       {
140         if (prop2[j] != -1)
141           Console.Write ("{0} ", value2[j]);
142         else
143           Console.Write ("  ");
144       }
145     Console.Write ("\n");
146     */
147
148     // delete
149     for (i = to; i < LENGTH; i++)
150       {
151         if (prop[i] != -1)
152           {
153             prop[i - l] = prop[i] - l;
154             end[i - l] = end[i] - l;
155             value[i - l] = value[i];
156           }
157         else
158           prop[i - l] = -1;
159       }
160
161     // sensitivity for insertion
162     if (from2 > 0 && prop[from2 - 1] != -1)
163       {
164         n = prop[from2 - 1] == prop[from2] ? end[prop[from2]] : from2;
165         for (i = prop[from2 - 1]; i < n; i++)
166           prop[i] = -1;
167       }
168     if (from2 + l < LENGTH && prop2[from2 + l - 1] != -1)
169       for (i = prop2[from2 + l -1]; i < from2 + l; i++)
170         prop2[i] = -1;
171
172     // move
173     for (i = LENGTH - 1; i >= from2 + l; i--)
174       {
175         if (prop[i - l] != -1)
176           {
177             prop[i] = prop[i - l] + l;
178             end[i] = end[i - l] + l;
179             value[i] = value[i - l];
180           }
181         else
182           prop[i] = -1;
183       }
184
185     // insert
186     for (i = from2; i < from2 + l; i++)
187       {
188         prop[i] = prop2[i];
189         if (prop2[i] != -1)
190           {
191             end[i] = end2[i];
192             value[i] = value2[i];
193           }
194       }
195
196     MText mt2 = mt.Dup ();
197     mt.Del (from, to);
198     mt.Ins (from2, mt2, from, to);
199   }
200
201   static bool Compare ()
202   {
203     for (int i = 0; i < LENGTH; i++)
204       {
205         object val = mt.GetProp (i, key);
206
207         if (prop[i] == -1 && (MSymbol) val != null)
208           {
209             Console.WriteLine ("value[{0}] is nil, GetProp returned {1}",
210                                i,
211                                val == null ? MSymbol.nil : val);
212             return false;
213           }
214
215         if (prop[i] != -1 && (value[prop[i]] != (MSymbol) val))
216           {
217             Console.WriteLine ("value[{0}] is {1}, GetProp returned {2}",
218                                i,
219                                value[prop[i]] == null ? MSymbol.nil : value[prop[i]],
220                                val == null ? MSymbol.nil : val);
221             return false;
222           }
223       }
224     return true;
225   }
226
227   static void Dump ()
228   {
229     for (int i = 0; i <= LENGTH; i++)
230       Console.Write ("{0} ", i);
231     Console.WriteLine ("\n-------------------");
232
233     if (prop[0] == -1)
234       Console.Write ("  ");
235     else
236       Console.Write ("{0} ", value[prop[0]]);
237
238     for (int i = 1; i < LENGTH; i++)
239       {
240         if (prop[i] == -1)
241           Console.Write ("  ");
242         else if (prop[i - 1] == prop[i])
243           Console.Write ("< ");
244         else
245           Console.Write ("{0} ", value[prop[i]]);
246       }
247     Console.Write ("\n");
248   }
249
250   /*
251   static void DebugDump (int n)
252   {
253     int i;
254
255     Console.Write ("\n#{0}\n  ", n);
256     for (i = 0; i <= LENGTH; i++)
257       Console.Write ("{0} ", i);
258     Console.Write ("\n----------------------\nP ");
259     for (i = 0; i < LENGTH; i++)
260       {
261         if (prop[i] != -1)
262           Console.Write ("{0} ", prop[i]);
263         else
264           Console.Write ("  ");
265       }
266     Console.Write ("\nE ");
267     if (prop[0] != -1)
268       Console.Write ("{0} ", end[0]);
269     else
270       Console.Write ("  ");
271     for (i = 1; i < LENGTH; i++)
272       {
273         if (prop[i - 1] != prop[i] && prop[i] != -1)
274           Console.Write ("{0} ", end[i]);
275         else
276           Console.Write ("  ");
277       }
278     Console.Write ("\nV ");
279     if (prop[0] != -1)
280       Console.Write ("{0} ", value[0]);
281     else
282       Console.Write ("  ");
283     for (i = 1; i < LENGTH; i++)
284       {
285         if (prop[i - 1] != prop[i] && prop[i] != -1)
286           Console.Write ("{0} ", value[i]);
287         else
288           Console.Write ("  ");
289       }
290     Console.Write ("\n");
291   }
292   */
293
294   public static void Main (string[] args)
295   {
296     Random r = new Random (int.Parse (args[0]));
297     int check = (args.Length > 1 ? int.Parse (args[1]) : 0xFFFFFFF);
298
299     for (int i = 0; i < LENGTH; i++)
300       prop[i] = -1;     
301
302     for (int loop = 0; loop < 1000000; loop++)
303       {
304         int from, to;
305
306         Console.WriteLine ("--- loop = {0} ---\n", loop);
307         if (loop >= check)
308           {
309             mt.DumpPropNested ();
310             Dump ();
311             M17n.debug = true;
312           }
313
314         do {
315           from = r.Next (LENGTH);
316           to = r.Next (LENGTH) + 1;
317         } while (from == to);
318         if (from > to)
319           {
320             int tmp = from;
321             from = to;
322             to = tmp;
323           }
324
325         MProperty prop;
326         switch (r.Next (3))
327           {
328           case 0:
329             prop = prop0;
330             break;
331           case 1:
332             prop = prop1;
333             break;
334           default:
335             prop = prop2;
336             break;
337           }
338
339         switch (r.Next (3))
340           {
341           case 0:
342             TestPushProp (from, to, prop);
343             break;
344           case 1:
345             TestPopProp (from, to, key);
346             break;
347           case 2:
348             TestDelIns (from, to, r.Next (LENGTH - (to - from) + 1));
349             break;
350           }
351
352         if (M17n.debug)
353           mt.DumpPropNested ();
354
355         if (Compare () == false)
356           {
357             Console.WriteLine ("");
358             Dump ();
359             Console.WriteLine ("Failed.");
360             return;
361           }
362       }
363   }
364 }