236 lines
7.8 KiB
MQL4
236 lines
7.8 KiB
MQL4
|
|
//+------------------------------------------------------------------+
|
||
|
|
//| UnitTests.mq5 |
|
||
|
|
//| Unit Tests for Package bar |
|
||
|
|
//| Organization: douglasrechia |
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
#property copyright "<Add copyright here>"
|
||
|
|
#property link "<Add link here>"
|
||
|
|
#property description ""
|
||
|
|
#property description "Version: 1.0.0"
|
||
|
|
#property description ""
|
||
|
|
#property description "Description: Unit tests for package bar"
|
||
|
|
#property description "Organization: douglasrechia"
|
||
|
|
#property description "Author: Doug"
|
||
|
|
#property description "License: MIT"
|
||
|
|
#property description ""
|
||
|
|
#property description "Powered by KnitPkg for MetaTrader"
|
||
|
|
#property description "https://knitpkg.dev"
|
||
|
|
|
||
|
|
// Include the headers under test
|
||
|
|
#include "../knitpkg/include/douglasrechia/bar/TimeSeriesArray.mqh"
|
||
|
|
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
//| |
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
bool TestArrayInt(const int &arrayInt[])
|
||
|
|
{
|
||
|
|
TimeSeriesArray<int> arrayIntS(arrayInt, -1, -1, true);
|
||
|
|
|
||
|
|
return arrayIntS.Size() == ArraySize(arrayInt) &&
|
||
|
|
arrayIntS.ValueAtShift(0) == arrayInt[0] &&
|
||
|
|
arrayIntS.ValueAtShift(1) == arrayInt[1] &&
|
||
|
|
arrayIntS.ValueAtShift(2) == arrayInt[2] &&
|
||
|
|
arrayIntS.ValueAtShift(3) == arrayInt[3];
|
||
|
|
}
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
//| |
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
bool TestArrayInt1()
|
||
|
|
{
|
||
|
|
int arrayInt[] = {0, 2, 4, 6, 8};
|
||
|
|
|
||
|
|
return TestArrayInt(arrayInt);
|
||
|
|
}
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
//| |
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
bool TestArrayDouble(const double &arrayD[])
|
||
|
|
{
|
||
|
|
TimeSeriesArray<double> arrayDS(arrayD, -1, -1, true);
|
||
|
|
|
||
|
|
return arrayDS.Size() == ArraySize(arrayD) &&
|
||
|
|
arrayDS.ValueAtShift(0) == arrayD[0] &&
|
||
|
|
arrayDS.ValueAtShift(1) == arrayD[1] &&
|
||
|
|
arrayDS.ValueAtShift(2) == arrayD[2];
|
||
|
|
}
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
//| |
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
bool TestArrayDouble1()
|
||
|
|
{
|
||
|
|
double arrayD[] = {1.2, 3.4, 6.4, 3.6};
|
||
|
|
|
||
|
|
return TestArrayDouble(arrayD);
|
||
|
|
}
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
//| |
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
bool TestArrayDouble2()
|
||
|
|
{
|
||
|
|
double arrayD[] = {1.2, 3.4, 6.4, 3.6};
|
||
|
|
|
||
|
|
TimeSeriesArray<double> arrayDS(arrayD, -1, -1, false);
|
||
|
|
|
||
|
|
return arrayDS.Size() == ArraySize(arrayD) &&
|
||
|
|
arrayDS.ValueAtShift(3) == arrayD[0] &&
|
||
|
|
arrayDS.ValueAtShift(2) == arrayD[1] &&
|
||
|
|
arrayDS.ValueAtShift(1) == arrayD[2] &&
|
||
|
|
arrayDS.ValueAtShift(0) == arrayD[3];
|
||
|
|
}
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
//| |
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
bool TestArrayDouble3()
|
||
|
|
{
|
||
|
|
double arrayD[] = {1.2, 3.4, 6.4, 3.6, 5, 6, 8, 4};
|
||
|
|
|
||
|
|
TimeSeriesArray<double> arrayDS(arrayD, 2, 4, false);
|
||
|
|
|
||
|
|
return arrayDS.Size() == 3 &&
|
||
|
|
arrayDS.ValueAtShift(2) == arrayD[2] &&
|
||
|
|
arrayDS.ValueAtShift(1) == arrayD[3] &&
|
||
|
|
arrayDS.ValueAtShift(0) == arrayD[4];
|
||
|
|
}
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
//| |
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
bool TestArrayDouble4()
|
||
|
|
{
|
||
|
|
double arrayD[] = {1.2, 3.4, 6.4, 3.6, 5, 6, 8, 4};
|
||
|
|
|
||
|
|
TimeSeriesArray<double> arrayDS(arrayD, 2, 4, true);
|
||
|
|
|
||
|
|
return arrayDS.Size() == 3 &&
|
||
|
|
arrayDS.ValueAtShift(2) == arrayD[4] &&
|
||
|
|
arrayDS.ValueAtShift(1) == arrayD[3] &&
|
||
|
|
arrayDS.ValueAtShift(0) == arrayD[2];
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
template<typename T>
|
||
|
|
class DumbTimeSeriesArraySetup: public ITimeSeriesArraySetup<T>
|
||
|
|
{
|
||
|
|
public:
|
||
|
|
void Setup(T &array[])
|
||
|
|
{
|
||
|
|
ArrayResize(array, 4);
|
||
|
|
|
||
|
|
double arrayD[] = {1.2, 3.4, 6.4, 3.6};
|
||
|
|
array[0] = arrayD[0];
|
||
|
|
array[1] = arrayD[1];
|
||
|
|
array[2] = arrayD[2];
|
||
|
|
array[3] = arrayD[3];
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
bool TestArraySetup1()
|
||
|
|
{
|
||
|
|
double arrayD[] = {1.2, 3.4, 6.4, 3.6};
|
||
|
|
DumbTimeSeriesArraySetup<double> setup;
|
||
|
|
|
||
|
|
TimeSeriesArray<double> arrayDS(setup);
|
||
|
|
|
||
|
|
return arrayDS.Size() == ArraySize(arrayD) &&
|
||
|
|
arrayDS.ValueAtShift(0) == arrayD[0] &&
|
||
|
|
arrayDS.ValueAtShift(1) == arrayD[1] &&
|
||
|
|
arrayDS.ValueAtShift(2) == arrayD[2];
|
||
|
|
}
|
||
|
|
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
//| DoTests |
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
void DoTests(int &testsPerformed,int &testsPassed)
|
||
|
|
{
|
||
|
|
string testName="";
|
||
|
|
|
||
|
|
//--- TestArrayInt1
|
||
|
|
testsPerformed++;
|
||
|
|
testName="TestArrayInt1";
|
||
|
|
if(TestArrayInt1())
|
||
|
|
{
|
||
|
|
testsPassed++;
|
||
|
|
PrintFormat("%s pass",testName);
|
||
|
|
}
|
||
|
|
else
|
||
|
|
PrintFormat("%s failed",testName);
|
||
|
|
|
||
|
|
//--- TestArrayDouble1
|
||
|
|
testsPerformed++;
|
||
|
|
testName="TestArrayDouble1";
|
||
|
|
if(TestArrayDouble1())
|
||
|
|
{
|
||
|
|
testsPassed++;
|
||
|
|
PrintFormat("%s pass",testName);
|
||
|
|
}
|
||
|
|
else
|
||
|
|
PrintFormat("%s failed",testName);
|
||
|
|
|
||
|
|
//--- TestArrayDouble2
|
||
|
|
testsPerformed++;
|
||
|
|
testName="TestArrayDouble2";
|
||
|
|
if(TestArrayDouble2())
|
||
|
|
{
|
||
|
|
testsPassed++;
|
||
|
|
PrintFormat("%s pass",testName);
|
||
|
|
}
|
||
|
|
else
|
||
|
|
PrintFormat("%s failed",testName);
|
||
|
|
|
||
|
|
//--- TestArrayDouble3
|
||
|
|
testsPerformed++;
|
||
|
|
testName="TestArrayDouble3";
|
||
|
|
if(TestArrayDouble3())
|
||
|
|
{
|
||
|
|
testsPassed++;
|
||
|
|
PrintFormat("%s pass",testName);
|
||
|
|
}
|
||
|
|
else
|
||
|
|
PrintFormat("%s failed",testName);
|
||
|
|
|
||
|
|
//--- TestArrayDouble4
|
||
|
|
testsPerformed++;
|
||
|
|
testName="TestArrayDouble4";
|
||
|
|
if(TestArrayDouble4())
|
||
|
|
{
|
||
|
|
testsPassed++;
|
||
|
|
PrintFormat("%s pass",testName);
|
||
|
|
}
|
||
|
|
else
|
||
|
|
PrintFormat("%s failed",testName);
|
||
|
|
|
||
|
|
//--- TestArraySetup1
|
||
|
|
testsPerformed++;
|
||
|
|
testName="TestArraySetup1";
|
||
|
|
if(TestArraySetup1())
|
||
|
|
{
|
||
|
|
testsPassed++;
|
||
|
|
PrintFormat("%s pass",testName);
|
||
|
|
}
|
||
|
|
else
|
||
|
|
PrintFormat("%s failed",testName);
|
||
|
|
//---
|
||
|
|
// Add more tests here as needed
|
||
|
|
}
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
//| UnitTests() |
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
void UnitTests(const string packageName)
|
||
|
|
{
|
||
|
|
PrintFormat("Unit tests for Package %s\n",packageName);
|
||
|
|
//--- initial values
|
||
|
|
int testsPerformed=0;
|
||
|
|
int testsPassed=0;
|
||
|
|
//--- test distributions
|
||
|
|
DoTests(testsPerformed,testsPassed);
|
||
|
|
//--- print statistics
|
||
|
|
PrintFormat("\n%d of %d passed",testsPassed,testsPerformed);
|
||
|
|
}
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
//| Script program start function |
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
void OnStart()
|
||
|
|
{
|
||
|
|
UnitTests("bar");
|
||
|
|
}
|
||
|
|
//+------------------------------------------------------------------+
|
||
|
|
//+------------------------------------------------------------------+
|