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