github.com/haraldrudell/parl@v0.4.176/pslices/ordered-pointers_test.go (about)

     1  /*
     2  © 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  package pslices
     7  
     8  import (
     9  	"testing"
    10  
    11  	"github.com/haraldrudell/parl/parli"
    12  	"golang.org/x/exp/slices"
    13  )
    14  
    15  func TestNewOrderedPointers(t *testing.T) {
    16  	v1 := 1
    17  	v1p := &v1
    18  	v2 := 2
    19  	v2p := &v2
    20  	exp := []*int{v1p, v1p, v2p}
    21  
    22  	var orderedPointers parli.OrderedPointers[int]
    23  	var actual []*int
    24  	cmp := func(a, b *int) (result int) {
    25  		if *a < *b {
    26  			return -1
    27  		} else if *a > *b {
    28  			return 1
    29  		}
    30  		return 0
    31  	}
    32  
    33  	orderedPointers = NewOrderedPointers[int]()
    34  
    35  	orderedPointers.Insert(v2p)
    36  	orderedPointers.Insert(v1p)
    37  	orderedPointers.Insert(v1p)
    38  	actual = orderedPointers.List()
    39  	if slices.CompareFunc(actual, exp, cmp) != 0 {
    40  		t.Errorf("bad slice: %v exp %v", ResolveSlice(orderedPointers.List()), ResolveSlice(exp))
    41  	}
    42  }