github.com/haraldrudell/parl@v0.4.176/pslices/insert_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  	"golang.org/x/exp/slices"
    12  )
    13  
    14  func TestInsertOrdered(t *testing.T) {
    15  	v1 := 1
    16  	exp := []int{v1}
    17  	exp2 := []int{v1, v1}
    18  
    19  	var slice0 []int
    20  	var slice1 []int
    21  	var slice2 []int
    22  
    23  	slice1 = InsertOrdered(slice0, v1)
    24  	if slices.Compare(slice1, exp) != 0 {
    25  		t.Errorf("bad slice1 %v exp %v", slice1, exp)
    26  	}
    27  
    28  	slice2 = InsertOrdered(slice1, v1)
    29  	if slices.Compare(slice2, exp2) != 0 {
    30  		t.Errorf("bad slice2 %v exp %v", slice1, exp)
    31  	}
    32  
    33  	//	{"insert", args{[]int{1, 3}, 2}, []int{1, 2, 3}},
    34  }
    35  
    36  func TestInsertOrderedFunc(t *testing.T) {
    37  	v1 := 1
    38  	v2 := 2
    39  	exp1 := []int{v1}
    40  	exp2 := []int{v2, v1}
    41  	exp3 := []int{v2, v1, v1}
    42  
    43  	var slice0 []int
    44  	var slice1 []int
    45  	var slice2 []int
    46  	var slice3 []int
    47  	descending := func(a, b int) (result int) {
    48  		if a < b {
    49  			return 1
    50  		} else if a > b {
    51  			return -1
    52  		}
    53  		return 0
    54  	}
    55  
    56  	if slice1 = InsertOrderedFunc(slice0, v1, descending); slices.Compare(slice1, exp1) != 0 {
    57  		t.Errorf("bad slice1 %v exp %v", slice1, exp1)
    58  	}
    59  	if slice2 = InsertOrderedFunc(slice1, v2, descending); slices.Compare(slice2, exp2) != 0 {
    60  		t.Errorf("bad slice2 %v exp %v", slice2, exp2)
    61  	}
    62  	if slice3 = InsertOrderedFunc(slice2, v1, descending); slices.Compare(slice3, exp3) != 0 {
    63  		t.Errorf("bad slice3 %v exp %v", slice3, exp3)
    64  	}
    65  }