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