scriptinvokertests.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. class ScriptInvokerTests : TestFramework
  2. {
  3. ref ScriptInvoker m_Invoker;
  4. int m_InvokeCount;
  5. //---------------------------------------------------------------------------
  6. // Ctor - Decides the tests to run
  7. //---------------------------------------------------------------------------
  8. void ScriptInvokerTests()
  9. {
  10. m_Invoker = new ScriptInvoker();
  11. //AddInitTest("TestFirstUnique");
  12. //AddInitTest("TestSecondUnique");
  13. //AddInitTest("TestInsertRemoveUnique");
  14. //AddInitTest("TestInsertUniqueImmediate");
  15. //AddInitTest("TestClearRunning");
  16. //AddInitTest("TestInvokeRunning");
  17. AddInitTest("TestInsertRunning");
  18. }
  19. //---------------------------------------------------------------------------
  20. // Dtor
  21. //---------------------------------------------------------------------------
  22. void ~ScriptInvokerTests()
  23. {
  24. }
  25. //---------------------------------------------------------------------------
  26. // Tests
  27. //---------------------------------------------------------------------------
  28. // Test first insert flagged as unique
  29. TFResult TestFirstUnique()
  30. {
  31. InvokeReset();
  32. bool insert1 = m_Invoker.Insert(InvokeLog, EScriptInvokerInsertFlags.UNIQUE);
  33. Assert(insert1);
  34. bool insert2 = m_Invoker.Insert(InvokeLog);
  35. Assert(!insert2);
  36. m_Invoker.Invoke("TestFirstUnique");
  37. bool count = Assert(m_InvokeCount == 1);
  38. InvokeReset();
  39. return BTFR(insert1 && !insert2 && count);
  40. }
  41. //---------------------------------------------------------------------------
  42. // Test second insert flagged as unique
  43. TFResult TestSecondUnique()
  44. {
  45. InvokeReset();
  46. bool insert1 = m_Invoker.Insert(InvokeLog);
  47. Assert(insert1);
  48. bool insert2 = m_Invoker.Insert(InvokeLog, EScriptInvokerInsertFlags.UNIQUE);
  49. Assert(!insert2);
  50. m_Invoker.Invoke("TestSecondUnique");
  51. bool count = Assert(m_InvokeCount == 1);
  52. InvokeReset();
  53. return BTFR(insert1 && !insert2 && count);
  54. }
  55. //---------------------------------------------------------------------------
  56. // Test inserting and removing of unique
  57. TFResult TestInsertRemoveUnique()
  58. {
  59. InvokeReset();
  60. bool insert1 = m_Invoker.Insert(InvokeLog, EScriptInvokerInsertFlags.UNIQUE);
  61. Assert(insert1);
  62. bool remove1 = m_Invoker.Remove(InvokeLog);
  63. Assert(remove1);
  64. bool insert2 = m_Invoker.Insert(InvokeLog);
  65. Assert(insert2);
  66. m_Invoker.Invoke("TestInsertRemoveUnique");
  67. bool count = Assert(m_InvokeCount == 1);
  68. InvokeReset();
  69. return BTFR(insert1 && remove1 && insert2 && count);
  70. }
  71. //---------------------------------------------------------------------------
  72. // Test inserting and of unique and immediate
  73. TFResult TestInsertUniqueImmediate()
  74. {
  75. InvokeReset();
  76. bool insert1 = m_Invoker.Insert(InvokeLog, EScriptInvokerInsertFlags.UNIQUE);
  77. Assert(insert1);
  78. bool insert2 = m_Invoker.Insert(InvokeLog, EScriptInvokerInsertFlags.IMMEDIATE);
  79. Assert(insert2);
  80. m_Invoker.Invoke("TestInsertUniqueImmediate");
  81. bool count = Assert(m_InvokeCount == 1);
  82. InvokeReset();
  83. return BTFR(insert1 && !insert2 && count);
  84. }
  85. //---------------------------------------------------------------------------
  86. // Test clearing while invoking
  87. TFResult TestClearRunning()
  88. {
  89. InvokeReset();
  90. m_Invoker.Insert(InvokeClear);
  91. m_Invoker.Insert(InvokeLog);
  92. m_Invoker.Invoke("TestClearRunning");
  93. bool count = Assert(m_InvokeCount == 1);
  94. InvokeReset();
  95. return BTFR(count);
  96. }
  97. //---------------------------------------------------------------------------
  98. // Test invoke while invoking (will result in overflow)
  99. TFResult TestInvokeRunning()
  100. {
  101. InvokeReset();
  102. m_Invoker.Insert(InvokeInvoke);
  103. m_Invoker.Invoke("TestInvokeRunning");
  104. InvokeReset();
  105. return BTFR(false); // This can never succeed, it will never even reach this point
  106. }
  107. //---------------------------------------------------------------------------
  108. // Test insert while invoking
  109. TFResult TestInsertRunning()
  110. {
  111. InvokeReset();
  112. m_Invoker.Insert(InvokeInsert);
  113. m_Invoker.Invoke("TestInvokeRunning");
  114. Print(m_InvokeCount);
  115. bool count = Assert(m_InvokeCount == 129);
  116. InvokeReset();
  117. return BTFR(count);
  118. }
  119. //---------------------------------------------------------------------------
  120. // Helpers
  121. //---------------------------------------------------------------------------
  122. void InvokeLog(string s)
  123. {
  124. //Debug.TFLog(s, this, "InvokeLog");
  125. ++m_InvokeCount;
  126. }
  127. void InvokeReset()
  128. {
  129. m_Invoker.Clear();
  130. m_InvokeCount = 0;
  131. }
  132. void InvokeClear(string s)
  133. {
  134. InvokeLog(s);
  135. m_Invoker.Clear();
  136. }
  137. void InvokeInvoke(string s)
  138. {
  139. InvokeLog(s);
  140. m_Invoker.Invoke(s);
  141. }
  142. void InvokeInsert(string s)
  143. {
  144. InvokeLog(s);
  145. m_Invoker.Insert(InvokeInsert, EScriptInvokerInsertFlags.IMMEDIATE);
  146. }
  147. }