1 // RUNNABLE_PHOBOS_TEST
2 // PERMUTE_ARGS:
3 
4 import core.stdc.stdio : printf;
5 import std.string : splitLines;
6 import std.utf : toUTF16, toUTF32;
7 
8 /***********************************************/
9 
test3()10 void test3()
11 {
12     char[] str;
13     str ~= "test"; // segfault
14 }
15 
16 /***********************************************/
17 
18 class A {
19 private:
20     int _i;
21 public:
this(int i)22     this(int i) { _i = i; }
i()23     int i() { return _i; };
24 }
25 
26 class B : A {
27 private:
28     char[] s;
29 public:
this(int i)30     this(int i) { super(i); }
31 }
32 
main()33 int main()
34 {
35     printf("Testing array of Chars\n");
36     CHAR();
37     printf("Testing array of WChars\n");
38     WCHAR();
39     printf("Testing array of DChars\n");
40     DCHAR();
41 
42     printf("Testing array of Bytes\n");
43     BYTE();
44     printf("Testing array of UBytes\n");
45     UBYTE();
46     printf("Testing array of Shorts\n");
47     SHORT();
48     printf("Testing array of UShorts\n");
49     USHORT();
50     printf("Testing array of Ints\n");
51     INT();
52     printf("Testing array of UInts\n");
53     UINT();
54     printf("Testing array of Longs\n");
55     LONG();
56     printf("Testing array of ULongs\n");
57     ULONG();
58     printf("Testing array of Floats\n");
59     FLOAT();
60     printf("Testing array of Doubles\n");
61     DOUBLE();
62     printf("Testing array of Reals\n");
63     REAL();
64 
65     printf("Testing multi-dim array of Chars\n");
66     MDCHAR();
67 
68     printf("Testing array of Objects\n");
69     CLASS();
70 
71     test3();
72     return 0;
73 }
74 
MDCHAR()75 void MDCHAR()
76 {
77     const int ITERS = 100;
78     alias char typ;
79     typ[][] str;
80 
81     str.length = ITERS;
82     for(int idx = 0; idx < ITERS; idx++) {
83         str[idx] = str[idx] ~ "TEST LINE\n";
84     }
85 
86     if(str.length != ITERS) printf("Length Error: %d\n",str.length);
87     if(str[0].length != 10) printf("Length Error: %d\n",str[0].length);
88     if(str[ITERS-1].sizeof != (typ[]).sizeof) printf("Size Error: %d\n",str[ITERS-1].sizeof);
89     if(str[ITERS-1][0].sizeof != (typ).sizeof) printf("Size Error: %d\n",str[ITERS-1][0].sizeof);
90 
91     foreach(s; str) {
92         size_t lstart;
93         foreach(size_t idx, char c; s) {
94             if(c == '\n') {
95                 typ[] t = s[lstart..idx];
96                 if(t != "TEST LINE") {
97                     printf("Error testing character array\n");
98                     break;
99                 }
100                 lstart = idx + 1;
101             }
102         }
103     }
104 
105     typ[] tmp;
106     foreach(char[] s; str) {
107         tmp = tmp ~ s;
108     }
109 
110     foreach(s; splitLines(cast(string)tmp)) {
111         size_t lstart;
112         foreach(size_t idx, char c; s) {
113             if(c == '\n') {
114                 if(s[lstart..idx] != "TEST LINE") {
115                     printf("Error testing character array\n");
116                     break;
117                 }
118                 lstart = idx + 1;
119             }
120         }
121     }
122 }
123 
CHAR()124 void CHAR()
125 {
126     const int ITERS = 1000;
127     alias char typ;
128     typ[] str;
129 
130     for(int idx = 0; idx < ITERS; idx++) {
131         str = str ~ "TEST LINE\n";
132     }
133 
134     if(str.length != (ITERS * 10)) printf("Length Error: %d\n",str.length);
135     if(str.sizeof != (typ[]).sizeof) printf("Size Error: %d\n",str.sizeof);
136 
137     size_t lstart;
138     foreach(size_t idx, char c; str) {
139         if(c == '\n') {
140             if(str[lstart..idx] != "TEST LINE") {
141                 printf("Error testing character array\n");
142                 break;
143             }
144             lstart = idx + 1;
145         }
146     }
147 }
148 
WCHAR()149 void WCHAR()
150 {
151     const int ITERS = 1000;
152     alias wchar typ;
153     typ[] str;
154 
155     for(int idx = 0; idx < ITERS; idx++) {
156         str = str ~ toUTF16(cast(char[])"TEST LINE\n");
157     }
158 
159     if(str.length != (ITERS * 10)) printf("Length Error: %d\n",str.length);
160     if(str.sizeof != (typ[]).sizeof) printf("Size Error: %d\n",str.sizeof);
161 
162     size_t lstart;
163     foreach(size_t idx, char c; str) {
164         if(c == '\n') {
165             if(str[lstart..idx] != toUTF16(cast(char[])"TEST LINE")) {
166                 printf("Error testing character array\n");
167                 break;
168             }
169             lstart = idx + 1;
170         }
171     }
172 }
173 
DCHAR()174 void DCHAR()
175 {
176     const int ITERS = 1000;
177     alias dchar typ;
178     typ[] str;
179 
180     for(int idx = 0; idx < ITERS; idx++) {
181         str = str ~ toUTF32(cast(char[])"TEST LINE\n");
182     }
183 
184     if(str.length != (ITERS * 10)) printf("Length Error: %d\n",str.length);
185     if(str.sizeof != (typ[]).sizeof) printf("Size Error: %d\n",str.sizeof);
186 
187     size_t lstart;
188     foreach(size_t idx, char c; str) {
189         if(c == '\n') {
190             if(str[lstart..idx] != toUTF32(cast(char[])"TEST LINE")) {
191                 printf("Error testing character array\n");
192                 break;
193             }
194             lstart = idx + 1;
195         }
196     }
197 }
198 
BYTE()199 void BYTE()
200 {
201     const int ITERS = 100;
202     alias byte typ;
203     typ[] a;
204 
205     for(typ idx = 0; idx < ITERS; idx++) {
206         a ~= idx;
207     }
208 
209     if(a.length != ITERS) printf("Length Error: %d\n",a.length);
210     if(a.sizeof != (typ[]).sizeof) printf("Size Error: %d\n",a.sizeof);
211     for(int idx = 0; idx < ITERS; idx++) {
212         if(a[idx] != idx) {
213             printf("a Data Error: %d\n",a[idx]);
214             break;
215         }
216     }
217 
218 
219     typ[] b = a[];
220 
221     if(b.length != ITERS) printf("Length Error: %d\n",b.length);
222     if(b.sizeof != (typ[]).sizeof) printf("Size Error: %d\n",b.sizeof);
223     for(int idx = 0; idx < ITERS; idx++) {
224         if(b[idx] != idx) {
225             printf("b Data Error: %d\n",b[idx]);
226             break;
227         }
228     }
229     typ[] c;
230     c = a[0..ITERS/2] ~ b[ITERS/2..$];
231 
232 
233     if(c.length != ITERS) printf("Length Error: %d\n",c.length);
234     if(c.sizeof != (typ[]).sizeof) printf("Size Error: %d\n",c.sizeof);
235     for(int idx = 0; idx < ITERS; idx++) {
236         if(c[idx] != idx) {
237             printf("c Data Error: %d\n",c[idx]);
238             break;
239         }
240     }
241 }
242 
UBYTE()243 void UBYTE()
244 {
245     const int ITERS = 100;
246     alias ubyte typ;
247     typ[] a;
248 
249     for(typ idx = 0; idx < ITERS; idx++) {
250         a ~= idx;
251     }
252 
253     if(a.length != ITERS) printf("Length Error: %d\n",a.length);
254     if(a.sizeof != (typ[]).sizeof) printf("Size Error: %d\n",a.sizeof);
255     for(int idx = 0; idx < ITERS; idx++) {
256         if(a[idx] != idx) {
257             printf("a Data Error: %d\n",a[idx]);
258             break;
259         }
260     }
261 
262 
263     typ[] b = a[];
264 
265     if(b.length != ITERS) printf("Length Error: %d\n",b.length);
266     if(b.sizeof != (typ[]).sizeof) printf("Size Error: %d\n",b.sizeof);
267     for(int idx = 0; idx < ITERS; idx++) {
268         if(b[idx] != idx) {
269             printf("b Data Error: %d\n",b[idx]);
270             break;
271         }
272     }
273 
274     typ[] c;
275     c = a[0..ITERS/2] ~ b[ITERS/2..$];
276 
277 
278     if(c.length != ITERS) printf("Length Error: %d\n",c.length);
279     if(c.sizeof != (typ[]).sizeof) printf("Size Error: %d\n",c.sizeof);
280     for(int idx = 0; idx < ITERS; idx++) {
281         if(c[idx] != idx) {
282             printf("c Data Error: %d\n",c[idx]);
283             break;
284         }
285     }
286 }
287 
SHORT()288 void SHORT()
289 {
290     const int ITERS = 10000;
291     alias short typ;
292     typ[] a;
293 
294     for(typ idx = 0; idx < ITERS; idx++) {
295         a ~= idx;
296     }
297 
298     if(a.length != ITERS) printf("Length Error: %d\n",a.length);
299     if(a.sizeof != (typ[]).sizeof) printf("Size Error: %d\n",a.sizeof);
300     for(int idx = 0; idx < ITERS; idx++) {
301         if(a[idx] != idx) {
302             printf("a Data Error: %d\n",a[idx]);
303             break;
304         }
305     }
306 
307     typ[] b = a[];
308 
309     if(b.length != ITERS) printf("Length Error: %d\n",b.length);
310     if(b.sizeof != (typ[]).sizeof) printf("Size Error: %d\n",b.sizeof);
311     for(int idx = 0; idx < ITERS; idx++) {
312         if(b[idx] != idx) {
313             printf("b Data Error: %d\n",b[idx]);
314             break;
315         }
316     }
317 
318     typ[] c;
319     c = a[0..ITERS/2] ~ b[ITERS/2..$];
320 
321 
322     if(c.length != ITERS) printf("Length Error: %d\n",c.length);
323     if(c.sizeof != (typ[]).sizeof) printf("Size Error: %d\n",c.sizeof);
324     for(int idx = 0; idx < ITERS; idx++) {
325         if(c[idx] != idx) {
326             printf("c Data Error: %d\n",c[idx]);
327             break;
328         }
329     }
330 }
331 
USHORT()332 void USHORT()
333 {
334     const int ITERS = 10000;
335     alias ushort typ;
336     typ[] a;
337 
338     for(typ idx = 0; idx < ITERS; idx++) {
339         a ~= idx;
340     }
341 
342     if(a.length != ITERS) printf("Length Error: %d\n",a.length);
343     if(a.sizeof != (typ[]).sizeof) printf("Size Error: %d\n",a.sizeof);
344     for(int idx = 0; idx < ITERS; idx++) {
345         if(a[idx] != idx) {
346             printf("a Data Error: %d\n",a[idx]);
347             break;
348         }
349     }
350 
351     typ[] b = a[];
352 
353     if(b.length != ITERS) printf("Length Error: %d\n",b.length);
354     if(b.sizeof != (typ[]).sizeof) printf("Size Error: %d\n",b.sizeof);
355     for(int idx = 0; idx < ITERS; idx++) {
356         if(b[idx] != idx) {
357             printf("b Data Error: %d\n",b[idx]);
358             break;
359         }
360     }
361 
362     typ[] c;
363     c = a[0..ITERS/2] ~ b[ITERS/2..$];
364 
365 
366     if(c.length != ITERS) printf("Length Error: %d\n",c.length);
367     if(c.sizeof != (typ[]).sizeof) printf("Size Error: %d\n",c.sizeof);
368     for(int idx = 0; idx < ITERS; idx++) {
369         if(c[idx] != idx) {
370             printf("c Data Error: %d\n",c[idx]);
371             break;
372         }
373     }
374 }
375 
INT()376 void INT()
377 {
378     const int ITERS = 1000000;
379     alias int typ;
380     typ[] a;
381 
382     for(int idx = 0; idx < ITERS; idx++) {
383         a ~= idx;
384     }
385 
386     if(a.length != ITERS) printf("Length Error: %d\n",a.length);
387     if(a.sizeof != (typ[]).sizeof) printf("Size Error: %d\n",a.sizeof);
388     for(int idx = 0; idx < ITERS; idx++) {
389         if(a[idx] != idx) {
390             printf("a Data Error: %d\n",a[idx]);
391             break;
392         }
393     }
394 
395 
396     typ[] b = a[];
397 
398     if(b.length != ITERS) printf("Length Error: %d\n",b.length);
399     if(b.sizeof != (typ[]).sizeof) printf("Size Error: %d\n",b.sizeof);
400     for(int idx = 0; idx < ITERS; idx++) {
401         if(b[idx] != idx) {
402             printf("b Data Error: %d\n",b[idx]);
403             break;
404         }
405     }
406 
407     typ[] c;
408     c = a[0..ITERS/2] ~ b[ITERS/2..$];
409 
410 
411     if(c.length != ITERS) printf("Length Error: %d\n",c.length);
412     if(c.sizeof != (typ[]).sizeof) printf("Size Error: %d\n",c.sizeof);
413     for(int idx = 0; idx < ITERS; idx++) {
414         if(c[idx] != idx) {
415             printf("c Data Error: %d\n",c[idx]);
416             break;
417         }
418     }
419 }
420 
UINT()421 void UINT()
422 {
423     const int ITERS = 1000000;
424     alias uint typ;
425     typ[] a;
426 
427     for(int idx = 0; idx < ITERS; idx++) {
428         a ~= idx;
429     }
430 
431     if(a.length != ITERS) printf("Length Error: %d\n",a.length);
432     if(a.sizeof != (typ[]).sizeof) printf("Size Error: %d\n",a.sizeof);
433     for(int idx = 0; idx < ITERS; idx++) {
434         if(a[idx] != idx) {
435             printf("a Data Error: %d\n",a[idx]);
436             break;
437         }
438     }
439 
440 
441     typ[] b = a[];
442 
443     if(b.length != ITERS) printf("Length Error: %d\n",b.length);
444     if(b.sizeof != (typ[]).sizeof) printf("Size Error: %d\n",b.sizeof);
445     for(int idx = 0; idx < ITERS; idx++) {
446         if(b[idx] != idx) {
447             printf("b Data Error: %d\n",b[idx]);
448             break;
449         }
450     }
451 
452     typ[] c;
453     c = a[0..ITERS/2] ~ b[ITERS/2..$];
454 
455 
456     if(c.length != ITERS) printf("Length Error: %d\n",c.length);
457     if(c.sizeof != (typ[]).sizeof) printf("Size Error: %d\n",c.sizeof);
458     for(int idx = 0; idx < ITERS; idx++) {
459         if(c[idx] != idx) {
460             printf("c Data Error: %d\n",c[idx]);
461             break;
462         }
463     }
464 }
465 
LONG()466 void LONG()
467 {
468     const int ITERS = 1000000;
469     alias long typ;
470     typ[] a;
471 
472     for(int idx = 0; idx < ITERS; idx++) {
473         a ~= idx;
474     }
475 
476     if(a.length != ITERS) printf("Length Error: %d\n",a.length);
477     if(a.sizeof != (typ[]).sizeof) printf("Size Error: %d\n",a.sizeof);
478     for(int idx = 0; idx < ITERS; idx++) {
479         if(a[idx] != idx) {
480             printf("a Data Error: %d\n",a[idx]);
481             break;
482         }
483     }
484 
485     typ[] b = a[];
486 
487     if(b.length != ITERS) printf("Length Error: %d\n",b.length);
488     if(b.sizeof != (typ[]).sizeof) printf("Size Error: %d\n",b.sizeof);
489     for(int idx = 0; idx < ITERS; idx++) {
490         if(b[idx] != idx) {
491             printf("b Data Error: %d\n",b[idx]);
492             break;
493         }
494     }
495 
496     typ[] c;
497     c = a[0..ITERS/2] ~ b[ITERS/2..$];
498 
499 
500     if(c.length != ITERS) printf("Length Error: %d\n",c.length);
501     if(c.sizeof != (typ[]).sizeof) printf("Size Error: %d\n",c.sizeof);
502     for(int idx = 0; idx < ITERS; idx++) {
503         if(c[idx] != idx) {
504             printf("c Data Error: %d\n",c[idx]);
505             break;
506         }
507     }
508 }
509 
ULONG()510 void ULONG()
511 {
512     const int ITERS = 1000000;
513     alias ulong typ;
514     typ[] a;
515 
516     for(int idx = 0; idx < ITERS; idx++) {
517         a ~= idx;
518     }
519 
520     if(a.length != ITERS) printf("Length Error: %d\n",a.length);
521     if(a.sizeof != (typ[]).sizeof) printf("Size Error: %d\n",a.sizeof);
522     for(int idx = 0; idx < ITERS; idx++) {
523         if(a[idx] != idx) {
524             printf("a Data Error: %d\n",a[idx]);
525             break;
526         }
527     }
528 
529     typ[] b = a[];
530 
531     if(b.length != ITERS) printf("Length Error: %d\n",b.length);
532     if(b.sizeof != (typ[]).sizeof) printf("Size Error: %d\n",b.sizeof);
533     for(int idx = 0; idx < ITERS; idx++) {
534         if(b[idx] != idx) {
535             printf("b Data Error: %d\n",b[idx]);
536             break;
537         }
538     }
539 
540     typ[] c;
541     c = a[0..ITERS/2] ~ b[ITERS/2..$];
542 
543 
544     if(c.length != ITERS) printf("Length Error: %d\n",c.length);
545     if(c.sizeof != (typ[]).sizeof) printf("Size Error: %d\n",c.sizeof);
546     for(int idx = 0; idx < ITERS; idx++) {
547         if(c[idx] != idx) {
548             printf("c Data Error: %d\n",c[idx]);
549             break;
550         }
551     }
552 }
553 
FLOAT()554 void FLOAT()
555 {
556     const int ITERS = 1000000;
557     alias float typ;
558     typ[] a;
559 
560     for(int idx = 0; idx < ITERS; idx++) {
561         a ~= idx;
562     }
563 
564     if(a.length != ITERS) printf("Length Error: %d\n",a.length);
565     if(a.sizeof != (typ[]).sizeof) printf("Size Error: %d\n",a.sizeof);
566     for(int idx = 0; idx < ITERS; idx++) {
567         if(a[idx] != idx) {
568             printf("a Data Error: %d\n",a[idx]);
569             break;
570         }
571     }
572 
573     typ[] b = a[];
574 
575     if(b.length != ITERS) printf("Length Error: %d\n",b.length);
576     if(b.sizeof != (typ[]).sizeof) printf("Size Error: %d\n",b.sizeof);
577     for(int idx = 0; idx < ITERS; idx++) {
578         if(b[idx] != idx) {
579             printf("b Data Error: %d\n",b[idx]);
580             break;
581         }
582     }
583 
584     typ[] c;
585     c = a[0..ITERS/2] ~ b[ITERS/2..$];
586 
587 
588     if(c.length != ITERS) printf("Length Error: %d\n",c.length);
589     if(c.sizeof != (typ[]).sizeof) printf("Size Error: %d\n",c.sizeof);
590     for(int idx = 0; idx < ITERS; idx++) {
591         if(c[idx] != idx) {
592             printf("c Data Error: %d\n",c[idx]);
593             break;
594         }
595     }
596 }
597 
DOUBLE()598 void DOUBLE()
599 {
600     const int ITERS = 1000000;
601     alias double typ;
602     typ[] a;
603 
604     for(int idx = 0; idx < ITERS; idx++) {
605         a ~= idx;
606     }
607 
608     if(a.length != ITERS) printf("Length Error: %d\n",a.length);
609     if(a.sizeof != (typ[]).sizeof) printf("Size Error: %d\n",a.sizeof);
610     for(int idx = 0; idx < ITERS; idx++) {
611         if(a[idx] != idx) {
612             printf("a Data Error: %d\n",a[idx]);
613             break;
614         }
615     }
616 
617     typ[] b = a[];
618 
619     if(b.length != ITERS) printf("Length Error: %d\n",b.length);
620     if(b.sizeof != (typ[]).sizeof) printf("Size Error: %d\n",b.sizeof);
621     for(int idx = 0; idx < ITERS; idx++) {
622         if(b[idx] != idx) {
623             printf("b Data Error: %d\n",b[idx]);
624             break;
625         }
626     }
627 
628     typ[] c;
629     c = a[0..ITERS/2] ~ b[ITERS/2..$];
630 
631 
632     if(c.length != ITERS) printf("Length Error: %d\n",c.length);
633     if(c.sizeof != (typ[]).sizeof) printf("Size Error: %d\n",c.sizeof);
634     for(int idx = 0; idx < ITERS; idx++) {
635         if(c[idx] != idx) {
636             printf("c Data Error: %d\n",c[idx]);
637             break;
638         }
639     }
640 }
641 
REAL()642 void REAL()
643 {
644     const int ITERS = 1000000;
645     alias real typ;
646     typ[] a;
647 
648     for(int idx = 0; idx < ITERS; idx++) {
649         a ~= idx;
650     }
651 
652     if(a.length != ITERS) printf("Length Error: %d\n",a.length);
653     if(a.sizeof != (typ[]).sizeof) printf("Size Error: %d\n",a.sizeof);
654     for(int idx = 0; idx < ITERS; idx++) {
655         if(a[idx] != idx) {
656             printf("a Data Error: %d\n",a[idx]);
657             break;
658         }
659     }
660 
661     typ[] b = a[];
662 
663     if(b.length != ITERS) printf("Length Error: %d\n",b.length);
664     if(b.sizeof != (typ[]).sizeof) printf("Size Error: %d\n",b.sizeof);
665     for(int idx = 0; idx < ITERS; idx++) {
666         if(b[idx] != idx) {
667             printf("b Data Error: %d\n",b[idx]);
668             break;
669         }
670     }
671 
672     typ[] c;
673     c = a[0..ITERS/2] ~ b[ITERS/2..$];
674 
675 
676     if(c.length != ITERS) printf("Length Error: %d\n",c.length);
677     if(c.sizeof != (typ[]).sizeof) printf("Size Error: %d\n",c.sizeof);
678     for(int idx = 0; idx < ITERS; idx++) {
679         if(c[idx] != idx) {
680             printf("c Data Error: %d\n",c[idx]);
681             break;
682         }
683     }
684 }
685 
CLASS()686 void CLASS()
687 {
688     const int ITERS = 1000000;
689     alias B typ;
690     typ[] a;
691 
692     for(int idx = 0; idx < ITERS; idx++) {
693         typ tc = new typ(idx);
694         a ~= tc;
695     }
696 
697     if(a.length != ITERS) printf("Length Error: %d\n",a.length);
698     if(a.sizeof != (typ[]).sizeof) printf("Size Error: %d\n",a.sizeof);
699     for(int idx = 0; idx < ITERS; idx++) {
700         if(a[idx].i != idx) {
701             printf("a Data Error: %d\n",a[idx].i);
702             break;
703         }
704     }
705 
706     typ[] b = a[];
707 
708     if(b.length != ITERS) printf("Length Error: %d\n",b.length);
709     if(b.sizeof != (typ[]).sizeof) printf("Size Error: %d\n",b.sizeof);
710     for(int idx = 0; idx < ITERS; idx++) {
711         if(b[idx].i != idx) {
712             printf("b Data Error: %d\n",b[idx].i);
713             break;
714         }
715     }
716 
717     typ[] c;
718     c = a[0..ITERS/2] ~ b[ITERS/2..$];
719 
720     if(c.length != ITERS) printf("Length Error: %d\n",c.length);
721     if(c.sizeof != (typ[]).sizeof) printf("Size Error: %d\n",c.sizeof);
722     for(int idx = 0; idx < ITERS; idx++) {
723         if(c[idx].i != idx) {
724             printf("c Data Error: %d\n",c[idx].i);
725             break;
726         }
727     }
728 }
729