github.com/go-board/x-go@v0.1.2-0.20220610024734-db1323f6cb15/xcontainer/priority_queue/queue_test.go (about)

     1  package priority_queue
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/go-board/x-go/types"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestStringPriorityQueue(t *testing.T) {
    12  	t.Run("max", func(t *testing.T) {
    13  		q := NewStringPriorityQueue(true, "1", "2", "3")
    14  		t.Logf("%+v\n", q.h)
    15  		assert.Equal(t, "3", q.Pop())
    16  	})
    17  
    18  	t.Run("min", func(t *testing.T) {
    19  		q := NewStringPriorityQueue(false, "1", "2", "3")
    20  		assert.Equal(t, "1", q.Pop())
    21  	})
    22  }
    23  
    24  func TestInt64PriorityQueue(t *testing.T) {
    25  	t.Run("max", func(t *testing.T) {
    26  		q := NewInt64PriorityQueue(true, 1, 3, 5, 2, 4, 6)
    27  		assert.Equal(t, q.Pop(), int64(6))
    28  	})
    29  
    30  	t.Run("min", func(t *testing.T) {
    31  		q := NewInt64PriorityQueue(false, 1, 3, 5, 2, 4, 6)
    32  		assert.Equal(t, q.Pop(), int64(1))
    33  	})
    34  }
    35  
    36  type String string
    37  
    38  func (s String) Compare(o types.Comparable) types.Ordering {
    39  	if s < o.(String) {
    40  		return types.OrderingLess
    41  	} else if s > o.(String) {
    42  		return types.OrderingGreater
    43  	}
    44  	return types.OrderingEqual
    45  }
    46  
    47  func TestComparablePriorityQueue(t *testing.T) {
    48  	t.Run("max", func(t *testing.T) {
    49  		q := NewComparablePriorityQueue(true, String("1"), String("4"), String("3"), String("2"))
    50  		assert.Equal(t, q.Pop(), String("4"))
    51  	})
    52  
    53  	t.Run("min", func(t *testing.T) {
    54  		q := NewComparablePriorityQueue(false, String("1"), String("4"), String("3"), String("2"))
    55  		assert.Equal(t, q.Pop(), String("1"))
    56  	})
    57  }