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