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 ("bse", MProperty.Flags.FrontSensitive | MProperty.Flags.RearSensitive); 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 MSymbol[] valtable = new MSymbol[LENGTH]; static void TestPushProp (int from, int to, MProperty prop) { int i; MSymbol sym; if (from > 0 && valtable[from] != null) { sym = valtable[from]; for (i = from - 1; i >= 0 && valtable[i] == sym; i--) valtable[i] = null; } if (to < LENGTH && valtable[to - 1] != null) { sym = valtable[to - 1]; for (i = to; i < LENGTH && valtable[i] == sym; i++) valtable[i] = null; } for (i = from; i < to; i++) valtable[i] = (MSymbol) prop.Val; Console.WriteLine ("from {0}, to {1}, push {2}.\n", from, to, prop.Val); mt.PushProp (from, to, prop); } static void TestPopProp (int from, int to, MSymbol key) { int i; MSymbol sym; if (from > 0 && valtable[from] != null) { sym = valtable[from]; for (i = from - 1; i >= 0 && valtable[i] == sym; i--) valtable[i] = null; } if (to < LENGTH && valtable[to - 1] != null) { sym = valtable[to - 1]; for (i = to; i < LENGTH && valtable[i] == sym; i++) valtable[i] = null; } for (i = from; i < to; i++) valtable[i] = null; 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, l = to - from; MSymbol[] valtable2 = new MSymbol[LENGTH]; MSymbol sym; // sensitivity for deletion if (from > 0) { if (valtable[from - 1] != null) { sym = valtable[from - 1]; for (i = from - 1; i >= 0 && valtable[i] == sym; i--) valtable[i] = null; } if (valtable[from] != null) { sym = valtable[from]; for (i = from; i < LENGTH && valtable[i] == sym; i++) valtable[i] = null; } } if (to < LENGTH) { if (valtable[to - 1] != null) { sym = valtable[to - 1]; for (i = to - 1; i >= 0 && valtable[i] == sym; i--) valtable[i] = null; } if (valtable[to] != null) { sym = valtable[to]; for (i = to; i < LENGTH && valtable[i] == sym; i++) valtable[i] = null; } } // copy for (i = from; i < to; i++) valtable2[i - from] = valtable[i]; // delete for (i = to; i < LENGTH; i++) valtable[i - l] = valtable[i]; valtable[LENGTH - l] = null; // sensitivity for insertion if (l < LENGTH && valtable[from2] != null) { sym = valtable[from2]; for (i = from2; i < LENGTH && valtable[i] == sym; i++) valtable[i] = null; } if (from2 > 0 && valtable[from2 - 1] != null) { sym = valtable[from2 - 1]; for (i = from2 - 1; i >= 0 && valtable[i] == sym; i--) valtable[i] = null; } if ((from > 0 || from2 > 0) && valtable2[0] != null) { sym = valtable2[0]; for (i = 0; i < LENGTH && valtable2[i] == sym; i++) valtable2[i] = null; } if ((to < LENGTH || from2 + l < LENGTH) && valtable2[l - 1] != null) { sym = valtable2[l - 1]; for (i = l - 1; i >= 0; i--) valtable2[i] = null; } // move for (i = LENGTH - 1; i >= from2 + l; i--) valtable[i] = valtable[i - l]; // insert for (i = from2; i < from2 + l; i++) valtable[i] = valtable2[i - from2]; 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++) { object val = mt.GetProp (i, key); if (valtable[i] != (MSymbol) val) { Console.WriteLine ("valtable[{0}] is {1}, GetProp returned {2}", i, valtable[i] == null ? MSymbol.nil : valtable[i], val == null ? MSymbol.nil : val); return false; } } return true; } static void Dump () { for (int i = 0; i < LENGTH; i++) Console.WriteLine ("{0} {1} :", i, valtable[i]); } 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++) { int from, to; Console.WriteLine ("--- loop = {0} ---\n", loop); if (loop >= check) { mt.DumpPropNested (); Dump (); M17n.debug = true; } do { from = r.Next (LENGTH); to = r.Next (LENGTH) + 1; } while (from == to); if (from > to) { int tmp = from; from = to; to = tmp; } MProperty prop; switch (r.Next (3)) { case 0: prop = prop0; break; case 1: prop = prop1; break; default: prop = prop2; break; } switch (r.Next (3)) { case 0: TestPushProp (from, to, prop); break; case 1: TestPopProp (from, to, key); break; case 2: 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; } } } }