Files
tidb/pkg/statistics/handle/autoanalyze/priorityqueue/queue_test.go
2024-02-05 07:55:13 +00:00

82 lines
2.4 KiB
Go

// Copyright 2024 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package priorityqueue
import (
"container/heap"
"testing"
"github.com/stretchr/testify/require"
)
func TestAnalysisInnerQueue(t *testing.T) {
// Test data
job1 := &TableAnalysisJob{Weight: 10}
job2 := &TableAnalysisJob{Weight: 5}
job3 := &TableAnalysisJob{Weight: 15}
// Create an empty priority queue
queue := analysisInnerQueue{}
// Push items into the queue
heap.Push(&queue, job1)
heap.Push(&queue, job2)
heap.Push(&queue, job3)
// Test Len()
require.Equal(t, 3, queue.Len(), "Length of the queue should be 3")
// Test Less()
require.True(t, queue.Less(0, 1), "Item at index 0 should have higher priority than item at index 1")
// Test Swap()
queue.Swap(0, 2)
require.NotEqual(t, float64(15), queue[0].Weight, "Item at index 0 should not have weight 15 after swap")
}
func TestPushPopAnalysisInnerQueue(t *testing.T) {
// Test Push and Pop operations together
queue := analysisInnerQueue{}
heap.Push(&queue, &TableAnalysisJob{Weight: 10})
heap.Push(&queue, &TableAnalysisJob{Weight: 5})
poppedItem := heap.Pop(&queue).(*TableAnalysisJob)
require.Equal(t, float64(10), poppedItem.Weight, "Popped item should have weight 10")
require.Equal(t, 1, queue.Len(), "After Pop, length of the queue should be 1")
}
func TestAnalysisPriorityQueue(t *testing.T) {
// Test data
job1 := &TableAnalysisJob{Weight: 10}
job2 := &TableAnalysisJob{Weight: 5}
job3 := &TableAnalysisJob{Weight: 15}
// Create a priority queue
queue := NewAnalysisPriorityQueue()
// Push items into the queue
queue.Push(job1)
queue.Push(job2)
queue.Push(job3)
// Test Len()
require.Equal(t, 3, queue.Len(), "Length of the queue should be 3")
// Test Pop()
poppedItem := queue.Pop()
require.Equal(t, float64(15), poppedItem.Weight, "Popped item should have weight 15")
require.Equal(t, 2, queue.Len(), "After Pop, length of the queue should be 2")
}