123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- package help
- import "testing"
- func TestEmpty(t *testing.T) {
- q := From([]string{}).OrderBy(func(in interface{}) interface{} {
- return 0
- })
- _, ok := q.Iterate()()
- if ok {
- t.Errorf("Iterator for empty collection must return ok=false")
- }
- }
- func TestOrderBy(t *testing.T) {
- slice := make([]foo, 100)
- for i := len(slice) - 1; i >= 0; i-- {
- slice[i].f1 = i
- }
- q := From(slice).OrderBy(func(i interface{}) interface{} {
- return i.(foo).f1
- })
- j := 0
- next := q.Iterate()
- for item, ok := next(); ok; item, ok = next() {
- if item.(foo).f1 != j {
- t.Errorf("OrderBy()[%v]=%v expected %v", j, item, foo{f1: j})
- }
- j++
- }
- }
- func TestOrderByT_PanicWhenSelectorFnIsInvalid(t *testing.T) {
- mustPanicWithError(t, "OrderByT: parameter [selectorFn] has a invalid function signature. Expected: 'func(T)T', actual: 'func(int,int)int'", func() {
- From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).OrderByT(func(item, j int) int { return item + 2 })
- })
- }
- func TestOrderByDescending(t *testing.T) {
- slice := make([]foo, 100)
- for i := 0; i < len(slice); i++ {
- slice[i].f1 = i
- }
- q := From(slice).OrderByDescending(func(i interface{}) interface{} {
- return i.(foo).f1
- })
- j := len(slice) - 1
- next := q.Iterate()
- for item, ok := next(); ok; item, ok = next() {
- if item.(foo).f1 != j {
- t.Errorf("OrderByDescending()[%v]=%v expected %v", j, item, foo{f1: j})
- }
- j--
- }
- }
- func TestOrderByDescendingT_PanicWhenSelectorFnIsInvalid(t *testing.T) {
- mustPanicWithError(t, "OrderByDescendingT: parameter [selectorFn] has a invalid function signature. Expected: 'func(T)T', actual: 'func(int,int)int'", func() {
- From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).OrderByDescendingT(func(item, j int) int { return item + 2 })
- })
- }
- func TestThenBy(t *testing.T) {
- slice := make([]foo, 1000)
- for i := len(slice) - 1; i >= 0; i-- {
- slice[i].f1 = i
- slice[i].f2 = i%2 == 0
- }
- q := From(slice).OrderBy(func(i interface{}) interface{} {
- return i.(foo).f2
- }).ThenBy(func(i interface{}) interface{} {
- return i.(foo).f1
- })
- next := q.Iterate()
- for item, ok := next(); ok; item, ok = next() {
- if item.(foo).f2 != (item.(foo).f1%2 == 0) {
- t.Errorf("OrderBy().ThenBy()=%v", item)
- }
- }
- }
- func TestThenByT_PanicWhenSelectorFnIsInvalid(t *testing.T) {
- mustPanicWithError(t, "ThenByT: parameter [selectorFn] has a invalid function signature. Expected: 'func(T)T', actual: 'func(int,int)bool'", func() {
- From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).
- OrderByT(func(item int) int { return item }).
- ThenByT(func(item, j int) bool { return true })
- })
- }
- func TestThenByDescending(t *testing.T) {
- slice := make([]foo, 1000)
- for i := len(slice) - 1; i >= 0; i-- {
- slice[i].f1 = i
- slice[i].f2 = i%2 == 0
- }
- q := From(slice).OrderBy(func(i interface{}) interface{} {
- return i.(foo).f2
- }).ThenByDescending(func(i interface{}) interface{} {
- return i.(foo).f1
- })
- next := q.Iterate()
- for item, ok := next(); ok; item, ok = next() {
- if item.(foo).f2 != (item.(foo).f1%2 == 0) {
- t.Errorf("OrderBy().ThenByDescending()=%v", item)
- }
- }
- }
- func TestThenByDescendingT_PanicWhenSelectorFnIsInvalid(t *testing.T) {
- mustPanicWithError(t, "ThenByDescending: parameter [selectorFn] has a invalid function signature. Expected: 'func(T)T', actual: 'func(int,int)bool'", func() {
- From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).
- OrderByT(func(item int) int { return item }).
- ThenByDescendingT(func(item, j int) bool { return true })
- })
- }
- func TestSort(t *testing.T) {
- slice := make([]foo, 100)
- for i := len(slice) - 1; i >= 0; i-- {
- slice[i].f1 = i
- }
- q := From(slice).Sort(func(i, j interface{}) bool {
- return i.(foo).f1 < j.(foo).f1
- })
- j := 0
- next := q.Iterate()
- for item, ok := next(); ok; item, ok = next() {
- if item.(foo).f1 != j {
- t.Errorf("Sort()[%v]=%v expected %v", j, item, foo{f1: j})
- }
- j++
- }
- }
- func TestSortT_PanicWhenLessFnIsInvalid(t *testing.T) {
- mustPanicWithError(t, "SortT: parameter [lessFn] has a invalid function signature. Expected: 'func(T,T)bool', actual: 'func(int,int)string'", func() {
- From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).SortT(func(i, j int) string { return "" })
- })
- }
|