using System; using System.Collections.Generic; using M17N; using M17N.Core; public class Test { const int LENGTH = 10; const int DEPTH = 10; static MText mt = new MText ("0123456789"); static MSymbol key = MSymbol.PropertyKey ("___"); #if false static MSymbol key_r = MSymbol.PropertyKey ("r__", MProperty.Flags.RearSticky); static MSymbol key_f = MSymbol.PropertyKey ("_f_", MProperty.Flags.FrontSticky); static MSymbol key_rf = MSymbol.PropertyKey ("rf_", (MProperty.Flags.RearSticky | MProperty.Flags.FrontSticky)); static MSymbol key_s = MSymbol.PropertyKey ("__s", MProperty.Flags.Sensitive); static MSymbol key_rs = MSymbol.PropertyKey ("r_s", (MProperty.Flags.RearSticky | MProperty.Flags.Sensitive)); static MSymbol key_fs = MSymbol.PropertyKey ("_fs", (MProperty.Flags.FrontSticky | MProperty.Flags.Sensitive)); static MSymbol key_rfs = MSymbol.PropertyKey ("rfs", (MProperty.Flags.RearSticky | MProperty.Flags.FrontSticky | MProperty.Flags.Sensitive)); #endif static MSymbol val0 = MSymbol.Of ("0"); static MSymbol val1 = MSymbol.Of ("1"); static MSymbol val2 = MSymbol.Of ("2"); static MProperty prop0 = new MProperty (key, val0); static MProperty prop1 = new MProperty (key, val1); static MProperty prop2 = new MProperty (key, val2); static int[] nvals = new int[LENGTH]; static MSymbol[,] valtable = new MSymbol[LENGTH, DEPTH + 1]; static void TestPushProp (int from, int to, MSymbol key, MSymbol val) { for (int i = from; i < to; i++) if (nvals[i] == DEPTH) return; for (int i = from; i < to; i++) { valtable[i, nvals[i]] = val; nvals[i]++; } Console.WriteLine ("from {0}, to {1}, push {2}, {3}.\n", from, to, key, val); mt.PushProp (from, to, key, val); } static void TestPushProp (int from, int to, MProperty prop) { for (int i = from; i < to; i++) if (nvals[i] == DEPTH) return; for (int i = from; i < to; i++) { valtable[i, nvals[i]] = (MSymbol) prop.Val; nvals[i]++; } Console.WriteLine ("from {0}, to {1}, push {2}.\n", from, to, prop); mt.PushProp (from, to, prop); } static void TestPopProp (int from, int to, MSymbol key) { for (int i = from; i < to; i++) if (nvals[i] > 0) { valtable[i, nvals[i] - 1] = null; nvals[i]--; } Console.WriteLine ("from {0}, to {1}, pop.\n", from, to); mt.PopProp (from, to, key); } static void TestDelIns (int from, int to, int from2) { int i, j, l = to - from; int[] nvals2 = new int[LENGTH]; MSymbol[,] valtable2 = new MSymbol[LENGTH, DEPTH + 1]; // copy for (i = from; i < to; i++) { nvals2[i - from] = nvals[i]; for (j = 0; j < nvals[i]; j++) valtable2[i - from, j] = valtable[i, j]; } // delete for (i = to; i < LENGTH; i++) { nvals[i - l] = nvals[i]; for (j = 0; j < nvals[i]; j++) valtable[i - l, j] = valtable[i, j]; } // move for (i = LENGTH - 1; i >= from2 + l; i--) { nvals[i] = nvals[i - l]; for (j = 0; j < nvals[i]; j++) valtable[i, j] = valtable[i - l, j]; } // insert for (i = from2; i < from2 + l; i++) { nvals[i] = nvals2[i - from2]; for (j = 0; j < nvals[i]; j++) valtable[i, j] = valtable2[i - from2, j]; } Console.WriteLine ("from {0}, to {1}, moveto {2}.\n", from, to, from2); MText mt2 = mt.Dup (); mt.Del (from, to); mt.Ins (from2, mt2, from, to); } static bool Compare () { for (int i = 0; i < LENGTH; i++) { MProperty[] array; object val = mt.GetProp (i, key, out array); if (array == null) { if (nvals[i] != 0) { Console.WriteLine ("nvals[{0}] is {1}, array.Length is null.\n", i, nvals[i]); return false; } } else if (nvals[i] != array.Length) { Console.WriteLine ("nvals[{0}] is {1}, array.Length is {2}.\n", i, nvals[i], array.Length); return false; } else { for (int j = 0; j < nvals[i]; j++) if (valtable[i, nvals[i] - 1 - j] != (MSymbol) array[j].Val) { Console.WriteLine ("valtable[{0}, {1}] is {2}, array[{1}] is {3}.\n", i, j, valtable[i, j], array[j]); return false; } } if (val == null) { if (nvals[i] != 0) { Console.WriteLine ("GetProp returned null for index {0}.\n", i); return false; } } else { if (nvals[i] == 0) { Console.WriteLine ("GetProp returned {0} for index {1}.\n", val, i); return false; } else if (valtable[i, nvals[i] - 1] == null) { Console.WriteLine ("GetProp returned {0} for index {1}.\n", val, i); return false; } else if ((MSymbol) val != valtable[i, nvals[i] - 1]) { Console.WriteLine ("GetProp returned {0} for index {1}.\n", val, i); return false; } } } return true; } static void Dump () { for (int i = 0; i < LENGTH; i++) { Console.Write (i + " "); for (int j = nvals[i] - 1; j >= 0; j--) Console.Write (valtable[i,j] + " "); Console.WriteLine (":"); } Console.WriteLine (""); } public static void Main (string[] args) { Random r = new Random (int.Parse (args[0])); int check = (args.Length > 1 ? int.Parse (args[1]) : 0xFFFFFFF); for (int loop = 0; loop < 1000000; loop++) { Console.WriteLine ("--- loop = {0} ---\n", loop); if (loop >= check) { mt.DumpPropNested (); Dump (); M17n.debug = true; } int from = r.Next (LENGTH); int to = r.Next (LENGTH + 1); if (from > to) { int tmp = from; from = to; to = tmp; } MSymbol val; //MSymbol val = r.Next (2) == 0 ? val0 : val1; switch (r.Next (3)) { case 0: val = val0; break; case 1: val = val1; break; default: val = val2; break; } MProperty prop; //MProperty prop = r.Next (2) == 0 ? prop0 : prop1; switch (r.Next (3)) { case 0: prop = prop0; break; case 1: prop = prop1; break; default: prop = prop2; break; } switch (r.Next (4)) { case 0: TestPushProp (from, to, key, val); break; case 1: TestPushProp (from, to, prop); break; case 2: TestPopProp (from, to, key); break; case 3: TestDelIns (from, to, r.Next (LENGTH - (to - from) + 1)); break; } if (M17n.debug) mt.DumpPropNested (); if (Compare () == false) { Console.WriteLine (""); Dump (); Console.WriteLine ("Failed."); return; } } } }