From 597dd8b153617bff1d9f8f0dea98b92e1a572d8f Mon Sep 17 00:00:00 2001 From: ntakahas Date: Tue, 16 Jun 2009 10:49:00 +0000 Subject: [PATCH] *** empty log message *** --- bothsensitive.cs | 249 +++++++++++++++++++++++++++++++++++++++++++++++++++++ frontsensitive.cs | 2 +- rearsensitive.cs | 4 +- 3 files changed, 252 insertions(+), 3 deletions(-) create mode 100644 bothsensitive.cs diff --git a/bothsensitive.cs b/bothsensitive.cs new file mode 100644 index 0000000..6486405 --- /dev/null +++ b/bothsensitive.cs @@ -0,0 +1,249 @@ +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; + } + } + } +} diff --git a/frontsensitive.cs b/frontsensitive.cs index 854dd0d..edaeb14 100644 --- a/frontsensitive.cs +++ b/frontsensitive.cs @@ -97,7 +97,7 @@ public class Test valtable[LENGTH - l] = null; // sensitivity for insertion - if (valtable[from2] != null) + if (l < LENGTH && valtable[from2] != null) { sym = valtable[from2]; for (i = from2; i < LENGTH && valtable[i] == sym; i++) diff --git a/rearsensitive.cs b/rearsensitive.cs index 37d5e98..d10f8cc 100644 --- a/rearsensitive.cs +++ b/rearsensitive.cs @@ -80,7 +80,7 @@ public class Test for (i = from - 1; i >= 0 && valtable[i] == sym; i--) valtable[i] = null; } - if (valtable[to - 1] != null) + if (to < LENGTH && valtable[to - 1] != null) { sym = valtable[to - 1]; for (i = to - 1; i >= 0 && valtable[i] == sym; i--) @@ -103,7 +103,7 @@ public class Test for (i = from2 - 1; i >= 0 && valtable[i] == sym; i--) valtable[i] = null; } - if ((to < LENGTH || from2 < LENGTH - 1) && valtable2[l - 1] != null) + if ((to < LENGTH || from2 + l < LENGTH) && valtable2[l - 1] != null) { sym = valtable2[l - 1]; for (i = l - 1; i >= 0; i--) -- 1.7.10.4