go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/iter/sort_test.go (about) 1 /* 2 3 Copyright (c) 2023 - Present. Will Charczuk. All rights reserved. 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository. 5 6 */ 7 8 package iter 9 10 import ( 11 "fmt" 12 "testing" 13 14 "go.charczuk.com/sdk/assert" 15 ) 16 17 type sortExample struct { 18 ID int 19 Year int 20 Name string 21 } 22 23 func Test_Sort_empty(t *testing.T) { 24 var values []sortExample 25 Sort(values, 26 SortKeyDesc(func(v sortExample) int { return v.ID }), 27 SortKeyAsc(func(v sortExample) int { return v.Year }), 28 SortKeyAsc(func(v sortExample) string { return v.Name }), 29 ) 30 assert.ItsEqual(t, nil, values) 31 } 32 33 func Test_Sort_noCompares(t *testing.T) { 34 values := []int{3, 2, 1, 4, 5} 35 Sort(values) 36 assert.ItsEqual(t, []int{3, 2, 1, 4, 5}, values) 37 } 38 39 func Test_Sort_chain(t *testing.T) { 40 var values []sortExample 41 42 for x := 0; x < 4; x++ { 43 for y := 0; y < 4; y++ { 44 for z := 0; z < 4; z++ { 45 values = append(values, sortExample{ 46 ID: x, 47 Year: y, 48 Name: fmt.Sprintf("name-%d", z), 49 }) 50 } 51 } 52 } 53 54 Sort(values, 55 SortKeyDesc(func(v sortExample) int { return v.ID }), 56 SortKeyAsc(func(v sortExample) int { return v.Year }), 57 SortKeyAsc(func(v sortExample) string { return v.Name }), 58 ) 59 60 var expected []sortExample 61 for x := 3; x >= 0; x-- { 62 for y := 0; y < 4; y++ { 63 for z := 0; z < 4; z++ { 64 expected = append(expected, sortExample{ 65 ID: x, 66 Year: y, 67 Name: fmt.Sprintf("name-%d", z), 68 }) 69 } 70 } 71 } 72 assert.ItsEqual(t, expected, values) 73 } 74 75 func Test_Sort_SortComparerAsc(t *testing.T) { 76 values := []int{5, 4, 3, 3, 2, 1} 77 Sort(values, SortAsc[int]()) 78 assert.ItsEqual(t, []int{1, 2, 3, 3, 4, 5}, values) 79 80 values = []int{2, 1, 3, 3, 4, 5} 81 Sort(values, SortAsc[int]()) 82 assert.ItsEqual(t, []int{1, 2, 3, 3, 4, 5}, values) 83 } 84 85 func Test_Sort_SortComparerDesc(t *testing.T) { 86 values := []int{1, 2, 3, 3, 4, 5} 87 Sort(values, SortDesc[int]()) 88 assert.ItsEqual(t, []int{5, 4, 3, 3, 2, 1}, values) 89 90 values = []int{5, 4, 3, 3, 1, 2} 91 Sort(values, SortDesc[int]()) 92 assert.ItsEqual(t, []int{5, 4, 3, 3, 2, 1}, values) 93 } 94 95 func Test_Sort_SortComparerKeyAsc(t *testing.T) { 96 values := []sortExample{ 97 {ID: 5, Name: "a_foo"}, 98 {ID: 2, Name: "b_foo"}, 99 {ID: 3, Name: "c_foo"}, 100 {ID: 4, Name: "d_foo"}, 101 {ID: 1, Name: "e_foo"}, 102 } 103 Sort(values, SortKeyAsc(func(v sortExample) int { return v.ID })) 104 assert.ItsEqual(t, []sortExample{ 105 {ID: 1, Name: "e_foo"}, 106 {ID: 2, Name: "b_foo"}, 107 {ID: 3, Name: "c_foo"}, 108 {ID: 4, Name: "d_foo"}, 109 {ID: 5, Name: "a_foo"}, 110 }, values) 111 } 112 113 func Test_Sort_SortComparerKeyDesc(t *testing.T) { 114 values := []sortExample{ 115 {ID: 5, Name: "a_foo"}, 116 {ID: 2, Name: "b_foo"}, 117 {ID: 3, Name: "c_foo"}, 118 {ID: 4, Name: "d_foo"}, 119 {ID: 1, Name: "e_foo"}, 120 } 121 Sort(values, SortKeyDesc(func(v sortExample) string { return v.Name })) 122 assert.ItsEqual(t, []sortExample{ 123 {ID: 1, Name: "e_foo"}, 124 {ID: 4, Name: "d_foo"}, 125 {ID: 3, Name: "c_foo"}, 126 {ID: 2, Name: "b_foo"}, 127 {ID: 5, Name: "a_foo"}, 128 }, values) 129 }