Debian Codegraph @

lib/iterator/index_test.go

raw
1 package iterator_test
2
3 import (
4 "testing"
5
6 "git.sr.ht/~rjarry/aerc/lib/iterator"
7 )
8
9 type indexer struct {
10 start int
11 end int
12 }
13
14 func (ip *indexer) StartIndex() int {
15 return ip.start
16 }
17
18 func (ip *indexer) EndIndex() int {
19 return ip.end
20 }
21
22 func TestMoveIndex(t *testing.T) {
23 tests := []struct {
24 idx int
25 delta int
26 start int
27 end int
28 cb iterator.BoundsCheckFunc
29 expected int
30 }{
31 {
32 idx: 0,
33 delta: 1,
34 start: 0,
35 end: 2,
36 cb: iterator.FixBounds,
37 expected: 1,
38 },
39 {
40 idx: 0,
41 delta: 5,
42 start: 0,
43 end: 2,
44 cb: iterator.FixBounds,
45 expected: 2,
46 },
47 {
48 idx: 0,
49 delta: -1,
50 start: 0,
51 end: 2,
52 cb: iterator.FixBounds,
53 expected: 0,
54 },
55 {
56 idx: 0,
57 delta: 2,
58 start: 0,
59 end: 2,
60 cb: iterator.WrapBounds,
61 expected: 2,
62 },
63 {
64 idx: 0,
65 delta: 3,
66 start: 0,
67 end: 2,
68 cb: iterator.WrapBounds,
69 expected: 0,
70 },
71 {
72 idx: 0,
73 delta: -1,
74 start: 0,
75 end: 2,
76 cb: iterator.WrapBounds,
77 expected: 2,
78 },
79 {
80 idx: 2,
81 delta: 2,
82 start: 0,
83 end: 2,
84 cb: iterator.WrapBounds,
85 expected: 1,
86 },
87 {
88 idx: 0,
89 delta: -2,
90 start: 0,
91 end: 2,
92 cb: iterator.WrapBounds,
93 expected: 1,
94 },
95 {
96 idx: 1,
97 delta: 1,
98 start: 2,
99 end: 0,
100 cb: iterator.FixBounds,
101 expected: 0,
102 },
103 {
104 idx: 0,
105 delta: 1,
106 start: 2,
107 end: 0,
108 cb: iterator.FixBounds,
109 expected: 0,
110 },
111 {
112 idx: 0,
113 delta: 1,
114 start: 2,
115 end: 0,
116 cb: iterator.WrapBounds,
117 expected: 2,
118 },
119 }
120
121 for i, test := range tests {
122 idx := iterator.MoveIndex(
123 test.idx,
124 test.delta,
125 &indexer{test.start, test.end},
126 test.cb,
127 )
128 if idx != test.expected {
129 t.Errorf("test %d [%#v] failed: got %d but expected %d",
130 i, test, idx, test.expected)
131 }
132 }
133 }