mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
reflect: add Swapper func
Swapper returns a func that swaps two elements in a slice. Updates #16721 Change-Id: I7f2287a675c10a05019e02b7d62fb870af31216f Reviewed-on: https://go-review.googlesource.com/30088 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
This commit is contained in:
parent
9491e7d65e
commit
ca04091f5b
2 changed files with 155 additions and 0 deletions
|
|
@ -5780,3 +5780,84 @@ func BenchmarkNew(b *testing.B) {
|
|||
New(v)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSwapper(t *testing.T) {
|
||||
type I int
|
||||
var a, b, c I
|
||||
type pair struct {
|
||||
x, y int
|
||||
}
|
||||
type pairPtr struct {
|
||||
x, y int
|
||||
p *I
|
||||
}
|
||||
type S string
|
||||
|
||||
tests := []struct {
|
||||
in interface{}
|
||||
i, j int
|
||||
want interface{}
|
||||
}{
|
||||
{
|
||||
in: []int{1, 20, 300},
|
||||
i: 0,
|
||||
j: 2,
|
||||
want: []int{300, 20, 1},
|
||||
},
|
||||
{
|
||||
in: []uintptr{1, 20, 300},
|
||||
i: 0,
|
||||
j: 2,
|
||||
want: []uintptr{300, 20, 1},
|
||||
},
|
||||
{
|
||||
in: []int16{1, 20, 300},
|
||||
i: 0,
|
||||
j: 2,
|
||||
want: []int16{300, 20, 1},
|
||||
},
|
||||
{
|
||||
in: []int8{1, 20, 100},
|
||||
i: 0,
|
||||
j: 2,
|
||||
want: []int8{100, 20, 1},
|
||||
},
|
||||
{
|
||||
in: []*I{&a, &b, &c},
|
||||
i: 0,
|
||||
j: 2,
|
||||
want: []*I{&c, &b, &a},
|
||||
},
|
||||
{
|
||||
in: []string{"eric", "sergey", "larry"},
|
||||
i: 0,
|
||||
j: 2,
|
||||
want: []string{"larry", "sergey", "eric"},
|
||||
},
|
||||
{
|
||||
in: []S{"eric", "sergey", "larry"},
|
||||
i: 0,
|
||||
j: 2,
|
||||
want: []S{"larry", "sergey", "eric"},
|
||||
},
|
||||
{
|
||||
in: []pair{{1, 2}, {3, 4}, {5, 6}},
|
||||
i: 0,
|
||||
j: 2,
|
||||
want: []pair{{5, 6}, {3, 4}, {1, 2}},
|
||||
},
|
||||
{
|
||||
in: []pairPtr{{1, 2, &a}, {3, 4, &b}, {5, 6, &c}},
|
||||
i: 0,
|
||||
j: 2,
|
||||
want: []pairPtr{{5, 6, &c}, {3, 4, &b}, {1, 2, &a}},
|
||||
},
|
||||
}
|
||||
for i, tt := range tests {
|
||||
inStr := fmt.Sprint(tt.in)
|
||||
Swapper(tt.in)(tt.i, tt.j)
|
||||
if !DeepEqual(tt.in, tt.want) {
|
||||
t.Errorf("%d. swapping %v and %v of %v = %v; want %v", i, tt.i, tt.j, inStr, tt.in, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue