//+------------------------------------------------------------------+ //| TestQueue.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 queue CQueuequeue_test1(array); CQueuequeue_test2(GetPointer(list)); CQueuequeue_test3(count); for(int i=0; iqueue_test1(-1); CQueuequeue_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; iqueue_test(array); //--- check elements for(int i=0; iqueue_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; iqueue_test(count); //--- enqueue values for(int i=0; iqueue_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); } //+------------------------------------------------------------------+