diff --git a/examples/ch2-06-qsort/v1-qsort/Makefile b/examples/ch2-06-qsort/01-qsort-v1/Makefile similarity index 100% rename from examples/ch2-06-qsort/v1-qsort/Makefile rename to examples/ch2-06-qsort/01-qsort-v1/Makefile diff --git a/examples/ch2-06-qsort/v1-qsort/main.c b/examples/ch2-06-qsort/01-qsort-v1/main.c similarity index 100% rename from examples/ch2-06-qsort/v1-qsort/main.c rename to examples/ch2-06-qsort/01-qsort-v1/main.c diff --git a/examples/ch2-06-qsort/v2-qsort/main.go b/examples/ch2-06-qsort/02-qsort-v2/main.go similarity index 100% rename from examples/ch2-06-qsort/v2-qsort/main.go rename to examples/ch2-06-qsort/02-qsort-v2/main.go diff --git a/examples/ch2-06-qsort/v2-qsort/qsort.go b/examples/ch2-06-qsort/02-qsort-v2/qsort.go similarity index 100% rename from examples/ch2-06-qsort/v2-qsort/qsort.go rename to examples/ch2-06-qsort/02-qsort-v2/qsort.go diff --git a/examples/ch2-06-qsort/02-qsort-v2/qsort_test.go b/examples/ch2-06-qsort/02-qsort-v2/qsort_test.go new file mode 100644 index 0000000..e151f0b --- /dev/null +++ b/examples/ch2-06-qsort/02-qsort-v2/qsort_test.go @@ -0,0 +1,26 @@ +// Copyright © 2018 ChaiShushan . +// 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") + } +} diff --git a/examples/ch2-06-qsort/02-qsort-v2/test_helper.go b/examples/ch2-06-qsort/02-qsort-v2/test_helper.go new file mode 100644 index 0000000..c0d373b --- /dev/null +++ b/examples/ch2-06-qsort/02-qsort-v2/test_helper.go @@ -0,0 +1,21 @@ +// Copyright © 2018 ChaiShushan . +// 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) +} diff --git a/examples/ch2-06-qsort/v3-qsort/main.go b/examples/ch2-06-qsort/03-qsort-v3/main.go similarity index 100% rename from examples/ch2-06-qsort/v3-qsort/main.go rename to examples/ch2-06-qsort/03-qsort-v3/main.go diff --git a/examples/ch2-06-qsort/v3-qsort/sort.go b/examples/ch2-06-qsort/03-qsort-v3/sort.go similarity index 100% rename from examples/ch2-06-qsort/v3-qsort/sort.go rename to examples/ch2-06-qsort/03-qsort-v3/sort.go diff --git a/examples/ch2-06-qsort/03-qsort-v3/sort_test.go b/examples/ch2-06-qsort/03-qsort-v3/sort_test.go new file mode 100644 index 0000000..53d5238 --- /dev/null +++ b/examples/ch2-06-qsort/03-qsort-v3/sort_test.go @@ -0,0 +1,28 @@ +// Copyright © 2018 ChaiShushan . +// 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") + } +} diff --git a/examples/ch2-06-qsort/v4-qsort/main.go b/examples/ch2-06-qsort/04-qsort-v4/main.go similarity index 100% rename from examples/ch2-06-qsort/v4-qsort/main.go rename to examples/ch2-06-qsort/04-qsort-v4/main.go diff --git a/examples/ch2-06-qsort/v4-qsort/sort.go b/examples/ch2-06-qsort/04-qsort-v4/sort.go similarity index 100% rename from examples/ch2-06-qsort/v4-qsort/sort.go rename to examples/ch2-06-qsort/04-qsort-v4/sort.go diff --git a/examples/ch2-06-qsort/04-qsort-v4/sort_test.go b/examples/ch2-06-qsort/04-qsort-v4/sort_test.go new file mode 100644 index 0000000..472c64a --- /dev/null +++ b/examples/ch2-06-qsort/04-qsort-v4/sort_test.go @@ -0,0 +1,24 @@ +// Copyright © 2018 ChaiShushan . +// 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") + } +}