From: ntakahas Date: Wed, 22 Apr 2009 11:04:41 +0000 (+0000) Subject: Test PushProp () and PopProp (). X-Git-Url: http://git.chise.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fde10fa25d42228618e192991b4d044a0dc1d538;p=m17n%2Fm17n-lib-cs.git Test PushProp () and PopProp (). Usage: $ mono textprop.exe 1 // 1 is the seed for random numbers. --- diff --git a/textprop.cs b/textprop.cs index 88af7cd..cf8b502 100644 --- a/textprop.cs +++ b/textprop.cs @@ -1,47 +1,223 @@ using System; using System.Collections.Generic; -using M17N; using M17N.Core; public class Test { - public static void Main () + const int LENGTH = 10; + const int DEPTH = 10; + static MText mt = new MText ("0123456789"); + static MSymbol key = new MSymbol ("k"); + static MSymbol val0 = new MSymbol ("0"); + static MSymbol val1 = new MSymbol ("1"); + static MSymbol val2 = new MSymbol ("2"); + static MTextProperty prop0 = new MTextProperty (key, val0); + static MTextProperty prop1 = new MTextProperty (key, val1); + static MTextProperty prop2 = new MTextProperty (key, val2); + + static int[] nvals = new int[10]; + 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}, {2}.\n", from, to, val); + + mt.PushProp (from, to, key, val); + } + + static void TestPushProp (int from, int to, MTextProperty 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}, {2}.\n", from, to, prop); + + mt.PushProp (from, to, prop); + } + + static void TestPopProp (int from, int to, MSymbol key) { - String str = "0123456789"; - MText mt = new MText (str); - MSymbol sym = new MSymbol ("key"); - MTextProperty prop1 = new MTextProperty (sym, "val0"); - MTextProperty prop2 = new MTextProperty (sym, "val1"); - - M17N.M17N.debug = true; - - mt.PushProp (1, 3, prop1); - mt.PushProp (0, 1, prop1); - mt.PushProp (0, 1, prop2); - mt.PushProp (3, 5, prop1); - mt.PushProp (3, 5, prop2); - mt.PushProp (7, 8, prop2); - mt.PushProp (5, 7, prop2); - mt.PushProp (5, 7, prop1); - mt.PushProp (8, 10, prop1); - mt.PopProp (8, 9, sym); - mt.DumpPropNested (); - mt.PopProp (5, 9, sym); - mt.DumpPropNested (); - - mt = new MText (str); - mt.PushProp (2, 5, prop1); - mt.DumpProp (); - mt.PushProp (3, 6, prop2); - mt.DumpProp (); - mt.Del (4, 9); - mt.DumpProp (); - mt.Ins (5, new MText ("45678")); - mt.DumpProp (); - mt.Dup ().DumpProp (); - mt.PopProp (0, 3, sym); - mt.DumpProp (); - mt.PopProp (2, 8, sym); - mt.DumpProp (); + 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 bool Compare () + { + for (int i = 0; i < LENGTH; i++) + { + MTextProperty[] 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] != 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])); + + for (int loop = 0; loop < 10000; loop++) + { + Console.WriteLine ("--- loop = {0} ---\n", loop); + mt.DumpPropNested (); + Dump (); + + 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; + } + + MTextProperty prop; + //MTextProperty 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 (3)) + { + case 0: + TestPushProp (from, to, key, val); + break; + case 1: + TestPushProp (from, to, prop); + break; + case 2: + TestPopProp (from, to, key); + break; + } + + if (Compare () == false) + { + mt.DumpPropNested (); + Console.WriteLine (""); + Dump (); + Console.WriteLine ("Failed."); + return; + } + } + } }