-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathreverse_test.go
93 lines (87 loc) · 1.9 KB
/
reverse_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package go2linq
import (
"fmt"
"iter"
"slices"
"testing"
"github.com/solsw/errorhelper"
"github.com/solsw/iterhelper"
)
// https://github.com/jskeet/edulinq/blob/master/src/Edulinq.Tests/ReverseTest.cs
func TestReverse_int(t *testing.T) {
type args struct {
source iter.Seq[int]
}
tests := []struct {
name string
args args
want iter.Seq[int]
wantErr bool
}{
{name: "EmptyInput",
args: args{
source: Empty[int](),
},
want: Empty[int](),
},
{name: "ReversedRange",
args: args{
source: errorhelper.Must(Range(5, 5)),
},
want: iterhelper.VarSeq(9, 8, 7, 6, 5),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, _ := Reverse(tt.args.source)
equal, _ := SequenceEqual(got, tt.want)
if !equal {
t.Errorf("Reverse() = %v, want %v", iterhelper.StringDef(got), iterhelper.StringDef(tt.want))
}
})
}
}
func TestReverse_string(t *testing.T) {
type args struct {
source iter.Seq[string]
}
tests := []struct {
name string
args args
want iter.Seq[string]
}{
{name: "ReversedStrs",
args: args{
source: iterhelper.VarSeq("one", "two", "three", "four", "five"),
},
want: iterhelper.VarSeq("five", "four", "three", "two", "one"),
},
{name: "1",
args: args{
source: iterhelper.VarSeq("1"),
},
want: iterhelper.VarSeq("1"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, _ := Reverse(tt.args.source)
equal, _ := SequenceEqual(got, tt.want)
if !equal {
t.Errorf("Reverse() = %v, want %v", iterhelper.StringDef(got), iterhelper.StringDef(tt.want))
}
})
}
}
// example from
// https://learn.microsoft.com/dotnet/api/system.linq.enumerable.reverse#examples
func ExampleReverse() {
apple := []string{"a", "p", "p", "l", "e"}
reverse, _ := Reverse(slices.Values(apple))
for num := range reverse {
fmt.Print(num)
}
fmt.Println()
// Output:
// elppa
}