2
0

asyncPacketQueueTest.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. /*
  23. #include "unit/test.h"
  24. #include "platform/async/asyncPacketQueue.h"
  25. #include "console/console.h"
  26. #include "core/util/tVector.h"
  27. #ifndef TORQUE_SHIPPING
  28. using namespace UnitTesting;
  29. #define TEST( x ) test( ( x ), "FAIL: " #x )
  30. CreateUnitTest( TestAsyncPacketQueue, "Platform/AsyncPacketQueue" )
  31. {
  32. struct Packet
  33. {
  34. typedef void Parent;
  35. StringChar mChar;
  36. U32 mDuration;
  37. S32 mWriteIndex;
  38. U32 mWriteTime;
  39. Packet() {}
  40. Packet( StringChar ch, U32 duration )
  41. : mChar( ch ), mDuration( duration ), mWriteIndex( -2 ), mWriteTime( 0 ) {}
  42. };
  43. struct TimeSource
  44. {
  45. typedef void Parent;
  46. U32 mStartTime;
  47. TimeSource()
  48. : mStartTime( Platform::getRealMilliseconds() ) {}
  49. U32 getPosition()
  50. {
  51. return ( Platform::getRealMilliseconds() - mStartTime );
  52. }
  53. };
  54. struct Consumer : public IOutputStream< Packet* >
  55. {
  56. typedef IOutputStream< Packet* > Parent;
  57. U32 mIndex;
  58. Consumer()
  59. : mIndex( 0 ) {}
  60. virtual void write( Packet* const* packets, U32 num )
  61. {
  62. for( U32 i = 0; i < num; ++ i )
  63. {
  64. Packet* p = packets[ i ];
  65. Con::printf( "%c", p->mChar );
  66. p->mWriteTime = Platform::getRealMilliseconds();
  67. p->mWriteIndex = mIndex;
  68. mIndex ++;
  69. }
  70. }
  71. };
  72. void test1( bool dropPackets, U32 queueLength )
  73. {
  74. F32 factor = Con::getFloatVariable( "$testAsyncPacketQueue::timeFactor", 100.0f );
  75. String str = Con::getVariable( "$testAsyncPacketQueue::string" );
  76. if( str.isEmpty() )
  77. str = "This is a test string";
  78. Vector< Packet > packets;
  79. for( U32 i = 0; i < str.size(); ++ i )
  80. packets.push_back( Packet( str[ i ], U32( Platform::getRandom() * factor ) ) );
  81. U32 totalTime = 0;
  82. for( U32 i = 0; i < packets.size(); ++ i )
  83. totalTime += packets[ i ].mDuration;
  84. TimeSource timeSource;
  85. Consumer consumer;
  86. AsyncPacketQueue< Packet*, TimeSource* > queue( queueLength, &timeSource, &consumer, totalTime, dropPackets );
  87. U32 index = 0;
  88. while( !queue.isAtEnd() )
  89. {
  90. if( queue.needPacket()
  91. && index < packets.size() )
  92. {
  93. Packet* packet = &packets[ index ];
  94. index ++;
  95. queue.submitPacket( packet, packet->mDuration );
  96. }
  97. }
  98. U32 time = timeSource.mStartTime;
  99. S32 lastIndex = -1;
  100. for( U32 i = 0; i < packets.size(); ++ i )
  101. {
  102. TEST( ( packets[ i ].mWriteIndex == -2 && dropPackets ) // not written = dropped
  103. || packets[ i ].mWriteIndex == lastIndex + 1 );
  104. if( packets[ i ].mWriteIndex != -2 )
  105. lastIndex ++;
  106. if( queueLength == 1 )
  107. TEST( packets[ i ].mWriteTime >= time || dropPackets ); // start time okay
  108. time += packets[ i ].mDuration;
  109. if( dropPackets )
  110. TEST( packets[ i ].mWriteTime < time ); // end time okay (if not dropping)
  111. }
  112. }
  113. void run()
  114. {
  115. test1( false, 1 );
  116. test1( true, 1 );
  117. test1( false, 4 );
  118. test1( true, 4 );
  119. }
  120. };
  121. #endif // !TORQUE_SHIPPING
  122. */