Consolidate Python ignore rules into root gitignore
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,186 @@
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestHashMap.mq5 |
|
||||
//| Copyright 2000-2026, MetaQuotes Ltd. |
|
||||
//| www.mql5.com |
|
||||
//+------------------------------------------------------------------+
|
||||
#include <Generic\ArrayList.mqh>
|
||||
#include <Generic\HashMap.mqh>
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestMisc_Constructor. |
|
||||
//+------------------------------------------------------------------+
|
||||
bool TestMisc_Constructor(const int count)
|
||||
{
|
||||
//--- create arrays
|
||||
int keys[];
|
||||
string values[];
|
||||
ArrayResize(keys,count);
|
||||
ArrayResize(values,count);
|
||||
for(int i=0; i<count; i++)
|
||||
{
|
||||
keys[i]=i+1;
|
||||
values[i]=(string)(i+1);
|
||||
}
|
||||
//--- create source map
|
||||
CDefaultEqualityComparer<int>comparer();
|
||||
CHashMap<int,string>map_test(GetPointer(comparer));
|
||||
for(int i=0; i<count; i++)
|
||||
map_test.Add(keys[i],values[i]);
|
||||
//--- create map on map
|
||||
CHashMap<int,string>map_copy(GetPointer(map_test));
|
||||
//--- check
|
||||
if(map_copy.Count()!=map_test.Count())
|
||||
return(false);
|
||||
if(map_test.Comparer()!=GetPointer(comparer))
|
||||
return(false);
|
||||
if(map_copy.Comparer()==GetPointer(comparer))
|
||||
return(false);
|
||||
//--- successful
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestMisc_Contains. |
|
||||
//+------------------------------------------------------------------+
|
||||
bool TestMisc_Contains(const int count)
|
||||
{
|
||||
//--- create arrays
|
||||
int keys[];
|
||||
string values[];
|
||||
ArrayResize(keys,count);
|
||||
ArrayResize(values,count);
|
||||
for(int i=0; i<count; i++)
|
||||
{
|
||||
keys[i]=i+1;
|
||||
values[i]=(string)(i+1);
|
||||
}
|
||||
//--- create source map
|
||||
CHashMap<int,string>map_test();
|
||||
for(int i=0; i<count; i++)
|
||||
map_test.Add(keys[i],values[i]);
|
||||
//--- create elemet
|
||||
int element=keys[0];
|
||||
while(map_test.Contains(element,(string)element) && map_test.ContainsKey(element))
|
||||
element++;
|
||||
//--- check
|
||||
if(map_test.Contains(element,(string)element) || map_test.ContainsKey(element))
|
||||
return(false);
|
||||
//--- add element to map
|
||||
map_test.Add(element,"new");
|
||||
//--- check
|
||||
if(!map_test.Contains(element,"new") ||
|
||||
!map_test.ContainsKey(element) ||
|
||||
!map_test.ContainsValue("new"))
|
||||
return(false);
|
||||
//--- clear
|
||||
map_test.Clear();
|
||||
//--- check
|
||||
if(map_test.Contains(keys[0],values[0]) ||
|
||||
map_test.ContainsKey(keys[0]) ||
|
||||
map_test.ContainsValue(values[0]))
|
||||
return(false);
|
||||
//--- successful
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestMisc_Remove. |
|
||||
//+------------------------------------------------------------------+
|
||||
bool TestMisc_Remove(const int count)
|
||||
{
|
||||
//--- create arrays
|
||||
int keys[];
|
||||
string values[];
|
||||
ArrayResize(keys,count);
|
||||
ArrayResize(values,count);
|
||||
for(int i=0; i<count; i++)
|
||||
{
|
||||
keys[i]=i+1;
|
||||
values[i]=(string)(i+1);
|
||||
}
|
||||
//--- create source map
|
||||
CHashMap<int,string>map_test();
|
||||
for(int i=0; i<count; i++)
|
||||
map_test.Add(keys[i],values[i]);
|
||||
//--- create element
|
||||
int element=-1;
|
||||
//--- remove element which not contained in map
|
||||
//--- check
|
||||
if(map_test.Remove(element))
|
||||
return(false);
|
||||
if(map_test.Count()!=count)
|
||||
return(false);
|
||||
//--- add element to map
|
||||
map_test.Add(element,(string)element);
|
||||
//--- remove element
|
||||
if(!map_test.Remove(element))
|
||||
return(false);
|
||||
//--- check
|
||||
string value="test";
|
||||
if(map_test.TryGetValue(element,value))
|
||||
return(false);
|
||||
if(value!="test")
|
||||
return(false);
|
||||
//--- remove all elements
|
||||
for(int i=0; i<count; i++)
|
||||
{
|
||||
CKeyValuePair<int,string>pair(keys[i],values[i]);
|
||||
if(!map_test.Remove(GetPointer(pair)))
|
||||
return(false);
|
||||
}
|
||||
//--- check
|
||||
if(map_test.Count()!=0)
|
||||
return(false);
|
||||
//--- successful
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestMisc. |
|
||||
//+------------------------------------------------------------------+
|
||||
bool TestMisc(const string test_name)
|
||||
{
|
||||
PrintFormat("%s started",test_name);
|
||||
//--- test 1
|
||||
PrintFormat("%s: Test 1: Testing Constructor of map based on another map.",test_name);
|
||||
if(!TestMisc_Constructor(16))
|
||||
return(false);
|
||||
//--- test 2
|
||||
PrintFormat("%s: Test 2: Complex validation of Contains, ContainsKey and ContainsValues methods.",test_name);
|
||||
if(!TestMisc_Contains(16))
|
||||
return(false);
|
||||
//--- test 3
|
||||
PrintFormat("%s: Test 3: Complex validation of Remove method for elements which contains and does not conatains in the map.",test_name);
|
||||
if(!TestMisc_Remove(16))
|
||||
return(false);
|
||||
//--- successful
|
||||
PrintFormat("%s passed",test_name);
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestHashMap. |
|
||||
//+------------------------------------------------------------------+
|
||||
void TestHashMap(int &tests_performed,int &tests_passed)
|
||||
{
|
||||
string test_name="";
|
||||
//--- Misc functions
|
||||
tests_performed++;
|
||||
test_name="Misc functions test";
|
||||
if(TestMisc(test_name))
|
||||
tests_passed++;
|
||||
else
|
||||
PrintFormat("%s failed",test_name);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| Script program start function |
|
||||
//+------------------------------------------------------------------+
|
||||
void OnStart()
|
||||
{
|
||||
MathSrand(0);
|
||||
string package_name="Generic";
|
||||
PrintFormat("Unit tests for Package %s\n",package_name);
|
||||
//--- initial values
|
||||
int tests_performed=0;
|
||||
int tests_passed=0;
|
||||
//--- test distributions
|
||||
TestHashMap(tests_performed,tests_passed);
|
||||
//--- print statistics
|
||||
PrintFormat("\n%d of %d passed",tests_passed,tests_performed);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
@@ -0,0 +1,147 @@
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestHashSet.mq5 |
|
||||
//| Copyright 2000-2026, MetaQuotes Ltd. |
|
||||
//| www.mql5.com |
|
||||
//+------------------------------------------------------------------+
|
||||
#include <Generic\ArrayList.mqh>
|
||||
#include <Generic\HashSet.mqh>
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestMisc_Constructor. |
|
||||
//+------------------------------------------------------------------+
|
||||
bool TestMisc_Constructor(const int count)
|
||||
{
|
||||
//--- create array
|
||||
int array[];
|
||||
ArrayResize(array,count);
|
||||
for(int i=0; i<count; i++)
|
||||
array[i]=MathRand();
|
||||
//--- create array with duplicates
|
||||
int array_duplicates[];
|
||||
ArrayResize(array_duplicates,count*2);
|
||||
for(int i=0; i<count; i++)
|
||||
array_duplicates[i]=array[i];
|
||||
for(int i=0; i<count; i++)
|
||||
array_duplicates[count+i]=array[i];
|
||||
//--- create source set
|
||||
CDefaultEqualityComparer<int>comparer();
|
||||
CHashSet<int>set_test1(array);
|
||||
CHashSet<int>set_test2(array_duplicates,GetPointer(comparer));
|
||||
//--- check
|
||||
if(set_test1.Count()!=set_test2.Count())
|
||||
return(false);
|
||||
if(GetPointer(comparer)==set_test1.Comparer())
|
||||
return(false);
|
||||
if(GetPointer(comparer)!=set_test2.Comparer())
|
||||
return(false);
|
||||
//--- successful
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestMisc_TrimExpress. |
|
||||
//+------------------------------------------------------------------+
|
||||
bool TestMisc_TrimExpress(const int count)
|
||||
{
|
||||
//--- create array
|
||||
int array[];
|
||||
ArrayResize(array,count);
|
||||
for(int i=0; i<count; i++)
|
||||
array[i]=MathRand();
|
||||
//--- create source set
|
||||
CHashSet<int>set_test();
|
||||
for(int i=0; i<count; i++)
|
||||
set_test.Add(array[i]);
|
||||
//--- copy set to array
|
||||
int expected[];
|
||||
set_test.CopyTo(expected);
|
||||
//--- trim
|
||||
set_test.TrimExcess();
|
||||
set_test.TrimExcess();
|
||||
set_test.TrimExcess();
|
||||
//--- copy set to array
|
||||
int actual[];
|
||||
set_test.CopyTo(actual);
|
||||
//--- check
|
||||
if(ArrayCompare(actual,expected)!=0)
|
||||
return(false);
|
||||
//--- get first element
|
||||
int elemnet=actual[0];
|
||||
//--- trim
|
||||
set_test.TrimExcess();
|
||||
//--- remove
|
||||
if(!set_test.Remove(elemnet))
|
||||
return(false);
|
||||
//--- trim
|
||||
set_test.TrimExcess();
|
||||
//--- check
|
||||
ArrayFree(actual);
|
||||
set_test.CopyTo(actual);
|
||||
for(int i=0; i<count-1;i++)
|
||||
if(expected[i+1]!=actual[i])
|
||||
return(false);
|
||||
//--- trim and clear
|
||||
set_test.TrimExcess();
|
||||
set_test.Clear();
|
||||
set_test.TrimExcess();
|
||||
//--- check
|
||||
if(set_test.Count()!=0)
|
||||
return(false);
|
||||
//--- add values
|
||||
for(int i=0; i<count; i++)
|
||||
set_test.Add(array[i]);
|
||||
//--- trim
|
||||
set_test.TrimExcess();
|
||||
//--- check
|
||||
if(set_test.Count()!=ArraySize(expected))
|
||||
return(false);
|
||||
//--- successful
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestMisc. |
|
||||
//+------------------------------------------------------------------+
|
||||
bool TestMisc(const string test_name)
|
||||
{
|
||||
PrintFormat("%s started",test_name);
|
||||
//--- test 1
|
||||
PrintFormat("%s: Test 1: Testing Constructor of set based on array with and without duplicates.",test_name);
|
||||
if(!TestMisc_Constructor(16))
|
||||
return(false);
|
||||
//--- test 2
|
||||
PrintFormat("%s: Test 2: Complex validation of TrimExpress method after adding, removing element and cleaning.",test_name);
|
||||
if(!TestMisc_TrimExpress(16))
|
||||
return(false);
|
||||
//--- successful
|
||||
PrintFormat("%s passed",test_name);
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestHashSet. |
|
||||
//+------------------------------------------------------------------+
|
||||
void TestHashSet(int &tests_performed,int &tests_passed)
|
||||
{
|
||||
string test_name="";
|
||||
//--- Misc functions
|
||||
tests_performed++;
|
||||
test_name="Misc functions test";
|
||||
if(TestMisc(test_name))
|
||||
tests_passed++;
|
||||
else
|
||||
PrintFormat("%s failed",test_name);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| Script program start function |
|
||||
//+------------------------------------------------------------------+
|
||||
void OnStart()
|
||||
{
|
||||
MathSrand(0);
|
||||
string package_name="Generic";
|
||||
PrintFormat("Unit tests for Package %s\n",package_name);
|
||||
//--- initial values
|
||||
int tests_performed=0;
|
||||
int tests_passed=0;
|
||||
//--- test distributions
|
||||
TestHashSet(tests_performed,tests_passed);
|
||||
//--- print statistics
|
||||
PrintFormat("\n%d of %d passed",tests_passed,tests_performed);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,259 @@
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestQueue.mq5 |
|
||||
//| Copyright 2000-2026, MetaQuotes Ltd. |
|
||||
//| www.mql5.com |
|
||||
//+------------------------------------------------------------------+
|
||||
#include <Generic\Queue.mqh>
|
||||
#include <Generic\ArrayList.mqh>
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestConstructor_Valid. |
|
||||
//+------------------------------------------------------------------+
|
||||
bool TestConstructor_Valid(const int count)
|
||||
{
|
||||
//--- create random array
|
||||
int array[];
|
||||
ArrayResize(array,count);
|
||||
for(int i=0; i<count; i++)
|
||||
array[i]=MathRand();
|
||||
//--- create list
|
||||
CArrayList<int>list(array);
|
||||
//--- create source queue
|
||||
CQueue<int>queue_test1(array);
|
||||
CQueue<int>queue_test2(GetPointer(list));
|
||||
CQueue<int>queue_test3(count);
|
||||
for(int i=0; i<count; i++)
|
||||
queue_test3.Add(array[i]);
|
||||
//--- check count
|
||||
if(queue_test1.Count()!=count)
|
||||
return(false);
|
||||
if(queue_test2.Count()!=count)
|
||||
return(false);
|
||||
if(queue_test3.Count()!=count)
|
||||
return(false);
|
||||
//--- check elements
|
||||
for(int i=0; i<count; i++)
|
||||
{
|
||||
if(array[i]!=queue_test1.Dequeue())
|
||||
return(false);
|
||||
if(array[i]!=queue_test2.Dequeue())
|
||||
return(false);
|
||||
if(array[i]!=queue_test3.Dequeue())
|
||||
return(false);
|
||||
}
|
||||
//--- successful
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestConstructor_Invalid. |
|
||||
//+------------------------------------------------------------------+
|
||||
bool TestConstructor_Invalid(const int count)
|
||||
{
|
||||
//--- create source queue
|
||||
CQueue<int>queue_test1(-1);
|
||||
CQueue<int>queue_test2(INT_MIN);
|
||||
//--- check
|
||||
if(queue_test1.Count()!=0)
|
||||
return(false);
|
||||
if(queue_test2.Count()!=0)
|
||||
return(false);
|
||||
//--- successful
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestConstructor. |
|
||||
//+------------------------------------------------------------------+
|
||||
bool TestConstructor(const string test_name)
|
||||
{
|
||||
PrintFormat("%s started",test_name);
|
||||
//--- test 1
|
||||
PrintFormat("%s: Test 1: Testing Constructor of queue with array, ICollection object and correct capacity.",test_name);
|
||||
if(!TestConstructor_Valid(10))
|
||||
return(false);
|
||||
//--- test 2
|
||||
PrintFormat("%s: Test 2: Testing Constructor of queue with incorrect capacity.",test_name);
|
||||
if(!TestConstructor_Invalid(10))
|
||||
return(false);
|
||||
//--- successful
|
||||
PrintFormat("%s passed",test_name);
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestMisc_Dequeue. |
|
||||
//+------------------------------------------------------------------+
|
||||
bool TestMisc_Dequeue(const int count)
|
||||
{
|
||||
//--- create random array
|
||||
int array[];
|
||||
ArrayResize(array,count);
|
||||
for(int i=0; i<count; i++)
|
||||
array[i]=MathRand();
|
||||
//--- create source queue
|
||||
CQueue<int>queue_test(array);
|
||||
//--- check elements
|
||||
for(int i=0; i<count; i++)
|
||||
if(queue_test.Dequeue()!=array[i])
|
||||
return(false);
|
||||
//--- check count
|
||||
if(queue_test.Count()!=0)
|
||||
return(false);
|
||||
//--- successful
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestMisc_Peek. |
|
||||
//+------------------------------------------------------------------+
|
||||
bool TestMisc_Peek(const int count)
|
||||
{
|
||||
//--- create random array
|
||||
int array[];
|
||||
ArrayResize(array,count);
|
||||
for(int i=0; i<count; i++)
|
||||
array[i]=MathRand();
|
||||
//--- create source queue
|
||||
CQueue<int>queue_test(array);
|
||||
//--- check element
|
||||
if(queue_test.Peek()!=array[0])
|
||||
return(false);
|
||||
//--- check count
|
||||
if(queue_test.Count()!=count)
|
||||
return(false);
|
||||
//--- successful
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestMisc_Complex. |
|
||||
//+------------------------------------------------------------------+
|
||||
bool TestMisc_Complex(const int count)
|
||||
{
|
||||
//--- create random array
|
||||
int array[];
|
||||
ArrayResize(array,count);
|
||||
for(int i=0; i<count; i++)
|
||||
array[i]=MathRand();
|
||||
//--- create source queue
|
||||
CQueue<int>queue_test(count);
|
||||
//--- enqueue values
|
||||
for(int i=0; i<count; i++)
|
||||
if(!queue_test.Enqueue(array[i]))
|
||||
return(false);
|
||||
//--- check count
|
||||
if(queue_test.Count()!=count)
|
||||
return(false);
|
||||
//--- check elements
|
||||
for(int i=0; i<count; i++)
|
||||
{
|
||||
if(array[i]!=queue_test.Dequeue())
|
||||
return(false);
|
||||
if(count-i-1!=queue_test.Count())
|
||||
return(false);
|
||||
}
|
||||
//--- recheck
|
||||
queue_test.Enqueue(0);
|
||||
if(queue_test.Dequeue()!=0)
|
||||
return(false);
|
||||
//--- successful
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestMisc_TrimExcess. |
|
||||
//+------------------------------------------------------------------+
|
||||
bool TestMisc_TrimExcess(const int count)
|
||||
{
|
||||
//--- create random array
|
||||
int array[];
|
||||
ArrayResize(array,count);
|
||||
for(int i=0; i<count; i++)
|
||||
array[i]=MathRand();
|
||||
//--- create source queue
|
||||
CQueue<int>queue_test(array);
|
||||
//--- trim
|
||||
queue_test.TrimExcess();
|
||||
int removed=queue_test.Dequeue();
|
||||
queue_test.TrimExcess();
|
||||
//--- check element
|
||||
if(queue_test.Peek()!=array[1])
|
||||
return(false);
|
||||
//--- check count
|
||||
if(queue_test.Count()!=count-1)
|
||||
return(false);
|
||||
//--- trim and clear
|
||||
queue_test.TrimExcess();
|
||||
queue_test.Clear();
|
||||
queue_test.TrimExcess();
|
||||
//--- check count
|
||||
if(queue_test.Count()!=0)
|
||||
return(false);
|
||||
//--- add elemnt and trim
|
||||
queue_test.Add(0);
|
||||
queue_test.TrimExcess();
|
||||
//--- check count
|
||||
if(queue_test.Count()!=1)
|
||||
return(false);
|
||||
//--- successful
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestMisc. |
|
||||
//+------------------------------------------------------------------+
|
||||
bool TestMisc(const string test_name)
|
||||
{
|
||||
PrintFormat("%s started",test_name);
|
||||
//--- test 1
|
||||
PrintFormat("%s: Test 1: Validation of Dequeue method for all element of the queue and check count after.",test_name);
|
||||
if(!TestMisc_Dequeue(10))
|
||||
return(false);
|
||||
//--- test 2
|
||||
PrintFormat("%s: Test 2: Validation of Peek method for all element of the queue and check count after.",test_name);
|
||||
if(!TestMisc_Peek(10))
|
||||
return(false);
|
||||
//--- test 3
|
||||
PrintFormat("%s: Test 3: Complex validation of Enqueue and Dequeue methods.",test_name);
|
||||
if(!TestMisc_Complex(10))
|
||||
return(false);
|
||||
//--- test 4
|
||||
PrintFormat("%s: Test 4: Testing TrimExcess method on the queue after filling, clearing and adding elements.",test_name);
|
||||
if(!TestMisc_TrimExcess(10))
|
||||
return(false);
|
||||
//--- successful
|
||||
PrintFormat("%s passed",test_name);
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestQueue. |
|
||||
//+------------------------------------------------------------------+
|
||||
void TestQueue(int &tests_performed,int &tests_passed)
|
||||
{
|
||||
string test_name="";
|
||||
//--- Constructor functions
|
||||
tests_performed++;
|
||||
test_name="TestConstructor functions test";
|
||||
if(TestConstructor(test_name))
|
||||
tests_passed++;
|
||||
else
|
||||
PrintFormat("%s failed",test_name);
|
||||
|
||||
//--- AddRange functions
|
||||
tests_performed++;
|
||||
test_name="TestMisc functions test";
|
||||
if(TestMisc(test_name))
|
||||
tests_passed++;
|
||||
else
|
||||
PrintFormat("%s failed",test_name);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| Script program start function |
|
||||
//+------------------------------------------------------------------+
|
||||
void OnStart()
|
||||
{
|
||||
MathSrand(0);
|
||||
string package_name="Generic";
|
||||
PrintFormat("Unit tests for Package %s\n",package_name);
|
||||
//--- initial values
|
||||
int tests_performed=0;
|
||||
int tests_passed=0;
|
||||
//--- test distributions
|
||||
TestQueue(tests_performed,tests_passed);
|
||||
//--- print statistics
|
||||
PrintFormat("\n%d of %d passed",tests_passed,tests_performed);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
@@ -0,0 +1,361 @@
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestRedBlackTree.mq5 |
|
||||
//| Copyright 2000-2026, MetaQuotes Ltd. |
|
||||
//| www.mql5.com |
|
||||
//+------------------------------------------------------------------+
|
||||
#include <Generic\RedBlackTree.mqh>
|
||||
#include <Generic\ArrayList.mqh>
|
||||
#include <Generic\HashSet.mqh>
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestMisc_Constructor. |
|
||||
//+------------------------------------------------------------------+
|
||||
bool TestMisc_Constructor(const int count)
|
||||
{
|
||||
//--- create list
|
||||
CArrayList<int>list(count);
|
||||
for(int i=0; i<count; i++)
|
||||
list.Add(MathRand());
|
||||
//--- create source tree
|
||||
CDefaultComparer<int>comparer();
|
||||
CRedBlackTree<int>tree_test1(GetPointer(list),GetPointer(comparer));
|
||||
CRedBlackTree<int>tree_test2(GetPointer(tree_test1));
|
||||
//--- check
|
||||
if(tree_test1.Count()!=tree_test2.Count())
|
||||
return(false);
|
||||
if(tree_test1.Comparer()!=GetPointer(comparer))
|
||||
return(false);
|
||||
if(tree_test2.Comparer()==GetPointer(comparer))
|
||||
return(false);
|
||||
//--- get unique sorted values
|
||||
CHashSet<int>set(GetPointer(list));
|
||||
int expected[];
|
||||
set.CopyTo(expected);
|
||||
ArraySort(expected);
|
||||
//--- check
|
||||
int actual1[];
|
||||
int actual2[];
|
||||
tree_test1.CopyTo(actual1);
|
||||
tree_test1.CopyTo(actual2);
|
||||
for(int i=0; i<ArraySize(expected); i++)
|
||||
if(actual1[i]!=expected[i] || actual2[i]!=expected[i])
|
||||
return(false);
|
||||
//--- successful
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestMisc_Contains. |
|
||||
//+------------------------------------------------------------------+
|
||||
bool TestMisc_Contains(const int count)
|
||||
{
|
||||
//--- create list
|
||||
CArrayList<int>list(count);
|
||||
for(int i=0; i<count; i++)
|
||||
list.Add(MathRand());
|
||||
//--- create source tree
|
||||
CRedBlackTree<int>tree_test(GetPointer(list));
|
||||
//--- check
|
||||
for(int i=0; i<count; i++)
|
||||
{
|
||||
int value;
|
||||
list.TryGetValue(i,value);
|
||||
if(!tree_test.Contains(value))
|
||||
return(false);
|
||||
}
|
||||
//--- clear tree
|
||||
tree_test.Clear();
|
||||
//--- check
|
||||
for(int i=0; i<count; i++)
|
||||
{
|
||||
int value;
|
||||
list.TryGetValue(i,value);
|
||||
if(tree_test.Contains(value))
|
||||
return(false);
|
||||
}
|
||||
//--- successful
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestMisc_Add. |
|
||||
//+------------------------------------------------------------------+
|
||||
bool TestMisc_Add(const int count)
|
||||
{
|
||||
//--- create list
|
||||
CArrayList<int>list(count);
|
||||
for(int i=0; i<count; i++)
|
||||
list.Add(MathRand());
|
||||
//--- create source tree
|
||||
CRedBlackTree<int>tree_test(GetPointer(list));
|
||||
//--- check
|
||||
if(tree_test.Count()>count)
|
||||
return(false);
|
||||
//--- create new unique element
|
||||
int element=MathRand();
|
||||
while(tree_test.Contains(element))
|
||||
element=MathRand();
|
||||
//--- add new unique element
|
||||
if(!tree_test.Add(element))
|
||||
return(false);
|
||||
if(tree_test.Add(element))
|
||||
return(false);
|
||||
//--- successful
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestMisc. |
|
||||
//+------------------------------------------------------------------+
|
||||
bool TestMisc(const string test_name)
|
||||
{
|
||||
PrintFormat("%s started",test_name);
|
||||
//--- test 1
|
||||
PrintFormat("%s: Test 1: Testing Constructor of map based on several different ICollection objects with and without comparer.",test_name);
|
||||
if(!TestMisc_Constructor(16))
|
||||
return(false);
|
||||
//--- test 2
|
||||
PrintFormat("%s: Test 2: Validation of Contains method before and after cleaning the tree.",test_name);
|
||||
if(!TestMisc_Contains(16))
|
||||
return(false);
|
||||
//--- test 3
|
||||
PrintFormat("%s: Test 3: Simple validation of Add method.",test_name);
|
||||
if(!TestMisc_Add(16))
|
||||
return(false);
|
||||
//--- successful
|
||||
PrintFormat("%s passed",test_name);
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestFind_Max. |
|
||||
//+------------------------------------------------------------------+
|
||||
bool TestFind_Max(const int count)
|
||||
{
|
||||
//--- create list
|
||||
CArrayList<int>list(count);
|
||||
for(int i=0; i<count; i++)
|
||||
list.Add(MathRand());
|
||||
//--- create source tree
|
||||
CRedBlackTree<int>tree_test(GetPointer(list));
|
||||
//--- get max
|
||||
list.Sort();
|
||||
CRedBlackTreeNode<int>*max_node=tree_test.FindMax();
|
||||
int expected;
|
||||
list.TryGetValue(count-1,expected);
|
||||
int actual=max_node.Value();
|
||||
//--- check
|
||||
if(expected!=actual)
|
||||
return(false);
|
||||
if(tree_test.Find(expected)!=max_node)
|
||||
return(false);
|
||||
//--- successful
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestFind_Min. |
|
||||
//+------------------------------------------------------------------+
|
||||
bool TestFind_Min(const int count)
|
||||
{
|
||||
//--- create list
|
||||
CArrayList<int>list(count);
|
||||
for(int i=0; i<count; i++)
|
||||
list.Add(MathRand());
|
||||
//--- create source tree
|
||||
CRedBlackTree<int>tree_test(GetPointer(list));
|
||||
//--- get max
|
||||
list.Sort();
|
||||
CRedBlackTreeNode<int>*min_node=tree_test.FindMin();
|
||||
int expected;
|
||||
list.TryGetValue(0, expected);
|
||||
int actual=min_node.Value();
|
||||
//--- check
|
||||
if(expected!=actual)
|
||||
return(false);
|
||||
if(tree_test.Find(expected)!=min_node)
|
||||
return(false);
|
||||
//--- successful
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestFind. |
|
||||
//+------------------------------------------------------------------+
|
||||
bool TestFind(const string test_name)
|
||||
{
|
||||
PrintFormat("%s started",test_name);
|
||||
//--- test 1
|
||||
PrintFormat("%s: Test 1: Simple validation of FindMax method for tree.",test_name);
|
||||
if(!TestFind_Max(16))
|
||||
return(false);
|
||||
//--- test 2
|
||||
PrintFormat("%s: Test 2: Simple validation of FindMin method for tree.",test_name);
|
||||
if(!TestFind_Min(16))
|
||||
return(false);
|
||||
//--- successful
|
||||
PrintFormat("%s passed",test_name);
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestRemove_Node. |
|
||||
//+------------------------------------------------------------------+
|
||||
bool TestRemove_Node(const int count)
|
||||
{
|
||||
//--- create list
|
||||
CArrayList<int>list(count);
|
||||
for(int i=0; i<count; i++)
|
||||
list.Add(MathRand());
|
||||
//--- create source tree
|
||||
CRedBlackTree<int>tree_test(GetPointer(list));
|
||||
//--- get max and min
|
||||
list.Sort();
|
||||
int min;
|
||||
int max;
|
||||
list.TryGetValue(0, min);
|
||||
list.TryGetValue(count-1, max);
|
||||
//--- check
|
||||
if(tree_test.Count()>0)
|
||||
{
|
||||
//--- remove min value
|
||||
if(!tree_test.Remove(min))
|
||||
return(false);
|
||||
}
|
||||
if(tree_test.Count()>0)
|
||||
{
|
||||
//--- remove max node
|
||||
CRedBlackTreeNode<int>*node=tree_test.FindMax();
|
||||
if(!tree_test.Remove(node))
|
||||
return(false);
|
||||
}
|
||||
//--- get unique sorted values
|
||||
CHashSet<int>set(GetPointer(list));
|
||||
set.Remove(min);
|
||||
set.Remove(max);
|
||||
int expected[];
|
||||
set.CopyTo(expected);
|
||||
ArraySort(expected);
|
||||
//--- check
|
||||
int actual[];
|
||||
tree_test.CopyTo(actual);
|
||||
if(ArrayCompare(expected,actual)!=0)
|
||||
return(false);
|
||||
//--- successful
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestRemove_Max. |
|
||||
//+------------------------------------------------------------------+
|
||||
bool TestRemove_Max(const int count)
|
||||
{
|
||||
//--- create list
|
||||
CArrayList<int>list(count);
|
||||
for(int i=0; i<count; i++)
|
||||
list.Add(MathRand());
|
||||
//--- create source tree
|
||||
CRedBlackTree<int>tree_test(GetPointer(list));
|
||||
//--- get unique sorted values
|
||||
CHashSet<int>set(GetPointer(list));
|
||||
int expected[];
|
||||
set.CopyTo(expected);
|
||||
ArraySort(expected);
|
||||
//--- check
|
||||
for(int i=0; i<ArraySize(expected); i++)
|
||||
{
|
||||
int actual[];
|
||||
tree_test.CopyTo(actual);
|
||||
if(ArrayCompare(expected,actual,0,0,count-i)!=0)
|
||||
return(false);
|
||||
tree_test.RemoveMax();
|
||||
}
|
||||
//--- successful
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestRemove_Min. |
|
||||
//+------------------------------------------------------------------+
|
||||
bool TestRemove_Min(const int count)
|
||||
{
|
||||
//--- create list
|
||||
CArrayList<int>list(count);
|
||||
for(int i=0; i<count; i++)
|
||||
list.Add(MathRand());
|
||||
//--- create source tree
|
||||
CRedBlackTree<int>tree_test(GetPointer(list));
|
||||
//--- get unique sorted values
|
||||
CHashSet<int>set(GetPointer(list));
|
||||
int expected[];
|
||||
set.CopyTo(expected);
|
||||
ArraySort(expected);
|
||||
//--- check
|
||||
for(int i=0; i<ArraySize(expected); i++)
|
||||
{
|
||||
int actual[];
|
||||
tree_test.CopyTo(actual);
|
||||
if(ArrayCompare(expected,actual,i,0,count-i)!=0)
|
||||
return(false);
|
||||
tree_test.RemoveMin();
|
||||
}
|
||||
//--- successful
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestRemove. |
|
||||
//+------------------------------------------------------------------+
|
||||
bool TestRemove(const string test_name)
|
||||
{
|
||||
PrintFormat("%s started",test_name);
|
||||
//--- test 1
|
||||
PrintFormat("%s: Test 2: Validation of Remove method used independent element and specified node of tree.",test_name);
|
||||
if(!TestRemove_Node(16))
|
||||
return(false);
|
||||
//--- test 2
|
||||
PrintFormat("%s: Test 2: Validation of RemoveMax method for cleaning the tree.",test_name);
|
||||
if(!TestRemove_Max(16))
|
||||
return(false);
|
||||
//--- test 3
|
||||
PrintFormat("%s: Test 3: Validation of RemoveMin method for cleaning the tree.",test_name);
|
||||
if(!TestRemove_Min(16))
|
||||
return(false);
|
||||
//--- successful
|
||||
PrintFormat("%s passed",test_name);
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestRedBlackTree. |
|
||||
//+------------------------------------------------------------------+
|
||||
void TestRedBlackTree(int &tests_performed,int &tests_passed)
|
||||
{
|
||||
string test_name="";
|
||||
//--- Misc functions
|
||||
tests_performed++;
|
||||
test_name="Misc functions test";
|
||||
if(TestMisc(test_name))
|
||||
tests_passed++;
|
||||
else
|
||||
PrintFormat("%s failed",test_name);
|
||||
//--- Find functions
|
||||
tests_performed++;
|
||||
test_name="Find functions test";
|
||||
if(TestFind(test_name))
|
||||
tests_passed++;
|
||||
else
|
||||
PrintFormat("%s failed",test_name);
|
||||
//--- Remove functions
|
||||
tests_performed++;
|
||||
test_name="Remove functions test";
|
||||
if(TestRemove(test_name))
|
||||
tests_passed++;
|
||||
else
|
||||
PrintFormat("%s failed",test_name);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| Script program start function |
|
||||
//+------------------------------------------------------------------+
|
||||
void OnStart()
|
||||
{
|
||||
MathSrand(0);
|
||||
string package_name="Generic";
|
||||
PrintFormat("Unit tests for Package %s\n",package_name);
|
||||
//--- initial values
|
||||
int tests_performed=0;
|
||||
int tests_passed=0;
|
||||
//--- test distributions
|
||||
TestRedBlackTree(tests_performed,tests_passed);
|
||||
//--- print statistics
|
||||
PrintFormat("\n%d of %d passed",tests_passed,tests_performed);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
@@ -0,0 +1,187 @@
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestSortedMap.mq5 |
|
||||
//| Copyright 2000-2026, MetaQuotes Ltd. |
|
||||
//| www.mql5.com |
|
||||
//+------------------------------------------------------------------+
|
||||
#include <Generic\ArrayList.mqh>
|
||||
#include <Generic\SortedMap.mqh>
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestMisc_Constructor. |
|
||||
//+------------------------------------------------------------------+
|
||||
bool TestMisc_Constructor(const int count)
|
||||
{
|
||||
//--- create arrays
|
||||
int keys[];
|
||||
string values[];
|
||||
ArrayResize(keys,count);
|
||||
ArrayResize(values,count);
|
||||
for(int i=0; i<count; i++)
|
||||
{
|
||||
keys[i]=i+1;
|
||||
values[i]=(string)(i+1);
|
||||
}
|
||||
//--- create source map
|
||||
CDefaultComparer<int>comparer();
|
||||
CSortedMap<int,string>map_test(GetPointer(comparer));
|
||||
for(int i=0; i<count; i++)
|
||||
map_test.Add(keys[i],values[i]);
|
||||
//--- create map on map
|
||||
CSortedMap<int,string>map_copy(GetPointer(map_test));
|
||||
//--- check
|
||||
if(map_copy.Count()!=map_test.Count())
|
||||
return(false);
|
||||
if(map_test.Comparer()!=GetPointer(comparer))
|
||||
return(false);
|
||||
if(map_copy.Comparer()==GetPointer(comparer))
|
||||
return(false);
|
||||
//--- successful
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestMisc_Contains. |
|
||||
//+------------------------------------------------------------------+
|
||||
bool TestMisc_Contains(const int count)
|
||||
{
|
||||
//--- create arrays
|
||||
int keys[];
|
||||
string values[];
|
||||
ArrayResize(keys,count);
|
||||
ArrayResize(values,count);
|
||||
for(int i=0; i<count; i++)
|
||||
{
|
||||
keys[i]=i+1;
|
||||
values[i]=(string)(i+1);
|
||||
}
|
||||
//--- create source map
|
||||
CSortedMap<int,string>map_test();
|
||||
for(int i=0; i<count; i++)
|
||||
map_test.Add(keys[i],values[i]);
|
||||
//--- create elemet
|
||||
int element=keys[0];
|
||||
while(map_test.Contains(element,(string)element) && map_test.ContainsKey(element))
|
||||
element++;
|
||||
//--- check
|
||||
if(map_test.Contains(element,(string)element) || map_test.ContainsKey(element))
|
||||
return(false);
|
||||
//--- add element to map
|
||||
map_test.Add(element,"new");
|
||||
//--- check
|
||||
if(!map_test.Contains(element,"new") ||
|
||||
!map_test.ContainsKey(element) ||
|
||||
!map_test.ContainsValue("new"))
|
||||
return(false);
|
||||
//--- clear
|
||||
map_test.Clear();
|
||||
//--- check
|
||||
if(map_test.Contains(keys[0],values[0]) ||
|
||||
map_test.ContainsKey(keys[0]) ||
|
||||
map_test.ContainsValue(values[0]))
|
||||
return(false);
|
||||
//--- successful
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestMisc_Ordering. |
|
||||
//+------------------------------------------------------------------+
|
||||
bool TestMisc_Ordering(const int count)
|
||||
{
|
||||
//--- create arrays
|
||||
int keys[];
|
||||
string values[];
|
||||
ArrayResize(keys,count);
|
||||
ArrayResize(values,count);
|
||||
for(int i=0; i<count; i++)
|
||||
{
|
||||
keys[i]=i+1;
|
||||
values[i]=(string)(i+1);
|
||||
}
|
||||
//--- create source map
|
||||
CSortedMap<int,string>map_test();
|
||||
for(int i=0; i<count; i++)
|
||||
map_test.Add(keys[i],values[i]);
|
||||
//--- copy map to array
|
||||
CKeyValuePair<int,string>*pairs[];
|
||||
int size=map_test.CopyTo(pairs);
|
||||
//--- check
|
||||
CDefaultComparer<CKeyValuePair<int,string>*>comparer();
|
||||
CArrayList<CKeyValuePair<int,string>*>expected(pairs);
|
||||
expected.Sort(GetPointer(comparer));
|
||||
int actual_keys[];
|
||||
string actual_values[];
|
||||
map_test.CopyTo(actual_keys,actual_values);
|
||||
for(int i=0; i<size; i++)
|
||||
{
|
||||
CKeyValuePair<int,string>*pair;
|
||||
expected.TryGetValue(i,pair);
|
||||
if(pair.Key()!=actual_keys[i] || pair.Value()!=actual_values[i])
|
||||
return(false);
|
||||
//--- check TryGetValue
|
||||
string value;
|
||||
if(!map_test.TryGetValue(pair.Key(),value))
|
||||
return(false);
|
||||
if(pair.Value()!=value)
|
||||
return(false);
|
||||
}
|
||||
//--- delete pairs
|
||||
for(int i=0; i<ArraySize(pairs); i++)
|
||||
{
|
||||
CKeyValuePair<int,string>*pair;
|
||||
expected.TryGetValue(i,pair);
|
||||
delete pair;
|
||||
}
|
||||
//--- successful
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestMisc. |
|
||||
//+------------------------------------------------------------------+
|
||||
bool TestMisc(const string test_name)
|
||||
{
|
||||
PrintFormat("%s started",test_name);
|
||||
//--- test 1
|
||||
PrintFormat("%s: Test 1: Testing Constructor of map based on another map.",test_name);
|
||||
if(!TestMisc_Constructor(16))
|
||||
return(false);
|
||||
//--- test 2
|
||||
PrintFormat("%s: Test 2: Complex validation of Contains, ContainsKey and ContainsValues methods.",test_name);
|
||||
if(!TestMisc_Contains(16))
|
||||
return(false);
|
||||
//--- test 3
|
||||
PrintFormat("%s: Test 3: Testing the ordering of elements in the map.",test_name);
|
||||
if(!TestMisc_Ordering(16))
|
||||
return(false);
|
||||
//--- successful
|
||||
PrintFormat("%s passed",test_name);
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestSortedMap. |
|
||||
//+------------------------------------------------------------------+
|
||||
void TestSortedMap(int &tests_performed,int &tests_passed)
|
||||
{
|
||||
string test_name="";
|
||||
//--- Misc functions
|
||||
tests_performed++;
|
||||
test_name="Misc functions test";
|
||||
if(TestMisc(test_name))
|
||||
tests_passed++;
|
||||
else
|
||||
PrintFormat("%s failed",test_name);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| Script program start function |
|
||||
//+------------------------------------------------------------------+
|
||||
void OnStart()
|
||||
{
|
||||
MathSrand(0);
|
||||
string package_name="Generic";
|
||||
PrintFormat("Unit tests for Package %s\n",package_name);
|
||||
//--- initial values
|
||||
int tests_performed=0;
|
||||
int tests_passed=0;
|
||||
//--- test distributions
|
||||
TestSortedMap(tests_performed,tests_passed);
|
||||
//--- print statistics
|
||||
PrintFormat("\n%d of %d passed",tests_passed,tests_performed);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
@@ -0,0 +1,147 @@
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestSortedSet.mq5 |
|
||||
//| Copyright 2000-2026, MetaQuotes Ltd. |
|
||||
//| www.mql5.com |
|
||||
//+------------------------------------------------------------------+
|
||||
#include <Generic\ArrayList.mqh>
|
||||
#include <Generic\SortedSet.mqh>
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestMisc_Constructor. |
|
||||
//+------------------------------------------------------------------+
|
||||
bool TestMisc_Constructor(const int count)
|
||||
{
|
||||
//--- create array
|
||||
int array[];
|
||||
ArrayResize(array,count);
|
||||
for(int i=0; i<count; i++)
|
||||
array[i]=MathRand();
|
||||
//--- create array with duplicates
|
||||
int array_duplicates[];
|
||||
ArrayResize(array_duplicates,count*2);
|
||||
for(int i=0; i<count; i++)
|
||||
array_duplicates[i]=array[i];
|
||||
for(int i=0; i<count; i++)
|
||||
array_duplicates[count+i]=array[i];
|
||||
//--- create source set
|
||||
CDefaultComparer<int>comparer();
|
||||
CSortedSet<int>set_test1(array);
|
||||
CSortedSet<int>set_test2(array_duplicates,GetPointer(comparer));
|
||||
//--- check
|
||||
if(set_test1.Count()!=set_test2.Count())
|
||||
return(false);
|
||||
if(GetPointer(comparer)==set_test1.Comparer())
|
||||
return(false);
|
||||
if(GetPointer(comparer)!=set_test2.Comparer())
|
||||
return(false);
|
||||
//--- check ordering of the first set
|
||||
int check_array[];
|
||||
set_test1.CopyTo(check_array);
|
||||
for(int i=0; i<ArraySize(check_array)-1; i++)
|
||||
if(comparer.Compare(check_array[i],check_array[i+1])>0 && check_array[i]>check_array[i+1])
|
||||
return(false);
|
||||
//--- check ordering of the second set
|
||||
ArrayFree(check_array);
|
||||
set_test2.CopyTo(check_array);
|
||||
for(int i=0; i<ArraySize(check_array)-1; i++)
|
||||
if(comparer.Compare(check_array[i],check_array[i+1])>0 && check_array[i]>check_array[i+1])
|
||||
return(false);
|
||||
//--- successful
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestMisc_GetViewBetween. |
|
||||
//+------------------------------------------------------------------+
|
||||
bool TestMisc_GetViewBetween(const int count)
|
||||
{
|
||||
int view[];
|
||||
int check_array[];
|
||||
//--- create array
|
||||
int array[];
|
||||
ArrayResize(array,count);
|
||||
for(int i=0; i<count; i++)
|
||||
array[i]=MathRand();
|
||||
//--- create source set
|
||||
CSortedSet<int>set_test(array);
|
||||
//--- calculate max and min
|
||||
int min = array[ArrayMinimum(array)];
|
||||
int max = array[ArrayMaximum(array)];
|
||||
if(!set_test.GetViewBetween(view,min,max))
|
||||
return(false);
|
||||
//--- get view
|
||||
set_test.GetViewBetween(view,min,max);
|
||||
//--- check
|
||||
set_test.CopyTo(check_array);
|
||||
if(ArrayCompare(check_array,view)!=0)
|
||||
return(false);
|
||||
//--- lower value greater than upper value
|
||||
min=array[ArrayMaximum(array)];
|
||||
max=array[ArrayMinimum(array)];
|
||||
//--- get view
|
||||
ArrayFree(view);
|
||||
if(set_test.GetViewBetween(view,min,max))
|
||||
return(false);
|
||||
//--- check
|
||||
if(ArraySize(view)!=0)
|
||||
return(false);
|
||||
//--- minimum lower value and maximum upper value
|
||||
min=INT_MIN;
|
||||
max=INT_MAX;
|
||||
//--- get view
|
||||
ArrayFree(view);
|
||||
if(!set_test.GetViewBetween(view,min,max))
|
||||
return(false);
|
||||
//--- check
|
||||
if(ArrayCompare(check_array,view)!=0)
|
||||
return(false);
|
||||
//--- successful
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestMisc. |
|
||||
//+------------------------------------------------------------------+
|
||||
bool TestMisc(const string test_name)
|
||||
{
|
||||
PrintFormat("%s started",test_name);
|
||||
//--- test 1
|
||||
PrintFormat("%s: Test 1: Testing Constructor of set based of array with and without duplicates.",test_name);
|
||||
if(!TestMisc_Constructor(16))
|
||||
return(false);
|
||||
//--- test 2
|
||||
PrintFormat("%s: Test 2: Complex validation of GetViewBetween method with correct and incorrect input parameters.",test_name);
|
||||
if(!TestMisc_GetViewBetween(16))
|
||||
return(false);
|
||||
//--- successful
|
||||
PrintFormat("%s passed",test_name);
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestSortedSet. |
|
||||
//+------------------------------------------------------------------+
|
||||
void TestSortedSet(int &tests_performed,int &tests_passed)
|
||||
{
|
||||
string test_name="";
|
||||
//--- Misc functions
|
||||
tests_performed++;
|
||||
test_name="Misc functions test";
|
||||
if(TestMisc(test_name))
|
||||
tests_passed++;
|
||||
else
|
||||
PrintFormat("%s failed",test_name);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| Script program start function |
|
||||
//+------------------------------------------------------------------+
|
||||
void OnStart()
|
||||
{
|
||||
MathSrand(0);
|
||||
string package_name="Generic";
|
||||
PrintFormat("Unit tests for Package %s\n",package_name);
|
||||
//--- initial values
|
||||
int tests_performed=0;
|
||||
int tests_passed=0;
|
||||
//--- test distributions
|
||||
TestSortedSet(tests_performed,tests_passed);
|
||||
//--- print statistics
|
||||
PrintFormat("\n%d of %d passed",tests_passed,tests_performed);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
@@ -0,0 +1,259 @@
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestStack.mq5 |
|
||||
//| Copyright 2000-2026, MetaQuotes Ltd. |
|
||||
//| www.mql5.com |
|
||||
//+------------------------------------------------------------------+
|
||||
#include <Generic\Stack.mqh>
|
||||
#include <Generic\ArrayList.mqh>
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestConstructor_Valid. |
|
||||
//+------------------------------------------------------------------+
|
||||
bool TestConstructor_Valid(const int count)
|
||||
{
|
||||
//--- create random array
|
||||
int array[];
|
||||
ArrayResize(array,count);
|
||||
for(int i=0; i<count; i++)
|
||||
array[i]=MathRand();
|
||||
//--- create list
|
||||
CArrayList<int>list(array);
|
||||
//--- create source stack
|
||||
CStack<int>stack_test1(array);
|
||||
CStack<int>stack_test2(GetPointer(list));
|
||||
CStack<int>stack_test3(count);
|
||||
for(int i=0; i<count; i++)
|
||||
stack_test3.Add(array[i]);
|
||||
//--- check count
|
||||
if(stack_test1.Count()!=count)
|
||||
return(false);
|
||||
if(stack_test2.Count()!=count)
|
||||
return(false);
|
||||
if(stack_test3.Count()!=count)
|
||||
return(false);
|
||||
//--- check elements
|
||||
for(int i=0; i<count; i++)
|
||||
{
|
||||
if(array[count-1-i]!=stack_test1.Pop())
|
||||
return(false);
|
||||
if(array[count-1-i]!=stack_test2.Pop())
|
||||
return(false);
|
||||
if(array[count-1-i]!=stack_test3.Pop())
|
||||
return(false);
|
||||
}
|
||||
//--- successful
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestConstructor_Invalid. |
|
||||
//+------------------------------------------------------------------+
|
||||
bool TestConstructor_Invalid(const int count)
|
||||
{
|
||||
//--- create source stack
|
||||
CStack<int>stack_test1(-1);
|
||||
CStack<int>stack_test2(INT_MIN);
|
||||
//--- check
|
||||
if(stack_test1.Count()!=0)
|
||||
return(false);
|
||||
if(stack_test2.Count()!=0)
|
||||
return(false);
|
||||
//--- successful
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestConstructor. |
|
||||
//+------------------------------------------------------------------+
|
||||
bool TestConstructor(const string test_name)
|
||||
{
|
||||
PrintFormat("%s started",test_name);
|
||||
//--- test 1
|
||||
PrintFormat("%s: Test 1: Testing Constructor of stack with array, ICollection object and correct capacity.",test_name);
|
||||
if(!TestConstructor_Valid(10))
|
||||
return(false);
|
||||
//--- test 2
|
||||
PrintFormat("%s: Test 2: Testing Constructor of stack with incorrect capacity.",test_name);
|
||||
if(!TestConstructor_Invalid(10))
|
||||
return(false);
|
||||
//--- successful
|
||||
PrintFormat("%s passed",test_name);
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestMisc_Pop. |
|
||||
//+------------------------------------------------------------------+
|
||||
bool TestMisc_Pop(const int count)
|
||||
{
|
||||
//--- create random array
|
||||
int array[];
|
||||
ArrayResize(array,count);
|
||||
for(int i=0; i<count; i++)
|
||||
array[i]=MathRand();
|
||||
//--- create source stack
|
||||
CStack<int>stack_test(array);
|
||||
//--- check elements
|
||||
for(int i=0; i<count; i++)
|
||||
if(stack_test.Pop()!=array[count-1-i])
|
||||
return(false);
|
||||
//--- check count
|
||||
if(stack_test.Count()!=0)
|
||||
return(false);
|
||||
//--- successful
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestMisc_Peek. |
|
||||
//+------------------------------------------------------------------+
|
||||
bool TestMisc_Peek(const int count)
|
||||
{
|
||||
//--- create random array
|
||||
int array[];
|
||||
ArrayResize(array,count);
|
||||
for(int i=0; i<count; i++)
|
||||
array[i]=MathRand();
|
||||
//--- create source stack
|
||||
CStack<int>stack_test(array);
|
||||
//--- check element
|
||||
if(stack_test.Peek()!=array[count-1])
|
||||
return(false);
|
||||
//--- check count
|
||||
if(stack_test.Count()!=count)
|
||||
return(false);
|
||||
//--- successful
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestMisc_Complex. |
|
||||
//+------------------------------------------------------------------+
|
||||
bool TestMisc_Complex(const int count)
|
||||
{
|
||||
//--- create random array
|
||||
int array[];
|
||||
ArrayResize(array,count);
|
||||
for(int i=0; i<count; i++)
|
||||
array[i]=MathRand();
|
||||
//--- create source stack
|
||||
CStack<int>stack_test(count);
|
||||
//--- push values
|
||||
for(int i=0; i<count; i++)
|
||||
if(!stack_test.Push(array[i]))
|
||||
return(false);
|
||||
//--- check count
|
||||
if(stack_test.Count()!=count)
|
||||
return(false);
|
||||
//--- check elements
|
||||
for(int i=0; i<count; i++)
|
||||
{
|
||||
if(array[count-1-i]!=stack_test.Pop())
|
||||
return(false);
|
||||
if(count-i-1!=stack_test.Count())
|
||||
return(false);
|
||||
}
|
||||
//--- recheck
|
||||
stack_test.Push(0);
|
||||
if(stack_test.Pop()!=0)
|
||||
return(false);
|
||||
//--- successful
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestMisc_TrimExcess. |
|
||||
//+------------------------------------------------------------------+
|
||||
bool TestMisc_TrimExcess(const int count)
|
||||
{
|
||||
//--- create random array
|
||||
int array[];
|
||||
ArrayResize(array,count);
|
||||
for(int i=0; i<count; i++)
|
||||
array[i]=MathRand();
|
||||
//--- create source stack
|
||||
CStack<int>stack_test(array);
|
||||
//--- trim
|
||||
stack_test.TrimExcess();
|
||||
int removed=stack_test.Pop();
|
||||
stack_test.TrimExcess();
|
||||
//--- check element
|
||||
if(stack_test.Peek()!=array[count-2])
|
||||
return(false);
|
||||
//--- check count
|
||||
if(stack_test.Count()!=count-1)
|
||||
return(false);
|
||||
//--- trim and clear
|
||||
stack_test.TrimExcess();
|
||||
stack_test.Clear();
|
||||
stack_test.TrimExcess();
|
||||
//--- check count
|
||||
if(stack_test.Count()!=0)
|
||||
return(false);
|
||||
//--- add elemnt and trim
|
||||
stack_test.Add(0);
|
||||
stack_test.TrimExcess();
|
||||
//--- check count
|
||||
if(stack_test.Count()!=1)
|
||||
return(false);
|
||||
//--- successful
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestMisc. |
|
||||
//+------------------------------------------------------------------+
|
||||
bool TestMisc(const string test_name)
|
||||
{
|
||||
PrintFormat("%s started",test_name);
|
||||
//--- test 1
|
||||
PrintFormat("%s: Test 1: Validation of Pop method for all element of the stack and check count after.",test_name);
|
||||
if(!TestMisc_Pop(10))
|
||||
return(false);
|
||||
//--- test 2
|
||||
PrintFormat("%s: Test 2: Validation of Peek method for all element of the stack and check count after.",test_name);
|
||||
if(!TestMisc_Peek(10))
|
||||
return(false);
|
||||
//--- test 3
|
||||
PrintFormat("%s: Test 3: Complex validation of Push and Pop methods.",test_name);
|
||||
if(!TestMisc_Complex(10))
|
||||
return(false);
|
||||
//--- test 4
|
||||
PrintFormat("%s: Test 4: Testing TrimExcess method on the stack after filling, clearing and adding elements.",test_name);
|
||||
if(!TestMisc_TrimExcess(10))
|
||||
return(false);
|
||||
//--- successful
|
||||
PrintFormat("%s passed",test_name);
|
||||
return(true);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| TestStack. |
|
||||
//+------------------------------------------------------------------+
|
||||
void TestStack(int &tests_performed,int &tests_passed)
|
||||
{
|
||||
string test_name="";
|
||||
//--- Constructor functions
|
||||
tests_performed++;
|
||||
test_name="TestConstructor functions test";
|
||||
if(TestConstructor(test_name))
|
||||
tests_passed++;
|
||||
else
|
||||
PrintFormat("%s failed",test_name);
|
||||
|
||||
//--- AddRange functions
|
||||
tests_performed++;
|
||||
test_name="TestMisc functions test";
|
||||
if(TestMisc(test_name))
|
||||
tests_passed++;
|
||||
else
|
||||
PrintFormat("%s failed",test_name);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| Script program start function |
|
||||
//+------------------------------------------------------------------+
|
||||
void OnStart()
|
||||
{
|
||||
MathSrand(0);
|
||||
string package_name="Generic";
|
||||
PrintFormat("Unit tests for Package %s\n",package_name);
|
||||
//--- initial values
|
||||
int tests_performed=0;
|
||||
int tests_passed=0;
|
||||
//--- test distributions
|
||||
TestStack(tests_performed,tests_passed);
|
||||
//--- print statistics
|
||||
PrintFormat("\n%d of %d passed",tests_passed,tests_performed);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
Reference in New Issue
Block a user