orderby_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package help
  2. import "testing"
  3. func TestEmpty(t *testing.T) {
  4. q := From([]string{}).OrderBy(func(in interface{}) interface{} {
  5. return 0
  6. })
  7. _, ok := q.Iterate()()
  8. if ok {
  9. t.Errorf("Iterator for empty collection must return ok=false")
  10. }
  11. }
  12. func TestOrderBy(t *testing.T) {
  13. slice := make([]foo, 100)
  14. for i := len(slice) - 1; i >= 0; i-- {
  15. slice[i].f1 = i
  16. }
  17. q := From(slice).OrderBy(func(i interface{}) interface{} {
  18. return i.(foo).f1
  19. })
  20. j := 0
  21. next := q.Iterate()
  22. for item, ok := next(); ok; item, ok = next() {
  23. if item.(foo).f1 != j {
  24. t.Errorf("OrderBy()[%v]=%v expected %v", j, item, foo{f1: j})
  25. }
  26. j++
  27. }
  28. }
  29. func TestOrderByT_PanicWhenSelectorFnIsInvalid(t *testing.T) {
  30. mustPanicWithError(t, "OrderByT: parameter [selectorFn] has a invalid function signature. Expected: 'func(T)T', actual: 'func(int,int)int'", func() {
  31. From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).OrderByT(func(item, j int) int { return item + 2 })
  32. })
  33. }
  34. func TestOrderByDescending(t *testing.T) {
  35. slice := make([]foo, 100)
  36. for i := 0; i < len(slice); i++ {
  37. slice[i].f1 = i
  38. }
  39. q := From(slice).OrderByDescending(func(i interface{}) interface{} {
  40. return i.(foo).f1
  41. })
  42. j := len(slice) - 1
  43. next := q.Iterate()
  44. for item, ok := next(); ok; item, ok = next() {
  45. if item.(foo).f1 != j {
  46. t.Errorf("OrderByDescending()[%v]=%v expected %v", j, item, foo{f1: j})
  47. }
  48. j--
  49. }
  50. }
  51. func TestOrderByDescendingT_PanicWhenSelectorFnIsInvalid(t *testing.T) {
  52. mustPanicWithError(t, "OrderByDescendingT: parameter [selectorFn] has a invalid function signature. Expected: 'func(T)T', actual: 'func(int,int)int'", func() {
  53. From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).OrderByDescendingT(func(item, j int) int { return item + 2 })
  54. })
  55. }
  56. func TestThenBy(t *testing.T) {
  57. slice := make([]foo, 1000)
  58. for i := len(slice) - 1; i >= 0; i-- {
  59. slice[i].f1 = i
  60. slice[i].f2 = i%2 == 0
  61. }
  62. q := From(slice).OrderBy(func(i interface{}) interface{} {
  63. return i.(foo).f2
  64. }).ThenBy(func(i interface{}) interface{} {
  65. return i.(foo).f1
  66. })
  67. next := q.Iterate()
  68. for item, ok := next(); ok; item, ok = next() {
  69. if item.(foo).f2 != (item.(foo).f1%2 == 0) {
  70. t.Errorf("OrderBy().ThenBy()=%v", item)
  71. }
  72. }
  73. }
  74. func TestThenByT_PanicWhenSelectorFnIsInvalid(t *testing.T) {
  75. mustPanicWithError(t, "ThenByT: parameter [selectorFn] has a invalid function signature. Expected: 'func(T)T', actual: 'func(int,int)bool'", func() {
  76. From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).
  77. OrderByT(func(item int) int { return item }).
  78. ThenByT(func(item, j int) bool { return true })
  79. })
  80. }
  81. func TestThenByDescending(t *testing.T) {
  82. slice := make([]foo, 1000)
  83. for i := len(slice) - 1; i >= 0; i-- {
  84. slice[i].f1 = i
  85. slice[i].f2 = i%2 == 0
  86. }
  87. q := From(slice).OrderBy(func(i interface{}) interface{} {
  88. return i.(foo).f2
  89. }).ThenByDescending(func(i interface{}) interface{} {
  90. return i.(foo).f1
  91. })
  92. next := q.Iterate()
  93. for item, ok := next(); ok; item, ok = next() {
  94. if item.(foo).f2 != (item.(foo).f1%2 == 0) {
  95. t.Errorf("OrderBy().ThenByDescending()=%v", item)
  96. }
  97. }
  98. }
  99. func TestThenByDescendingT_PanicWhenSelectorFnIsInvalid(t *testing.T) {
  100. mustPanicWithError(t, "ThenByDescending: parameter [selectorFn] has a invalid function signature. Expected: 'func(T)T', actual: 'func(int,int)bool'", func() {
  101. From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).
  102. OrderByT(func(item int) int { return item }).
  103. ThenByDescendingT(func(item, j int) bool { return true })
  104. })
  105. }
  106. func TestSort(t *testing.T) {
  107. slice := make([]foo, 100)
  108. for i := len(slice) - 1; i >= 0; i-- {
  109. slice[i].f1 = i
  110. }
  111. q := From(slice).Sort(func(i, j interface{}) bool {
  112. return i.(foo).f1 < j.(foo).f1
  113. })
  114. j := 0
  115. next := q.Iterate()
  116. for item, ok := next(); ok; item, ok = next() {
  117. if item.(foo).f1 != j {
  118. t.Errorf("Sort()[%v]=%v expected %v", j, item, foo{f1: j})
  119. }
  120. j++
  121. }
  122. }
  123. func TestSortT_PanicWhenLessFnIsInvalid(t *testing.T) {
  124. mustPanicWithError(t, "SortT: parameter [lessFn] has a invalid function signature. Expected: 'func(T,T)bool', actual: 'func(int,int)string'", func() {
  125. From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).SortT(func(i, j int) string { return "" })
  126. })
  127. }