LbsApi/src/calculationmethodsstep.cpp
branchSymbian3
changeset 54 a4835904093b
equal deleted inserted replaced
53:7f293ed715ec 54:a4835904093b
       
     1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // Example CTestStep derived implementation
       
    15 // 
       
    16 //
       
    17 
       
    18 /**
       
    19  @file CalculationMethodsStep.cpp
       
    20  @internalTechnology
       
    21 */
       
    22 #include "calculationmethodsstep.h"
       
    23 #include "te_lbsapisuitedefs.h"
       
    24 
       
    25 #include "lcfsbucommondefinitions.h"
       
    26 
       
    27 #include <lbs.h>
       
    28 #include <lbssatellite.h>
       
    29 
       
    30 CCalculationMethodsStep::~CCalculationMethodsStep()
       
    31 /**
       
    32  * Destructor
       
    33  */
       
    34 	{
       
    35 	}
       
    36 
       
    37 CCalculationMethodsStep::CCalculationMethodsStep()
       
    38 /**
       
    39  * Constructor
       
    40  */
       
    41 	{
       
    42 	// **MUST** call SetTestStepName in the constructor as the controlling
       
    43 	// framework uses the test step name immediately following construction to set
       
    44 	// up the step's unique logging ID.
       
    45 	SetTestStepName(KCalculationMethodsStep);
       
    46 	}
       
    47 
       
    48 TVerdict CCalculationMethodsStep::doTestStepPreambleL()
       
    49 /**
       
    50  * @return - TVerdict code
       
    51  * Override of base class virtual
       
    52  */
       
    53 	{
       
    54 	CTe_LbsApiSuiteStepBase::doTestStepPreambleL();
       
    55 	if (TestStepResult()!=EPass)
       
    56 		return   TestStepResult();
       
    57 	// process some pre setting to this test step then set SetTestStepResult to EFail or Epass.
       
    58 	SetTestStepResult(EPass);
       
    59 	return TestStepResult();
       
    60 	}
       
    61 
       
    62 
       
    63 TVerdict CCalculationMethodsStep::doTestStepL()
       
    64 /**
       
    65  * @return - TVerdict code
       
    66  * Override of base class pure virtual
       
    67  * Our implementation only gets called if the base class doTestStepPreambleL() did
       
    68  * not leave. That being the case, the current test result value will be EPass.
       
    69  */
       
    70 	{
       
    71 	  if (TestStepResult()==EPass)
       
    72 		{
       
    73 		TInt test;
       
    74 		if(!GetIntFromConfig(ConfigSection(),KTe_LbsApiSuiteInt,test)
       
    75 			)
       
    76 			{
       
    77 			// Leave if there's any error.
       
    78 			User::Leave(KErrNotFound);
       
    79 			}
       
    80 		SetTestStepResult(EPass);
       
    81 		StartL(test);
       
    82 		}
       
    83 	  return TestStepResult();
       
    84 	}
       
    85 
       
    86 
       
    87 
       
    88 TVerdict CCalculationMethodsStep::doTestStepPostambleL()
       
    89 /**
       
    90  * @return - TVerdict code
       
    91  * Override of base class virtual
       
    92  */
       
    93 	{
       
    94 	// process something post setting to the test step
       
    95 	// uncomment the following line if you have common post setting to all the test steps in there
       
    96 	// CTe_LbsApiVariant2SuiteStepBase::doTestStepPostambleL();
       
    97 	// uncomment the following line if you have post process or remove the following line if no post process
       
    98 	// SetTestStepResult(EPass);		// or EFail
       
    99 	return TestStepResult();
       
   100 	}
       
   101 
       
   102 
       
   103 void CCalculationMethodsStep::StartL(TInt aIndex)
       
   104 	{
       
   105 	switch(aIndex)
       
   106 		{
       
   107 		case 0:
       
   108 			Calculation_InvalidL();
       
   109 			break;
       
   110 		case 1:
       
   111 			Calculation_BasicsL();
       
   112 			break;
       
   113 		case 2:
       
   114 			Calculation_TCoordinate_MoveBigL();
       
   115 			break;
       
   116 		}
       
   117 	}
       
   118 
       
   119 
       
   120 #define REPORT_REAL(_real) \
       
   121     report.Format(_L("%15.9f"), _real); \
       
   122     LOG_DES(report);
       
   123 
       
   124 #define REPORT_REAL_16(_real) \
       
   125     report.Format(_L("%22.16f"), _real); \
       
   126     LOG_DES(report);
       
   127 
       
   128 #define REPORT_REAL_G(_real) \
       
   129     report.Format(_L("%10g"), _real); \
       
   130     LOG_DES(report);
       
   131 
       
   132 #define REPORT_CRLF \
       
   133     report.Format(_L("\n")); \
       
   134     LOG_DES(report);
       
   135 
       
   136 #define REPORT_TAB \
       
   137     report.Format(_L("\t")); \
       
   138     LOG_DES(report);
       
   139 
       
   140 //
       
   141 //
       
   142 // TCoordinate
       
   143 //
       
   144 //
       
   145 
       
   146 void CCalculationMethodsStep::Calculation_BasicsL()
       
   147     {
       
   148     DECLARE_ERROR_LOGGING;
       
   149     StandardPrepareL();
       
   150     
       
   151     TRealX nan;
       
   152     nan.SetNaN();
       
   153 
       
   154     const TReal tlat = 10, tlon = 20, acc1 = 5, acc2 = 15;
       
   155     TReal32 distance, bearing, delta, speed;
       
   156 
       
   157     TCoordinate sCoord, tCoord, zero(0,0), target(tlat, tlon);
       
   158     TLocality sLoc, tLoc;
       
   159     TPosition sPos, tPos;
       
   160 
       
   161     //
       
   162     // 1. TCoordinate
       
   163     
       
   164     sCoord.SetCoordinate(0, 0);
       
   165     tCoord.SetCoordinate(tlat, tlon);
       
   166     
       
   167     CHECK(sCoord.Distance(tCoord, distance) == KErrNone);
       
   168     CHECK(sCoord.Distance(sCoord, distance) == KErrNone);
       
   169     CHECK(sCoord.BearingTo(tCoord, bearing) == KErrNone);
       
   170     CHECK(sCoord.Move(distance, bearing) == KErrNone);
       
   171     CHECK(sCoord.Move(distance, bearing) == KErrNone);
       
   172 
       
   173     //
       
   174     // 2. TLocality
       
   175     
       
   176     sLoc = TLocality(zero, acc1);
       
   177     tLoc = TLocality(target, acc2);
       
   178 
       
   179     CHECK(sLoc.Distance(tCoord, distance) == KErrNone);
       
   180     CHECK(sLoc.Distance(sCoord, distance) == KErrNone);
       
   181     CHECK(sLoc.Distance(tLoc, distance, delta) == KErrNone);
       
   182     CHECK(sLoc.Distance(sLoc, distance, delta) == KErrNone);
       
   183     CHECK(sLoc.BearingTo(tCoord, bearing) == KErrNone);
       
   184     CHECK(sLoc.BearingTo(tLoc, bearing, delta) == KErrNone);
       
   185 
       
   186     //
       
   187     // 2. TPosition
       
   188 
       
   189     TTime time1, time2;
       
   190     time1.UniversalTime();
       
   191     time2 = time1 + TTimeIntervalMinutes(60);
       
   192 
       
   193     sPos = TPosition(sLoc, time1);
       
   194     tPos = TPosition(tLoc, time2);
       
   195 
       
   196     CHECK(sPos.Speed(tPos, speed) == KErrNone);
       
   197     CHECK(sPos.Speed(TPosition(sLoc, time2), speed) == KErrNone);
       
   198     CHECK(sPos.Speed(tPos, speed, delta) == KErrNone);
       
   199     CHECK(sPos.Speed(TPosition(sLoc, time2), speed, delta) == KErrNone);
       
   200 
       
   201     StandardCleanup();
       
   202     }
       
   203     
       
   204 void CCalculationMethodsStep::Calculation_InvalidL()
       
   205     {
       
   206     DECLARE_ERROR_LOGGING;
       
   207     StandardPrepareL();
       
   208     
       
   209     TRealX nan;
       
   210     nan.SetNaN();
       
   211     
       
   212     const TReal lat = 10, lon = 20, acc = 5;
       
   213     const TReal32 dist = 1000, bear = 60;
       
   214     
       
   215     const TCoordinate zeroCoord(0, 0), north(90, 0), south(-90, 0);
       
   216     
       
   217     enum ENanParam
       
   218         {
       
   219         LAT, LON, ACC,
       
   220         };
       
   221 
       
   222     const TCoordinate nanCoord[] = 
       
   223         {
       
   224         TCoordinate(nan, lon),
       
   225         TCoordinate(lat, nan)
       
   226         };
       
   227 
       
   228     const TLocality nanLoc[] = 
       
   229         {
       
   230         TLocality(nanCoord[LAT], acc),
       
   231         TLocality(nanCoord[LON], acc),
       
   232         TLocality(zeroCoord, nan)
       
   233         };
       
   234 
       
   235     //
       
   236     // 1. TCoordinate
       
   237     
       
   238     TCoordinate sCoord, tCoord;
       
   239     TReal32 distance, bearing;
       
   240 
       
   241     // NaN in Distance, BearingTo, Move
       
   242 
       
   243     tCoord = zeroCoord;
       
   244 
       
   245     sCoord = nanCoord[LAT];
       
   246     CHECK(sCoord.Distance(tCoord, distance) == KErrArgument);
       
   247     CHECK(sCoord.BearingTo(tCoord, bearing) == KErrArgument);
       
   248     CHECK(sCoord.Move(dist, bear) == KErrArgument);
       
   249 
       
   250     sCoord = nanCoord[LON];
       
   251     CHECK(sCoord.Distance(tCoord, distance) == KErrArgument);
       
   252     CHECK(sCoord.BearingTo(tCoord, bearing) == KErrArgument);
       
   253     CHECK(sCoord.Move(dist, bear) == KErrArgument);
       
   254 
       
   255     sCoord = zeroCoord;
       
   256 
       
   257     tCoord = nanCoord[LAT];
       
   258     CHECK(sCoord.Distance(tCoord, distance) == KErrArgument);
       
   259     CHECK(sCoord.BearingTo(tCoord, bearing) == KErrArgument);
       
   260 
       
   261     tCoord = nanCoord[LON];
       
   262     CHECK(sCoord.Distance(tCoord, distance) == KErrArgument);
       
   263     CHECK(sCoord.BearingTo(tCoord, bearing) == KErrArgument);
       
   264     
       
   265     // Incalculable cases
       
   266     
       
   267     // poles
       
   268     tCoord = zeroCoord;
       
   269 
       
   270     sCoord = north;
       
   271     CHECK(sCoord.BearingTo(tCoord, bearing) == KErrPositionIncalculable);
       
   272     CHECK(sCoord.Move(dist, bear) == KErrPositionIncalculable);
       
   273 
       
   274     sCoord = south;
       
   275     CHECK(sCoord.BearingTo(tCoord, bearing) == KErrPositionIncalculable);
       
   276     CHECK(sCoord.Move(dist, bear) == KErrPositionIncalculable);
       
   277     
       
   278     // same coords
       
   279     sCoord = tCoord = zeroCoord;
       
   280     CHECK(sCoord.BearingTo(tCoord, bearing) == KErrPositionIncalculable);
       
   281     
       
   282     // antipodal
       
   283     tCoord.SetCoordinate(-zeroCoord.Latitude(), zeroCoord.Longitude() + 180);
       
   284     CHECK(sCoord.BearingTo(tCoord, bearing) == KErrPositionIncalculable);
       
   285 
       
   286     //
       
   287     // 2. TLocality
       
   288     
       
   289     TLocality sLoc, tLoc;
       
   290     TReal32 delta;
       
   291 
       
   292     // NaN in Distance and BearingTo
       
   293 
       
   294     tLoc = TLocality(zeroCoord, acc);
       
   295 
       
   296     sLoc = nanLoc[LAT];
       
   297     CHECK(sLoc.Distance(tLoc, distance, delta) == KErrArgument);
       
   298     CHECK(sLoc.BearingTo(tLoc, bearing, delta) == KErrArgument);
       
   299 
       
   300     sLoc = nanLoc[LON];
       
   301     CHECK(sLoc.Distance(tLoc, distance, delta) == KErrArgument);
       
   302     CHECK(sLoc.BearingTo(tLoc, bearing, delta) == KErrArgument);
       
   303 
       
   304     sLoc = nanLoc[ACC];
       
   305     CHECK(sLoc.Distance(tLoc, distance, delta) == KErrArgument);
       
   306     CHECK(sLoc.BearingTo(tLoc, bearing, delta) == KErrArgument);
       
   307 
       
   308     sLoc = TLocality(zeroCoord, acc);
       
   309 
       
   310     tLoc = nanLoc[LAT];
       
   311     CHECK(sLoc.Distance(tLoc, distance, delta) == KErrArgument);
       
   312     CHECK(sLoc.BearingTo(tLoc, bearing, delta) == KErrArgument);
       
   313 
       
   314     tLoc = nanLoc[LON];
       
   315     CHECK(sLoc.Distance(tLoc, distance, delta) == KErrArgument);
       
   316     CHECK(sLoc.BearingTo(tLoc, bearing, delta) == KErrArgument);
       
   317     
       
   318     tLoc = nanLoc[ACC];
       
   319     CHECK(sLoc.Distance(tLoc, distance, delta) == KErrArgument);
       
   320     CHECK(sLoc.BearingTo(tLoc, bearing, delta) == KErrArgument);
       
   321     
       
   322     // Incalculable cases
       
   323     
       
   324     // 1 latitude degree is about 117 km
       
   325     const TReal32 circle = 200 * 1000;
       
   326     
       
   327     // pole in error circle
       
   328     sLoc = TLocality(TCoordinate(89, 0), circle); 
       
   329     tLoc = TLocality(zeroCoord, 0);
       
   330     CHECK(sLoc.BearingTo(tLoc, bearing, delta) == KErrPositionIncalculable);
       
   331     
       
   332     // overlapping error circles
       
   333     sLoc = TLocality(TCoordinate(1, 0), circle);
       
   334     tLoc = TLocality(TCoordinate(-1, 0), circle);
       
   335     CHECK(sLoc.BearingTo(tLoc, bearing, delta) == KErrPositionIncalculable);
       
   336 
       
   337     // overlapping error circles if target is antipodal
       
   338     sLoc = TLocality(TCoordinate(1, 0), circle);
       
   339     tLoc = TLocality(TCoordinate(1, 180), circle); // antipodal is (-1, 0)
       
   340     CHECK(sLoc.BearingTo(tLoc, bearing, delta) == KErrPositionIncalculable);
       
   341 
       
   342     //
       
   343     // 3. TPosition
       
   344     
       
   345     TPosition sPos, tPos;
       
   346     TReal32 speed;
       
   347     
       
   348     // Nan in Speed methods
       
   349     TTime time1, time2;
       
   350     time1.UniversalTime();
       
   351     time2 = time1 + TTimeIntervalMinutes(60);
       
   352     
       
   353     tPos = TPosition(TLocality(zeroCoord, acc), time2);
       
   354 
       
   355     sPos = TPosition(nanLoc[LAT], time1);
       
   356     CHECK(sPos.Speed(tPos, speed) == KErrArgument);
       
   357     CHECK(sPos.Speed(tPos, speed, delta) == KErrArgument);
       
   358 
       
   359     sPos = TPosition(nanLoc[LON], time1);
       
   360     CHECK(sPos.Speed(tPos, speed) == KErrArgument);
       
   361     CHECK(sPos.Speed(tPos, speed, delta) == KErrArgument);
       
   362 
       
   363     sPos = TPosition(nanLoc[ACC], time1);
       
   364     CHECK(sPos.Speed(tPos, speed, delta) == KErrArgument);
       
   365     
       
   366     sPos = TPosition(TLocality(zeroCoord, acc), time1);
       
   367 
       
   368     tPos = TPosition(nanLoc[LAT], time2);
       
   369     CHECK(sPos.Speed(tPos, speed) == KErrArgument);
       
   370     CHECK(sPos.Speed(tPos, speed, delta) == KErrArgument);
       
   371 
       
   372     tPos = TPosition(nanLoc[LON], time2);
       
   373     CHECK(sPos.Speed(tPos, speed) == KErrArgument);
       
   374     CHECK(sPos.Speed(tPos, speed, delta) == KErrArgument);
       
   375 
       
   376     tPos = TPosition(nanLoc[ACC], time2);
       
   377     CHECK(sPos.Speed(tPos, speed, delta) == KErrArgument);
       
   378     
       
   379     // Zero time
       
   380 
       
   381     sPos = TPosition(TLocality(zeroCoord, acc), 0);
       
   382     tPos = TPosition(TLocality(zeroCoord, acc), time2);
       
   383     CHECK(sPos.Speed(tPos, speed) == KErrArgument);
       
   384     CHECK(sPos.Speed(tPos, speed, delta) == KErrArgument);
       
   385 
       
   386     sPos = TPosition(TLocality(zeroCoord, acc), time1);
       
   387     tPos = TPosition(TLocality(zeroCoord, acc), 0);
       
   388     CHECK(sPos.Speed(tPos, speed) == KErrArgument);
       
   389     CHECK(sPos.Speed(tPos, speed, delta) == KErrArgument);
       
   390     
       
   391     // Same time
       
   392 
       
   393     sPos = TPosition(TLocality(zeroCoord, acc), time1);
       
   394     tPos = TPosition(TLocality(zeroCoord, acc), time1);
       
   395     CHECK(sPos.Speed(tPos, speed) == KErrArgument);
       
   396     CHECK(sPos.Speed(tPos, speed, delta) == KErrArgument);
       
   397 
       
   398     StandardCleanup();
       
   399     }
       
   400 
       
   401 
       
   402 
       
   403 
       
   404 TReal32 NextReal32Value(TReal32 aReal32)
       
   405 	{
       
   406     TInt32* pint32 = (TInt32*) &(aReal32);
       
   407     (*pint32) = (*pint32) + 1; // works ok only if increment does not touch exponent!!!
       
   408     return *((TReal32*) pint32);
       
   409 	}
       
   410 
       
   411 TReal32 PrevReal32Value(TReal32 aReal32)
       
   412 	{
       
   413     TInt32* pint32 = (TInt32*) &(aReal32);
       
   414     (*pint32) = (*pint32) - 1; // works ok only if decrement does not touch exponent!!!
       
   415     return *((TReal32*) pint32);
       
   416 	}
       
   417 
       
   418 void CCalculationMethodsStep::Calculation_TCoordinate_MoveBigL()
       
   419 	{
       
   420     DECLARE_ERROR_LOGGING;
       
   421     StandardPrepareL();
       
   422     
       
   423     TBuf<256> report;
       
   424     const TReal32 KEarthRadius = 6371010;
       
   425     const TReal32 KAccuracy = 0.1;
       
   426 
       
   427     TInt bearing;
       
   428     TReal32 alpha, arc = 2*KPi*KEarthRadius;
       
   429     TCoordinate center(0,0), farPoint, closePoint, diff;
       
   430 
       
   431     _LIT(KHeader, "=== TCoordinate::Move() test for big distances ===\n");
       
   432     INFO_PRINTF1(KHeader);
       
   433     
       
   434     for (int lon = -180; lon < 180; lon += 60)
       
   435     	{
       
   436         center = TCoordinate(0, lon);
       
   437 
       
   438         report.Format(_L("Center = (%f,%f), Arc(m) = %g(%g)\n"),
       
   439                 center.Latitude(), center.Longitude(), arc, NextReal32Value(arc));
       
   440 
       
   441         for (bearing = 0; bearing < 360; bearing +=60)
       
   442         	{
       
   443             for (alpha = 0; alpha < 180; alpha += 30)
       
   444             	{
       
   445                 farPoint = closePoint = center;
       
   446 
       
   447                 TReal32 smallDistance = alpha * KDegToRad * KEarthRadius;
       
   448                 closePoint.Move(bearing, smallDistance);
       
   449 
       
   450                 TReal bigDistance = ((2*KPi) - alpha*KDegToRad) * KEarthRadius;
       
   451                 farPoint.Move(bearing + 180, bigDistance);
       
   452 
       
   453                 diff = TCoordinate(closePoint.Latitude() - farPoint.Latitude(),
       
   454                                    closePoint.Longitude() - farPoint.Longitude());
       
   455 
       
   456                 if ((Abs(diff.Latitude()) > KAccuracy) ||
       
   457                     (Abs(diff.Longitude()) > KAccuracy))
       
   458                 	{
       
   459                     report.Format(_L("Alpha %g\n"), alpha);
       
   460                     LOG_DES(report);
       
   461 
       
   462                     _LIT(KBearingDistance, "\tBearing %3d, Dist(m) %g: (%f;%f)\n");
       
   463                     report.Format(KBearingDistance, bearing, smallDistance, closePoint.Latitude(), closePoint.Longitude());
       
   464                     LOG_DES(report);
       
   465 
       
   466                     report.Format(KBearingDistance, bearing + 180, bigDistance, farPoint.Latitude(), farPoint.Longitude());
       
   467                     LOG_DES(report);
       
   468 
       
   469                     report.Format(_L("\tDiff (%f;%f)\n"), diff.Latitude(), diff.Longitude());
       
   470                     LOG_DES(report);
       
   471                 	}
       
   472             	}
       
   473         	}
       
   474     	}
       
   475 
       
   476     _LIT(KFooter, "=== TCoordinate::Move() test done ===\n");
       
   477     INFO_PRINTF1(KFooter);
       
   478     
       
   479     StandardCleanup();
       
   480 	}