/* -*- mode: C++ -*- */
//////////////////////////////////////////////////////////////////////////////
// FILE   : test.tol
// PURPOSE: Package MatQuery test
//////////////////////////////////////////////////////////////////////////////

Text email_ = "vdebuen@tol-project.org"; 
Text link_ = ""; 
Text summary_ = "checking matrix row selection";

//Write here the initial test TOL code if needed
Real numErr0 = Copy(NError);
Real numWar0 = Copy(NWarning);


#Embed "../../MatQuery.tol";
//#Require MatQuery;

Real t1 = Copy(Time);
Real rndSeed = 0;
Real data.num = 300000;
Real class.num =   100;

VMatrix Length = Rand(data.num,1,-10000,10000);

WriteLn("Quantile over edge's length ... ");
Set q.def = Range(1/class.num,1-1/class.num,1/class.num);
Set q = MatSet(Tra(Quantile(VMat2Mat(Length), SetCol(q.def))))[1];
Real t2 = Copy(Time);
WriteLn("Quantile time "<<(t2-t1)+" seconds");

WriteLn("ClassifyColumnByRank over edge's length... ");
VMatrix length.class = MatQuery::ClassifyRowsByRank(Length, q);
Real t3 = Copy(Time);
WriteLn("ClassifyColumnByRank time "<<(t3-t2)+" seconds");

Real q.select = IntRand(2,Card(q)-1);
Set length.select.1 = MatQuery::SelectRowsWithValue(
  length.class,q.select-1);
Set length.select.2 = MatQuery::SelectRowsWithValue(
  length.class,q.select);
Set length.select.3 = MatQuery::SelectRowsWithValue(
  length.class,q.select+1);
Set length.select.4 = MatQuery::SelectRowsInInterval(
  length.class,True,q.select-1,q.select+1,True);

VMatrix aux.123 = 
{
  Set sel = length.select.1<<length.select.2<<length.select.3;
  SubRow(Length, Sort(sel,Real(Real a, Real b) {Compare(a,b)}))
};
VMatrix aux.4 = SubRow(Length, length.select.4);

Real ok.1 = VMatMax(Abs(aux.123-aux.4)) == 0;

Real numErr1 = Copy(NError);
Real numWar1 = Copy(NWarning);

Set partialResults_ = [[numErr0, numErr1, 
                        ok.1]];

//This is a messure of the success of the test 
Real quality_ = And(numErr1 == numErr0, 
                    numWar1 <= numWar0+1, 
                    ok.1);

//Return the results 
Set resultStr_ = @strTestStatus(summary_, link_, quality_,
                  "Partial results = "<<partialResults_,
                  "NO DBServerType", "NO DBServerHost", "NO DBAlias",
                  email_);
WriteLn(""<<resultStr_);
resultStr_;

/* */

