コード例 #1
0
/**** Global functions definitions.   ****/
ErrorNumber test_pm( void )
{
   ErrorNumber errorNumber;
   TA_UDBase *udb;
   unsigned int i, j;

   printf( "Testing Performance Measurement\n" );

   /* Side Note:
    * Why all these allocLib/freeLib in this function?
    *   Each time freeLib is being called, it is verified
    *   that all ressource has been freed. So that's a good
    *   way to verify for any potential memory leak.
    */

   /* Initialize some globals used throughout these tests. */
   TA_SetTimeNow( &timestampNow );
   TA_SetDateNow( &timestampNow );
   TA_NextWeekday( &timestampNow );
   TA_TimestampCopy( &timestampNowPlusOneYear, &timestampNow );
   TA_NextYear( &timestampNowPlusOneYear );
   TA_PrevDay( &timestampNowPlusOneYear );

   /* Using a user defined kkey */
   TA_InstrumentInitWithUserKey( &id1_1, 12 );
   TA_InstrumentInitWithUserKey( &id1_2,  9 );

   /* Using a category / symbol strings. */
   TA_InstrumentInit( &id2_1, "AB", "CD" );
   TA_InstrumentInit( &id2_1, "AB", "CE" );

   /* Using a category only. */
   TA_InstrumentInit( &id3_1, "ABCD", NULL );
   TA_InstrumentInit( &id3_2, "EFGH", NULL );

   /* Using only a symbol string */
   TA_InstrumentInit( &id4_1, NULL, "A" );
   TA_InstrumentInit( &id4_2, NULL, "B" );

   /* Test limit cases with empty TA_TradeLog */
   errorNumber = allocLib( &udb );
   if( errorNumber != TA_TEST_PASS )
      return errorNumber;    
   errorNumber = test_emptytradelog();
   if( errorNumber != TA_TEST_PASS )
   {
      printf( "Failed: Empty trade log cases\n" );   
      return errorNumber;
   }
   errorNumber = freeLib( udb );
   if( errorNumber != TA_TEST_PASS )
      return errorNumber;

   /* Test with only one TA_Transaction.
    * Repeat all tests for each possible
    * TA_Instrument key type.
    */
   for( i=0; i < NB_TA_KEY_TYPE; i++ )
   {
      errorNumber = allocLib( &udb );
      if( errorNumber != TA_TEST_PASS )
         return errorNumber;    
      errorNumber = test_onetransaction_only( (TA_KEY_TYPE)i );
      if( errorNumber != TA_TEST_PASS )
      {
         printf( "Failed: one transaction cases (key=%d,errorNumber=%d)\n", (TA_KEY_TYPE)i, errorNumber );
         return errorNumber;
      }
      errorNumber = freeLib( udb );
      if( errorNumber != TA_TEST_PASS )
         return errorNumber;
   }

   /* Tests with two TA_Transaction for the
    * same given TA_Instrument.
    *
    * Repeat the test for each combination
    * of:
    *  - TA_Instrument key type
    *  - long and short trade.
    *  - winning and losing trade
    */
   for( i=0; i <= 1; i++ )
   {
      /* 0 = test a loosing trade
       * 1 = test a winning trade
       */   
      for( j=0; j < NB_TA_KEY_TYPE; j++ )
      {
         /* Test Long */
         errorNumber = allocLib( &udb );
         if( errorNumber != TA_TEST_PASS )
            return errorNumber;    
         errorNumber = test_onetrade_only( (TA_KEY_TYPE)j, TA_LONG_ENTRY, i );
         if( errorNumber != TA_TEST_PASS )
         {
            printf( "Failed: one trade only (key=%d,type=%d,winning=%d)\n", (TA_KEY_TYPE)j, TA_LONG_ENTRY, i );
            return errorNumber;
         }
         errorNumber = freeLib( udb );
         if( errorNumber != TA_TEST_PASS )
            return errorNumber;

         /* Test Short */
         errorNumber = allocLib( &udb );
         if( errorNumber != TA_TEST_PASS )
            return errorNumber;    
         errorNumber = test_onetrade_only( (TA_KEY_TYPE)j, TA_SHORT_ENTRY, i );
         if( errorNumber != TA_TEST_PASS )
         {
            printf( "Failed: one trade only (key=%d,type=%d,winning=%d)\n", (TA_KEY_TYPE)j, TA_SHORT_ENTRY, i );
            return errorNumber;
         }
         errorNumber = freeLib( udb );
         if( errorNumber != TA_TEST_PASS )
            return errorNumber;
      }
   }

   /* Test TA_PMValueId using a list of tests
    * defined in static variables.
    */
   for( i=0; i < NB_PMVALUEID_TEST; i++ )
   {
      errorNumber = allocLib( &udb );
      if( errorNumber != TA_TEST_PASS )
         return errorNumber;
      errorNumber = test_valueId( &pmValueIdTests[i] );
      if( errorNumber != TA_TEST_PASS )
      {
         printf( "Failed: test_valueId #%d\n", i);
         return errorNumber;
      }
      errorNumber = freeLib( udb );
      if( errorNumber != TA_TEST_PASS )
         return errorNumber;
   }

   /* Test TA_PMArrayId using a list of tests
    * defined in static variables.
    */
   for( i=0; i < NB_PMARRAYID_TEST; i++ )
   {
      errorNumber = allocLib( &udb );
      if( errorNumber != TA_TEST_PASS )
         return errorNumber;
      errorNumber = test_arrayId( &pmArrayIdTests[i] );
      if( errorNumber != TA_TEST_PASS )
      {
         printf( "Failed: test_arrayId #%d\n", i);
         return errorNumber;
      }
      errorNumber = freeLib( udb );
      if( errorNumber != TA_TEST_PASS )
         return errorNumber;
   }

   return TA_TEST_PASS;
}
コード例 #2
0
static TA_Timestamp *allocTimestampArray( const TA_Timestamp *start,
                                          const TA_Timestamp *end,
                                          int                *nbDays )
{
   TA_RetCode    retCode;
   TA_Timestamp *array;
   int outIdx;
   TA_Timestamp curDate;
   TA_DayOfWeek dayOfTheWeek;

   TA_ASSERT_RET( TA_TimestampValidate(start) == TA_SUCCESS, (TA_Timestamp *)NULL );
   TA_ASSERT_RET( TA_TimestampValidate(end  ) == TA_SUCCESS, (TA_Timestamp *)NULL );
   TA_ASSERT_RET( nbDays != NULL, (TA_Timestamp *)NULL );

   /* Calculate the exact number of week days
    * between start and end inclusive.
    * Excluding week-ends.
    */
   retCode = TA_TimestampDeltaWeekday( start, end, (unsigned int *)nbDays );
   if( retCode != TA_SUCCESS )
      return (TA_Timestamp *)NULL;

   /* Allocate the array. Add two element just to be on the safe side. */
   array = TA_Malloc( sizeof( TA_Timestamp ) * ((*nbDays)+2) );
   if( !array )
      return (TA_Timestamp *)NULL;

   /* Fill up the array. */
   TA_TimestampCopy( &curDate, start );

   /* Write the start point, if it is a weekday. */
   outIdx = 0;
   dayOfTheWeek = TA_GetDayOfTheWeek( &curDate );
   if( (dayOfTheWeek != TA_SUNDAY) && (dayOfTheWeek != TA_SATURDAY) )
   {
      TA_TimestampCopy( &array[outIdx], &curDate );
      outIdx++;
      TA_NextWeekday( &curDate );
      TA_ASSERT_RET( TA_TimestampValidate(&curDate) == TA_SUCCESS, (TA_Timestamp *)NULL );
   }

   /* Count the number of weekday */
   while( TA_TimestampLess( &curDate, end ) )
   {
      TA_TimestampCopy( &array[outIdx], &curDate );
      outIdx++;
      TA_NextWeekday( &curDate );
      TA_ASSERT_RET( TA_TimestampValidate(&curDate) == TA_SUCCESS, (TA_Timestamp *)NULL );
   } 

   /* Check if the ending point is a weekday. */
   if( TA_TimestampEqual( &curDate, end ) )
   {
      dayOfTheWeek = TA_GetDayOfTheWeek( &curDate );
      if( (dayOfTheWeek != TA_SUNDAY) && (dayOfTheWeek != TA_SATURDAY) )
         TA_TimestampCopy( &array[outIdx++], &curDate );
   }

   TA_ASSERT_RET( outIdx == (*nbDays), (TA_Timestamp *)NULL );

   return array;
}