Torque3D Documentation / _generateds / asyncPacketQueueTest.cpp

asyncPacketQueueTest.cpp

Engine/source/platform/async/test/asyncPacketQueueTest.cpp

More...

Detailed Description

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