Debian Codegraph @

libphobos/testsuite/libphobos.phobos/std_array.d

raw
1 @safe pure nothrow unittest
2 {
3 import std.array;
4
5 auto a = array([1, 2, 3, 4, 5][]);
6 assert(a == [ 1, 2, 3, 4, 5 ]);
7 }
8
9 @safe pure nothrow unittest
10 {
11 import std.array;
12
13 import std.range.primitives : isRandomAccessRange;
14 import std.traits : isAutodecodableString;
15
16 // note that if autodecoding is turned off, `array` will not transcode these.
17 static if (isAutodecodableString!string)
18 assert("Hello D".array == "Hello D"d);
19 else
20 assert("Hello D".array == "Hello D");
21
22 static if (isAutodecodableString!wstring)
23 assert("Hello D"w.array == "Hello D"d);
24 else
25 assert("Hello D"w.array == "Hello D"w);
26
27 static assert(isRandomAccessRange!dstring == true);
28 }
29
30 @safe pure unittest
31 {
32 import std.array;
33
34 import std.range : repeat, zip;
35 import std.typecons : tuple;
36 import std.range.primitives : autodecodeStrings;
37 auto a = assocArray(zip([0, 1, 2], ["a", "b", "c"])); // aka zipMap
38 static assert(is(typeof(a) == string[int]));
39 assert(a == [0:"a", 1:"b", 2:"c"]);
40
41 auto b = assocArray([ tuple("foo", "bar"), tuple("baz", "quux") ]);
42 static assert(is(typeof(b) == string[string]));
43 assert(b == ["foo":"bar", "baz":"quux"]);
44
45 static if (autodecodeStrings)
46 alias achar = dchar;
47 else
48 alias achar = immutable(char);
49 auto c = assocArray("ABCD", true.repeat);
50 static assert(is(typeof(c) == bool[achar]));
51 bool[achar] expected = ['D':true, 'A':true, 'B':true, 'C':true];
52 assert(c == expected);
53 }
54
55 @safe pure nothrow unittest
56 {
57 import std.array;
58
59 import std.algorithm.sorting : sort;
60 import std.typecons : tuple, Tuple;
61
62 auto aa = ["a": 1, "b": 2, "c": 3];
63 Tuple!(string, int)[] pairs;
64
65 // Iteration over key/value pairs.
66 foreach (pair; aa.byPair)
67 {
68 if (pair.key == "b")
69 pairs ~= tuple("B", pair.value);
70 else
71 pairs ~= pair;
72 }
73
74 // Iteration order is implementation-dependent, so we should sort it to get
75 // a fixed order.
76 pairs.sort();
77 assert(pairs == [
78 tuple("B", 2),
79 tuple("a", 1),
80 tuple("c", 3)
81 ]);
82 }
83
84 @system nothrow pure unittest
85 {
86 import std.array;
87
88 double[] arr = uninitializedArray!(double[])(100);
89 assert(arr.length == 100);
90
91 double[][] matrix = uninitializedArray!(double[][])(42, 31);
92 assert(matrix.length == 42);
93 assert(matrix[0].length == 31);
94
95 char*[] ptrs = uninitializedArray!(char*[])(100);
96 assert(ptrs.length == 100);
97 }
98
99 @safe pure nothrow unittest
100 {
101 import std.array;
102
103 import std.algorithm.comparison : equal;
104 import std.range : repeat;
105
106 auto arr = minimallyInitializedArray!(int[])(42);
107 assert(arr.length == 42);
108
109 // Elements aren't necessarily initialized to 0, so don't do this:
110 // assert(arr.equal(0.repeat(42)));
111 // If that is needed, initialize the array normally instead:
112 auto arr2 = new int[42];
113 assert(arr2.equal(0.repeat(42)));
114 }
115
116 @safe pure nothrow unittest
117 {
118 import std.array;
119
120 int[] a = [ 10, 11, 12, 13, 14 ];
121 int[] b = a[1 .. 3];
122 assert(overlap(a, b) == [ 11, 12 ]);
123 b = b.dup;
124 // overlap disappears even though the content is the same
125 assert(overlap(a, b).empty);
126
127 static test()() @nogc
128 {
129 auto a = "It's three o'clock"d;
130 auto b = a[5 .. 10];
131 return b.overlap(a);
132 }
133
134 //works at compile-time
135 static assert(test == "three"d);
136 }
137
138 @safe pure nothrow unittest
139 {
140 import std.array;
141
142 import std.meta : AliasSeq;
143
144 // can be used as an alternative implementation of overlap that returns
145 // `true` or `false` instead of a slice of the overlap
146 bool isSliceOf(T)(const scope T[] part, const scope T[] whole)
147 {
148 return part.overlap(whole) is part;
149 }
150
151 auto x = [1, 2, 3, 4, 5];
152
153 assert(isSliceOf(x[3..$], x));
154 assert(isSliceOf(x[], x));
155 assert(!isSliceOf(x, x[3..$]));
156 assert(!isSliceOf([7, 8], x));
157 assert(isSliceOf(null, x));
158
159 // null is a slice of itself
160 assert(isSliceOf(null, null));
161
162 foreach (T; AliasSeq!(int[], const(int)[], immutable(int)[], const int[], immutable int[]))
163 {
164 T a = [1, 2, 3, 4, 5];
165 T b = a;
166 T c = a[1 .. $];
167 T d = a[0 .. 1];
168 T e = null;
169
170 assert(isSliceOf(a, a));
171 assert(isSliceOf(b, a));
172 assert(isSliceOf(a, b));
173
174 assert(isSliceOf(c, a));
175 assert(isSliceOf(c, b));
176 assert(!isSliceOf(a, c));
177 assert(!isSliceOf(b, c));
178
179 assert(isSliceOf(d, a));
180 assert(isSliceOf(d, b));
181 assert(!isSliceOf(a, d));
182 assert(!isSliceOf(b, d));
183
184 assert(isSliceOf(e, a));
185 assert(isSliceOf(e, b));
186 assert(isSliceOf(e, c));
187 assert(isSliceOf(e, d));
188
189 //verifies R-value compatibilty
190 assert(!isSliceOf(a[$ .. $], a));
191 assert(isSliceOf(a[0 .. 0], a));
192 assert(isSliceOf(a, a[0.. $]));
193 assert(isSliceOf(a[0 .. $], a));
194 }
195 }
196
197 @safe pure unittest
198 {
199 import std.array;
200
201 int[] a = [ 1, 2, 3, 4 ];
202 a.insertInPlace(2, [ 1, 2 ]);
203 assert(a == [ 1, 2, 1, 2, 3, 4 ]);
204 a.insertInPlace(3, 10u, 11);
205 assert(a == [ 1, 2, 1, 10, 11, 2, 3, 4]);
206
207 union U
208 {
209 float a = 3.0;
210 int b;
211 }
212
213 U u1 = { b : 3 };
214 U u2 = { b : 4 };
215 U u3 = { b : 5 };
216 U[] unionArr = [u2, u3];
217 unionArr.insertInPlace(2, [u1]);
218 assert(unionArr == [u2, u3, u1]);
219 unionArr.insertInPlace(0, [u3, u2]);
220 assert(unionArr == [u3, u2, u2, u3, u1]);
221
222 static class C
223 {
224 int a;
225 float b;
226
227 this(int a, float b) { this.a = a; this.b = b; }
228 }
229
230 C c1 = new C(42, 1.0);
231 C c2 = new C(0, 0.0);
232 C c3 = new C(int.max, float.init);
233
234 C[] classArr = [c1, c2, c3];
235 insertInPlace(classArr, 3, [c2, c3]);
236 C[5] classArr1 = classArr;
237 assert(classArr1 == [c1, c2, c3, c2, c3]);
238 insertInPlace(classArr, 0, c3, c1);
239 C[7] classArr2 = classArr;
240 assert(classArr2 == [c3, c1, c1, c2, c3, c2, c3]);
241 }
242
243 @safe pure nothrow unittest
244 {
245 import std.array;
246
247 auto a = [1, 2, 3, 4, 5];
248 auto b = a[0 .. 2];
249
250 assert(a.sameHead(b));
251 }
252
253 @safe pure nothrow unittest
254 {
255 import std.array;
256
257 auto a = [1, 2, 3, 4, 5];
258 auto b = a[3..$];
259
260 assert(a.sameTail(b));
261 }
262
263 @safe unittest
264 {
265 import std.array;
266
267 auto a = "abc";
268 auto s = replicate(a, 3);
269
270 assert(s == "abcabcabc");
271
272 auto b = [1, 2, 3];
273 auto c = replicate(b, 3);
274
275 assert(c == [1, 2, 3, 1, 2, 3, 1, 2, 3]);
276
277 auto d = replicate(b, 0);
278
279 assert(d == []);
280 }
281
282 @safe unittest
283 {
284 import std.array;
285
286 import std.uni : isWhite;
287 assert("Learning,D,is,fun".split(",") == ["Learning", "D", "is", "fun"]);
288 assert("Learning D is fun".split!isWhite == ["Learning", "D", "is", "fun"]);
289 assert("Learning D is fun".split(" D ") == ["Learning", "is fun"]);
290 }
291
292 @safe unittest
293 {
294 import std.array;
295
296 string str = "Hello World!";
297 assert(str.split == ["Hello", "World!"]);
298
299 string str2 = "Hello\t\tWorld\t!";
300 assert(str2.split == ["Hello", "World", "!"]);
301 }
302
303 @safe unittest
304 {
305 import std.array;
306
307 assert(split("hello world") == ["hello","world"]);
308 assert(split("192.168.0.1", ".") == ["192", "168", "0", "1"]);
309
310 auto a = split([1, 2, 3, 4, 5, 1, 2, 3, 4, 5], [2, 3]);
311 assert(a == [[1], [4, 5, 1], [4, 5]]);
312 }
313
314 @safe pure nothrow unittest
315 {
316 import std.array;
317
318 assert(join(["hello", "silly", "world"], " ") == "hello silly world");
319 assert(join(["hello", "silly", "world"]) == "hellosillyworld");
320
321 assert(join([[1, 2, 3], [4, 5]], [72, 73]) == [1, 2, 3, 72, 73, 4, 5]);
322 assert(join([[1, 2, 3], [4, 5]]) == [1, 2, 3, 4, 5]);
323
324 const string[] arr = ["apple", "banana"];
325 assert(arr.join(",") == "apple,banana");
326 assert(arr.join() == "applebanana");
327 }
328
329 @safe unittest
330 {
331 import std.array;
332
333 assert("Hello Wörld".replace("o Wö", "o Wo") == "Hello World");
334 assert("Hello Wörld".replace("l", "h") == "Hehho Wörhd");
335 }
336
337 @safe unittest
338 {
339 import std.array;
340
341 size_t changed = 0;
342 assert("Hello Wörld".replace("o Wö", "o Wo", changed) == "Hello World");
343 assert(changed == 1);
344
345 changed = 0;
346 assert("Hello Wörld".replace("l", "h", changed) == "Hehho Wörhd");
347 import std.stdio : writeln;
348 writeln(changed);
349 assert(changed == 3);
350 }
351
352 @safe unittest
353 {
354 import std.array;
355
356 auto arr = [1, 2, 3, 4, 5];
357 auto from = [2, 3];
358 auto to = [4, 6];
359 auto sink = appender!(int[])();
360
361 replaceInto(sink, arr, from, to);
362
363 assert(sink.data == [1, 4, 6, 4, 5]);
364 }
365
366 @safe unittest
367 {
368 import std.array;
369
370 auto arr = [1, 2, 3, 4, 5];
371 auto from = [2, 3];
372 auto to = [4, 6];
373 auto sink = appender!(int[])();
374
375 size_t changed = 0;
376 replaceInto(sink, arr, from, to, changed);
377
378 assert(sink.data == [1, 4, 6, 4, 5]);
379 assert(changed == 1);
380 }
381
382 @safe unittest
383 {
384 import std.array;
385
386 auto a = [ 1, 2, 3, 4 ];
387 auto b = a.replace(1, 3, [ 9, 9, 9 ]);
388 assert(a == [ 1, 2, 3, 4 ]);
389 assert(b == [ 1, 9, 9, 9, 4 ]);
390 }
391
392 @safe unittest
393 {
394 import std.array;
395
396 int[] a = [1, 4, 5];
397 replaceInPlace(a, 1u, 2u, [2, 3, 4]);
398 assert(a == [1, 2, 3, 4, 5]);
399 replaceInPlace(a, 1u, 2u, cast(int[])[]);
400 assert(a == [1, 3, 4, 5]);
401 replaceInPlace(a, 1u, 3u, a[2 .. 4]);
402 assert(a == [1, 4, 5, 5]);
403 }
404
405 @safe unittest
406 {
407 import std.array;
408
409 auto a = [1, 2, 2, 3, 4, 5];
410 auto b = a.replaceFirst([2], [1337]);
411 assert(b == [1, 1337, 2, 3, 4, 5]);
412
413 auto s = "This is a foo foo list";
414 auto r = s.replaceFirst("foo", "silly");
415 assert(r == "This is a silly foo list");
416 }
417
418 @safe unittest
419 {
420 import std.array;
421
422 auto a = [1, 2, 2, 3, 4, 5];
423 auto b = a.replaceLast([2], [1337]);
424 assert(b == [1, 2, 1337, 3, 4, 5]);
425
426 auto s = "This is a foo foo list";
427 auto r = s.replaceLast("foo", "silly");
428 assert(r == "This is a foo silly list", r);
429 }
430
431 @safe unittest
432 {
433 import std.array;
434
435 auto a = [1, 2, 3, 4, 5];
436 auto b = replaceSlice(a, a[1 .. 4], [0, 0, 0]);
437
438 assert(b == [1, 0, 0, 0, 5]);
439 }
440
441 @safe pure nothrow unittest
442 {
443 import std.array;
444
445 auto app = appender!string();
446 string b = "abcdefg";
447 foreach (char c; b)
448 app.put(c);
449 assert(app[] == "abcdefg");
450
451 int[] a = [ 1, 2 ];
452 auto app2 = appender(a);
453 app2.put(3);
454 app2.put([ 4, 5, 6 ]);
455 assert(app2[] == [ 1, 2, 3, 4, 5, 6 ]);
456 }
457
458 @safe pure nothrow unittest
459 {
460 import std.array;
461
462 auto app = appender!string();
463 string b = "abcdefg";
464 foreach (char c; b)
465 app.put(c);
466 assert(app[] == "abcdefg");
467
468 int[] a = [ 1, 2 ];
469 auto app2 = appender(a);
470 app2.put(3);
471 assert(app2.length == 3);
472 app2.put([ 4, 5, 6 ]);
473 assert(app2[] == [ 1, 2, 3, 4, 5, 6 ]);
474 }
475
476 @safe pure nothrow unittest
477 {
478 import std.array;
479
480 int[] a = [1, 2];
481 auto app2 = appender(&a);
482 assert(app2[] == [1, 2]);
483 assert(a == [1, 2]);
484 app2 ~= 3;
485 assert(app2.length == 3);
486 app2 ~= [4, 5, 6];
487 assert(app2[] == [1, 2, 3, 4, 5, 6]);
488 assert(a == [1, 2, 3, 4, 5, 6]);
489
490 app2.reserve(5);
491 assert(app2.capacity >= 5);
492 }
493
494 @safe pure nothrow unittest
495 {
496 import std.array;
497
498 auto w = appender!string;
499 // pre-allocate space for at least 10 elements (this avoids costly reallocations)
500 w.reserve(10);
501 assert(w.capacity >= 10);
502
503 w.put('a'); // single elements
504 w.put("bc"); // multiple elements
505
506 // use the append syntax
507 w ~= 'd';
508 w ~= "ef";
509
510 assert(w[] == "abcdef");
511 }
512
513 @safe pure nothrow unittest
514 {
515 import std.array;
516
517 int[] a = [1, 2];
518 auto app2 = appender(&a);
519 assert(app2[] == [1, 2]);
520 assert(a == [1, 2]);
521 app2 ~= 3;
522 app2 ~= [4, 5, 6];
523 assert(app2[] == [1, 2, 3, 4, 5, 6]);
524 assert(a == [1, 2, 3, 4, 5, 6]);
525
526 app2.reserve(5);
527 assert(app2.capacity >= 5);
528 }
529
530 nothrow pure @safe @nogc unittest
531 {
532 import std.array;
533
534 auto a = [0, 1].staticArray;
535 static assert(is(typeof(a) == int[2]));
536 assert(a == [0, 1]);
537 }
538
539 nothrow pure @safe @nogc unittest
540 {
541 import std.array;
542
543 auto b = [0, 1].staticArray!long;
544 static assert(is(typeof(b) == long[2]));
545 assert(b == [0, 1]);
546 }
547
548 nothrow pure @safe @nogc unittest
549 {
550 import std.array;
551
552 import std.range : iota;
553
554 auto input = 3.iota;
555 auto a = input.staticArray!2;
556 static assert(is(typeof(a) == int[2]));
557 assert(a == [0, 1]);
558 auto b = input.staticArray!(long[4]);
559 static assert(is(typeof(b) == long[4]));
560 assert(b == [0, 1, 2, 0]);
561 }
562
563 nothrow pure @safe @nogc unittest
564 {
565 import std.array;
566
567 import std.range : iota;
568
569 enum a = staticArray!(2.iota);
570 static assert(is(typeof(a) == int[2]));
571 assert(a == [0, 1]);
572
573 enum b = staticArray!(long, 2.iota);
574 static assert(is(typeof(b) == long[2]));
575 assert(b == [0, 1]);
576 }
577