github.com/igggame/nebulas-go@v2.1.0+incompatible/common/pdeque/pdeque_test.go (about)

     1  package pdeque
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestPdeq_1(t *testing.T) {
    10  	tests := []struct {
    11  		name string
    12  		val  int
    13  	}{
    14  		{"31", 31},
    15  		{"46", 46},
    16  		{"51", 51},
    17  		{"10", 10},
    18  		{"30", 30},
    19  		{"21", 21},
    20  		{"71", 71},
    21  		{"41", 41},
    22  		{"11", 11},
    23  		{"13", 13},
    24  		{"16", 16},
    25  		{"8", 8},
    26  	}
    27  	q := NewPriorityDeque(func(a interface{}, b interface{}) bool { return a.(int) < b.(int) })
    28  	for _, tt := range tests {
    29  		t.Run(tt.name, func(t *testing.T) {
    30  			q.Insert(tt.val)
    31  		})
    32  	}
    33  	assert.Equal(t, q.PopMax(), 71)
    34  	assert.Equal(t, q.PopMin(), 8)
    35  	assert.Equal(t, q.PopMin(), 10)
    36  	assert.Equal(t, q.PopMax(), 51)
    37  	assert.Equal(t, q.PopMin(), 11)
    38  	assert.Equal(t, q.PopMin(), 13)
    39  	assert.Equal(t, q.PopMin(), 16)
    40  	assert.Equal(t, q.PopMax(), 46)
    41  	assert.Equal(t, q.PopMax(), 41)
    42  	assert.Equal(t, q.PopMax(), 31)
    43  	assert.Equal(t, q.PopMax(), 30)
    44  }
    45  
    46  func TestPdeq_2(t *testing.T) {
    47  	tests := []struct {
    48  		name string
    49  		val  int
    50  	}{
    51  		{"1", 1},
    52  		{"2", 2},
    53  		{"0", 0},
    54  		{"4", 4},
    55  		{"5", 5},
    56  	}
    57  	q := NewPriorityDeque(func(a interface{}, b interface{}) bool { return a.(int) < b.(int) })
    58  	for _, tt := range tests {
    59  		t.Run(tt.name, func(t *testing.T) {
    60  			q.Insert(tt.val)
    61  		})
    62  	}
    63  	assert.Equal(t, q.PopMin(), 0)
    64  	assert.Equal(t, q.PopMin(), 1)
    65  	assert.Equal(t, q.PopMin(), 2)
    66  	assert.Equal(t, q.PopMin(), 4)
    67  	assert.Equal(t, q.PopMin(), 5)
    68  }