wp-go/helper/slice/sort_test.go

203 lines
3.3 KiB
Go
Raw Permalink Normal View History

2023-01-21 11:31:23 +00:00
package slice
import (
2023-02-28 11:19:24 +00:00
"fmt"
"github.com/fthvgb1/wp-go/helper/number"
2023-02-28 11:19:24 +00:00
"golang.org/x/exp/constraints"
2023-01-21 11:31:23 +00:00
"reflect"
"testing"
)
func TestSort(t *testing.T) {
type xy struct {
x int
y int
}
type args[T any] struct {
arr []T
fn func(i, j T) bool
}
type testCase[T any] struct {
name string
args args[T]
wantR []T
}
tests := []testCase[xy]{
{
name: "t1",
args: args[xy]{
arr: []xy{
{1, 2},
{3, 4},
{1, 3},
{2, 1},
{1, 6},
},
fn: func(i, j xy) bool {
if i.x < j.x {
return true
}
if i.x == j.x && i.y > i.y {
return true
}
return false
},
},
wantR: []xy{
{1, 2},
{1, 3},
{1, 6},
{2, 1},
{3, 4},
},
},
2023-03-02 12:36:58 +00:00
{
name: "t1",
args: args[xy]{
arr: []xy{{1, 2}, {1, 3}, {1, 6}},
fn: func(i, j xy) bool {
return i.x > j.x
},
},
wantR: []xy{{1, 2}, {1, 3}, {1, 6}},
},
2023-01-21 11:31:23 +00:00
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2023-03-02 12:36:58 +00:00
if Sort[xy](tt.args.arr, tt.args.fn); !reflect.DeepEqual(tt.args.arr, tt.wantR) {
t.Errorf("SimpleSortR() = %v, want %v", tt.args.arr, tt.wantR)
2023-01-21 11:31:23 +00:00
}
})
}
2023-02-28 11:19:24 +00:00
}
func TestSorts(t *testing.T) {
type args[T constraints.Ordered] struct {
a []T
order string
2023-02-28 11:19:24 +00:00
}
type testCase[T constraints.Ordered] struct {
name string
args args[T]
}
tests := []testCase[int]{
{
name: "asc",
args: args[int]{
a: []int{1, -3, 6, 10, 3, 2, 8},
order: ASC,
}, //[-3 1 2 3 6 8 10]
},
{
name: "desc",
args: args[int]{
a: []int{1, -3, 6, 10, 3, 2, 8},
order: DESC,
}, //[10 8 6 3 2 1 -3]
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Sorts(tt.args.a, tt.args.order)
fmt.Println(tt.args.a)
})
}
}
2023-05-10 09:02:58 +00:00
func TestStableSort(t *testing.T) {
type xy struct {
x int
y int
}
type args[T any] struct {
arr []T
fn func(i, j T) bool
}
type testCase[T any] struct {
name string
args args[T]
wantR []T
}
tests := []testCase[xy]{
{
name: "t1",
args: args[xy]{
arr: []xy{
{1, 2},
{3, 4},
{1, 3},
{2, 1},
{1, 6},
},
fn: func(i, j xy) bool {
if i.x < j.x {
return true
}
if i.x == j.x && i.y > i.y {
return true
}
return false
},
},
wantR: []xy{
{1, 2},
{1, 3},
{1, 6},
{2, 1},
{3, 4},
},
},
{
name: "t1",
args: args[xy]{
arr: []xy{{1, 2}, {1, 3}, {1, 6}},
fn: func(i, j xy) bool {
return i.x > j.x
},
},
wantR: []xy{{1, 2}, {1, 3}, {1, 6}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
StableSort(tt.args.arr, tt.args.fn)
})
}
}
func TestSimpleSort(t *testing.T) {
type args[T any, O constraints.Ordered] struct {
a []T
order string
fn func(t T) O
}
type testCase[T any, O constraints.Ordered] struct {
name string
args args[T, O]
}
tests := []testCase[int, int]{
{
name: "t1",
args: args[int, int]{
a: func() []int {
f := number.Range(1, 20)
Shuffle(&f)
fmt.Println(f)
return f
}(),
order: ASC,
fn: func(t int) int {
return t
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
SimpleSort(tt.args.a, tt.args.order, tt.args.fn)
fmt.Println(tt.args.a)
})
}
}