package test import ( "reflect" "testing" "git.wens.org.uk/heapqueue/pkg/queue" ) func TestQueue(t *testing.T) { // building input := []queue.QueueObject[int, string]{{Key: 0, Value: "task a"}, {Key: 5, Value: "task b"}} q := queue.BuildQueue(input, queue.MAXQUEUE) if !queue.IsMaxQueue(q) { t.Fatalf("Failed to build max queue. Queue object: %v\n", &q) } // maximum key max, err := queue.Maximum(q) if err != nil { t.Errorf(err.Error()) } if max.Key != 5 || max.Value != "task b" { t.Errorf("Unexpected value of %v for max. Expected {5, \"task b\"}\n", max.Key) } // increasing key to new maximum queue.IncreaseKey(q, queue.QueueObject[int, string]{Key: 0, Value: "task a"}, 10) max, err = queue.Maximum(q) if err != nil { t.Errorf(err.Error()) } if max.Key != 10 || max.Value != "task a" { t.Errorf("Failed to increase key to 10\n") } // inserting in between queue.Insert(q, queue.QueueObject[int, string]{Key: 7, Value: "task c"}) // extracting maximum maxExtracted, err := queue.ExtractMaximum(q) if err != nil { t.Errorf(err.Error()) } if !reflect.DeepEqual(max, maxExtracted) { t.Errorf("Extracted maximum not equal to peeked maximum, %v and %v\n", max, maxExtracted) } // check new maximum is the inserted value max, err = queue.ExtractMaximum(q) if err != nil { t.Errorf(err.Error()) } if max.Key != 7 || max.Value != "task c" { t.Errorf("Unexpected maximum after extraction, expected {7 \"task c\"}, got %v\n", max) } }