//+------------------------------------------------------------------+ //| TestSortedMap.mq5 | //| Copyright 2000-2025, MetaQuotes Ltd. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #include #include //+------------------------------------------------------------------+ //| TestMisc_Constructor. | //+------------------------------------------------------------------+ bool TestMisc_Constructor(const int count) { //--- create arrays int keys[]; string values[]; ArrayResize(keys,count); ArrayResize(values,count); for(int i=0; icomparer(); CSortedMapmap_test(GetPointer(comparer)); for(int i=0; imap_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; imap_test(); for(int i=0; imap_test(); for(int i=0; i*pairs[]; int size=map_test.CopyTo(pairs); //--- check CDefaultComparer*>comparer(); CArrayList*>expected(pairs); expected.Sort(GetPointer(comparer)); int actual_keys[]; string actual_values[]; map_test.CopyTo(actual_keys,actual_values); for(int i=0; i*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*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); } //+------------------------------------------------------------------+