Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

308 lines
9.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Unit test program for CommandBuffer
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "unitlib/unitlib.h"
  8. #include "tier1/commandbuffer.h"
  9. #include "tier1/strtools.h"
  10. DEFINE_TESTSUITE( CommandBufferTestSuite )
  11. DEFINE_TESTCASE( CommandBufferTestSimple, CommandBufferTestSuite )
  12. {
  13. Msg( "Simple command buffer test...\n" );
  14. CCommandBuffer buffer;
  15. buffer.AddText( "test_command test_arg1 test_arg2" );
  16. buffer.AddText( "test_command2 test_arg3; test_command3 test_arg4" );
  17. buffer.AddText( "test_command4\ntest_command5" );
  18. buffer.AddText( "test_command6 // Comment; test_command7" );
  19. buffer.AddText( "test_command8 // Comment; test_command9\ntest_command10" );
  20. buffer.AddText( "test_command11 \"test_arg5 test_arg6\"" );
  21. buffer.AddText( "test_command12 \"\"" );
  22. buffer.AddText( "// Comment\ntest_command13\t\t\"test_arg7\"" );
  23. buffer.AddText( "test_command14\"test_arg8\"test_arg9" );
  24. buffer.AddText( "test_command15 test_arg10" );
  25. buffer.AddText( "test_command16 test_arg11:test_arg12" );
  26. int argc;
  27. const char **argv;
  28. buffer.BeginProcessingCommands( 1 );
  29. argc = buffer.DequeueNextCommand( argv );
  30. Shipping_Assert( argc == 3 );
  31. Shipping_Assert( !Q_stricmp( argv[0], "test_command" ) );
  32. Shipping_Assert( !Q_stricmp( argv[1], "test_arg1" ) );
  33. Shipping_Assert( !Q_stricmp( argv[2], "test_arg2" ) );
  34. Shipping_Assert( !Q_stricmp( buffer.ArgS(), "test_arg1 test_arg2" ) );
  35. argc = buffer.DequeueNextCommand( argv );
  36. Shipping_Assert( argc == 2 );
  37. Shipping_Assert( !Q_stricmp( argv[0], "test_command2" ) );
  38. Shipping_Assert( !Q_stricmp( argv[1], "test_arg3" ) );
  39. Shipping_Assert( !Q_stricmp( buffer.ArgS(), "test_arg3" ) );
  40. argc = buffer.DequeueNextCommand( argv );
  41. Shipping_Assert( argc == 2 );
  42. Shipping_Assert( !Q_stricmp( argv[0], "test_command3" ) );
  43. Shipping_Assert( !Q_stricmp( argv[1], "test_arg4" ) );
  44. Shipping_Assert( !Q_stricmp( buffer.ArgS(), "test_arg4" ) );
  45. argc = buffer.DequeueNextCommand( argv );
  46. Shipping_Assert( argc == 1 );
  47. Shipping_Assert( !Q_stricmp( argv[0], "test_command4" ) );
  48. Shipping_Assert( !Q_stricmp( buffer.ArgS(), "" ) );
  49. argc = buffer.DequeueNextCommand( argv );
  50. Shipping_Assert( argc == 1 );
  51. Shipping_Assert( !Q_stricmp( argv[0], "test_command5" ) );
  52. Shipping_Assert( !Q_stricmp( buffer.ArgS(), "" ) );
  53. argc = buffer.DequeueNextCommand( argv );
  54. Shipping_Assert( argc == 1 );
  55. Shipping_Assert( !Q_stricmp( argv[0], "test_command6" ) );
  56. Shipping_Assert( !Q_stricmp( buffer.ArgS(), "" ) );
  57. argc = buffer.DequeueNextCommand( argv );
  58. Shipping_Assert( argc == 1 );
  59. Shipping_Assert( !Q_stricmp( argv[0], "test_command8" ) );
  60. Shipping_Assert( !Q_stricmp( buffer.ArgS(), "" ) );
  61. argc = buffer.DequeueNextCommand( argv );
  62. Shipping_Assert( argc == 1 );
  63. Shipping_Assert( !Q_stricmp( argv[0], "test_command10" ) );
  64. Shipping_Assert( !Q_stricmp( buffer.ArgS(), "" ) );
  65. argc = buffer.DequeueNextCommand( argv );
  66. Shipping_Assert( argc == 2 );
  67. Shipping_Assert( !Q_stricmp( argv[0], "test_command11" ) );
  68. Shipping_Assert( !Q_stricmp( argv[1], "test_arg5 test_arg6" ) );
  69. Shipping_Assert( !Q_stricmp( buffer.ArgS(), "\"test_arg5 test_arg6\"" ) );
  70. argc = buffer.DequeueNextCommand( argv );
  71. Shipping_Assert( argc == 2 );
  72. Shipping_Assert( !Q_stricmp( argv[0], "test_command12" ) );
  73. Shipping_Assert( !Q_stricmp( argv[1], "" ) );
  74. Shipping_Assert( !Q_stricmp( buffer.ArgS(), "\"\"" ) );
  75. argc = buffer.DequeueNextCommand( argv );
  76. Shipping_Assert( argc == 2 );
  77. Shipping_Assert( !Q_stricmp( argv[0], "test_command13" ) );
  78. Shipping_Assert( !Q_stricmp( argv[1], "test_arg7" ) );
  79. Shipping_Assert( !Q_stricmp( buffer.ArgS(), "\"test_arg7\"" ) );
  80. argc = buffer.DequeueNextCommand( argv );
  81. Shipping_Assert( argc == 3 );
  82. Shipping_Assert( !Q_stricmp( argv[0], "test_command14" ) );
  83. Shipping_Assert( !Q_stricmp( argv[1], "test_arg8" ) );
  84. Shipping_Assert( !Q_stricmp( argv[2], "test_arg9" ) );
  85. Shipping_Assert( !Q_stricmp( buffer.ArgS(), "\"test_arg8\"test_arg9" ) );
  86. argc = buffer.DequeueNextCommand( argv );
  87. Shipping_Assert( argc == 2 );
  88. Shipping_Assert( !Q_stricmp( argv[0], "test_command15" ) );
  89. Shipping_Assert( !Q_stricmp( argv[1], "test_arg10" ) );
  90. Shipping_Assert( !Q_stricmp( buffer.ArgS(), "test_arg10" ) );
  91. argc = buffer.DequeueNextCommand( argv );
  92. Shipping_Assert( argc == 4 );
  93. Shipping_Assert( !Q_stricmp( argv[0], "test_command16" ) );
  94. Shipping_Assert( !Q_stricmp( argv[1], "test_arg11" ) );
  95. Shipping_Assert( !Q_stricmp( argv[2], ":" ) );
  96. Shipping_Assert( !Q_stricmp( argv[3], "test_arg12" ) );
  97. Shipping_Assert( !Q_stricmp( buffer.ArgS(), "test_arg11:test_arg12" ) );
  98. argc = buffer.DequeueNextCommand( argv );
  99. Shipping_Assert( argc == 0 );
  100. buffer.EndProcessingCommands( );
  101. }
  102. DEFINE_TESTCASE( CommandBufferTestTiming, CommandBufferTestSuite )
  103. {
  104. Msg( "Delayed command buffer test...\n" );
  105. CCommandBuffer buffer;
  106. buffer.AddText( "test_command test_arg1 test_arg2" );
  107. buffer.AddText( "test_command2 test_arg1 test_arg2 test_arg3", 1 );
  108. buffer.AddText( "test_command3;wait;test_command4;wait 2;test_command5" );
  109. int argc;
  110. const char **argv;
  111. {
  112. buffer.BeginProcessingCommands( 1 );
  113. argc = buffer.DequeueNextCommand( argv );
  114. Shipping_Assert( argc == 3 );
  115. Shipping_Assert( !Q_stricmp( argv[0], "test_command" ) );
  116. Shipping_Assert( !Q_stricmp( argv[1], "test_arg1" ) );
  117. Shipping_Assert( !Q_stricmp( argv[2], "test_arg2" ) );
  118. argc = buffer.DequeueNextCommand( argv );
  119. Shipping_Assert( argc == 1 );
  120. Shipping_Assert( !Q_stricmp( argv[0], "test_command3" ) );
  121. argc = buffer.DequeueNextCommand( argv );
  122. Shipping_Assert( argc == 0 );
  123. buffer.EndProcessingCommands( );
  124. }
  125. {
  126. buffer.BeginProcessingCommands( 1 );
  127. argc = buffer.DequeueNextCommand( argv );
  128. Shipping_Assert( argc == 4 );
  129. Shipping_Assert( !Q_stricmp( argv[0], "test_command2" ) );
  130. Shipping_Assert( !Q_stricmp( argv[1], "test_arg1" ) );
  131. Shipping_Assert( !Q_stricmp( argv[2], "test_arg2" ) );
  132. Shipping_Assert( !Q_stricmp( argv[3], "test_arg3" ) );
  133. argc = buffer.DequeueNextCommand( argv );
  134. Shipping_Assert( argc == 1 );
  135. Shipping_Assert( !Q_stricmp( argv[0], "test_command4" ) );
  136. argc = buffer.DequeueNextCommand( argv );
  137. Shipping_Assert( argc == 0 );
  138. buffer.EndProcessingCommands( );
  139. }
  140. {
  141. buffer.BeginProcessingCommands( 1 );
  142. argc = buffer.DequeueNextCommand( argv );
  143. Shipping_Assert( argc == 0 );
  144. buffer.EndProcessingCommands( );
  145. }
  146. {
  147. buffer.BeginProcessingCommands( 1 );
  148. argc = buffer.DequeueNextCommand( argv );
  149. Shipping_Assert( argc == 1 );
  150. Shipping_Assert( !Q_stricmp( argv[0], "test_command5" ) );
  151. argc = buffer.DequeueNextCommand( argv );
  152. Shipping_Assert( argc == 0 );
  153. buffer.EndProcessingCommands( );
  154. }
  155. }
  156. DEFINE_TESTCASE( CommandBufferTestNested, CommandBufferTestSuite )
  157. {
  158. Msg( "Nested command buffer test...\n" );
  159. CCommandBuffer buffer;
  160. buffer.AddText( "test_command test_arg1 test_arg2" );
  161. buffer.AddText( "test_command2 test_arg3 test_arg4 test_arg5", 2 );
  162. int argc;
  163. const char **argv;
  164. {
  165. buffer.BeginProcessingCommands( 2 );
  166. argc = buffer.DequeueNextCommand( argv );
  167. Shipping_Assert( argc == 3 );
  168. Shipping_Assert( !Q_stricmp( argv[0], "test_command" ) );
  169. Shipping_Assert( !Q_stricmp( argv[1], "test_arg1" ) );
  170. Shipping_Assert( !Q_stricmp( argv[2], "test_arg2" ) );
  171. argc = buffer.DequeueNextCommand( argv );
  172. Shipping_Assert( argc == 0 );
  173. buffer.AddText( "test_command3;test_command4", 1 );
  174. argc = buffer.DequeueNextCommand( argv );
  175. Shipping_Assert( argc == 1 );
  176. Shipping_Assert( !Q_stricmp( argv[0], "test_command3" ) );
  177. argc = buffer.DequeueNextCommand( argv );
  178. Shipping_Assert( argc == 1 );
  179. Shipping_Assert( !Q_stricmp( argv[0], "test_command4" ) );
  180. argc = buffer.DequeueNextCommand( argv );
  181. Shipping_Assert( argc == 0 );
  182. buffer.EndProcessingCommands( );
  183. }
  184. {
  185. buffer.BeginProcessingCommands( 1 );
  186. argc = buffer.DequeueNextCommand( argv );
  187. Shipping_Assert( argc == 4 );
  188. Shipping_Assert( !Q_stricmp( argv[0], "test_command2" ) );
  189. Shipping_Assert( !Q_stricmp( argv[1], "test_arg3" ) );
  190. Shipping_Assert( !Q_stricmp( argv[2], "test_arg4" ) );
  191. Shipping_Assert( !Q_stricmp( argv[3], "test_arg5" ) );
  192. argc = buffer.DequeueNextCommand( argv );
  193. Shipping_Assert( argc == 0 );
  194. buffer.EndProcessingCommands( );
  195. }
  196. }
  197. DEFINE_TESTCASE( CommandBufferTestOverflow, CommandBufferTestSuite )
  198. {
  199. Msg( "Command buffer overflow test...\n" );
  200. CCommandBuffer buffer;
  201. buffer.LimitArgumentBufferSize( 40 );
  202. bool bOk = buffer.AddText( "test_command test_arg1 test_arg2" ); // 32 chars
  203. Shipping_Assert( bOk );
  204. bOk = buffer.AddText( "test_command2 test_arg3 test_arg4 test_arg5", 2 ); // 43 chars
  205. Shipping_Assert( !bOk );
  206. int argc;
  207. const char **argv;
  208. {
  209. buffer.BeginProcessingCommands( 1 );
  210. argc = buffer.DequeueNextCommand( argv );
  211. Shipping_Assert( argc == 3 );
  212. Shipping_Assert( !Q_stricmp( argv[0], "test_command" ) );
  213. Shipping_Assert( !Q_stricmp( argv[1], "test_arg1" ) );
  214. Shipping_Assert( !Q_stricmp( argv[2], "test_arg2" ) );
  215. bOk = buffer.AddText( "test_command3 test_arg6;wait;test_command4" );
  216. Shipping_Assert( bOk );
  217. // This makes sure that AddText doesn't cause argv to become bogus after
  218. // compacting memory
  219. Shipping_Assert( !Q_stricmp( argv[0], "test_command" ) );
  220. Shipping_Assert( !Q_stricmp( argv[1], "test_arg1" ) );
  221. Shipping_Assert( !Q_stricmp( argv[2], "test_arg2" ) );
  222. argc = buffer.DequeueNextCommand( argv );
  223. Shipping_Assert( argc == 2 );
  224. Shipping_Assert( !Q_stricmp( argv[0], "test_command3" ) );
  225. Shipping_Assert( !Q_stricmp( argv[1], "test_arg6" ) );
  226. argc = buffer.DequeueNextCommand( argv );
  227. Shipping_Assert( argc == 0 );
  228. buffer.EndProcessingCommands( );
  229. }
  230. {
  231. buffer.BeginProcessingCommands( 1 );
  232. argc = buffer.DequeueNextCommand( argv );
  233. Shipping_Assert( argc == 1 );
  234. Shipping_Assert( !Q_stricmp( argv[0], "test_command4" ) );
  235. argc = buffer.DequeueNextCommand( argv );
  236. Shipping_Assert( argc == 0 );
  237. buffer.EndProcessingCommands( );
  238. }
  239. }