Func_queue_stack: Implementation of Queue and Stack in TSL

 

There are situations when WinRunner's lack of more complex data structure support can be an annoyance. Queues (FIFO) and Stacks (LIFO) are 2 of the more common data structures that are used regularly in any programming languages. This implementation is written completely in TSL, and basically provides wrapper functions to access a sequential array in a FIFO (first in first out) or LIFO (last in first out) manner.

A test script (test_queue_stack) has been provided together with the module to verify that the module is working correctly. The test script should also provide a very good example of how to use the queue and stack.

In addition, loading the module will automatically add the functions to the WinRunner function generator.

Implementation Overview

Queue

The queue implementation will create 2 entries in the associative array to maintain pointers to the start and end of the queue ( 'curr' and 'next'). The 'curr' pointer points to the current element in the queue (the one that will be returned when the 'dequeue' function is called); while the 'next' pointer points to the position where the next 'enqueue' should insert the item to. The following diagrams illustrates the operation performed on the array as 2 elements are added to the queue, and then a function call to 'dequeue' is made at the end. The numbers to the left of the queue is the associative index of the array (by defining 'curr' and 'next', the array becomes an associative array).

Queue empty and initialized:
Statement: queue_init (my_queue);

 3  |   |
 2  |   |
 1  |   |
 0  |   | <- curr, next

Queue with 1 element enqueued:
Statement: enqueue (my_queue, "a");

 3  |   |
 2  |   |
 1  |   | <- next
 0  | a | <- curr

Queue with 1 more element enqueued:
Statement: enqueue (my_queue, "b");

 3  |   |
 2  |   | <- next
 1  | b |
 0  | a | <- curr

Queue after a 'dequeue' function call (the function returns "a"):
Statement: dequeue (my_queue, myval); # myval will contain "a"

 3  |   |
 2  |   | <- next
 1  | b | <- curr
 0  |   |

Stack

The stack implementation is a lot simpler than the queue, as only the pointer to the last element is needed. In this case, we use the entry: 'next', which points to the location where the next 'push' to the stack should place the element to. The following diagrams illustrate the operations performed on a stack. The example shows two 'push' operations to the stack, and followed by a 'pop' at the end.

Stack empty and initialized:
Statement: stack_init (my_stack);

 3  |   |
 2  |   |
 1  |   |
 0  |   | <- next
    +---+

Stack with 1 element 'push'ed
Statement: stack_push (my_stack, "a");

 3  |   |
 2  |   |
 1  |   | <- next
 0  | a |
    +---+

Stack with 1 more element 'push'ed
Statement: stack_push (my_stack, "b");

 3  |   |
 2  |   | <- next
 1  | b |
 0  | a |
    +---+

Stack after a 'pop' function call:
Statement: stack_pop (my_stack, myval); # myval will contain "b"

 3  |   |
 2  |   |
 1  |   | <- next
 0  | a |
    +---+

Here is a summary of the features (the functions for queue and stack are similar):

Queue

queue_init
Initializes a queue. The queue will be cleared of all its existing entries.
queue_length
Returns the length of the queue.
enqueue or queue_push
Adds an entry to the end of the queue.
queue_unpop
Adds an entry to the beginning of the queue. (New in 2.1)
dequeue or queue_pop
Remove an entry from the beginning of the queue.
queue_isempty
Whether the queue is empty.

Stack

stack_init
Initializes a stack. The stack will be cleared of all its existing entries.
stack_length
Returns the length of the stack.
stack_push
Adds an entry to the end of the stack.
stack_pop
Remove an entry from the end of the stack.
stack_isempty
Whether the stack is empty.

Differences between version 1 and 2

Other than the addition of several functions ('enqueue', 'dequeue', 'queue_isempty', 'stack_isempty'), the implementation of the queue and stack library has also changed. Previously, all of the functions accepts a string representing the name of the queue or stack; however, in version 2 the functions need to accept the actual array variable directly. I.e.:

Version 1: queue_init ("my_queue");
           queue_push ("my_queue", "value1");

Version 2: queue_init (my_queue);
           queue_push (my_queue, "value1");
where my_queue is an array. If you need to update from version 1 to version 2 of the library, the quickest way would be to remove the double quotes from the first parameter of all the func_queue_stack function calls.

Can't wait to get your hands on it? Get it here:

func_queue_stack.zip


Tech Page @ itechnologist.com