*** 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 - 1; i >= 0 && valtable[i] == sym; i--)
81           valtable[i] = null;
82         for (i = from; i < LENGTH && valtable[i] == sym; i++)
83           valtable[i] = null;
84       }
85     if (to < LENGTH && valtable[to] != null)
86       {
87         sym = valtable[to];
88         for (i = to - 1; i >= 0 && valtable[i] == sym; i--)
89           valtable[i] = null;
90         for (i = to; i < LENGTH && valtable[i] == sym; i++)
91           valtable[i] = null;
92       }
93
94     // copy
95     for (i = from; i < to; i++)
96       valtable2[i - from] = valtable[i];
97
98     // delete
99     for (i = to; i < LENGTH; i++)
100       valtable[i - l] = valtable[i];
101     valtable[LENGTH - l] = null;
102
103     // sensitivity for insertion
104     if (valtable[from2] != null)
105       {
106         sym = valtable[from2];
107         for (i = from2 - 1; i >= 0 && valtable[i] == sym; i--)
108           valtable[i] = null;
109         for (i = from2; i < LENGTH && valtable[i] == sym; i++)
110           valtable[i] = null;
111       }
112     if ((from > 0 || from2 > 0) && valtable2[0] != null)
113       {
114         sym = valtable2[0];
115         for (i = 0; i < LENGTH && valtable2[i] == sym; i++)
116           valtable2[i] = null;
117       }
118     
119     // move
120     for (i = LENGTH - 1; i >= from2 + l; i--)
121       valtable[i] = valtable[i - l];
122
123     // insert
124     for (i = from2; i < from2 + l; i++)
125       valtable[i] = valtable2[i - from2];
126     Console.WriteLine ("from {0}, to {1}, moveto {2}.\n", from, to, from2);
127
128     MText mt2 = mt.Dup ();
129     mt.Del (from, to);
130     mt.Ins (from2, mt2, from, to);
131   }
132
133   static bool Compare ()
134   {
135     for (int i = 0; i < LENGTH; i++)
136       {
137         object val = mt.GetProp (i, key);
138
139         if (valtable[i] != (MSymbol) val)
140           {
141             Console.WriteLine ("valtable[{0}] is {1}, GetProp returned {2}",
142                                i,
143                                valtable[i] == null ? MSymbol.nil : valtable[i],
144                                val == null ? MSymbol.nil : val);
145             return false;
146           }
147       }
148     return true;
149   }
150
151   static void Dump ()
152   {
153     for (int i = 0; i < LENGTH; i++)
154       Console.WriteLine ("{0} {1} :", i, valtable[i]);
155   }
156
157   public static void Main (string[] args)
158   {
159     Random r = new Random (int.Parse (args[0]));
160     int check = (args.Length > 1 ? int.Parse (args[1]) : 0xFFFFFFF);
161
162     for (int loop = 0; loop < 1000000; loop++)
163       {
164         int from, to;
165
166         Console.WriteLine ("--- loop = {0} ---\n", loop);
167         if (loop >= check)
168           {
169             mt.DumpPropNested ();
170             Dump ();
171             M17n.debug = true;
172           }
173
174         do {
175           from = r.Next (LENGTH);
176           to = r.Next (LENGTH) + 1;
177         } while (from == to);
178         if (from > to)
179           {
180             int tmp = from;
181             from = to;
182             to = tmp;
183           }
184
185         MProperty prop;
186         switch (r.Next (3))
187           {
188           case 0:
189             prop = prop0;
190             break;
191           case 1:
192             prop = prop1;
193             break;
194           default:
195             prop = prop2;
196             break;
197           }
198
199         switch (r.Next (3))
200           {
201           case 0:
202             TestPushProp (from, to, prop);
203             break;
204           case 1:
205             TestPopProp (from, to, key);
206             break;
207           case 2:
208             TestDelIns (from, to, r.Next (LENGTH - (to - from) + 1));
209             break;
210           }
211
212         if (M17n.debug)
213           mt.DumpPropNested ();
214
215         if (Compare () == false)
216           {
217             Console.WriteLine ("");
218             Dump ();
219             Console.WriteLine ("Failed.");
220             return;
221           }
222       }
223   }
224 }