Index: common/data/sortby/sortby_test.go |
diff --git a/common/data/sortby/sortby_test.go b/common/data/sortby/sortby_test.go |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a2e10bd17bece9294a2c9c92923eacdc0fec2580 |
--- /dev/null |
+++ b/common/data/sortby/sortby_test.go |
@@ -0,0 +1,72 @@ |
+package sortby |
Vadim Sh.
2017/01/25 03:43:34
copyright
iannucci
2017/01/25 03:47:24
Done.
|
+ |
+import ( |
+ "sort" |
+ "testing" |
+ |
+ . "github.com/smartystreets/goconvey/convey" |
+) |
+ |
+type CoolStruct struct { |
+ A int |
+ B int |
+ C int |
+} |
+ |
+type CoolStructSlice []CoolStruct |
+ |
+func (s CoolStructSlice) Len() int { return len(s) } |
+func (s CoolStructSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] } |
+ |
+// these could be defined inline inside of the Less function, too. |
+func (s CoolStructSlice) LessA(i, j int) bool { return s[i].A < s[j].A } |
+func (s CoolStructSlice) LessB(i, j int) bool { return s[i].B < s[j].B } |
+func (s CoolStructSlice) LessC(i, j int) bool { return s[i].C < s[j].C } |
+ |
+func (s CoolStructSlice) Less(i, j int) bool { |
+ return Chain{s.LessA, s.LessB, s.LessC}.Use(i, j) |
+} |
+ |
+func TestSortBy(t *testing.T) { |
+ t.Parallel() |
+ |
+ Convey("sortby", t, func() { |
+ s := CoolStructSlice{ |
+ {194, 771, 222}, |
Vadim Sh.
2017/01/25 03:43:34
add some partially equal keys, to actually hit the
iannucci
2017/01/25 03:47:24
I just generated these with python, I'll add some
|
+ {209, 28, 300}, |
+ {413, 639, 772}, |
+ {14, 761, 759}, |
+ {866, 821, 447}, |
+ {447, 373, 817}, |
+ {132, 510, 149}, |
+ {778, 513, 156}, |
+ {713, 831, 596}, |
+ {288, 83, 898}, |
+ {679, 688, 903}, |
+ {864, 100, 199}, |
+ {229, 975, 648}, |
+ {381, 290, 156}, |
+ {311, 614, 434}, |
+ } |
+ |
+ sort.Sort(s) |
+ |
+ So(s, ShouldResemble, CoolStructSlice{ |
+ {14, 761, 759}, |
+ {132, 510, 149}, |
+ {194, 771, 222}, |
+ {209, 28, 300}, |
+ {229, 975, 648}, |
+ {288, 83, 898}, |
+ {311, 614, 434}, |
+ {381, 290, 156}, |
+ {413, 639, 772}, |
+ {447, 373, 817}, |
+ {679, 688, 903}, |
+ {713, 831, 596}, |
+ {778, 513, 156}, |
+ {864, 100, 199}, |
+ {866, 821, 447}, |
+ }) |
+ }) |
+} |