1 module auxil.cursor_test;
2 
3 import std;
4 
5 version(unittest)
6 import unit_threaded;
7 
8 import auxil.cursor;
9 
10 //          128 118  97  85  72  48  33  17    0
11 //                0  10  31  43  56  80  95  111 128
12 enum sequence = [10, 21, 12, 13, 24, 15, 16, 17];
13 
14 struct LogRecord
15 {
16 	Cursor.Type pos;
17 	Cursor.Type value;
18 }
19 
20 auto test(Cursor.Order order, R)(ref Cursor a, R r, ref LogRecord[] log)
21 {
22 	static if (order == Cursor.Order.forward)
23 		auto data = r;
24 	else
25 		auto data = r.retro;
26 	foreach(e; data)
27 	{
28 		a.begin(e);
29 		log ~= LogRecord(a.calcPosition!order, e);
30 		a.next(e);
31 		if (a.complete)
32 			break;
33 	}
34 }
35 
36 version(unittest)
37 @Name("start2end")
38 unittest
39 {
40 	const order = Cursor.Order.forward;
41 	LogRecord[] posLog;
42 	auto seq = sequence.dup;
43 	const scroll = 130;
44 	const total = sum(seq);
45 
46 	// scroll from the start to the end in forward direction
47 	auto a = Cursor();
48 	a.scroll = scroll;
49 	test!order(a, seq, posLog);
50 	a.fixedPosition.should.be == total;
51 	a.fixedPosition.should.be == sum(seq);
52 	posLog.map!"a.pos".should.be == [0, 10, 31, 43, 56, 80, 95, 111];
53 	a.phase.should.be == scroll - total;
54 
55 	// before fixUp the Cursor points to the next elements after the sequence
56 	a.fixUp;
57 	// after fixUp the Cursor points to the last element of the sequence
58 	a.fixedPosition.should.be == posLog[$-1].pos;
59 }
60 
61 version(unittest)
62 @Name("start2endPosition50")
63 unittest
64 {
65 	const order = Cursor.Order.forward;
66 	LogRecord[] posLog;
67 	auto seq = sequence.dup;
68 
69 	// scroll from the start to the end in forward direction from
70 	// non zero position
71 	auto init_pos = 50;
72 	auto a = Cursor(init_pos);
73 	a.scroll = a.phase.max;
74 	test!order(a, seq, posLog);
75 	a.fixedPosition.should.be == 128;
76 	a.fixedPosition.should.be == sum(seq);
77 	posLog.map!"a.pos".should.be == [0, 10, 31, 43, 56, 80, 95, 111].map!(a=>a+init_pos);
78 	a.phase.should.be == a.phase.max - sum(seq);
79 
80 	a.fixUp;
81 	a.fixedPosition.should.be == 111;
82 }
83 
84 version(unittest)
85 @Name("start2endFor40")
86 unittest
87 {
88 	const order = Cursor.Order.forward;
89 	LogRecord[] posLog;
90 	auto seq = sequence.dup;
91 
92 	// scroll from the start for destinationShift in forward direction
93 	auto a = Cursor();
94 	a.scroll(40);
95 	test!order(a, seq, posLog);
96 	a.fixedPosition.should.be == 31;
97 	a.fixedPosition.should.be == sum(seq[0..2]);
98 	posLog.map!"a.pos".should.be == [0, 10, 31];
99 	a.phase.should.be == 9;
100 }
101 
102 version(unittest)
103 @Name("start2endFor43")
104 unittest
105 {
106 	const order = Cursor.Order.forward;
107 	LogRecord[] posLog;
108 	auto seq = sequence.dup;
109 
110 	// scroll from the start for destinationShift in forward direction
111 	// destinationShift is equal to the start position of the element
112 	auto a = Cursor();
113 	a.scroll(43);
114 	test!order(a, seq, posLog);
115 	a.fixedPosition.should.be == 43;
116 	posLog.map!"a.pos".should.be == [0, 10, 31, 43];
117 	a.phase.should.be == 0;
118 }
119 
120 version(unittest)
121 @Name("end2start")
122 unittest
123 {
124 	const order = Cursor.Order.backward;
125 	LogRecord[] posLog;
126 	auto seq = sequence.dup;
127 	const scroll = 130;
128 	const total = sum(seq);
129 
130 	// scroll from the end to the start in backward direction
131 	auto a = Cursor(sum(seq));
132 	a.scroll(scroll);
133 	test!order(a, seq, posLog);
134 	a.fixedPosition.should.be == total;
135 	posLog.map!"a.pos".should.be == [111, 95, 80, 56, 43, 31, 10, 0];
136 	total.should.be == 128;
137 	a.phase.should.be == scroll - total;
138 	a.phase.should.be == 2;
139 }
140 
141 version(unittest)
142 @Name("end2startFor83")
143 unittest
144 {
145 	const order = Cursor.Order.backward;
146 	LogRecord[] posLog;
147 	auto seq = sequence.dup;
148 
149 	// scroll from the end to the start for destinationShift in backward direction
150 	auto a = Cursor(sum(seq));
151 	a.scroll(83);
152 	test!order(a, seq, posLog);
153 	a.fixedPosition.should.be == 72;
154 	posLog.map!"a.pos".should.be == [111, 95, 80, 56, 43];
155 	a.phase.should.be == 11;
156 }
157 
158 version(unittest)
159 @Name("end2startFor85")
160 unittest
161 {
162 	const order = Cursor.Order.backward;
163 	LogRecord[] posLog;
164 	auto seq = sequence.dup;
165 
166 	// scroll from the end to the start for destinationShift in backward direction
167 	// destinationShift is equal to the start position of the element
168 	auto a = Cursor(sum(seq));
169 	a.scroll(85);
170 	test!order(a, seq, posLog);
171 	a.fixedPosition.should.be == 85;
172 	posLog.map!"a.pos".should.be == [111, 95, 80, 56, 43, 31];
173 	a.phase.should.be == 0;
174 }
175 
176 version(unittest)
177 @Name("end2startFor128")
178 unittest
179 {
180 	const order = Cursor.Order.backward;
181 	LogRecord[] posLog;
182 	auto seq = sequence.dup;
183 
184 	// scroll from the end to the start for destinationShift in backward direction
185 	// destinationShift is equal to the start position of the element
186 	auto a = Cursor(sum(seq));
187 	a.scroll(128);
188 	test!order(a, seq, posLog);
189 	a.fixedPosition.should.be == 128;
190 	posLog.map!"a.pos".should.be == [111, 95, 80, 56, 43, 31, 10, 0];
191 	a.phase.should.be == 0;
192 }
193 
194 version(unittest)
195 @Name("end2startFor300")
196 unittest
197 {
198 	const order = Cursor.Order.backward;
199 	LogRecord[] posLog;
200 	auto seq = sequence.dup;
201 
202 	// scroll from the end to the start for destinationShift in backward direction
203 	// destinationShift is equal to the start position of the element
204 	auto a = Cursor(sum(seq));
205 	a.scroll(300);
206 	test!order(a, seq, posLog);
207 	a.fixedPosition.should.be == 128;
208 	posLog.map!"a.pos".should.be == [111, 95, 80, 56, 43, 31, 10, 0];
209 	a.phase.should.be == 172;
210 }
211 
212 version(unittest)
213 @Name("end2startSubSequence")
214 unittest
215 {
216 	const order = Cursor.Order.backward;
217 	LogRecord[] posLog;
218 
219 	// working with subsequence
220 	// scroll from the end to the start for destinationShift in backward direction
221 	auto subseq = sequence.dup[0..5];
222 
223 	auto a = Cursor(sum(subseq));
224 	a.scroll(85);
225 	test!order(a, subseq, posLog);
226 	a.fixedPosition.should.be == 80;
227 	posLog.map!"a.pos".should.be == [56, 43, 31, 10, 0];
228 	a.phase.should.be == 5;
229 }