mirror of
https://github.com/chai2010/advanced-go-programming-book.git
synced 2025-05-24 04:22:22 +00:00
examples/ch2-06-qsort: 调整目录名称, 增加测试
This commit is contained in:
parent
245063d26a
commit
7a55d875c4
26
examples/ch2-06-qsort/02-qsort-v2/qsort_test.go
Normal file
26
examples/ch2-06-qsort/02-qsort-v2/qsort_test.go
Normal file
@ -0,0 +1,26 @@
|
||||
// Copyright © 2018 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
|
||||
package qsort
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"testing"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func TestSort(t *testing.T) {
|
||||
values := []int32{42, 9, 101, 95, 27, 25}
|
||||
|
||||
Sort(unsafe.Pointer(&values[0]),
|
||||
len(values), int(unsafe.Sizeof(values[0])),
|
||||
t_get_go_qsort_compare(),
|
||||
)
|
||||
|
||||
isSorted := sort.SliceIsSorted(values, func(i, j int) bool {
|
||||
return values[i] < values[j]
|
||||
})
|
||||
if !isSorted {
|
||||
t.Fatal("should be sorted")
|
||||
}
|
||||
}
|
21
examples/ch2-06-qsort/02-qsort-v2/test_helper.go
Normal file
21
examples/ch2-06-qsort/02-qsort-v2/test_helper.go
Normal file
@ -0,0 +1,21 @@
|
||||
// Copyright © 2018 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
|
||||
package qsort
|
||||
|
||||
//extern int t_go_qsort_compare(void* a, void* b);
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func t_get_go_qsort_compare() CompareFunc {
|
||||
return CompareFunc(C.t_go_qsort_compare)
|
||||
}
|
||||
|
||||
//export t_go_qsort_compare
|
||||
func t_go_qsort_compare(a, b unsafe.Pointer) C.int {
|
||||
pa, pb := (*C.int)(a), (*C.int)(b)
|
||||
return C.int(*pa - *pb)
|
||||
}
|
28
examples/ch2-06-qsort/03-qsort-v3/sort_test.go
Normal file
28
examples/ch2-06-qsort/03-qsort-v3/sort_test.go
Normal file
@ -0,0 +1,28 @@
|
||||
// Copyright © 2018 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
|
||||
package qsort
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"testing"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
func TestSort(t *testing.T) {
|
||||
values := []int32{42, 9, 101, 95, 27, 25}
|
||||
|
||||
Sort(unsafe.Pointer(&values[0]), len(values), int(unsafe.Sizeof(values[0])),
|
||||
func(a, b unsafe.Pointer) int {
|
||||
pa, pb := (*int32)(a), (*int32)(b)
|
||||
return int(*pa - *pb)
|
||||
},
|
||||
)
|
||||
|
||||
isSorted := sort.SliceIsSorted(values, func(i, j int) bool {
|
||||
return values[i] < values[j]
|
||||
})
|
||||
if !isSorted {
|
||||
t.Fatal("should be sorted")
|
||||
}
|
||||
}
|
24
examples/ch2-06-qsort/04-qsort-v4/sort_test.go
Normal file
24
examples/ch2-06-qsort/04-qsort-v4/sort_test.go
Normal file
@ -0,0 +1,24 @@
|
||||
// Copyright © 2018 ChaiShushan <chaishushan{AT}gmail.com>.
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
|
||||
package qsort
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSlice(t *testing.T) {
|
||||
values := []int32{42, 9, 101, 95, 27, 25}
|
||||
|
||||
Slice(values, func(i, j int) bool {
|
||||
return values[i] < values[j]
|
||||
})
|
||||
|
||||
isSorted := sort.SliceIsSorted(values, func(i, j int) bool {
|
||||
return values[i] < values[j]
|
||||
})
|
||||
if !isSorted {
|
||||
t.Fatal("should be sorted")
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user