Skip to content

The first() operation of Queue returns null when the queue is not empty #101

@alperkocatas

Description

@alperkocatas

I am trying to use the Queue data structure in Alf, with the reference implementation. I can insert and remove elements from the queue, however, when I want to peek the queue using the "first" function, the value returned is null.

Here is a test code which demonstrates the problem:

private import Alf::Library::PrimitiveBehaviors::IntegerFunctions::ToString;
private import Alf::Library::PrimitiveBehaviors::IntegerFunctions::*;
private import Alf::Library::CollectionClasses::Queue;

activity QueueFunctionsTest() {
    Queue<Integer> buffer = new Queue<Integer>();
    buffer.add(1);
    buffer.add(2);
    buffer.add(3);
    
    Integer queueSize = buffer.size();
    if (queueSize != null)
    {
        WriteLine("Queue size is: " + ToString(queueSize));
    }
    
    Integer i = 0;
    while (buffer.size() != 0) {
        WriteLine("Peeking queue...");
        Integer val = buffer.first();
        if (val != null) {
            WriteLine("Value in front of the queue is: " + ToString(val));
        }
        else {
            // Problem: we don't expect to arrive here!
            WriteLine("Ooops: peeked value in front of the queue seems to be null");
        }
        
        WriteLine("Removing first element...");
        val = buffer.removeFirst();
        
        if (val != null) {
            WriteLine("Removed value in front of the queue: " + ToString(val));
        }
    }
}

Output from executing the code is:

Queue size is: 3
Peeking queue...
Ooops: peeked value in front of the queue seems to be null
Removing first element...
Removed value in front of the queue: 1
Peeking queue...
Ooops: peeked value in front of the queue seems to be null
Removing first element...
Removed value in front of the queue: 2
Peeking queue...
Ooops: peeked value in front of the queue seems to be null
Removing first element...
Removed value in front of the queue: 3

I wonder if this is the expected normal behavior, or is this a bug?

Thanks.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions