• R/O
  • SSH
  • HTTPS

Commit

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

FreeTrainの進化系を目指す


Commit MetaInfo

Revision19 (tree)
Time2014-04-02 00:32:38
Authorc477

Log Message

Test and Debug for ConditionedResource and related classes.

Change Summary

Incremental Difference

--- trunk/core/core/view/ConditionedResourceFactory.cs (revision 18)
+++ trunk/core/core/view/ConditionedResourceFactory.cs (revision 19)
@@ -29,17 +29,18 @@
2929 T _default = callback(dnode.IsNull ? resource : dnode);
3030 ParamsReader conds = resource[CONDITION_NODE];
3131 #if DEBUG
32- debug_indent = "";
33- Debug.WriteLine("default value = " + _default);
32+ //debug_indent = "";
33+ //Debug.WriteLine("default value = " + _default);
3434 #endif
3535 if (conds.IsNull) {
36- Debug.WriteLine("NO condition");
37- return new ConditionedResource<T>(_default);
36+ ConditionedResource<T> ret = new ConditionedResource<T>(_default);
37+ //Debug.WriteLine("+condition:" + ret);
38+ return ret;
3839 } else {
3940 NestedConditionsResouceRoot<T> ret = new NestedConditionsResouceRoot<T>(_default);
40- Debug.WriteLine("+condition:"+ret);
41+ //Debug.WriteLine("+condition:"+ret);
4142 foreach (ParamsReader child in conds.Each) {
42- ConditionedResource<T> temp = LoadChildConditions(child, callback);
43+ ConditionedResource<T> temp = LoadChildConditions(_default, child, callback);
4344 ret.AddChild(temp);
4445 }
4546 return ApplyOptimization<T>(ret);
@@ -47,29 +48,32 @@
4748 }
4849
4950 #region private functions
50- private static ConditionedResource<T> LoadChildConditions<T>(ParamsReader resource, ResourceParser<T> callback){
51+ private static ConditionedResource<T> LoadChildConditions<T>(T val_defult, ParamsReader resource, ResourceParser<T> callback){
5152 ISceneCondition sc = GetConditionFor(resource);
52- ParamsReader dnode = resource[VALUE_NODE];
53- T _default = callback(dnode.IsNull ? resource : dnode);
5453 #if DEBUG
55- string indent_saved = debug_indent;
56- debug_indent += " ";
57- Debug.WriteLine(debug_indent+"value=" + _default);
54+ //string indent_saved = debug_indent;
55+ //debug_indent += " ";
5856 #endif
5957 ParamsReader conds = resource[CONDITION_NODE];
6058 if (conds.IsNull) {
61- return new SingleConditionResouce<T>(sc,_default);
59+ ParamsReader dnode = resource[VALUE_NODE];
60+ T _default = callback(dnode.IsNull ? resource : dnode);
61+ //Debug.WriteLine(debug_indent + "value=" + _default);
62+ SingleConditionResouce<T> ret = new SingleConditionResouce<T>(sc, _default);
63+ //Debug.WriteLine(debug_indent + "+condition:" + ret);
64+ return ret;
6265 } else {
66+ ParamsReader dnode = resource[VALUE_NODE];
67+ T _default = dnode.IsNull ? val_defult : callback(dnode);
68+ //Debug.WriteLine(debug_indent + "value=" + _default);
6369 NestedConditionsResouce<T> ret = new NestedConditionsResouce<T>(sc, _default);
64-#if DEBUG
65- Debug.WriteLine(debug_indent + "+condition:" + ret);
66-#endif
70+ //Debug.WriteLine(debug_indent + "+condition:" + ret);
6771 foreach (ParamsReader child in conds.Each) {
68- ConditionedResource<T> temp = LoadChildConditions(child, callback);
72+ ConditionedResource<T> temp = LoadChildConditions(_default, child, callback);
6973 ret.AddChild(temp);
7074 }
7175 #if DEBUG
72- debug_indent = indent_saved;
76+ //debug_indent = indent_saved;
7377 #endif
7478 return ApplyOptimization<T>(ret);
7579 }
--- trunk/core/core/view/ConditionedResource.cs (revision 18)
+++ trunk/core/core/view/ConditionedResource.cs (revision 19)
@@ -1,6 +1,7 @@
11 using System;
22 using System.Collections.Generic;
33 using System.Text;
4+using System.Diagnostics;
45
56 namespace nft.core.view
67 {
@@ -24,6 +25,10 @@
2425 res = Default;
2526 return true;
2627 }
28+
29+ public override string ToString() {
30+ return GetType().Name;
31+ }
2732 }
2833
2934 internal class SingleConditionResouce<T> : ConditionedResource<T>
@@ -41,6 +46,10 @@
4146 return false;
4247 }
4348 }
49+
50+ public override string ToString() {
51+ return String.Format("{0}[when={1}]", GetType().Name, Condition);
52+ }
4453 }
4554
4655 internal class NestedConditionsResouceRoot<T> : ConditionedResource<T>
@@ -53,6 +62,7 @@
5362
5463 internal protected override bool Choose(ISceneParams p, ref T res) {
5564 foreach (ConditionedResource<T> cc in children) {
65+ //Debug.WriteLine(" enter "+ cc);
5666 if (cc.Choose(p, ref res)) {
5767 return true;
5868 }
@@ -64,6 +74,10 @@
6474 public void AddChild(ConditionedResource<T> child) {
6575 children.Add(child);
6676 }
77+
78+ public override string ToString() {
79+ return String.Format("{0}", GetType().Name);
80+ }
6781 }
6882
6983 internal class NestedConditionsResouce<T> : NestedConditionsResouceRoot<T>
@@ -81,5 +95,9 @@
8195 return false;
8296 }
8397 }
98+
99+ public override string ToString() {
100+ return String.Format("{0}[when={1}]", GetType().Name, Condition);
101+ }
84102 }
85103 }
--- trunk/core/core/schedule/IClockEventQueue.cs (revision 18)
+++ trunk/core/core/schedule/IClockEventQueue.cs (revision 19)
@@ -32,7 +32,7 @@
3232 /// <summary>
3333 /// Dispatch events earlier than the specified Time 'To'
3434 /// </summary>
35- /// <param name="to"></param>
35+ /// <param name="clock"></param>
3636 void Dispatch( Clock clock );
3737
3838 }
--- trunk/core/core/schedule/SimpleTimeTickConverter.cs (revision 18)
+++ trunk/core/core/schedule/SimpleTimeTickConverter.cs (revision 19)
@@ -65,14 +65,14 @@
6565 public Date ToDate(long ticks) {
6666 long d = ticks/TicksIn1Day;
6767 long y,m;
68- d = Math.DivRem(d, DaysIn1Month, out m);
69- m = Math.DivRem(m, MonthsIn1Year, out y);
68+ m = Math.DivRem(d, DaysIn1Month, out d);
69+ y = Math.DivRem(m, MonthsIn1Year, out m);
7070 return new Date((int)y, (int)m+1, (int)d+1);
7171 }
7272
7373 public TimeOfDay ToTimeOfDay(long ticks) {
74- long m;
75- long s = Math.DivRem(ticks, TicksIn1Minute, out m);
74+ long s;
75+ long m = Math.DivRem(ticks, TicksIn1Minute, out s);
7676 s = 60 * s / TicksIn1Minute; // Normarize
7777 long h = Math.DivRem(m, MinutesIn1Hour, out m);
7878 h = h % HoursIn1Day;
@@ -102,8 +102,8 @@
102102 }
103103
104104 public int GetMonth(long ticks) {
105- long m = ticks / TicksIn1Month;
106- return 1+(int)(m % MonthsIn1Year);
105+ long m = ticks % TicksIn1Year;
106+ return 1+(int)(m / TicksIn1Month);
107107 }
108108
109109 public int GetYear(long ticks) {
--- trunk/core/core/schedule/DateTime.cs (revision 18)
+++ trunk/core/core/schedule/DateTime.cs (revision 19)
@@ -38,6 +38,10 @@
3838 }
3939 }
4040
41+ public override string ToString() {
42+ return string.Format("Date:[{0}/{1}/{2}]", Year, Month, Day);
43+ }
44+
4145 #region IComparable メンバ
4246 public int CompareTo(Date other) {
4347 return Math.Sign(this.SerialValue - other.SerialValue);
@@ -122,6 +126,10 @@
122126 return (Hour << 12) + (Minute << 6) + Second;
123127 }
124128 }
129+
130+ public override string ToString() {
131+ return string.Format("TimeOfDay:[{0,2:d}:{1,2:d}:{2,2:d}]",Hour,Minute,Second);
132+ }
125133
126134 #region IComparable メンバ
127135 public int CompareTo(TimeOfDay other) {
--- trunk/core/core/schedule/Time.cs (revision 18)
+++ trunk/core/core/schedule/Time.cs (revision 19)
@@ -128,6 +128,7 @@
128128
129129 public Date ToDate() {
130130 return Converter.ToDate(ticks);
131+ //return new Date(Year,Month,Day);
131132 }
132133
133134 /// <summary>
--- trunk/core/core/schedule/PrimitiveConditions.cs (revision 18)
+++ trunk/core/core/schedule/PrimitiveConditions.cs (revision 19)
@@ -249,6 +249,9 @@
249249
250250 protected abstract bool IsMatchCore(ISceneParams p);
251251
252+ public override string ToString() {
253+ return String.Format("{0}:({1}/{2}/{3})", GetType().Name, ConditionType, GroupName, ConditionName);
254+ }
252255 }
253256
254257 public abstract class ComparebleSceneCondition : CacheableSceneCondition {
--- trunk/core/core/schedule/ConditionValueExtracter.cs (revision 18)
+++ trunk/core/core/schedule/ConditionValueExtracter.cs (revision 19)
@@ -49,7 +49,7 @@
4949 }
5050
5151 public override IComparable GetTestValue(ISceneParams p) {
52- return p.Time.Month;
52+ return (long)p.Time.Month;
5353 }
5454 }
5555
@@ -90,11 +90,10 @@
9090 }
9191 }
9292
93- public class ScaleConditionValueExtracter : LongConditionValueExtracter {
94- public ScaleConditionValueExtracter()
95- : base(ConditionTypes.Scale) {
93+ public class ScaleConditionValueExtracter : IConditionValueExtracter {
94+ public ScaleConditionValueExtracter(){
9695 }
97- public override IComparable GetTestValue(ISceneParams p) {
96+ public IComparable GetTestValue(ISceneParams p) {
9897 return p.Scaler.Value;
9998 }
10099
--- trunk/core/core/schedule/Calendar.cs (revision 18)
+++ trunk/core/core/schedule/Calendar.cs (revision 19)
@@ -68,8 +68,8 @@
6868 ParamsReader match = r2["match"];
6969 if(match.IsNull){
7070 string v_from = r2["from"].InnerText;
71- string v_to = r2["to"].InnerText;
72- PrimitiveConditions.CreateSceneConditionFromDateFormat(grp_name, cname, ctype, format, v_from, v_to );
71+ string v_until = r2["until"].InnerText;
72+ PrimitiveConditions.CreateSceneConditionFromDateFormat(grp_name, cname, ctype, format, v_from, v_until );
7373 } else {
7474 string v_match = match.InnerText;
7575 PrimitiveConditions.CreateSceneConditionFromDateFormat(grp_name, cname, ctype, format, v_match);
--- trunk/TestLauncher/test/ClockTest.cs (revision 18)
+++ trunk/TestLauncher/test/ClockTest.cs (revision 19)
@@ -7,19 +7,83 @@
77 using nft.util;
88 using System.Diagnostics;
99 using nft.core.schedule;
10+using System.IO;
11+using nft.framework.loader;
12+using nft.core.view;
13+using nft.framework.drawing;
1014
1115 namespace nft.test.test {
12- class ClockTest {
16+ class ClockTest {
1317 [TestEntry]
14- public static void InitCalender() {
15- try {
16- GlobalModules.Initialize();
17- }catch(Exception ex){
18- Debug.WriteLine(ex.Message);
19- Debug.WriteLine(ex.StackTrace);
18+ public static void InitCalendar() {
19+ if (Calendar.ActiveCalendar != null) {
20+ Debug.WriteLine("The calendar is already initialized.");
21+ return;
2022 }
23+ XmlDocument doc = XmlUtil.LoadFile(Path.Combine(Directories.AppBaseDir, "core_modules.xml"));
24+ ParamsReader reader = new ParamsReader(doc.BaseURI, new XmlParamParser(doc));
25+ ParamsReader root = reader["modules"];
26+ foreach (ParamsReader cn in root.EnumChildren("module")) {
27+ if (cn["name"].InnerText.Equals("Game Time Calendar")) {
28+ Debug.WriteLine("Initialize the calendar.");
29+ new Calendar(cn);
30+ }
31+ }
2132 }
2233
34+ [TestEntry(new object[]{new int[]{0,3,13,31}})]
35+ public static void TestDateConversion(int[] years) {
36+ ITimeTickConverter ttc = Calendar.ActiveCalendar.TimeTickConverter;
37+ long day = ttc.DailyTicks;
38+ long yer = ttc.YearlyTicksAve;
39+ long mnt = ttc.MonthlyTicksAve;
40+ int mcnt = (int)Math.Ceiling(yer / (double)mnt);
41+ int dcnt = (int)Math.Ceiling(mnt / (double)day);
42+ int erros = 0;
43+ Debug.WriteLine(String.Format("{0} months per year, {1} days per month.", mcnt, dcnt));
44+ for (int i = 0; i < years.Length; i++) {
45+ Debug.WriteLine(String.Format("## Start loop for the {0}th year ##", years[i]));
46+ for (int m = 1; m <= mcnt; m++) {
47+ for (int d = 1; d <= dcnt; d++) {
48+ Date date = new Date(years[i], m, d);
49+ Time time = Time.Create(date, ttc);
50+ Date dat2 = time.ToDate();
51+ if (!date.Equals(dat2)) {
52+ erros++;
53+ Debug.WriteLine(String.Format("Conversion Error: {0} -> {1}", date, dat2));
54+ }
55+ }
56+ }
57+ }
58+ if(erros>0) throw new Exception(String.Format("Detect {0} conversion errors!",erros));
59+ }
60+
61+ [TestEntry(new object[] { new int[] { 0, 31, 977, 4073 } })]
62+ public static void TestTimeOfDayConversion(int[] days) {
63+ ITimeTickConverter ttc = Calendar.ActiveCalendar.TimeTickConverter;
64+ long day = ttc.DailyTicks;
65+ long secs = ttc.MinutelyTicks;
66+ int erros = 0;
67+ for (int i = 0; i < days.Length; i++) {
68+ Date offset = TimeLength.FromDays(ttc, days[i]).ToDate();
69+ Debug.WriteLine(String.Format("## Start 24H loop for the {0} ##", offset));
70+ for (int h = 0; h < 24; h++) {
71+ for (int m = 0; m < 60; m++) {
72+ for (int s = 0; s < secs; s++) {
73+ TimeOfDay tod = new TimeOfDay(h, m, s);
74+ Time time = Time.Create(offset, tod, ttc);
75+ TimeOfDay tod2 = time.GetTimeOfTheDay();
76+ if (!tod.Equals(tod2)) {
77+ erros++;
78+ Debug.WriteLine(String.Format("Conversion Error: {0} -> {1}", tod, tod2));
79+ }
80+ }
81+ }
82+ }
83+ }
84+ if (erros > 0) throw new Exception(String.Format("Detect {0} conversion errors!", erros));
85+ }
86+
2387 [TestEntry]
2488 public static void TestClock() {
2589 Clock clock = Calendar.ActiveCalendar.CreateNewClock(new Date());
@@ -39,6 +103,44 @@
39103
40104 }
41105
106+ [TestEntry]
107+ public static void TestConditionedResource() {
108+ XmlDocument doc = new XmlDocument();
109+ doc.LoadXml(@"<root>
110+<default>Apple</default>
111+<condition when='winter'><value>Banana</value>
112+ <condition when='night'>Orange</condition>
113+</condition>
114+<condition when='night'>
115+ <condition when='summer'>Pinapple</condition>
116+</condition>
117+</root>");
118+ ParamsReader reader = new ParamsReader("test", new XmlParamParser(doc.FirstChild));
119+ ConditionedResource<String> res = ConditionedResourceFactory.LoadAsConditiondResources<String>(reader, ParseString );
120+ ITimeTickConverter ttc = Calendar.ActiveCalendar.TimeTickConverter;
121+ Date spring = new Date(1, 4, 6);
122+ Date summer = new Date(1, 7, 10);
123+ Date winter = new Date(1, 1, 5);
124+ TimeOfDay night = new TimeOfDay(0, 0);
125+ TimeOfDay day = new TimeOfDay(13, 30);
126+ Debug.WriteLine("Spring,Day="+res[new TestParams(Time.Create(spring, day))]);
127+ PrimitiveConditions.InvalidateCachedConditions();
128+ Debug.WriteLine("Spring,Night=" + res[new TestParams(Time.Create(spring, night))]);
129+ PrimitiveConditions.InvalidateCachedConditions();
130+ Debug.WriteLine("Summer,Day=" + res[new TestParams(Time.Create(summer, day))]);
131+ PrimitiveConditions.InvalidateCachedConditions();
132+ Debug.WriteLine("Summer,Night=" + res[new TestParams(Time.Create(summer, night))]);
133+ PrimitiveConditions.InvalidateCachedConditions();
134+ Debug.WriteLine("Winter,Day=" + res[new TestParams(Time.Create(winter, day))]);
135+ PrimitiveConditions.InvalidateCachedConditions();
136+ Debug.WriteLine("Winter,Night=" + res[new TestParams(Time.Create(winter, night))]);
137+
138+ }
139+
140+ static private String ParseString(ParamsReader r) {
141+ return r.InnerText;
142+ }
143+
42144 class ClockEventCallback {
43145 String msg;
44146 int n;
@@ -54,5 +156,19 @@
54156 return n < 10;
55157 }
56158 }
159+
160+ class TestParams : ISceneParams {
161+ private Time time;
162+ public TestParams(Time t) {
163+ time = t;
164+ Debug.WriteLine("Time="+time);
165+ }
166+
167+ public Time Time { get{ return time; }}
168+
169+ public object Weather { get { return null; }}
170+
171+ public Scaler Scaler { get {return Scaler.Default; }}
172+ }
57173 }
58174 }