package slicesx import ( "testing" ) func TestLast(t *testing.T) { var v any var e any var intSl []int var strSl []string var boolSl []bool var anySl []any /* int */ // nil v = Last(intSl) e = 0 if v != e { t.Errorf("%T %#v, expected %T %#v", v, v, e, e) } // empty intSl = make([]int, 0) v = Last(intSl) if v != e { // still expect 0 t.Errorf("%T %#v, expected %T %#v", v, v, e, e) } // populated intSl = []int{0, 1, 2, 3} v = Last(intSl) e = 3 if v != e { t.Errorf("%T %#v, expected %T %#v", v, v, e, e) } /* string */ // nil v = Last(strSl) e = "" if v != e { t.Errorf("%T %#v, expected %T %#v", v, v, e, e) } // empty strSl = make([]string, 0) v = Last(strSl) if v != e { // still expect "" t.Errorf("%T %#v, expected %T %#v", v, v, e, e) } // populated strSl = []string{"a", "b", "c"} v = Last(strSl) e = "c" if v != e { t.Errorf("%T %#v, expected %T %#v", v, v, e, e) } /* bool */ // nil v = Last(boolSl) e = false if v != e { t.Errorf("%T %#v, expected %T %#v", v, v, e, e) } // empty boolSl = make([]bool, 0) v = Last(boolSl) if v != e { // still expect false t.Errorf("%T %#v, expected %T %#v", v, v, e, e) } // populated boolSl = []bool{true, false, true, false, true} v = Last(boolSl) e = true if v != e { t.Errorf("%T %#v, expected %T %#v", v, v, e, e) } /* any/interface{} */ // nil v = Last(anySl) e = nil if v != e { t.Errorf("%T %#v, expected %T %#v", v, v, e, e) } // empty anySl = make([]any, 0) v = Last(anySl) if v != e { // still expect nil t.Errorf("%T %#v, expected %T %#v", v, v, e, e) } // populated anySl = []any{3, true, "foo", "bar"} v = Last(anySl) e = "bar" if v != e { t.Errorf("%T %#v, expected %T %#v", v, v, e, e) } } /* this test only runs in Go 1.26+; which lets you new() on values instead of just types. commented out until req bumps to 1.26+ in go.mod. */ /* func TestLastPtr(t *testing.T) { var v any var i *int var s *string var b *bool var ptrIntSl []*int var ptrStrSl []*string var ptrBoolSl []*bool // // int // // nil v = Last(ptrIntSl) i = nil if v != i { t.Errorf("%T %#v, expected %T %#v", v, v, i, i) } // empty ptrIntSl = make([]*int, 0) v = Last(ptrIntSl) if v != i { // still expect nil t.Errorf("%T %#v, expected %T %#v", v, v, i, i) } // populated ptrIntSl = []*int{new(0), new(1), new(2), new(3)} v = Last(ptrIntSl) i = ptrIntSl[len(ptrIntSl)-1] if v != i { t.Errorf("%T %#v, expected %T %#v", v, v, i, i) } // // string // // nil v = Last(ptrStrSl) if v != s { t.Errorf("%T %#v, expected %T %#v", v, v, s, s) } // empty ptrStrSl = make([]*string, 0) v = Last(ptrStrSl) if v != s { // still expect nil t.Errorf("%T %#v, expected %T %#v", v, v, s, s) } // populated ptrStrSl = []*string{new("a"), new("b"), new("c")} v = Last(ptrStrSl) s = ptrStrSl[len(ptrStrSl)-1] if v != s { t.Errorf("%T %#v, expected %T %#v", v, v, s, s) } // // bool // // nil v = Last(ptrBoolSl) if v != b { t.Errorf("%T %#v, expected %T %#v", v, v, b, b) } // empty ptrBoolSl = make([]*bool, 0) v = Last(ptrBoolSl) if v != b { // still expect nil t.Errorf("%T %#v, expected %T %#v", v, v, b, b) } // populated ptrBoolSl = []*bool{new(true), new(false), new(true), new(false), new(true)} v = Last(ptrBoolSl) b = ptrBoolSl[len(ptrBoolSl)-1] if v != b { t.Errorf("%T %#v, expected %T %#v", v, v, b, b) } } */ func TestLastIndex(t *testing.T) { var e int var v int var s []string e = -2 v = LastIndex(s) if e != v { t.Errorf("Got %d, expected %d", v, e) } e = -1 s = make([]string, 0) v = LastIndex(s) if e != v { t.Errorf("Got %d, expected %d", v, e) } e = 0 s = []string{"a"} v = LastIndex(s) if e != v { t.Errorf("Got %d, expected %d", v, e) } e = 1 s = []string{"a", "b"} v = LastIndex(s) if e != v { t.Errorf("Got %d, expected %d", v, e) } e = 2 s = []string{"a", "b", "c"} v = LastIndex(s) if e != v { t.Errorf("Got %d, expected %d", v, e) } }