*** 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   // Cut buffers
33   static int[] subprop = new int[LENGTH], subend = new int[LENGTH];
34   static MSymbol[] subvalue = new MSymbol[LENGTH];
35
36   static void TestPushProp (int from, int to, MProperty newprop)
37   {
38     Console.WriteLine ("from {0}, to {1}, push {2}.\n", from, to, newprop.Val);
39
40     int i, n;
41
42     if (from > 0 && prop[from - 1] == prop[from] && prop[from] != -1)
43       {
44         n = end[prop[from]];
45         for (i = prop[from]; i < n; i++)
46           prop[i] = -1;
47       }
48     if (to < LENGTH && prop[to - 1] == prop[to] && prop[to] != -1)
49       {
50         n = end[prop[to]];
51         for (i = to; i < n; i++)
52           prop[i] = -1;
53       }
54     for (i = from; i < to; i++)
55       prop[i] = from;
56     end[from] = to;
57     value[from] = (MSymbol) newprop.Val;
58
59     mt.PushProp (from, to, newprop);
60   }
61
62   static void TestPopProp (int from, int to, MSymbol key)
63   {
64     Console.WriteLine ("from {0}, to {1}, pop.\n", from, to);
65
66     int i, n;
67
68     if (from > 0 && prop[from - 1] == prop[from] && prop[from] != -1)
69       for (i = prop[from]; i < from; i++)
70           prop[i] = -1;
71
72     if (to < LENGTH && prop[to - 1] == prop[to] && prop[to] != -1)
73       {
74         n = end[prop[to]];
75         for (i = to; i < n; i++)
76           prop[i] = -1;
77       }
78
79     for (i = from; i < to; i++)
80       prop[i] = -1;
81
82     mt.PopProp (from, to, key);
83   }
84     
85   static void TestDelIns (int from, int to, int from2)
86   {
87     Console.WriteLine ("from {0}, to {1}, moveto {2}.\n", from, to, from2);
88
89     int i, n, l = to - from;
90
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     // copy
107     for (i = from; i < to; i++)
108       {
109         if (prop[i] != -1)
110           {
111             subprop[i - from] = prop[i] - from + from2;
112             subend[i - from] = end[i] - from + from2;
113             subvalue[i - from] = value[i];
114           }
115         else
116           subprop[i - from] = -1;
117       }
118
119     // delete
120     for (i = to; i < LENGTH; i++)
121       {
122         if (prop[i] != -1)
123           {
124             prop[i - l] = prop[i] - l;
125             end[i - l] = end[i] - l;
126             value[i - l] = value[i];
127           }
128         else
129           prop[i - l] = -1;
130       }
131     prop[LENGTH - l] = -1;
132
133     // sensitivity for insertion
134     if (prop[from2] != -1)
135       {
136         n = end[prop[from2]];
137         if (from2 > 0 && prop[from2 - 1] == prop[from2])
138           i = prop[from2];
139         else
140           i = from2;
141         for (; i < n; i++)
142           prop[i] = -1;
143       }
144     if (from2 > 0 && subprop[0] != -1)
145       for (i = 0; i < subend[0] - from2; i++)
146         subprop[i] = -1;
147
148     // move
149     for (i = LENGTH - 1; i >= from2 + l; i--)
150       {
151         if (prop[i - l] != -1)
152           {
153             prop[i] = prop[i - l] + l;
154             end[i] = end[i - l] + l;
155             value[i] = value[i - l];
156           }
157         else
158           prop[i] = -1;
159       }
160
161     // insert
162     for (i = from2; i < from2 + l; i++)
163       {
164         if (subprop[i - from2] != -1)
165           {
166             prop[i] = subprop[i - from2];
167             end[i] = subend[i - from2];
168             value[i] = subvalue[i - from2];
169           }
170         else
171           prop[i] = -1;
172       }
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   public static void Main (string[] args)
229   {
230     Random r = new Random (int.Parse (args[0]));
231     int check = (args.Length > 1 ? int.Parse (args[1]) : 0xFFFFFFF);
232
233     for (int i = 0; i < LENGTH; i++)
234       prop[i] = -1;     
235
236     for (int loop = 0; loop < 1000000; loop++)
237       {
238         int from, to;
239
240         Console.WriteLine ("--- loop = {0} ---\n", loop);
241         if (loop >= check)
242           {
243             mt.DumpPropNested ();
244             Dump ();
245             M17n.debug = true;
246           }
247
248         do {
249           from = r.Next (LENGTH);
250           to = r.Next (LENGTH) + 1;
251         } while (from == to);
252         if (from > to)
253           {
254             int tmp = from;
255             from = to;
256             to = tmp;
257           }
258
259         MProperty prop;
260         switch (r.Next (3))
261           {
262           case 0:
263             prop = prop0;
264             break;
265           case 1:
266             prop = prop1;
267             break;
268           default:
269             prop = prop2;
270             break;
271           }
272
273         switch (r.Next (3))
274           {
275           case 0:
276             TestPushProp (from, to, prop);
277             break;
278           case 1:
279             TestPopProp (from, to, key);
280             break;
281           case 2:
282             TestDelIns (from, to, r.Next (LENGTH - (to - from) + 1));
283             break;
284           }
285
286         if (M17n.debug)
287           mt.DumpPropNested ();
288
289         if (Compare () == false)
290           {
291             Console.WriteLine ("");
292             Dump ();
293             Console.WriteLine ("Failed.");
294             return;
295           }
296       }
297   }
298 }