| 28 | |
| 29 | func (s S) String() string { return fmt.Sprint(s.I) } |
| 30 | func TestSort(t *testing.T) { |
| 31 | ctx := log.Testing(t) |
| 32 | |
| 33 | for _, test := range []struct { |
| 34 | unsorted interface{} |
| 35 | expected interface{} |
| 36 | }{ |
| 37 | {[]int{1, 2, 3, 4, 5}, []int{1, 2, 3, 4, 5}}, |
| 38 | {[]int{5, 4, 3, 2, 1}, []int{1, 2, 3, 4, 5}}, |
| 39 | {[]int8{1, 2, 3, 4, 5}, []int8{1, 2, 3, 4, 5}}, |
| 40 | {[]int8{5, 4, 3, 2, 1}, []int8{1, 2, 3, 4, 5}}, |
| 41 | {[]int16{1, 2, 3, 4, 5}, []int16{1, 2, 3, 4, 5}}, |
| 42 | {[]int16{5, 4, 3, 2, 1}, []int16{1, 2, 3, 4, 5}}, |
| 43 | {[]int32{1, 2, 3, 4, 5}, []int32{1, 2, 3, 4, 5}}, |
| 44 | {[]int32{5, 4, 3, 2, 1}, []int32{1, 2, 3, 4, 5}}, |
| 45 | {[]int64{1, 2, 3, 4, 5}, []int64{1, 2, 3, 4, 5}}, |
| 46 | {[]int64{5, 4, 3, 2, 1}, []int64{1, 2, 3, 4, 5}}, |
| 47 | |
| 48 | {[]uint{1, 2, 3, 4, 5}, []uint{1, 2, 3, 4, 5}}, |
| 49 | {[]uint{5, 4, 3, 2, 1}, []uint{1, 2, 3, 4, 5}}, |
| 50 | {[]uint8{1, 2, 3, 4, 5}, []uint8{1, 2, 3, 4, 5}}, |
| 51 | {[]uint8{5, 4, 3, 2, 1}, []uint8{1, 2, 3, 4, 5}}, |
| 52 | {[]uint16{1, 2, 3, 4, 5}, []uint16{1, 2, 3, 4, 5}}, |
| 53 | {[]uint16{5, 4, 3, 2, 1}, []uint16{1, 2, 3, 4, 5}}, |
| 54 | {[]uint32{1, 2, 3, 4, 5}, []uint32{1, 2, 3, 4, 5}}, |
| 55 | {[]uint32{5, 4, 3, 2, 1}, []uint32{1, 2, 3, 4, 5}}, |
| 56 | {[]uint64{1, 2, 3, 4, 5}, []uint64{1, 2, 3, 4, 5}}, |
| 57 | {[]uint64{5, 4, 3, 2, 1}, []uint64{1, 2, 3, 4, 5}}, |
| 58 | |
| 59 | {[]float32{5, 4, 3, 2, 1}, []float32{1, 2, 3, 4, 5}}, |
| 60 | {[]float64{1, 2, 3, 4, 5}, []float64{1, 2, 3, 4, 5}}, |
| 61 | |
| 62 | {[]bool{false, false, true, true}, []bool{false, false, true, true}}, |
| 63 | {[]bool{false, true, false, true}, []bool{false, false, true, true}}, |
| 64 | |
| 65 | {[]rune{'a', 'b', 'c', 'd', 'e'}, []rune{'a', 'b', 'c', 'd', 'e'}}, |
| 66 | {[]rune{'e', 'd', 'c', 'b', 'a'}, []rune{'a', 'b', 'c', 'd', 'e'}}, |
| 67 | |
| 68 | {[]string{"a", "b", "c", "d", "e"}, []string{"a", "b", "c", "d", "e"}}, |
| 69 | {[]string{"e", "d", "c", "b", "a"}, []string{"a", "b", "c", "d", "e"}}, |
| 70 | |
| 71 | {[]S{{1}, {2}, {10}, {11}}, []S{{1}, {10}, {11}, {2}}}, |
| 72 | {[]S{{11}, {10}, {2}, {1}}, []S{{1}, {10}, {11}, {2}}}, |
| 73 | } { |
| 74 | { // Sort() |
| 75 | s := slice.Clone(test.unsorted) |
| 76 | slice.Sort(s) |
| 77 | assert.For(ctx, "Sort(%v)", test.unsorted). |
| 78 | That(s).DeepEquals(test.expected) |
| 79 | } |
| 80 | { // SortValues() |
| 81 | unsorted := reflect.ValueOf(test.unsorted) |
| 82 | s := make([]reflect.Value, unsorted.Len()) |
| 83 | for i := range s { |
| 84 | s[i] = unsorted.Index(i) |
| 85 | } |
| 86 | slice.SortValues(s, unsorted.Type().Elem()) |
| 87 | sorted := slice.New(unsorted.Type(), len(s), len(s)) |