Test PushProp () and PopProp ().
[m17n/m17n-lib-cs.git] / textprop.cs
1 using System;
2 using System.Collections.Generic;
3 using M17N.Core;
4
5 public class Test
6 {
7   const int LENGTH = 10;
8   const int DEPTH = 10;
9   static MText mt = new MText ("0123456789");
10   static MSymbol key = new MSymbol ("k");
11   static MSymbol val0 = new MSymbol ("0");
12   static MSymbol val1 = new MSymbol ("1");
13   static MSymbol val2 = new MSymbol ("2");
14   static MTextProperty prop0 = new MTextProperty (key, val0);
15   static MTextProperty prop1 = new MTextProperty (key, val1);
16   static MTextProperty prop2 = new MTextProperty (key, val2);
17
18   static int[] nvals = new int[10];
19   static MSymbol[,] valtable = new MSymbol[LENGTH, DEPTH + 1];
20
21   static void TestPushProp (int from, int to, MSymbol key, MSymbol val)
22   {
23     for (int i = from; i < to; i++)
24       if (nvals[i] == DEPTH)
25         return;
26
27     for (int i = from; i < to; i++)
28       {
29         valtable[i, nvals[i]] = val;
30         nvals[i]++;
31       }
32     Console.WriteLine ("from {0}, to {1}, {2}.\n", from, to, val);
33
34     mt.PushProp (from, to, key, val);
35   }
36
37   static void TestPushProp (int from, int to, MTextProperty prop)
38   {
39     for (int i = from; i < to; i++)
40       if (nvals[i] == DEPTH)
41         return;
42
43     for (int i = from; i < to; i++)
44       {
45         valtable[i, nvals[i]] = (MSymbol) prop.Val;
46         nvals[i]++;
47       }
48     Console.WriteLine ("from {0}, to {1}, {2}.\n", from, to, prop);
49
50     mt.PushProp (from, to, prop);
51   }
52
53   static void TestPopProp (int from, int to, MSymbol key)
54   {
55     for (int i = from; i < to; i++)
56       if (nvals[i] > 0)
57         {
58           valtable[i, nvals[i] - 1] = null;
59           nvals[i]--;
60         }
61     Console.WriteLine ("from {0}, to {1}, pop.\n", from, to);
62
63     mt.PopProp (from, to, key);
64   }
65     
66   static bool Compare ()
67   {
68     for (int i = 0; i < LENGTH; i++)
69       {
70         MTextProperty[] array;
71         object val = mt.GetProp (i, key, out array);
72
73         if (array == null)
74           {
75             if (nvals[i] != 0)
76               {
77                 Console.WriteLine ("nvals[{0}] is {1}, array.Length is null.\n",
78                                    i, nvals[i]);
79                 return false;
80               }
81           }
82
83         else if (nvals[i] != array.Length)
84           {
85             Console.WriteLine ("nvals[{0}] is {1}, array.Length is {2}.\n",
86                                i, nvals[i], array.Length);
87             return false;
88           }
89
90         else
91           {
92             for (int j = 0; j < nvals[i]; j++)
93               if (valtable[i, nvals[i] - 1 - j] != array[j].Val)
94                 {
95                   Console.WriteLine ("valtable[{0}, {1}] is {2}, array[{1}] is {3}.\n",
96                                      i, j, valtable[i, j], array[j]);
97                   return false;
98                 }
99           }
100
101         if (val == null)
102           {
103             if (nvals[i] != 0)
104               {
105                 Console.WriteLine ("GetProp returned null for index {0}.\n",
106                                    i);
107                 return false;
108               }
109           }
110
111         else
112           {
113             if (nvals[i] == 0)
114               {
115                 Console.WriteLine ("GetProp returned {0} for index {1}.\n",
116                                    val, i);
117                 return false;
118               }
119
120             else if (valtable[i, nvals[i] - 1] == null)
121               {
122                 Console.WriteLine ("GetProp returned {0} for index {1}.\n",
123                                    val, i);
124                 return false;
125               }
126
127             else if ((MSymbol) val != valtable[i, nvals[i] - 1])
128               {
129                 Console.WriteLine ("GetProp returned {0} for index {1}.\n",
130                                    val, i);
131                 return false;
132               }
133           }
134       }
135     return true;
136   }
137
138   static void Dump ()
139   {
140     for (int i = 0; i < LENGTH; i++)
141       {
142         Console.Write (i + " ");
143         for (int j = nvals[i] - 1; j >= 0; j--)
144           Console.Write (valtable[i,j] + " ");
145         Console.WriteLine (":");
146       }
147     Console.WriteLine ("");
148   }
149
150   public static void Main (string[] args)
151   {
152     Random r = new Random (int.Parse (args[0]));
153
154     for (int loop = 0; loop < 10000; loop++)
155       {
156         Console.WriteLine ("--- loop = {0} ---\n", loop);
157         mt.DumpPropNested ();
158         Dump ();
159
160         int from = r.Next (LENGTH);
161         int to = r.Next (LENGTH + 1);
162         if (from > to)
163           {
164             int tmp = from;
165             from = to;
166             to = tmp;
167           }
168
169         MSymbol val;
170         //MSymbol val = r.Next (2) == 0 ? val0 : val1;
171         switch (r.Next (3))
172           {
173           case 0:
174             val = val0;
175             break;
176           case 1:
177             val = val1;
178             break;
179           default:
180             val = val2;
181             break;
182           }
183
184         MTextProperty prop;
185         //MTextProperty prop = r.Next (2) == 0 ? prop0 : prop1;
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, key, val);
203             break;
204           case 1:
205             TestPushProp (from, to, prop);
206             break;
207           case 2:
208             TestPopProp (from, to, key);
209             break;
210           }
211
212         if (Compare () == false)
213           {
214             mt.DumpPropNested ();
215             Console.WriteLine ("");
216             Dump ();
217             Console.WriteLine ("Failed.");
218             return;
219           }
220       }
221
222   }
223 }