//+------------------------------------------------------------------+ //| TestStack.mq5 | //| Copyright 2000-2025, MetaQuotes Ltd. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #include #include //+------------------------------------------------------------------+ //| TestConstructor_Valid. | //+------------------------------------------------------------------+ bool TestConstructor_Valid(const int count) { //--- create random array int array[]; ArrayResize(array,count); for(int i=0; ilist(array); //--- create source stack CStackstack_test1(array); CStackstack_test2(GetPointer(list)); CStackstack_test3(count); for(int i=0; istack_test1(-1); CStackstack_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; istack_test(array); //--- check elements for(int i=0; istack_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; istack_test(count); //--- push values for(int i=0; istack_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); } //+------------------------------------------------------------------+