blob: 76468495bfd2cbf7078ee41f201b0a78f9383237 [file] [log] [blame]
///////////////////////////////////////////////////////////////////////////////
// //
// Copyright (c) 2000-2019 Ericsson Telecom AB //
// //
// All rights reserved. This program and the accompanying materials //
// are made available under the terms of the Eclipse Public License v2.0 //
// which accompanies this distribution, and is available at //
// https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html //
///////////////////////////////////////////////////////////////////////////////
module IntegerRingBuffer_TestCases
{
import from EPTF_CLL_RingBuffer_Definitions all;
import from EPTF_CLL_RingBuffer_PrivateFunctions all;
import from EPTF_CLL_IntegerRingBuffer_Functions all;
type component MTC_CT {}
// helper method - dumps a ring buffer to simple array and compares it with an expected array
function f_checkBuffer(
in EPTF_IntegerRingBuffer checkedRingBuffer,
in EPTF_RingBufferDataList expectedBuffer)
{
var EPTF_RingBufferDataList dump;
f_EPTF_RB_dump(checkedRingBuffer, dump);
if (dump == expectedBuffer)
{
setverdict(pass);
}
else
{
setverdict(fail);
};
}
// test ring buffer initialization
testcase tc_RB_init() runs on MTC_CT system MTC_CT
{
var EPTF_IntegerRingBuffer rb1, rb2, rb3, rb4;
// initialise a ring buffer and check capacity and empty content
f_EPTF_RB_init(rb1, 238);
if (238 == f_EPTF_RB_capacity(rb1))
{
setverdict(pass);
}
else
{
setverdict(fail);
}
f_checkBuffer(rb1, {});
// initialize to default capacity
f_EPTF_RB_init(rb2);
// initialize to invalid capacity values
f_EPTF_RB_init(rb3, 0);
f_EPTF_RB_init(rb4, -100);
// check that ring buffers are initialized to default capacity
if (rb2 == rb3 and rb2 == rb4 and c_EPTF_RB_default_capacity == f_EPTF_RB_capacity(rb4))
{
setverdict(pass);
}
else
{
setverdict(fail);
}
// check that ring buffers are empty
f_checkBuffer(rb2, {});
}
// test empty and size methods
testcase tc_RB_size() runs on MTC_CT system MTC_CT
{
var EPTF_IntegerRingBuffer rb;
// new ring buffer shall be empty
f_EPTF_RB_init(rb, 5);
if (f_EPTF_RB_empty(rb) and 0 == f_EPTF_RB_size(rb))
{
setverdict(pass);
}
else
{
setverdict(fail);
}
// after adding 3 items buffer is not empty and shall contain 3 items
f_EPTF_RB_push_back(rb, 1);
f_EPTF_RB_push_back(rb, 1);
f_EPTF_RB_push_back(rb, 1);
if (not f_EPTF_RB_empty(rb) and 3 == f_EPTF_RB_size(rb))
{
setverdict(pass);
}
else
{
setverdict(fail);
}
// after adding 3 more items buffer shall contain 5 items
f_EPTF_RB_push_back(rb, 1);
f_EPTF_RB_push_back(rb, 1);
f_EPTF_RB_push_back(rb, 1);
if (5 == f_EPTF_RB_size(rb))
{
setverdict(pass);
}
else
{
setverdict(fail);
}
// after removing 2 items buffer shall contain 3 items
f_EPTF_RB_pop_front(rb);
f_EPTF_RB_pop_front(rb);
if (3 == f_EPTF_RB_size(rb))
{
setverdict(pass);
}
else
{
setverdict(fail);
}
// after clear operation buffer shall be empty
f_EPTF_RB_clear(rb);
if (f_EPTF_RB_empty(rb) and 0 == f_EPTF_RB_size(rb))
{
setverdict(pass);
}
else
{
setverdict(fail);
}
}
// test adding and removing elements to/from ring buffer
testcase tc_RB_push_pop() runs on MTC_CT system MTC_CT
{
var EPTF_IntegerRingBuffer buffer;
var EPTF_RingBufferDataList myData;
// init buffer with capacity of 5 elements...
f_EPTF_RB_init(buffer, 5);
//////////////////////////////////////////////
// Test push_back() and pop_front() //
// push 3 items into buffer and check content...
f_EPTF_RB_push_back(buffer, 1);
f_EPTF_RB_push_back(buffer, 2);
f_EPTF_RB_push_back(buffer, 3);
myData := {1, 2, 3};
f_checkBuffer(buffer,myData);
// pop 2 items from buffer and check content...
f_EPTF_RB_pop_front(buffer);
f_EPTF_RB_pop_front(buffer);
myData := {3};
f_checkBuffer(buffer,myData);
// push 6 items into buffer and check content...
f_EPTF_RB_push_back(buffer, 4);
f_EPTF_RB_push_back(buffer, 5);
f_EPTF_RB_push_back(buffer, 6);
f_EPTF_RB_push_back(buffer, 7);
f_EPTF_RB_push_back(buffer, 8);
f_EPTF_RB_push_back(buffer, 9);
myData := {5, 6, 7, 8, 9};
f_checkBuffer(buffer,myData);
// pop 3 items from buffer and check content...
f_EPTF_RB_pop_front(buffer);
f_EPTF_RB_pop_front(buffer);
f_EPTF_RB_pop_front(buffer);
myData := {8, 9};
f_checkBuffer(buffer,myData);
// pop other 2 items from buffer and check buffer is empty...
f_EPTF_RB_pop_front(buffer);
f_EPTF_RB_pop_front(buffer);
myData := {};
f_checkBuffer(buffer,myData);
// pop from empty buffer shall not couse ant effect...
f_EPTF_RB_pop_front(buffer);
myData := {};
f_checkBuffer(buffer,myData);
//////////////////////////////////////////////
// Test push_front() and pop_back() //
// push 3 items into buffer and check content...
f_EPTF_RB_push_front(buffer, 2);
f_EPTF_RB_push_back(buffer, 3);
f_EPTF_RB_push_front(buffer, 1);
myData := {1, 2, 3};
f_checkBuffer(buffer,myData);
// pop 2 items from buffer and check content...
f_EPTF_RB_pop_back(buffer);
f_EPTF_RB_pop_back(buffer);
myData := {1};
f_checkBuffer(buffer,myData);
// push 6 items into buffer and check content...
f_EPTF_RB_push_front(buffer, 2);
f_EPTF_RB_push_front(buffer, 3);
f_EPTF_RB_push_front(buffer, 4);
f_EPTF_RB_push_front(buffer, 5);
f_EPTF_RB_push_front(buffer, 6);
f_EPTF_RB_push_front(buffer, 7);
myData := {7, 6, 5, 4, 3};
f_checkBuffer(buffer,myData);
// pop 3 items from buffer and check content...
f_EPTF_RB_pop_back(buffer);
f_EPTF_RB_pop_back(buffer);
f_EPTF_RB_pop_back(buffer);
myData := {7, 6};
f_checkBuffer(buffer,myData);
// pop other 2 items from buffer and check buffer is empty...
f_EPTF_RB_pop_back(buffer);
f_EPTF_RB_pop_back(buffer);
myData := {};
f_checkBuffer(buffer,myData);
// pop from empty buffer shall not couse ant effect...
f_EPTF_RB_pop_back(buffer);
myData := {};
f_checkBuffer(buffer,myData);
}
// test accessing elements of ring buffer
testcase tc_RB_access() runs on MTC_CT system MTC_CT
{
var EPTF_IntegerRingBuffer rb;
var EPTF_RingBufferDataItem item;
// init ring buffer
f_EPTF_RB_init(rb, 5);
// checked access to empty buffer shall return false
if (not f_EPTF_RB_at(rb, 0, item))
{
setverdict(pass);
}
else
{
setverdict(fail);
}
// push an item into buffer...
f_EPTF_RB_push_back(rb, 42);
// verify checked access...
if (f_EPTF_RB_at(rb, 0, item) and 42 == item)
{
setverdict(pass);
}
else
{
setverdict(fail);
}
// verify checked access at invalid index...
if ( not f_EPTF_RB_at(rb, -100, item) and not f_EPTF_RB_at(rb, 2, item) and not f_EPTF_RB_at(rb, 5, item) )
{
setverdict(pass);
}
else
{
setverdict(fail);
}
// verify unchecked access...
if (42 == f_EPTF_RB_front(rb) and 42 == f_EPTF_RB_back(rb) and 42 == f_EPTF_RB_get(rb, 0))
{
setverdict(pass);
}
else
{
setverdict(fail);
}
// push 6 items into buffer and pop 2 item
// rb.data should contain: {5,6,x,x,4}
f_EPTF_RB_push_back(rb, 1);
f_EPTF_RB_push_back(rb, 2);
f_EPTF_RB_push_back(rb, 3);
f_EPTF_RB_push_back(rb, 4);
f_EPTF_RB_push_back(rb, 5);
f_EPTF_RB_push_back(rb, 6);
f_EPTF_RB_pop_front(rb);
f_EPTF_RB_pop_front(rb);
// check access...
if ( 4 == f_EPTF_RB_front(rb) and
6 == f_EPTF_RB_back(rb) and
4 == f_EPTF_RB_get(rb, 0) and
6 == f_EPTF_RB_get(rb, 2) )
{
setverdict(pass);
}
else
{
setverdict(fail);
}
}
// test solution for HK72355
// - check RB works fine with capacity=1 (head points to valid place after init)
// - check no unbound value at beginnning of data buffer after push_bach if capacity>1
testcase tc_RB_HK72355() runs on MTC_CT system MTC_CT
{
var EPTF_IntegerRingBuffer rb;
// check RB with capacity=1
// calling front after init and push_back caused dynamic error in case capacity=1
f_EPTF_RB_init(rb, 1);
f_EPTF_RB_push_back(rb, 42);
if (42 == f_EPTF_RB_front(rb) and 42 == f_EPTF_RB_back(rb))
{
setverdict(pass);
}
else
{
setverdict(fail);
}
// inverse test: back after push_front
f_EPTF_RB_init(rb, 1);
f_EPTF_RB_push_front(rb, 4242);
if (4242 == f_EPTF_RB_back(rb) and 4242 == f_EPTF_RB_front(rb))
{
setverdict(pass);
}
else
{
setverdict(fail);
}
// adding more values
f_EPTF_RB_push_front(rb, 1);
f_EPTF_RB_push_back(rb, 2);
f_EPTF_RB_push_back(rb, 3);
if (3 == f_EPTF_RB_back(rb) and 3 == f_EPTF_RB_front(rb))
{
setverdict(pass);
}
else
{
setverdict(fail);
}
// check length of data buffer
if (lengthof(rb.data) <= 1)
{
setverdict(pass);
}
else
{
setverdict(fail);
}
// check RB with capacity=2
// check that data_buffer[0] is not unbound after first push_back
// NOTE: data can contain unbound items in other cases, e.g. after push_front!
f_EPTF_RB_init(rb, 2);
f_EPTF_RB_push_back(rb, 42);
if ( 42 == f_EPTF_RB_front(rb) and 42 == f_EPTF_RB_back(rb) and //< check content correct
rb.data[0] == 42 and //< check 0. item not unbound
lengthof(rb.data) <= 2)
{
setverdict(pass);
}
else
{
setverdict(fail);
}
// check RB with capacity>2
// check that data_buffer[0] is not unbound after first push_back
// NOTE: data can contain unbound items in other cases, e.g. after push_front!
f_EPTF_RB_init(rb, 5);
f_EPTF_RB_push_back(rb, 42);
if ( 42 == f_EPTF_RB_front(rb) and 42 == f_EPTF_RB_back(rb) and //< check content correct
rb.data[0] == 42 and //< check 0. item not unbound
lengthof(rb.data) <= 5)
{
setverdict(pass);
}
else
{
setverdict(fail);
}
}
control
{
execute(tc_RB_init());
execute(tc_RB_size());
execute(tc_RB_push_pop());
execute(tc_RB_access());
execute(tc_RB_HK72355());
}
} //module