implement and test the Peek for the RecordQueue type
All checks were successful
Tests / tests (pull_request) Successful in 6s

This commit is contained in:
2025-11-13 13:58:07 +00:00
parent 7800b1163b
commit 93689754be
2 changed files with 58 additions and 10 deletions

View File

@@ -14,6 +14,7 @@ func TestRecordQueue(t *testing.T) {
}
var rq RecordQueue
if rq.Len() != 0 {
t.Fatalf("zero value should have zero lenght")
}
@@ -23,6 +24,11 @@ func TestRecordQueue(t *testing.T) {
t.Fatalf("Pop() should return (_,false) on a zero value")
}
_, ok = rq.Peek()
if ok {
t.Fatalf("Peek() should return (_,false) on a zero value")
}
rq.Push(nil)
if rq.Len() != 0 {
t.Fatalf("pushing nil should be a no-op")
@@ -38,18 +44,39 @@ func TestRecordQueue(t *testing.T) {
t.Fatalf("pushing 2nd record should result in lenght of 2")
}
rec, ok := rq.Pop()
peekRec, ok := rq.Peek()
if !ok {
t.Fatalf("Peek() should return (_,true) when the list is not empty")
}
if peekRec, ok := peekRec.(testRecord); ok {
if peekRec.id != 1 {
t.Fatalf("Peek() should return the 1st record pushed but returned %d", peekRec.id)
}
} else {
t.Fatalf("Peek() should return the original record type")
}
if rq.Len() != 2 {
t.Fatalf("Peek() should not affect the list length")
}
popRec, ok := rq.Pop()
if !ok {
t.Fatalf("Pop() should return (_,true) when the list is not empty")
}
if rec, ok := rec.(testRecord); ok {
if rec, ok := popRec.(testRecord); ok {
if rec.id != 1 {
t.Fatalf("Pop() should return the first record pushed but returned %d", rec.id)
}
} else {
t.Fatalf("Pop() should return the original record")
}
if rq.Len() != 1 {
t.Fatalf("Pop() should remove an element from the list")
}
}
func TestRecordQueueNilReceiver(t *testing.T) {
@@ -59,7 +86,12 @@ func TestRecordQueueNilReceiver(t *testing.T) {
t.Fatalf("nil receiver should have zero lenght")
}
_, ok := rq.Pop()
_, ok := rq.Peek()
if ok {
t.Fatalf("Peek() on a nil receiver should return (_,false)")
}
_, ok = rq.Pop()
if ok {
t.Fatalf("Pop() on a nil receiver should return (_,false)")
}