From: ntakahas Date: Fri, 1 May 2009 09:00:16 +0000 (+0000) Subject: New file. Test RearSticky & FrontSticky textprop. X-Git-Url: http://git.chise.org/gitweb/?a=commitdiff_plain;h=47dd52ed6e15677a1945e18ba32df853144420f4;p=m17n%2Fm17n-lib-cs.git New file. Test RearSticky & FrontSticky textprop. --- diff --git a/rf_.cs b/rf_.cs new file mode 100644 index 0000000..fb2fcd9 --- /dev/null +++ b/rf_.cs @@ -0,0 +1,259 @@ +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 ("rf_", + MProperty.Flags.FrontSticky + | MProperty.Flags.RearSticky); + 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, 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 + if (from2 > 0 && nvals[from2 - 1] > 0) + for (i = from2; i < from2 + l; i++) + { + nvals[i] = nvals[from2 - 1]; + for (j = 0; j < nvals[from2 - 1]; j++) + valtable[i, j] = valtable[from2 - 1, j]; + } + else if (from2 + l < LENGTH && nvals[from2 + l] > 0) + for (i = from2; i < from2 + l; i++) + { + nvals[i] = nvals[from2 + l]; + for (j = 0; j < nvals[from2 + l]; j++) + valtable[i, j] = valtable[from2 + l, j]; + } + else + for (i = from2; i < from2 + l; i++) + { + nvals[i] = nvals2[i - from2]; + for (j = 0; j < nvals2[i - from2]; 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; + } + + 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; + } + } + } +}