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