Debian Codegraph @

libphobos/src/std/internal/test/dummyrange.d

raw
1 /**
2 For testing only.
3 Used with the dummy ranges for testing higher order ranges.
4 */
5 module std.internal.test.dummyrange;
6
7 import std.meta;
8 import std.range.primitives;
9 import std.typecons;
10
11 enum RangeType
12 {
13 Input,
14 Forward,
15 Bidirectional,
16 Random
17 }
18
19 enum Length
20 {
21 Yes,
22 No
23 }
24
25 enum ReturnBy
26 {
27 Reference,
28 Value
29 }
30
31 import std.traits : isArray;
32
33 // Range that's useful for testing other higher order ranges,
34 // can be parametrized with attributes. It just dumbs down an array of
35 // numbers 1 .. 10.
36 struct DummyRange(ReturnBy _r, Length _l, RangeType _rt, T = uint[])
37 if (isArray!T)
38 {
39 private static immutable uinttestData =
40 [1U, 2U, 3U, 4U, 5U, 6U, 7U, 8U, 9U, 10U];
41 // These enums are so that the template params are visible outside
42 // this instantiation.
43 enum r = _r;
44 enum l = _l;
45 enum rt = _rt;
46
47 static if (is(T == uint[]))
48 {
49 T arr = uinttestData;
50 }
51 else
52 {
53 T arr;
54 }
55
56 alias RetType = ElementType!(T);
57 alias RetTypeNoAutoDecoding = ElementEncodingType!(T);
58
59 void reinit()
60 {
61 // Workaround for DMD bug 4378
62 static if (is(T == uint[]))
63 {
64 arr = uinttestData.dup;
65 }
66 }
67
68 void popFront()
69 {
70 arr = arr[1..$];
71 }
72
73 @property bool empty() const
74 {
75 return arr.length == 0;
76 }
77
78 static if (r == ReturnBy.Reference)
79 {
80 @property ref inout(RetType) front() inout
81 {
82 return arr[0];
83 }
84 }
85 else
86 {
87 @property RetType front() const
88 {
89 return arr[0];
90 }
91
92 @property void front(RetTypeNoAutoDecoding val)
93 {
94 arr[0] = val;
95 }
96 }
97
98 static if (rt >= RangeType.Forward)
99 {
100 @property typeof(this) save()
101 {
102 return this;
103 }
104 }
105
106 static if (rt >= RangeType.Bidirectional)
107 {
108 void popBack()
109 {
110 arr = arr[0..$ - 1];
111 }
112
113 static if (r == ReturnBy.Reference)
114 {
115 @property ref inout(RetType) back() inout
116 {
117 return arr[$ - 1];
118 }
119 }
120 else
121 {
122 @property RetType back() const
123 {
124 return arr[$ - 1];
125 }
126
127 @property void back(RetTypeNoAutoDecoding val)
128 {
129 arr[$ - 1] = val;
130 }
131 }
132 }
133
134 static if (rt >= RangeType.Random)
135 {
136 static if (r == ReturnBy.Reference)
137 {
138 ref inout(RetType) opIndex(size_t index) inout
139 {
140 return arr[index];
141 }
142 }
143 else
144 {
145 RetType opIndex(size_t index) const
146 {
147 return arr[index];
148 }
149
150 RetType opIndexAssign(RetTypeNoAutoDecoding val, size_t index)
151 {
152 return arr[index] = val;
153 }
154
155 RetType opIndexOpAssign(string op)(RetTypeNoAutoDecoding value, size_t index)
156 {
157 mixin("return arr[index] " ~ op ~ "= value;");
158 }
159
160 RetType opIndexUnary(string op)(size_t index)
161 {
162 mixin("return " ~ op ~ "arr[index];");
163 }
164 }
165
166 typeof(this) opSlice(size_t lower, size_t upper)
167 {
168 auto ret = this;
169 ret.arr = arr[lower .. upper];
170 return ret;
171 }
172
173 typeof(this) opSlice()
174 {
175 return this;
176 }
177 }
178
179 static if (l == Length.Yes)
180 {
181 @property size_t length() const
182 {
183 return arr.length;
184 }
185
186 alias opDollar = length;
187 }
188 }
189
190 enum dummyLength = 10;
191
192 alias AllDummyRanges = AliasSeq!(
193 DummyRange!(ReturnBy.Reference, Length.Yes, RangeType.Forward),
194 DummyRange!(ReturnBy.Reference, Length.Yes, RangeType.Bidirectional),
195 DummyRange!(ReturnBy.Reference, Length.Yes, RangeType.Random),
196 DummyRange!(ReturnBy.Reference, Length.No, RangeType.Forward),
197 DummyRange!(ReturnBy.Reference, Length.No, RangeType.Bidirectional),
198 DummyRange!(ReturnBy.Value, Length.Yes, RangeType.Input),
199 DummyRange!(ReturnBy.Value, Length.Yes, RangeType.Forward),
200 DummyRange!(ReturnBy.Value, Length.Yes, RangeType.Bidirectional),
201 DummyRange!(ReturnBy.Value, Length.Yes, RangeType.Random),
202 DummyRange!(ReturnBy.Value, Length.No, RangeType.Input),
203 DummyRange!(ReturnBy.Value, Length.No, RangeType.Forward),
204 DummyRange!(ReturnBy.Value, Length.No, RangeType.Bidirectional)
205 );
206
207 template AllDummyRangesType(T)
208 {
209 alias AllDummyRangesType = AliasSeq!(
210 DummyRange!(ReturnBy.Reference, Length.Yes, RangeType.Forward, T),
211 DummyRange!(ReturnBy.Reference, Length.Yes, RangeType.Bidirectional, T),
212 DummyRange!(ReturnBy.Reference, Length.Yes, RangeType.Random, T),
213 DummyRange!(ReturnBy.Reference, Length.No, RangeType.Forward, T),
214 DummyRange!(ReturnBy.Reference, Length.No, RangeType.Bidirectional, T),
215 DummyRange!(ReturnBy.Value, Length.Yes, RangeType.Input, T),
216 DummyRange!(ReturnBy.Value, Length.Yes, RangeType.Forward, T),
217 DummyRange!(ReturnBy.Value, Length.Yes, RangeType.Bidirectional, T),
218 DummyRange!(ReturnBy.Value, Length.Yes, RangeType.Random, T),
219 DummyRange!(ReturnBy.Value, Length.No, RangeType.Input, T),
220 DummyRange!(ReturnBy.Value, Length.No, RangeType.Forward, T),
221 DummyRange!(ReturnBy.Value, Length.No, RangeType.Bidirectional, T)
222 );
223 }
224
225 /**
226 Tests whether forward, bidirectional and random access properties are
227 propagated properly from the base range(s) R to the higher order range
228 H. Useful in combination with DummyRange for testing several higher
229 order ranges.
230 */
231 template propagatesRangeType(H, R...)
232 {
233 static if (allSatisfy!(isRandomAccessRange, R))
234 enum bool propagatesRangeType = isRandomAccessRange!H;
235 else static if (allSatisfy!(isBidirectionalRange, R))
236 enum bool propagatesRangeType = isBidirectionalRange!H;
237 else static if (allSatisfy!(isForwardRange, R))
238 enum bool propagatesRangeType = isForwardRange!H;
239 else
240 enum bool propagatesRangeType = isInputRange!H;
241 }
242
243 template propagatesLength(H, R...)
244 {
245 static if (allSatisfy!(hasLength, R))
246 enum bool propagatesLength = hasLength!H;
247 else
248 enum bool propagatesLength = !hasLength!H;
249 }
250
251 /**
252 Reference type input range
253 */
254 class ReferenceInputRange(T)
255 {
256 import std.array : array;
257
258 this(Range)(Range r)
259 if (isInputRange!Range) {_payload = array(r);}
260 final @property ref T front() {return _payload.front;}
261 final void popFront() {_payload.popFront();}
262 final @property bool empty() {return _payload.empty;}
263 protected T[] _payload;
264 }
265
266 /**
267 Infinite input range
268 */
269 class ReferenceInfiniteInputRange(T)
270 {
271 this(T first = T.init) {_val = first;}
272 final @property T front() {return _val;}
273 final void popFront() {++_val;}
274 enum bool empty = false;
275 protected T _val;
276 }
277
278 /**
279 Reference forward range
280 */
281 class ReferenceForwardRange(T) : ReferenceInputRange!T
282 {
283 this(Range)(Range r)
284 if (isInputRange!Range) {super(r);}
285 final @property auto save(this This)() {return new This( _payload);}
286 }
287
288 /**
289 Infinite forward range
290 */
291 class ReferenceInfiniteForwardRange(T) : ReferenceInfiniteInputRange!T
292 {
293 this(T first = T.init) {super(first);}
294 final @property ReferenceInfiniteForwardRange save()
295 {return new ReferenceInfiniteForwardRange!T(_val);}
296 }
297
298 /**
299 Reference bidirectional range
300 */
301 class ReferenceBidirectionalRange(T) : ReferenceForwardRange!T
302 {
303 this(Range)(Range r)
304 if (isInputRange!Range) {super(r);}
305 final @property ref T back() {return _payload.back;}
306 final void popBack() {_payload.popBack();}
307 }
308
309 @safe unittest
310 {
311 static assert(isInputRange!(ReferenceInputRange!int));
312 static assert(isInputRange!(ReferenceInfiniteInputRange!int));
313
314 static assert(isForwardRange!(ReferenceForwardRange!int));
315 static assert(isForwardRange!(ReferenceInfiniteForwardRange!int));
316
317 static assert(isBidirectionalRange!(ReferenceBidirectionalRange!int));
318 }
319
320 private:
321
322 pure struct Cmp(T)
323 if (is(T == uint))
324 {
325 static auto iota(size_t low = 1, size_t high = 11)
326 {
327 import std.range : iota;
328 return iota(cast(uint) low, cast(uint) high);
329 }
330
331 static void initialize(ref uint[] arr)
332 {
333 import std.array : array;
334 arr = iota().array;
335 }
336
337 static bool function(uint,uint) cmp = function(uint a, uint b) { return a == b; };
338
339 enum dummyValue = 1337U;
340 enum dummyValueRslt = 1337U * 2;
341 }
342
343 pure struct Cmp(T)
344 if (is(T == double))
345 {
346 import std.math.operations : isClose;
347
348 static auto iota(size_t low = 1, size_t high = 11)
349 {
350 import std.range : iota;
351 return iota(cast(double) low, cast(double) high, 1.0);
352 }
353
354 static void initialize(ref double[] arr)
355 {
356 import std.array : array;
357 arr = iota().array;
358 }
359
360 alias cmp = isClose!(double,double,double);
361
362 enum dummyValue = 1337.0;
363 enum dummyValueRslt = 1337.0 * 2.0;
364 }
365
366 struct TestFoo
367 {
368 int a;
369
370 bool opEquals(const ref TestFoo other) const
371 {
372 return this.a == other.a;
373 }
374
375 TestFoo opBinary(string op)(TestFoo other)
376 {
377 TestFoo ret = this;
378 mixin("ret.a " ~ op ~ "= other.a;");
379 return ret;
380 }
381
382 TestFoo opOpAssign(string op)(TestFoo other)
383 {
384 mixin("this.a " ~ op ~ "= other.a;");
385 return this;
386 }
387 }
388
389 pure struct Cmp(T)
390 if (is(T == TestFoo))
391 {
392 static auto iota(size_t low = 1, size_t high = 11)
393 {
394 import std.algorithm.iteration : map;
395 import std.range : iota;
396 return iota(cast(int) low, cast(int) high).map!(a => TestFoo(a));
397 }
398
399 static void initialize(ref TestFoo[] arr)
400 {
401 import std.array : array;
402 arr = iota().array;
403 }
404
405 static bool function(TestFoo,TestFoo) cmp = function(TestFoo a, TestFoo b)
406 {
407 return a.a == b.a;
408 };
409
410 @property static TestFoo dummyValue()
411 {
412 return TestFoo(1337);
413 }
414
415 @property static TestFoo dummyValueRslt()
416 {
417 return TestFoo(1337 * 2);
418 }
419 }
420
421 @system unittest
422 {
423 import std.algorithm.comparison : equal;
424 import std.range : iota, retro, repeat;
425
426 static void testInputRange(T,Cmp)()
427 {
428 T it;
429 Cmp.initialize(it.arr);
430 for (size_t numRuns = 0; numRuns < 2; ++numRuns)
431 {
432 if (numRuns == 1)
433 {
434 static if (is(immutable ElementType!(T) == immutable uint))
435 {
436 it.reinit();
437 }
438
439 Cmp.initialize(it.arr);
440 }
441
442 assert(equal!(Cmp.cmp)(it, Cmp.iota(1, 11)));
443
444 static if (hasLength!T)
445 {
446 assert(it.length == 10);
447 }
448
449 assert(!Cmp.cmp(it.front, Cmp.dummyValue));
450 auto s = it.front;
451 it.front = Cmp.dummyValue;
452 assert(Cmp.cmp(it.front, Cmp.dummyValue));
453 it.front = s;
454
455 auto cmp = Cmp.iota(1,11);
456
457 size_t jdx = 0;
458 while (!it.empty && !cmp.empty)
459 {
460 static if (hasLength!T)
461 {
462 assert(it.length == 10 - jdx);
463 }
464
465 assert(Cmp.cmp(it.front, cmp.front));
466 it.popFront();
467 cmp.popFront();
468
469 ++jdx;
470 }
471
472 assert(it.empty);
473 assert(cmp.empty);
474 }
475
476 }
477
478 static void testForwardRange(T,Cmp)()
479 {
480 T it;
481 Cmp.initialize(it.arr);
482 auto s = it.save();
483 s.popFront();
484 assert(!Cmp.cmp(s.front, it.front));
485 }
486
487 static void testBidirectionalRange(T,Cmp)()
488 {
489 T it;
490 Cmp.initialize(it.arr);
491 assert(equal!(Cmp.cmp)(it.retro, Cmp.iota().retro));
492
493 auto s = it.back;
494 assert(!Cmp.cmp(s, Cmp.dummyValue));
495 it.back = Cmp.dummyValue;
496 assert( Cmp.cmp(it.back, Cmp.dummyValue));
497 it.back = s;
498 }
499
500 static void testRandomAccessRange(T,Cmp)()
501 {
502 T it;
503 Cmp.initialize(it.arr);
504 size_t idx = 0;
505 foreach (jt; it)
506 {
507 assert(it[idx] == jt);
508
509 T copy = it[idx .. $];
510 auto cmp = Cmp.iota(idx + 1, it.length + 1);
511 assert(equal!(Cmp.cmp)(copy, cmp));
512
513 ++idx;
514 }
515
516 {
517 auto copy = it;
518 copy.arr = it.arr.dup;
519 for (size_t i = 0; i < copy.length; ++i)
520 {
521 copy[i] = Cmp.dummyValue;
522 copy[i] += Cmp.dummyValue;
523 }
524 assert(equal!(Cmp.cmp)(copy, Cmp.dummyValueRslt.repeat(copy.length)));
525 }
526
527 static if (it.r == ReturnBy.Reference)
528 {
529 T copy;
530 copy.arr = it.arr.dup;
531 for (size_t i = 0; i < copy.length; ++i)
532 {
533 copy[i] = Cmp.dummyValue;
534 copy[i] += Cmp.dummyValue;
535 }
536
537 assert(equal!(Cmp.cmp)(copy, Cmp.dummyValueRslt.repeat(copy.length)));
538 }
539 }
540
541 import std.meta : AliasSeq;
542
543 static foreach (S; AliasSeq!(uint, double, TestFoo))
544 {
545 foreach (T; AllDummyRangesType!(S[]))
546 {
547 testInputRange!(T,Cmp!S)();
548
549 static if (isForwardRange!T)
550 {
551 testForwardRange!(T,Cmp!S)();
552 }
553
554 static if (isBidirectionalRange!T)
555 {
556 testBidirectionalRange!(T,Cmp!S)();
557 }
558
559 static if (isRandomAccessRange!T)
560 {
561 testRandomAccessRange!(T,Cmp!S)();
562 }
563 }
564 }
565 }