//+------------------------------------------------------------------+ //| TestRedBlackTree.mq5 | //| Copyright 2000-2025, MetaQuotes Ltd. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #include #include #include //+------------------------------------------------------------------+ //| TestMisc_Constructor. | //+------------------------------------------------------------------+ bool TestMisc_Constructor(const int count) { //--- create list CArrayListlist(count); for(int i=0; icomparer(); CRedBlackTreetree_test1(GetPointer(list),GetPointer(comparer)); CRedBlackTreetree_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 CHashSetset(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; ilist(count); for(int i=0; itree_test(GetPointer(list)); //--- check for(int i=0; ilist(count); for(int i=0; itree_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 CArrayListlist(count); for(int i=0; itree_test(GetPointer(list)); //--- get max list.Sort(); CRedBlackTreeNode*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 CArrayListlist(count); for(int i=0; itree_test(GetPointer(list)); //--- get max list.Sort(); CRedBlackTreeNode*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 CArrayListlist(count); for(int i=0; itree_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*node=tree_test.FindMax(); if(!tree_test.Remove(node)) return(false); } //--- get unique sorted values CHashSetset(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 CArrayListlist(count); for(int i=0; itree_test(GetPointer(list)); //--- get unique sorted values CHashSetset(GetPointer(list)); int expected[]; set.CopyTo(expected); ArraySort(expected); //--- check for(int i=0; ilist(count); for(int i=0; itree_test(GetPointer(list)); //--- get unique sorted values CHashSetset(GetPointer(list)); int expected[]; set.CopyTo(expected); ArraySort(expected); //--- check for(int i=0; i