*** 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 (from2 > 0 && prop[from2 - 1] == prop[from2] && prop[from2] != -1)
138       {
139         n = end[prop[from2]];
140         for (i = prop[from2 - 1] == prop[from2] ? prop[from2] : from2;
141              i < n; i++)
142           prop[i] = -1;
143       }
144
145     DebugDump (4);
146     // move
147     for (i = LENGTH - 1; i >= from2 + l; i--)
148       {
149         if (prop[i - l] != -1)
150           {
151             prop[i] = prop[i - l] + l;
152             end[prop[i]] = end[prop[i - l]] + l;
153             value[prop[i]] = value[prop[i - l]];
154           }
155         else
156           prop[i] = -1;
157       }
158
159     DebugDump (5);
160     // insert
161     for (i = from2; i < from2 + l; i++)
162       {
163         if (prop2[i - from2] != -1)
164           {
165             prop[i] = prop2[i - from2];
166             end[prop[i]] = end2[prop2[i - from2]];
167             value[prop[i]] = value2[prop2[i - from2]];
168           }
169         else
170           prop[i] = -1;
171       }
172     DebugDump (6);
173
174     MText mt2 = mt.Dup ();
175     mt.Del (from, to);
176     mt.Ins (from2, mt2, from, to);
177   }
178
179   static bool Compare ()
180   {
181     for (int i = 0; i < LENGTH; i++)
182       {
183         object val = mt.GetProp (i, key);
184
185         if (prop[i] == -1 && (MSymbol) val != null)
186           {
187             Console.WriteLine ("value[{0}] is nil, GetProp returned {1}",
188                                i,
189                                val == null ? MSymbol.nil : val);
190             return false;
191           }
192
193         if (prop[i] != -1 && (value[prop[i]] != (MSymbol) val))
194           {
195             Console.WriteLine ("value[{0}] is {1}, GetProp returned {2}",
196                                i,
197                                value[prop[i]] == null ? MSymbol.nil : value[prop[i]],
198                                val == null ? MSymbol.nil : val);
199             return false;
200           }
201       }
202     return true;
203   }
204
205   static void Dump ()
206   {
207     for (int i = 0; i <= LENGTH; i++)
208       Console.Write ("{0} ", i);
209     Console.WriteLine ("\n-------------------");
210
211     if (prop[0] == -1)
212       Console.Write ("  ");
213     else
214       Console.Write ("{0} ", value[prop[0]]);
215
216     for (int i = 1; i < LENGTH; i++)
217       {
218         if (prop[i] == -1)
219           Console.Write ("  ");
220         else if (prop[i - 1] == prop[i])
221           Console.Write ("< ");
222         else
223           Console.Write ("{0} ", value[prop[i]]);
224       }
225     Console.Write ("\n");
226   }
227
228   static void DebugDump (int n)
229   {
230     /*
231     int i;
232
233     Console.Write ("\n#{0}\n  ", n);
234     for (i = 0; i <= LENGTH; i++)
235       Console.Write ("{0} ", i);
236     Console.Write ("\n----------------------\nP ");
237     for (i = 0; i < LENGTH; i++)
238       {
239         if (prop[i] != -1)
240           Console.Write ("{0} ", prop[i]);
241         else
242           Console.Write ("  ");
243       }
244     Console.Write ("\nE ");
245     if (prop[0] != -1)
246       Console.Write ("{0} ", end[0]);
247     else
248       Console.Write ("  ");
249     for (i = 1; i < LENGTH; i++)
250       {
251         if (prop[i - 1] != prop[i] && prop[i] != -1)
252           Console.Write ("{0} ", end[i]);
253         else
254           Console.Write ("  ");
255       }
256     Console.Write ("\nV ");
257     if (prop[0] != -1)
258       Console.Write ("{0} ", value[0]);
259     else
260       Console.Write ("  ");
261     for (i = 1; i < LENGTH; i++)
262       {
263         if (prop[i - 1] != prop[i] && prop[i] != -1)
264           Console.Write ("{0} ", value[i]);
265         else
266           Console.Write ("  ");
267       }
268     Console.Write ("\n");
269     */
270   }
271
272   public static void Main (string[] args)
273   {
274     Random r = new Random (int.Parse (args[0]));
275     int check = (args.Length > 1 ? int.Parse (args[1]) : 0xFFFFFFF);
276
277     for (int i = 0; i < LENGTH; i++)
278       prop[i] = -1;     
279
280     for (int loop = 0; loop < 1000000; loop++)
281       {
282         int from, to;
283
284         Console.WriteLine ("--- loop = {0} ---\n", loop);
285         if (loop >= check)
286           {
287             mt.DumpPropNested ();
288             Dump ();
289             M17n.debug = true;
290           }
291
292         do {
293           from = r.Next (LENGTH);
294           to = r.Next (LENGTH) + 1;
295         } while (from == to);
296         if (from > to)
297           {
298             int tmp = from;
299             from = to;
300             to = tmp;
301           }
302
303         MProperty prop;
304         switch (r.Next (3))
305           {
306           case 0:
307             prop = prop0;
308             break;
309           case 1:
310             prop = prop1;
311             break;
312           default:
313             prop = prop2;
314             break;
315           }
316
317         switch (r.Next (3))
318           {
319           case 0:
320             TestPushProp (from, to, prop);
321             break;
322           case 1:
323             TestPopProp (from, to, key);
324             break;
325           case 2:
326             TestDelIns (from, to, r.Next (LENGTH - (to - from) + 1));
327             break;
328           }
329
330         if (M17n.debug)
331           mt.DumpPropNested ();
332
333         if (Compare () == false)
334           {
335             Console.WriteLine ("");
336             Dump ();
337             Console.WriteLine ("Failed.");
338             return;
339           }
340       }
341   }
342 }