Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1877)

Unified Diff: common/data/sortby/sortby_test.go

Issue 2657733002: Add sort function helper library. (Closed)
Patch Set: copyright Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « common/data/sortby/sortby.go ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..983dee60e390ff9172730c9fab3fbfa4c85925e1
--- /dev/null
+++ b/common/data/sortby/sortby_test.go
@@ -0,0 +1,78 @@
+// Copyright 2017 The LUCI Authors. All rights reserved.
+// Use of this source code is governed under the Apache License, Version 2.0
+// that can be found in the LICENSE file.
+
+package sortby
+
+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, nil, s.LessC}.Use(i, j)
+}
+
+func TestSortBy(t *testing.T) {
+ t.Parallel()
+
+ Convey("sortby", t, func() {
+ s := CoolStructSlice{
+ {194, 771, 222},
+ {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},
+ {447, 290, 156}, // intentionally partially equal
+ {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, 290, 156},
+ {447, 373, 817},
+ {679, 688, 903},
+ {713, 831, 596},
+ {778, 513, 156},
+ {864, 100, 199},
+ {866, 821, 447},
+ })
+ })
+}
« no previous file with comments | « common/data/sortby/sortby.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698