*** empty log message ***
[m17n/m17n-lib-cs.git] / bothsensitive.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 = 10;
9   const int DEPTH = 10;
10   static MText mt = new MText ("0123456789");
11   static MSymbol key = MSymbol.PropertyKey ("bse",
12                                             MProperty.Flags.FrontSensitive
13                                             | MProperty.Flags.RearSensitive);
14   static MSymbol val0 = MSymbol.Of ("0");
15   static MSymbol val1 = MSymbol.Of ("1");
16   static MSymbol val2 = MSymbol.Of ("2");
17   static MProperty prop0 = new MProperty (key, val0);
18   static MProperty prop1 = new MProperty (key, val1);
19   static MProperty prop2 = new MProperty (key, val2);
20
21   static MSymbol[] valtable = new MSymbol[LENGTH];
22
23   static void TestPushProp (int from, int to, MProperty prop)
24   {
25     int i;
26     MSymbol sym;
27
28     if (from > 0 && valtable[from] != null)
29       {
30         sym = valtable[from];
31         for (i = from - 1; i >= 0 && valtable[i] == sym; i--)
32           valtable[i] = null;
33       }
34     if (to < LENGTH && valtable[to - 1] != null)
35       {
36         sym = valtable[to - 1];
37         for (i = to; i < LENGTH && valtable[i] == sym; i++)
38           valtable[i] = null;
39       }
40     for (i = from; i < to; i++)
41       valtable[i] = (MSymbol) prop.Val;
42     Console.WriteLine ("from {0}, to {1}, push {2}.\n", from, to, prop.Val);
43
44     mt.PushProp (from, to, prop);
45   }
46
47   static void TestPopProp (int from, int to, MSymbol key)
48   {
49     int i;
50     MSymbol sym;
51
52     if (from > 0 && valtable[from] != null)
53       {
54         sym = valtable[from];
55         for (i = from - 1; i >= 0 && valtable[i] == sym; i--)
56           valtable[i] = null;
57       }
58     if (to < LENGTH && valtable[to - 1] != null)
59       {
60         sym = valtable[to - 1];
61         for (i = to; i < LENGTH && valtable[i] == sym; i++)
62           valtable[i] = null;
63       }
64     for (i = from; i < to; i++)
65       valtable[i] = null;
66     Console.WriteLine ("from {0}, to {1}, pop.\n", from, to);
67
68     mt.PopProp (from, to, key);
69   }
70     
71   static void TestDelIns (int from, int to, int from2)
72   {
73     int i, l = to - from;
74     MSymbol[] valtable2 = new MSymbol[LENGTH];
75     MSymbol sym;
76
77     // sensitivity for deletion
78     if (from > 0)
79       {
80         if (valtable[from - 1] != null)
81           {
82             sym = valtable[from - 1];
83             for (i = from - 1; i >= 0 && valtable[i] == sym; i--)
84               valtable[i] = null;
85           }
86         if (valtable[from] != null)
87           {
88             sym = valtable[from];
89             for (i = from; i < LENGTH && valtable[i] == sym; i++)
90               valtable[i] = null;
91           }
92       }
93     if (to < LENGTH)
94       {
95         if (valtable[to - 1] != null)
96           {
97             sym = valtable[to - 1];
98             for (i = to - 1; i >= 0 && valtable[i] == sym; i--)
99               valtable[i] = null;
100           }
101         if (valtable[to] != null)
102           {
103             sym = valtable[to];
104             for (i = to; i < LENGTH && valtable[i] == sym; i++)
105               valtable[i] = null;
106           }
107       }
108
109     // copy
110     for (i = from; i < to; i++)
111       valtable2[i - from] = valtable[i];
112
113     // delete
114     for (i = to; i < LENGTH; i++)
115       valtable[i - l] = valtable[i];
116     valtable[LENGTH - l] = null;
117
118     // sensitivity for insertion
119     if (l < LENGTH && valtable[from2] != null)
120       {
121         sym = valtable[from2];
122         for (i = from2; i < LENGTH && valtable[i] == sym; i++)
123           valtable[i] = null;
124       }
125     if (from2 > 0 && valtable[from2 - 1] != null)
126       {
127         sym = valtable[from2 - 1];
128         for (i = from2 - 1; i >= 0 && valtable[i] == sym; i--)
129           valtable[i] = null;
130       }
131     if ((from > 0 || from2 > 0) && valtable2[0] != null)
132       {
133         sym = valtable2[0];
134         for (i = 0; i < LENGTH && valtable2[i] == sym; i++)
135           valtable2[i] = null;
136       }
137     if ((to < LENGTH || from2 + l < LENGTH) && valtable2[l - 1] != null)
138       {
139         sym = valtable2[l - 1];
140         for (i = l - 1; i >= 0; i--)
141           valtable2[i] = null;
142       }
143
144     // move
145     for (i = LENGTH - 1; i >= from2 + l; i--)
146       valtable[i] = valtable[i - l];
147
148     // insert
149     for (i = from2; i < from2 + l; i++)
150       valtable[i] = valtable2[i - from2];
151     Console.WriteLine ("from {0}, to {1}, moveto {2}.\n", from, to, from2);
152
153     MText mt2 = mt.Dup ();
154     mt.Del (from, to);
155     mt.Ins (from2, mt2, from, to);
156   }
157
158   static bool Compare ()
159   {
160     for (int i = 0; i < LENGTH; i++)
161       {
162         object val = mt.GetProp (i, key);
163
164         if (valtable[i] != (MSymbol) val)
165           {
166             Console.WriteLine ("valtable[{0}] is {1}, GetProp returned {2}",
167                                i,
168                                valtable[i] == null ? MSymbol.nil : valtable[i],
169                                val == null ? MSymbol.nil : val);
170             return false;
171           }
172       }
173     return true;
174   }
175
176   static void Dump ()
177   {
178     for (int i = 0; i < LENGTH; i++)
179       Console.WriteLine ("{0} {1} :", i, valtable[i]);
180   }
181
182   public static void Main (string[] args)
183   {
184     Random r = new Random (int.Parse (args[0]));
185     int check = (args.Length > 1 ? int.Parse (args[1]) : 0xFFFFFFF);
186
187     for (int loop = 0; loop < 1000000; loop++)
188       {
189         int from, to;
190
191         Console.WriteLine ("--- loop = {0} ---\n", loop);
192         if (loop >= check)
193           {
194             mt.DumpPropNested ();
195             Dump ();
196             M17n.debug = true;
197           }
198
199         do {
200           from = r.Next (LENGTH);
201           to = r.Next (LENGTH) + 1;
202         } while (from == to);
203         if (from > to)
204           {
205             int tmp = from;
206             from = to;
207             to = tmp;
208           }
209
210         MProperty prop;
211         switch (r.Next (3))
212           {
213           case 0:
214             prop = prop0;
215             break;
216           case 1:
217             prop = prop1;
218             break;
219           default:
220             prop = prop2;
221             break;
222           }
223
224         switch (r.Next (3))
225           {
226           case 0:
227             TestPushProp (from, to, prop);
228             break;
229           case 1:
230             TestPopProp (from, to, key);
231             break;
232           case 2:
233             TestDelIns (from, to, r.Next (LENGTH - (to - from) + 1));
234             break;
235           }
236
237         if (M17n.debug)
238           mt.DumpPropNested ();
239
240         if (Compare () == false)
241           {
242             Console.WriteLine ("");
243             Dump ();
244             Console.WriteLine ("Failed.");
245             return;
246           }
247       }
248   }
249 }