//+------------------------------------------------------------------+ //| TestLinkedList.mq5 | //| Copyright 2000-2025, MetaQuotes Ltd. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #include #include //+------------------------------------------------------------------+ //| TestAddAfter_Node. | //+------------------------------------------------------------------+ bool TestAddAfter_Node(const int count) { CLinkedListlist_test(); //--- create test arrays int check_array[]; int temp_items[]; int head_items[]; int head_reverse_items[]; int tail_items[]; ArrayResize(temp_items,count); ArrayResize(head_items,count); ArrayResize(head_reverse_items,count); ArrayResize(tail_items,count); for(int i=0; i*node=list_test.AddFirst(head_items[0]); list_test.AddAfter(node,-1); //--- check list_test.CopyTo(check_array); if(check_array[0]!=head_items[0] && check_array[1]!=-1) return(false); //--- clear list_test.Clear(); //--- test node in the head //--- add values list_test.AddFirst(head_items[0]); ArrayCopy(temp_items,head_items); ArrayReverse(temp_items,1,count-1); for(int i=1; ilist_test(); CLinkedListlist_temp(); //--- test verify null node //--- check if(list_test.AddAfter(NULL,0)!=NULL) return(false); if(list_test.Count()!=0) return(false); //--- test verify Node that is a new Node //--- add values list_test.AddLast(0); CLinkedListNodenode(1); //--- check if(list_test.AddAfter(GetPointer(node),-1)!=NULL) return(false); if(list_test.Count()!=1) return(false); //--- clear list_test.Clear(); //--- test verify Node that already exists in another collection //--- add values list_test.AddLast(MathRand()); list_test.AddLast(MathRand()); list_temp.AddLast(MathRand()); list_temp.AddLast(MathRand()); //--- check if(list_test.AddAfter(list_temp.Last(),MathRand())!=NULL) return(false); if(list_test.Count()!=2) return(false); //--- successful return(true); } //+------------------------------------------------------------------+ //| TestAddAfter. | //+------------------------------------------------------------------+ bool TestAddAfter(const string test_name) { PrintFormat("%s started",test_name); //--- test 1 PrintFormat("%s: Test 1: Complex validation of AddAfter method for head node, tail node, some node in the middle, "+ "also testing after remove several element, remove all element and cleaning of list.",test_name); if(!TestAddAfter_Node(16)) return(false); //--- test 2 PrintFormat("%s: Test 2: Validation of AddAfter method for incorrect node.",test_name); if(!TestAddAfter_Node_Negative(16)) return(false); //--- successful PrintFormat("%s passed",test_name); return(true); } //+------------------------------------------------------------------+ //| TestAddBefore_Node. | //+------------------------------------------------------------------+ bool TestAddBefore_Node(const int count) { CLinkedListlist_test(); //--- create test arrays int check_array[]; int temp_items[]; int head_items[]; int head_reverse_items[]; int tail_items[]; int tail_reverse_items[]; ArrayResize(temp_items,count); ArrayResize(head_items,count); ArrayResize(head_reverse_items,count); ArrayResize(tail_items,count); ArrayResize(tail_reverse_items,count); for(int i=0; ilist_test(); CLinkedListlist_temp(); //--- test verify null node //--- check if(list_test.AddBefore(NULL,0)!=NULL) return(false); if(list_test.Count()!=0) return(false); //--- test verify Node that is a new Node //--- add values list_test.AddLast(0); CLinkedListNodenode(1); //--- check if(list_test.AddBefore(GetPointer(node),-1)!=NULL) return(false); if(list_test.Count()!=1) return(false); //--- clear list_test.Clear(); //--- test verify Node that already exists in another collection //--- add values list_test.AddLast(MathRand()); list_test.AddLast(MathRand()); list_temp.AddLast(MathRand()); list_temp.AddLast(MathRand()); //--- check if(list_test.AddBefore(list_temp.Last(),MathRand())!=NULL) return(false); if(list_test.Count()!=2) return(false); //--- successful return(true); } //+------------------------------------------------------------------+ //| TestAddBefore. | //+------------------------------------------------------------------+ bool TestAddBefore(const string test_name) { PrintFormat("%s started",test_name); //--- test 1 PrintFormat("%s: Test 1: Complex validation of AddBefore method for head node, tail node, some node in the middle, "+ "also testing after remove several element, remove all element and cleaning of list.",test_name); if(!TestAddBefore_Node(16)) return(false); //--- test 2 PrintFormat("%s: Test 2: Validation of AddBefore method for incorrect node.",test_name); if(!TestAddBefore_Node_Negative(16)) return(false); //--- successful PrintFormat("%s passed",test_name); return(true); } //+------------------------------------------------------------------+ //| TestAddFirst_Node. | //+------------------------------------------------------------------+ bool TestAddFirst_Node(const int count) { CLinkedListlist_test(); //--- create test arrays int check_array[]; int temp_items[]; int head_items[]; int head_reverse_items[]; int tail_items[]; int tail_reverse_items[]; ArrayResize(temp_items,count); ArrayResize(head_items,count); ArrayResize(head_reverse_items,count); ArrayResize(tail_items,count); ArrayResize(tail_reverse_items,count); for(int i=0; ilist_test(); CLinkedListlist_temp(); //--- test verify null node //--- check if(list_test.AddFirst((CLinkedListNode*)NULL)==true) return(false); if(list_test.Count()!=0) return(false); //--- test verify Node that is a new Node //--- add values list_test.AddLast(0); CLinkedListNodenode(1); //--- check if(list_test.AddFirst(list_test.First())==true) return(false); if(list_test.Count()!=1) return(false); //--- clear list_test.Clear(); //--- test verify Node that already exists in another collection //--- add values list_test.AddLast(MathRand()); list_test.AddLast(MathRand()); //--- check if(list_test.AddFirst(list_test.Last())==true) return(false); if(list_test.Count()!=2) return(false); //--- clear list_test.Clear(); //--- test verify Node that already exists in another collection //--- add values list_test.AddLast(MathRand()); list_test.AddLast(MathRand()); list_temp.AddLast(MathRand()); list_temp.AddLast(MathRand()); //--- check if(list_test.AddFirst(list_temp.Last())==true) return(false); if(list_test.Count()!=2) return(false); //--- successful return(true); } //+------------------------------------------------------------------+ //| TestAddFirst. | //+------------------------------------------------------------------+ bool TestAddFirst(const string test_name) { PrintFormat("%s started",test_name); //--- test 1 PrintFormat("%s: Test 1: Complex validation of AddFirst method after remove several element, remove all, several calling method of clearing.",test_name); if(!TestAddFirst_Node(16)) return(false); //--- test 2 PrintFormat("%s: Test 2: Validation of AddFirst method for incorrect node.",test_name); if(!TestAddFirst_Node_Negative(16)) return(false); //--- successful PrintFormat("%s passed",test_name); return(true); } //+------------------------------------------------------------------+ //| TestAddLast_Node. | //+------------------------------------------------------------------+ bool TestAddLast_Node(const int count) { CLinkedListlist_test(); //--- create test arrays int check_array[]; int temp_items[]; int head_items[]; int head_reverse_items[]; int tail_items[]; int tail_reverse_items[]; ArrayResize(temp_items,count); ArrayResize(head_items,count); ArrayResize(head_reverse_items,count); ArrayResize(tail_items,count); ArrayResize(tail_reverse_items,count); for(int i=0; ilist_test(); CLinkedListlist_temp(); //--- test verify null node //--- check if(list_test.AddLast((CLinkedListNode*)NULL)==true) return(false); if(list_test.Count()!=0) return(false); //--- test verify Node that is a new Node //--- add values list_test.AddLast(0); CLinkedListNodenode(1); //--- check if(list_test.AddLast(list_test.First())==true) return(false); if(list_test.Count()!=1) return(false); //--- clear list_test.Clear(); //--- test verify Node that already exists in another collection //--- add values list_test.AddLast(MathRand()); list_test.AddLast(MathRand()); //--- check if(list_test.AddLast(list_test.Last())==true) return(false); if(list_test.Count()!=2) return(false); //--- clear list_test.Clear(); //--- test verify Node that already exists in another collection //--- add values list_test.AddLast(MathRand()); list_test.AddLast(MathRand()); list_temp.AddLast(MathRand()); list_temp.AddLast(MathRand()); //--- check if(list_test.AddLast(list_temp.Last())==true) return(false); if(list_test.Count()!=2) return(false); //--- successful return(true); } //+------------------------------------------------------------------+ //| TestAddLast. | //+------------------------------------------------------------------+ bool TestAddLast(const string test_name) { PrintFormat("%s started",test_name); //--- test 1 PrintFormat("%s: Test 1: Complex validation of AddLast method after remove several element, remove all, several calling method of clearing.",test_name); if(!TestAddLast_Node(16)) return(false); //--- test 2 PrintFormat("%s: Test 2: Validation of AddLast method for incorrect node.",test_name); if(!TestAddLast_Node_Negative(16)) return(false); //--- successful PrintFormat("%s passed",test_name); return(true); } //+------------------------------------------------------------------+ //| TestConstructor_Complex. | //+------------------------------------------------------------------+ bool TestConstructor_Complex(const int count) { //--- create source array and list int array[]; ArrayResize(array,count); for(int i=0; ilist(array); //--- crete linked list CLinkedListllist1(array); CLinkedListllist2(GetPointer(list)); CLinkedListllist3(GetPointer(llist1)); CLinkedListllist4(NULL); //--- check int check_array1[]; llist1.CopyTo(check_array1); if(ArrayCompare(check_array1,array)!=0) return(false); int check_array2[]; llist1.CopyTo(check_array2); if(ArrayCompare(check_array2,array)!=0) return(false); int check_array3[]; llist1.CopyTo(check_array3); if(ArrayCompare(check_array3,array)!=0) return(false); if(llist4.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: Complex validation constructor of linked list dased on array, several ICollection objects and NULL object.",test_name); if(!TestConstructor_Complex(16)) return(false); //--- successful PrintFormat("%s passed",test_name); return(true); } //+------------------------------------------------------------------+ //| TestMisc_Find. | //+------------------------------------------------------------------+ bool TestMisc_Find(const int count) { CLinkedListlist_test(); //--- create test arrays int check_array[]; int head_items[]; int tail_items[]; int head_prepend_items[]; int tail_prepend_items[]; ArrayResize(head_items,count); ArrayResize(tail_items,count); ArrayResize(head_prepend_items,count+1); ArrayResize(tail_prepend_items,count+1); head_prepend_items[0]=-1; tail_prepend_items[0]=-1; for(int i=0; ilist_test(); //--- create test arrays int check_array[]; int head_items[]; int tail_items[]; int head_prepend_items[]; int tail_prepend_items[]; ArrayResize(head_items,count); ArrayResize(tail_items,count); ArrayResize(head_prepend_items,count+1); ArrayResize(tail_prepend_items,count+1); head_prepend_items[0]=-1; tail_prepend_items[0]=-1; for(int i=0; ilist_test(); int temp_value1; int temp_value2; int temp_value3; //--- create test arrays int check_array[]; int head_items[]; int tail_items[]; int temp_items[]; ArrayResize(head_items,count); ArrayResize(tail_items,count); for(int i=0; i*nodes[]; CLinkedListNode*current_node=list_test.First(); int index=0; ArrayResize(nodes,count*2); while(index*)NULL) && list_test.Remove(0)) return(false); //--- test verify Node that is a new Node //--- add values list_test.AddLast(0); CLinkedListNodenode(1); //--- check if(list_test.Remove(GetPointer(node))) return(false); //--- clear list_test.Clear(); //--- test verify Node that already exists in another collection list_test.AddLast(MathRand()); list_test.AddLast(MathRand()); CLinkedListother(); other.AddLast(MathRand()); other.AddLast(MathRand()); if(list_test.Remove(other.Last())) return(false); //--- successful return(true); } //+------------------------------------------------------------------+ //| TestMisc_RemoveFirst. | //+------------------------------------------------------------------+ bool TestMisc_RemoveFirst(const int count) { CLinkedListlist_test(); int temp_value1; int temp_value2; int temp_value3; //--- create test arrays int check_array[]; int head_items[]; int tail_items[]; int temp_items[]; ArrayResize(head_items,count); ArrayResize(tail_items,count); ArrayResize(temp_items,count); for(int i=0; ilist_test(); int temp_value1; int temp_value2; int temp_value3; //--- create test arrays int check_array[]; int head_items[]; int tail_items[]; int temp_items[]; ArrayResize(head_items,count); ArrayResize(tail_items,count); ArrayResize(temp_items,count); for(int i=0; i