Debian Codegraph @

libphobos/testsuite/libphobos.phobos/std_range_primitives.d

raw
1 @safe unittest
2 {
3 import std.range.primitives;
4
5 struct A {}
6 struct B
7 {
8 void popFront();
9 @property bool empty();
10 @property int front();
11 }
12 static assert(!isInputRange!A);
13 static assert( isInputRange!B);
14 static assert( isInputRange!(int[]));
15 static assert( isInputRange!(char[]));
16 static assert(!isInputRange!(char[4]));
17 static assert( isInputRange!(inout(int)[]));
18 static assert(!isInputRange!(int[], string));
19 static assert( isInputRange!(int[], int));
20 static assert( isInputRange!(int[], const int));
21 static assert(!isInputRange!(int[], immutable int));
22
23 static assert(!isInputRange!(const(int)[], int));
24 static assert( isInputRange!(const(int)[], const int));
25 static assert(!isInputRange!(const(int)[], immutable int));
26
27 static assert(!isInputRange!(immutable(int)[], int));
28 static assert( isInputRange!(immutable(int)[], const int));
29 static assert( isInputRange!(immutable(int)[], immutable int));
30
31 static struct NotDefaultConstructible
32 {
33 @disable this();
34 void popFront();
35 @property bool empty();
36 @property int front();
37 }
38 static assert( isInputRange!NotDefaultConstructible);
39
40 static struct NotDefaultConstructibleOrCopyable
41 {
42 @disable this();
43 @disable this(this);
44 void popFront();
45 @property bool empty();
46 @property int front();
47 }
48 static assert(isInputRange!NotDefaultConstructibleOrCopyable);
49
50 static struct Frontless
51 {
52 void popFront();
53 @property bool empty();
54 }
55 static assert(!isInputRange!Frontless);
56
57 static struct VoidFront
58 {
59 void popFront();
60 @property bool empty();
61 void front();
62 }
63 static assert(!isInputRange!VoidFront);
64 }
65
66 @safe pure unittest
67 {
68 import std.range.primitives;
69
70 import std.traits : isSomeChar;
71
72 static struct A
73 {
74 string data;
75
76 void put(C)(C c)
77 if (isSomeChar!C)
78 {
79 data ~= c;
80 }
81 }
82 static assert(isOutputRange!(A, char));
83
84 auto a = A();
85 put(a, "Hello");
86 assert(a.data == "Hello");
87 }
88
89 @safe pure nothrow unittest
90 {
91 import std.range.primitives;
92
93 int[] a = [1, 2, 3], b = [10, 20];
94 auto c = a;
95 put(a, b);
96 assert(c == [10, 20, 3]);
97 // at this point, a was advanced twice, so it only contains
98 // its last element while c represents the whole array
99 assert(a == [3]);
100 }
101
102 @safe pure unittest
103 {
104 import std.range.primitives;
105
106 // the elements must be mutable, so using string or const(char)[]
107 // won't compile
108 char[] s1 = new char[13];
109 auto r1 = s1;
110 put(r1, "Hello, World!"w);
111 assert(s1 == "Hello, World!");
112 }
113
114 @safe unittest
115 {
116 import std.range.primitives;
117
118 void myprint(scope const(char)[] s) { }
119 static assert(isOutputRange!(typeof(&myprint), char));
120
121 static assert( isOutputRange!(char[], char));
122 static assert( isOutputRange!(dchar[], wchar));
123 static assert( isOutputRange!(dchar[], dchar));
124 }
125
126 @safe unittest
127 {
128 import std.range.primitives;
129
130 static assert(!isForwardRange!(int));
131 static assert( isForwardRange!(int[]));
132 static assert( isForwardRange!(inout(int)[]));
133
134 static assert( isForwardRange!(int[], const int));
135 static assert(!isForwardRange!(int[], immutable int));
136
137 static assert(!isForwardRange!(const(int)[], int));
138 static assert( isForwardRange!(const(int)[], const int));
139 static assert(!isForwardRange!(const(int)[], immutable int));
140
141 static assert(!isForwardRange!(immutable(int)[], int));
142 static assert( isForwardRange!(immutable(int)[], const int));
143 static assert( isForwardRange!(immutable(int)[], immutable int));
144 }
145
146 @safe unittest
147 {
148 import std.range.primitives;
149
150 alias R = int[];
151 R r = [0,1];
152 static assert(isForwardRange!R); // is forward range
153 r.popBack(); // can invoke popBack
154 auto t = r.back; // can get the back of the range
155 auto w = r.front;
156 static assert(is(typeof(t) == typeof(w))); // same type for front and back
157
158 // Checking the element type
159 static assert( isBidirectionalRange!(int[], const int));
160 static assert(!isBidirectionalRange!(int[], immutable int));
161
162 static assert(!isBidirectionalRange!(const(int)[], int));
163 static assert( isBidirectionalRange!(const(int)[], const int));
164 static assert(!isBidirectionalRange!(const(int)[], immutable int));
165
166 static assert(!isBidirectionalRange!(immutable(int)[], int));
167 static assert( isBidirectionalRange!(immutable(int)[], const int));
168 static assert( isBidirectionalRange!(immutable(int)[], immutable int));
169 }
170
171 @safe unittest
172 {
173 import std.range.primitives;
174
175 import std.traits : isAggregateType, isAutodecodableString;
176
177 alias R = int[];
178
179 // range is finite and bidirectional or infinite and forward.
180 static assert(isBidirectionalRange!R ||
181 isForwardRange!R && isInfinite!R);
182
183 R r = [0,1];
184 auto e = r[1]; // can index
185 auto f = r.front;
186 static assert(is(typeof(e) == typeof(f))); // same type for indexed and front
187 static assert(!(isAutodecodableString!R && !isAggregateType!R)); // narrow strings cannot be indexed as ranges
188 static assert(hasLength!R || isInfinite!R); // must have length or be infinite
189
190 // $ must work as it does with arrays if opIndex works with $
191 static if (is(typeof(r[$])))
192 {
193 static assert(is(typeof(f) == typeof(r[$])));
194
195 // $ - 1 doesn't make sense with infinite ranges but needs to work
196 // with finite ones.
197 static if (!isInfinite!R)
198 static assert(is(typeof(f) == typeof(r[$ - 1])));
199 }
200
201 // Checking the element type
202 static assert( isRandomAccessRange!(int[], const int));
203 static assert(!isRandomAccessRange!(int[], immutable int));
204
205 static assert(!isRandomAccessRange!(const(int)[], int));
206 static assert( isRandomAccessRange!(const(int)[], const int));
207 static assert(!isRandomAccessRange!(const(int)[], immutable int));
208
209 static assert(!isRandomAccessRange!(immutable(int)[], int));
210 static assert( isRandomAccessRange!(immutable(int)[], const int));
211 static assert( isRandomAccessRange!(immutable(int)[], immutable int));
212 }
213
214 @safe unittest
215 {
216 import std.range.primitives;
217
218 import std.algorithm.iteration : map;
219 import std.range : iota, repeat;
220
221 static struct HasPostblit
222 {
223 this(this) {}
224 }
225
226 auto nonMobile = map!"a"(repeat(HasPostblit.init));
227 static assert(!hasMobileElements!(typeof(nonMobile)));
228 static assert( hasMobileElements!(int[]));
229 static assert( hasMobileElements!(inout(int)[]));
230 static assert( hasMobileElements!(typeof(iota(1000))));
231
232 static assert( hasMobileElements!( string));
233 static assert( hasMobileElements!(dstring));
234 static assert( hasMobileElements!( char[]));
235 static assert( hasMobileElements!(dchar[]));
236 }
237
238 @safe unittest
239 {
240 import std.range.primitives;
241
242 import std.range : iota;
243
244 // Standard arrays: returns the type of the elements of the array
245 static assert(is(ElementType!(int[]) == int));
246
247 // Accessing .front retrieves the decoded dchar
248 static assert(is(ElementType!(char[]) == dchar)); // rvalue
249 static assert(is(ElementType!(dchar[]) == dchar)); // lvalue
250
251 // Ditto
252 static assert(is(ElementType!(string) == dchar));
253 static assert(is(ElementType!(dstring) == immutable(dchar)));
254
255 // For ranges it gets the type of .front.
256 auto range = iota(0, 10);
257 static assert(is(ElementType!(typeof(range)) == int));
258 }
259
260 @safe unittest
261 {
262 import std.range.primitives;
263
264 import std.range : iota;
265 // internally the range stores the encoded type
266 static assert(is(ElementEncodingType!(char[]) == char));
267
268 static assert(is(ElementEncodingType!(wstring) == immutable(wchar)));
269
270 static assert(is(ElementEncodingType!(byte[]) == byte));
271
272 auto range = iota(0, 10);
273 static assert(is(ElementEncodingType!(typeof(range)) == int));
274 }
275
276 @safe unittest
277 {
278 import std.range.primitives;
279
280 static assert(!hasSwappableElements!(const int[]));
281 static assert(!hasSwappableElements!(const(int)[]));
282 static assert(!hasSwappableElements!(inout(int)[]));
283 static assert( hasSwappableElements!(int[]));
284
285 static assert(!hasSwappableElements!( string));
286 static assert(!hasSwappableElements!(dstring));
287 static assert(!hasSwappableElements!( char[]));
288 static assert( hasSwappableElements!(dchar[]));
289 }
290
291 @safe unittest
292 {
293 import std.range.primitives;
294
295 static assert(!hasAssignableElements!(const int[]));
296 static assert(!hasAssignableElements!(const(int)[]));
297 static assert( hasAssignableElements!(int[]));
298 static assert(!hasAssignableElements!(inout(int)[]));
299
300 static assert(!hasAssignableElements!( string));
301 static assert(!hasAssignableElements!(dstring));
302 static assert(!hasAssignableElements!( char[]));
303 static assert( hasAssignableElements!(dchar[]));
304 }
305
306 @safe unittest
307 {
308 import std.range.primitives;
309
310 import std.range : iota, chain;
311
312 static assert( hasLvalueElements!(int[]));
313 static assert( hasLvalueElements!(const(int)[]));
314 static assert( hasLvalueElements!(inout(int)[]));
315 static assert( hasLvalueElements!(immutable(int)[]));
316 static assert(!hasLvalueElements!(typeof(iota(3))));
317
318 static assert(!hasLvalueElements!( string));
319 static assert( hasLvalueElements!(dstring));
320 static assert(!hasLvalueElements!( char[]));
321 static assert( hasLvalueElements!(dchar[]));
322
323 auto c = chain([1, 2, 3], [4, 5, 6]);
324 static assert( hasLvalueElements!(typeof(c)));
325 }
326
327 @safe unittest
328 {
329 import std.range.primitives;
330
331 static assert(!hasLength!(char[]));
332 static assert( hasLength!(int[]));
333 static assert( hasLength!(inout(int)[]));
334
335 struct A { size_t length() { return 0; } }
336 struct B { @property size_t length() { return 0; } }
337 static assert( hasLength!(A));
338 static assert( hasLength!(B));
339 }
340
341 @safe unittest
342 {
343 import std.range.primitives;
344
345 import std.range : Repeat;
346 static assert(!isInfinite!(int[]));
347 static assert( isInfinite!(Repeat!(int)));
348 }
349
350 @safe unittest
351 {
352 import std.range.primitives;
353
354 import std.range : takeExactly;
355 static assert( hasSlicing!(int[]));
356 static assert( hasSlicing!(const(int)[]));
357 static assert(!hasSlicing!(const int[]));
358 static assert( hasSlicing!(inout(int)[]));
359 static assert(!hasSlicing!(inout int []));
360 static assert( hasSlicing!(immutable(int)[]));
361 static assert(!hasSlicing!(immutable int[]));
362 static assert(!hasSlicing!string);
363 static assert( hasSlicing!dstring);
364
365 enum rangeFuncs = "@property int front();" ~
366 "void popFront();" ~
367 "@property bool empty();" ~
368 "@property auto save() { return this; }" ~
369 "@property size_t length();";
370
371 struct A { mixin(rangeFuncs); int opSlice(size_t, size_t); }
372 struct B { mixin(rangeFuncs); B opSlice(size_t, size_t); }
373 struct C { mixin(rangeFuncs); @disable this(); C opSlice(size_t, size_t); }
374 struct D { mixin(rangeFuncs); int[] opSlice(size_t, size_t); }
375 static assert(!hasSlicing!(A));
376 static assert( hasSlicing!(B));
377 static assert( hasSlicing!(C));
378 static assert(!hasSlicing!(D));
379
380 struct InfOnes
381 {
382 enum empty = false;
383 void popFront() {}
384 @property int front() { return 1; }
385 @property InfOnes save() { return this; }
386 auto opSlice(size_t i, size_t j) { return takeExactly(this, j - i); }
387 auto opSlice(size_t i, Dollar d) { return this; }
388
389 struct Dollar {}
390 Dollar opDollar() const { return Dollar.init; }
391 }
392
393 static assert(hasSlicing!InfOnes);
394 }
395
396 @safe unittest
397 {
398 import std.range.primitives;
399
400 import std.range : iota;
401
402 assert(10.iota.walkLength == 10);
403 // iota has a length function, and therefore the
404 // doesn't have to be walked, and the upTo
405 // parameter is ignored
406 assert(10.iota.walkLength(5) == 10);
407 }
408
409 @safe unittest
410 {
411 import std.range.primitives;
412
413 int[] a = [ 1, 2, 3, 4, 5 ];
414 a.popFrontN(2);
415 assert(a == [ 3, 4, 5 ]);
416 a.popFrontN(7);
417 assert(a == [ ]);
418 }
419
420 @safe unittest
421 {
422 import std.range.primitives;
423
424 import std.algorithm.comparison : equal;
425 import std.range : iota;
426 auto LL = iota(1L, 7L);
427 auto r = popFrontN(LL, 2);
428 assert(equal(LL, [3L, 4L, 5L, 6L]));
429 assert(r == 2);
430 }
431
432 @safe unittest
433 {
434 import std.range.primitives;
435
436 int[] a = [ 1, 2, 3, 4, 5 ];
437 a.popBackN(2);
438 assert(a == [ 1, 2, 3 ]);
439 a.popBackN(7);
440 assert(a == [ ]);
441 }
442
443 @safe unittest
444 {
445 import std.range.primitives;
446
447 import std.algorithm.comparison : equal;
448 import std.range : iota;
449 auto LL = iota(1L, 7L);
450 auto r = popBackN(LL, 2);
451 assert(equal(LL, [1L, 2L, 3L, 4L]));
452 assert(r == 2);
453 }
454
455 @safe unittest
456 {
457 import std.range.primitives;
458
459 import std.algorithm.comparison : equal;
460 import std.algorithm.iteration : filterBidirectional;
461
462 auto a = [1, 2, 3];
463 a.popFrontExactly(1);
464 assert(a == [2, 3]);
465 a.popBackExactly(1);
466 assert(a == [2]);
467
468 string s = "日本語";
469 s.popFrontExactly(1);
470 assert(s == "本語");
471 s.popBackExactly(1);
472 assert(s == "本");
473
474 auto bd = filterBidirectional!"true"([1, 2, 3]);
475 bd.popFrontExactly(1);
476 assert(bd.equal([2, 3]));
477 bd.popBackExactly(1);
478 assert(bd.equal([2]));
479 }
480
481 @safe unittest
482 {
483 import std.range.primitives;
484
485 auto a = [ 1, 2, 3 ];
486 assert(moveFront(a) == 1);
487 assert(a.length == 3);
488
489 // define a perfunctory input range
490 struct InputRange
491 {
492 enum bool empty = false;
493 enum int front = 7;
494 void popFront() {}
495 int moveFront() { return 43; }
496 }
497 InputRange r;
498 // calls r.moveFront
499 assert(moveFront(r) == 43);
500 }
501
502 @safe unittest
503 {
504 import std.range.primitives;
505
506 struct TestRange
507 {
508 int payload = 5;
509 @property bool empty() { return false; }
510 @property TestRange save() { return this; }
511 @property ref int front() return { return payload; }
512 @property ref int back() return { return payload; }
513 void popFront() { }
514 void popBack() { }
515 }
516 static assert(isBidirectionalRange!TestRange);
517 TestRange r;
518 auto x = moveBack(r);
519 assert(x == 5);
520 }
521
522 @safe unittest
523 {
524 import std.range.primitives;
525
526 auto a = [1,2,3,4];
527 foreach (idx, it; a)
528 {
529 assert(it == moveAt(a, idx));
530 }
531 }
532
533 @safe pure nothrow unittest
534 {
535 import std.range.primitives;
536
537 auto a = [ 1, 2, 3 ];
538 assert(!a.empty);
539 assert(a[3 .. $].empty);
540
541 int[string] b;
542 assert(b.empty);
543 b["zero"] = 0;
544 assert(!b.empty);
545 }
546
547 @safe pure nothrow unittest
548 {
549 import std.range.primitives;
550
551 auto a = [ 1, 2, 3 ];
552 auto b = a.save;
553 assert(b is a);
554 }
555
556 @safe pure nothrow unittest
557 {
558 import std.range.primitives;
559
560 auto a = [ 1, 2, 3 ];
561 a.popFront();
562 assert(a == [ 2, 3 ]);
563 }
564
565 @safe pure nothrow unittest
566 {
567 import std.range.primitives;
568
569 auto a = [ 1, 2, 3 ];
570 a.popBack();
571 assert(a == [ 1, 2 ]);
572 }
573
574 @safe pure nothrow unittest
575 {
576 import std.range.primitives;
577
578 int[] a = [ 1, 2, 3 ];
579 assert(a.front == 1);
580 }
581
582 @safe pure nothrow unittest
583 {
584 import std.range.primitives;
585
586 int[] a = [ 1, 2, 3 ];
587 assert(a.back == 3);
588 a.back += 4;
589 assert(a.back == 7);
590 }
591