-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from yourbasic/tip
Tip
- Loading branch information
Showing
4 changed files
with
228 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package radix_test | ||
|
||
import ( | ||
"fmt" | ||
"github.com/yourbasic/radix" | ||
) | ||
|
||
func ExampleSortSlice() { | ||
people := []struct { | ||
Name string | ||
Age int | ||
}{ | ||
{"Gopher", 7}, | ||
{"Alice", 55}, | ||
{"Vera", 24}, | ||
{"Bob", 75}, | ||
} | ||
radix.SortSlice(people, func(i int) string { return people[i].Name }) | ||
fmt.Println(people) | ||
// Output: [{Alice 55} {Bob 75} {Gopher 7} {Vera 24}] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
package radix | ||
|
||
import ( | ||
"bufio" | ||
"log" | ||
"os" | ||
"reflect" | ||
"sort" | ||
"strconv" | ||
"testing" | ||
) | ||
|
||
func TestSortSlice(t *testing.T) { | ||
data := [...]string{"", "Hello", "foo", "fo", "xb", "xa", "bar", "foo", "f00", "%*&^*&^&", "***"} | ||
sorted := data[0:] | ||
sort.Strings(sorted) | ||
|
||
a := data[0:] | ||
SortSlice(a, func(i int) string { return a[i] }) | ||
if !reflect.DeepEqual(a, sorted) { | ||
t.Errorf(" got %v", a) | ||
t.Errorf("want %v", sorted) | ||
} | ||
|
||
SortSlice(nil, func(i int) string { return a[i] }) | ||
a = []string{} | ||
SortSlice(a, func(i int) string { return a[i] }) | ||
if !reflect.DeepEqual(a, []string{}) { | ||
t.Errorf(" got %v", a) | ||
t.Errorf("want %v", []string{}) | ||
} | ||
a = []string{""} | ||
SortSlice(a, func(i int) string { return a[i] }) | ||
if !reflect.DeepEqual(a, []string{""}) { | ||
t.Errorf(" got %v", a) | ||
t.Errorf("want %v", []string{""}) | ||
} | ||
} | ||
|
||
func TestSortSlice1k(t *testing.T) { | ||
data := make([]string, 1<<10) | ||
for i := range data { | ||
data[i] = strconv.Itoa(i ^ 0x2cc) | ||
} | ||
|
||
sorted := make([]string, len(data)) | ||
copy(sorted, data) | ||
sort.Strings(sorted) | ||
|
||
SortSlice(data, func(i int) string { return data[i] }) | ||
if !reflect.DeepEqual(data, sorted) { | ||
t.Errorf(" got %v", data) | ||
t.Errorf("want %v", sorted) | ||
} | ||
} | ||
|
||
func TestSortSliceBible(t *testing.T) { | ||
var data []string | ||
f, err := os.Open("res/bible.txt") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
for sc := bufio.NewScanner(f); sc.Scan(); { | ||
data = append(data, sc.Text()) | ||
} | ||
|
||
sorted := make([]string, len(data)) | ||
copy(sorted, data) | ||
sort.Strings(sorted) | ||
|
||
SortSlice(data, func(i int) string { return data[i] }) | ||
if !reflect.DeepEqual(data, sorted) { | ||
for i, s := range data { | ||
if s != sorted[i] { | ||
t.Errorf("%v got: %v", i, s) | ||
t.Errorf("%v want: %v", i, sorted[i]) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkRadixSortSliceBible(b *testing.B) { | ||
b.StopTimer() | ||
var data []string | ||
f, err := os.Open("res/bible.txt") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
for sc := bufio.NewScanner(f); sc.Scan(); { | ||
data = append(data, sc.Text()) | ||
} | ||
|
||
a := make([]string, len(data)) | ||
for i := 0; i < b.N; i++ { | ||
copy(a, data) | ||
b.StartTimer() | ||
SortSlice(a, func(i int) string { return a[i] }) | ||
b.StopTimer() | ||
} | ||
if err := f.Close(); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
func BenchmarkSortSliceBible(b *testing.B) { | ||
b.StopTimer() | ||
var data []string | ||
f, err := os.Open("res/bible.txt") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
for sc := bufio.NewScanner(f); sc.Scan(); { | ||
data = append(data, sc.Text()) | ||
} | ||
|
||
a := make([]string, len(data)) | ||
for i := 0; i < b.N; i++ { | ||
copy(a, data) | ||
b.StartTimer() | ||
sort.Slice(a, func(i, j int) bool { return a[i] < a[j] }) | ||
b.StopTimer() | ||
} | ||
if err := f.Close(); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
func BenchmarkRadixSortSlice1k(b *testing.B) { | ||
b.StopTimer() | ||
data := make([]string, 1<<10) | ||
for i := range data { | ||
data[i] = strconv.Itoa(i ^ 0x2cc) | ||
} | ||
|
||
a := make([]string, len(data)) | ||
for i := 0; i < b.N; i++ { | ||
copy(a, data) | ||
b.StartTimer() | ||
SortSlice(a, func(i int) string { return a[i] }) | ||
b.StopTimer() | ||
} | ||
} | ||
|
||
func BenchmarkSortSlice1k(b *testing.B) { | ||
b.StopTimer() | ||
data := make([]string, 1<<10) | ||
for i := range data { | ||
data[i] = strconv.Itoa(i ^ 0x2cc) | ||
} | ||
|
||
a := make([]string, len(data)) | ||
for i := 0; i < b.N; i++ { | ||
copy(a, data) | ||
b.StartTimer() | ||
sort.Slice(a, func(i, j int) bool { return a[i] < a[j] }) | ||
b.StopTimer() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters