mirror of
https://github.com/duke-git/lancet.git
synced 2025-10-05 07:26:51 +08:00
feat: add BubbleSort and InsertionSort
This commit is contained in:
@@ -8,18 +8,18 @@ import (
|
||||
)
|
||||
|
||||
// People test mock data
|
||||
type People struct {
|
||||
type people struct {
|
||||
Name string
|
||||
Age int
|
||||
}
|
||||
|
||||
// PeopleAageComparator sort people slice by age field
|
||||
type PeopleAageComparator struct{}
|
||||
type peopleAageComparator struct{}
|
||||
|
||||
// Compare implements github.com/duke-git/lancet/lancetconstraints/constraints.go/Comparator
|
||||
func (pc *PeopleAageComparator) Compare(v1 interface{}, v2 interface{}) int {
|
||||
p1, _ := v1.(People)
|
||||
p2, _ := v2.(People)
|
||||
func (pc *peopleAageComparator) Compare(v1 interface{}, v2 interface{}) int {
|
||||
p1, _ := v1.(people)
|
||||
p2, _ := v2.(people)
|
||||
|
||||
//ascending order
|
||||
if p1.Age < p2.Age {
|
||||
@@ -37,7 +37,7 @@ func (pc *PeopleAageComparator) Compare(v1 interface{}, v2 interface{}) int {
|
||||
// }
|
||||
}
|
||||
|
||||
var peoples = []People{
|
||||
var peoples = []people{
|
||||
{Name: "a", Age: 20},
|
||||
{Name: "b", Age: 10},
|
||||
{Name: "c", Age: 17},
|
||||
@@ -45,9 +45,63 @@ var peoples = []People{
|
||||
{Name: "e", Age: 28},
|
||||
}
|
||||
|
||||
func TestSelectionSort(t *testing.T) {
|
||||
asssert := internal.NewAssert(t, "TestSelectionSort")
|
||||
comparator := &PeopleAageComparator{}
|
||||
type intComparator struct{}
|
||||
|
||||
func (c *intComparator) Compare(v1 interface{}, v2 interface{}) int {
|
||||
val1, _ := v1.(int)
|
||||
val2, _ := v2.(int)
|
||||
|
||||
//ascending order
|
||||
if val1 < val2 {
|
||||
return -1
|
||||
} else if val1 > val2 {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
var intSlice = []int{2, 1, 5, 3, 6, 4}
|
||||
|
||||
func TestBubbleSortForStructSlice(t *testing.T) {
|
||||
asssert := internal.NewAssert(t, "TestBubbleSortForStructSlice")
|
||||
|
||||
comparator := &peopleAageComparator{}
|
||||
sortedPeopleByAge := BubbleSort(peoples, comparator)
|
||||
t.Log(sortedPeopleByAge)
|
||||
|
||||
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
|
||||
actual := fmt.Sprintf("%v", sortedPeopleByAge)
|
||||
|
||||
asssert.Equal(expected, actual)
|
||||
}
|
||||
|
||||
func TestBubbleSortForIntSlice(t *testing.T) {
|
||||
asssert := internal.NewAssert(t, "TestBubbleSortForIntSlice")
|
||||
|
||||
comparator := &intComparator{}
|
||||
sortedInt := BubbleSort(intSlice, comparator)
|
||||
expected := []int{1, 2, 3, 4, 5, 6}
|
||||
|
||||
asssert.Equal(expected, sortedInt)
|
||||
}
|
||||
|
||||
func TestInsertionSort(t *testing.T) {
|
||||
asssert := internal.NewAssert(t, "TestInsertionSort")
|
||||
|
||||
comparator := &peopleAageComparator{}
|
||||
sortedPeopleByAge := SelectionSort(peoples, comparator)
|
||||
t.Log(sortedPeopleByAge)
|
||||
|
||||
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
|
||||
actual := fmt.Sprintf("%v", sortedPeopleByAge)
|
||||
|
||||
asssert.Equal(expected, actual)
|
||||
}
|
||||
|
||||
func TestSelectionSort(t *testing.T) {
|
||||
asssert := internal.NewAssert(t, "TestSelectionSort")
|
||||
|
||||
comparator := &peopleAageComparator{}
|
||||
sortedPeopleByAge := SelectionSort(peoples, comparator)
|
||||
t.Log(sortedPeopleByAge)
|
||||
|
||||
|
Reference in New Issue
Block a user