1 
2 module testxxx8;
3 
4 import core.vararg;
5 
6 extern(C)
7 {
8     int atoi(const char*);
9     int printf(const char*, ...);
10     size_t strlen(const char*);
11     version(Windows)
12     {
13         int _snprintf(char*, size_t, const char*, ...);
14         alias _snprintf snprintf;
15     }
16     else
17         int snprintf(char*, size_t, const char*, ...);
18 }
19 
20 /***********************************/
21 
22 struct Foo1
23 {
24 	static int x = 3;
25 	int y = 4;
26 }
27 
28 void test1()
29 {
30     Foo1 f;
31 
32     assert(Foo1.x == 3);
33     assert(f.x == 3);
34     assert(f.y == 4);
35 }
36 
37 /***********************************/
38 
39 class Foo2
40 {
41 	static int x = 5;
42 	int y = 6;
43 }
44 
45 void test2()
46 {
47     Foo2 f = new Foo2();
48 
49     assert(Foo2.x == 5);
50     assert(f.x == 5);
51     assert(f.y == 6);
52 }
53 
54 
55 /***********************************/
56 
57 struct Foo3
58 {
59 	static int bar() { return 3; }
60 	int y = 4;
61 }
62 
63 void test3()
64 {
65     Foo3 f;
66 
67     assert(Foo3.bar() == 3);
68     assert(f.bar() == 3);
69 }
70 
71 /***********************************/
72 
73 class Foo4
74 {
75 	static int bar() { return 3; }
76 	int y = 4;
77 }
78 
79 void test4()
80 {
81     Foo4 f = new Foo4();
82 
83     assert(Foo4.bar() == 3);
84     assert(f.bar() == 3);
85 }
86 
87 
88 /***********************************/
89 
90 struct Foo5
91 {
92 	int bar() { return y + 3; }
93 	int y = 4;
94 }
95 
96 void test5()
97 {
98     Foo5 f;
99 
100     assert(f.bar() == 7);
101 }
102 
103 /***********************************/
104 
105 class Foo6
106 {
107 	int bar() { return y + 3; }
108 	final int abc() { return y + 8; }
109 	int y = 4;
110 }
111 
112 class FooX6 : Foo6
113 {
114 	override int bar() { return y + 5; }
115 }
116 
117 void test6()
118 {
119     Foo6 f = new FooX6();
120 
121     assert(f.bar() == 9);
122     assert(f.abc() == 12);
123 }
124 
125 
126 /***********************************/
127 
128 void bar7(char[3] cad)
129 {
130     assert(cad.length == 3);
131     printf("cad[0] = %d\n", cad[0]);
132     assert(cad[0] == 0xFF);
133     assert(cad[1] == 1);
134     assert(cad[2] == 0xFF);
135 }
136 
137 void test7()
138 {
139     char[3] foo;
140 
141     foo[1] = 1;
142     bar7(foo);
143 }
144 
145 
146 /***********************************/
147 
148 class gap8
149 {
150     this(char[3] cad)
151     {
152 	assert(cad[0] == 0xFF);
153 	assert(cad[1] == 1);
154 	assert(cad[2] == 0xFF);
155     }
156 }
157 
158 void test8()
159 {
160     char[3] foo;
161     gap8 g;
162 
163     foo[1] = 1;
164     g = new gap8(foo);
165 }
166 
167 
168 /***********************************/
169 
170 void test9()
171 {
172     ireal imag = 2.5i;
173     //printf ("test of imag*imag = %Lf\n",imag*imag);
174     real f = imag * imag;
175     assert(f == -6.25);
176 }
177 
178 /***********************************/
179 
180 void test10()
181 {
182     creal z = 1 + 2.5i;
183     real e = z.im;
184 
185     printf ("e = %Lf\n", e);
186     assert(e == 2.5);
187 }
188 
189 
190 /***********************************/
191 
192 class Foo11
193 {
194   public:
195     int a = 47;
196 
197   protected:
198     int b;
199 
200   private:
201     int c;
202 
203     int bar()
204     {
205 	return a + b + c;
206     }
207 }
208 
209 class Bar11 : Foo11
210 {
211     int abc()
212     {
213 	return a + b;
214     }
215 }
216 
217 void test11()
218 {
219     Foo11 f = new Foo11();
220 
221     int i = f.a;
222     assert(i == 47);
223 }
224 
225 /***********************************/
226 
227 class A12
228 {
229     protected void foo() { }
230 }
231 
232 class B12: A12
233 {
234     override void foo() { super.foo(); }
235 }
236 
237 void test12()
238 {
239 }
240 
241 /***********************************/
242 
243 alias void *HWND;
244 
245 const HWND hWnd = cast(HWND)(null); 
246 
247 void test13()
248 {
249 }
250 
251 /***********************************/
252 
253 string bar14()
254 {
255     return "f";
256 }
257 
258 char foo14()
259 {
260     return bar14()[0];
261 }
262 
263 void test14()
264 {
265     char f = foo14();
266     assert(f == 'f');
267 }
268 
269 
270 /***********************************/
271 
272 void test15()
273 {
274 	char[30] a;
275 	char[30] b;
276 
277 	assert(a !is b);
278 }
279 
280 /***********************************/
281 
282 void test16()
283 {
284     static int function() fp = &func16;
285     int i = fp();
286     assert(i == 648);
287 }
288 
289 int func16()
290 {
291     return 648;
292 }
293 
294 
295 /***********************************/
296 
297 string returnSameString(string inputstr)
298 {
299     return inputstr;
300 }
301 
302 string passString()
303 {
304     return returnSameString("First string" ~ "Concatenated with second");
305 }
306 
307 string butThisWorks()
308 {
309     string s = "Third string";
310     s = s ~ "Concatenated with fourth";
311     return returnSameString(s);
312 }
313 
314 void test17()
315 {
316     string s;
317 
318     s = passString();
319     printf("passString() = %.*s\n", s.length, s.ptr);
320     assert(s == "First stringConcatenated with second");
321 
322     s = butThisWorks();
323     printf("butThisWorks() = %.*s\n", s.length, s.ptr);
324     assert(s == "Third stringConcatenated with fourth");
325 }
326 
327 /***********************************/
328 
329 void test18()
330 {
331     string[] str;
332 
333     str.length = 2;
334 
335     version (none)
336     {
337 	str[1] = "cba";
338 	str[0] = "zyx";
339     }
340     else
341     {
342 	str[1] = (cast(string)"cba").idup;
343 	str[0] = (cast(string)"zyx").idup;
344     }
345 
346     // This sorts the strs
347     str.sort;
348 
349     // This will crash the compiler
350     str[0] = str[0].dup.sort.idup;
351 
352     // This will give sintax error
353     //str[0].sort();
354 
355     printf("%.*s", str[0].length, str[0].ptr);
356     printf("%.*s", str[1].length, str[1].ptr);
357     printf("\n");
358 
359     string s = str[0] ~ str[1];
360     assert(s == "abczyx");
361 }
362 
363 /***********************************/
364 
365 void test19()
366 {
367     string array = "foobar";
368 
369     array = array.idup;
370     array = array.dup.sort.idup;
371     assert(array == "abfoor");
372 }
373 
374 /***********************************/
375 
376 
377 
378 class A20
379 {
380     private:
381 	static int a;
382 
383     public:
384 	int foo(B20 j) { return j.b; }
385 }
386 
387 class B20
388 {
389     private:
390 	static int b;
391 
392     public:
393 	int bar(A20 j) { return j.a; }
394 }
395 
396 void test20()
397 {
398 }
399 
400 /***********************************/
401 
402 alias int* IP;
403 
404 void test21()
405 {
406     int i = 5;
407     IP ip = cast(IP) &i; 
408     assert(*ip == 5);
409 }
410 
411 /***********************************/
412 
413 struct RECT
414 {
415     int    left = 1;
416     int    top = 2;
417     int    right = 3;
418     int    bottom = 4;
419 }
420 
421 struct Rect
422 {
423   RECT theRect;
424 }
425 
426 
427 void Test(Rect pos)
428 {
429     //printf("left = %d\n", pos.theRect.left);
430     assert(pos.theRect.left == 1);
431     assert(pos.theRect.top == 2);
432     assert(pos.theRect.right == 3);
433     assert(pos.theRect.bottom == 4);
434 }
435 
436 class Window 
437 {
438   Rect position;   
439   
440   void createWindow()
441   {
442     Test(position);
443   }
444 }
445 
446 void test22()
447 {
448     Window w = new Window();
449     w.createWindow();
450 }
451 
452 /***********************************/
453 
454 struct Size
455 {
456   int width;
457   int height;
458 }
459 
460 Size computeSize()
461 {
462   Size foo;
463   
464   foo.width = 12;
465   foo.height = 34;
466   
467   printf("Inside: %d,%d\n",foo.width,foo.height);
468   
469   return foo;
470 }
471 
472 
473 void test24()
474 {
475   Size bar;
476   bar = computeSize();
477   
478   printf("Outside: %d,%d\n",bar.width,bar.height);
479   assert(bar.width == 12);
480   assert(bar.height == 34);
481 }
482 
483 /***********************************/
484 
485 void test25()
486 {   int i = 5;
487 
488     while (i)
489     {
490 	break;
491     }
492 }
493 
494 /***********************************/
495 
496 int test26()
497 in
498 {
499 }
500 out (result)
501 {
502 }
503 body
504 {   int i = 5;
505 
506     while (i)
507     {
508 	break;
509     }
510     return i;
511 }
512 
513 /***********************************/
514 
515 class A27
516 {
517     int a;
518 
519     this()
520     {
521 	a = 1;
522     }
523 }
524 
525 class B27 : A27
526 {
527 }
528 
529 class C27 : B27
530 {
531     this()
532     {
533 	super();
534     }
535 
536     this(int i)
537     {
538     }
539 }
540 
541 void test27()
542 {
543     A27 a = new A27();
544     assert(a.a == 1);
545 
546     B27 b = new B27();
547     assert(b.a == 1);
548 
549     C27 c = new C27();
550     assert(c.a == 1);
551 
552     C27 c2 = new C27(2);
553     assert(c2.a == 1);
554 }
555 
556 
557 /***********************************/
558 
559 const char[1] sep = '/';
560 
561 string testx28(string s, string t)
562 {
563     return cast(string)(s ~ sep ~ t);
564 }
565 
566 void test28()
567 {
568     string r;
569 
570     r = testx28("ab", "cd");
571     assert(r == "ab/cd");
572 }
573 
574 /***********************************/
575 
576 void test29()
577 {
578 }
579 
580 
581 /***********************************/
582 
583 bool func30(int x, int y)
584 {
585     bool b;
586     b|=(x==y);
587     return b;
588 }
589 
590 void test30()
591 {
592     bool b;
593 
594     b = func30(1,1);
595     assert(b == true);
596     b = func30(1,2);
597     assert(b == false);
598 }
599 
600 /***********************************/
601 
602 int a31;
603 
604 void test31()
605 {
606     testxxx8.a31 = 3;
607     assert(a31 == 3);
608 }
609 
610 /***********************************/
611 
612 void test32()
613 {
614     string[] foo;
615     int i;
616 
617     foo = new string[45];
618     for (i = 0; i < 45; i++)
619 	foo[i] = "hello";
620     for (i = 0; i < 45; i++)
621 	assert(foo[i] == "hello");
622 }
623 
624 
625 /***********************************/
626 
627 void test33()
628 {
629     string[] foo;
630     int i = 45;
631 
632     foo = new string[i];
633     for (i = 0; i < 45; i++)
634 	foo[i] = "hello";
635     for (i = 0; i < 45; i++)
636 	assert(foo[i] == "hello");
637 }
638 
639 
640 /***********************************/
641 
642 void test34()
643 {
644     int[3][4] a;
645     int[5][6] b = 16;
646     int i, j;
647 
648     for (i = 0; i < 4; i++)
649 	for (j = 0; j < 3; j++)
650 	    assert(a[i][j] == 0);
651 
652     for (i = 0; i < 6; i++)
653 	for (j = 0; j < 5; j++)
654 	    assert(b[i][j] == 16);
655 }
656 
657 
658 /***********************************/
659 
660 void test35()
661 {
662     ifloat b = cast(ifloat)1i;
663     assert(b == 1.0i);
664 
665     ifloat c = 2fi;
666     assert(c == 2.0i);
667 
668     c = 0fi;
669     assert(c == 0i);
670 }
671 
672 /***********************************/
673 
674 string itoa(int i)
675 {
676     char[32] buffer;
677     snprintf(buffer.ptr, 32, "%d", i);
678     return buffer[0 .. strlen(buffer.ptr)].idup;
679 }
680 
681 string testa36(int i, int j, string a, string b, string c)
682 {
683     string s =  "string 0;" ~ itoa(i) ~
684 		"string 1;" ~ itoa(j) ~
685 		"string 2;" ~ itoa(i) ~
686 		"string 3;";
687 
688 //    string s = a ~ b ~ c;
689     return s;
690 }
691 
692 void test36()
693 {
694     string s = testa36(26, 47, "a", "b", "c");
695 
696     printf("s = '%.*s'\n", s.length, s.ptr);
697     assert(s == "string 0;26string 1;47string 2;26string 3;");
698 }
699 
700 /***********************************/
701 
702 void test37()
703 {
704     string[ulong] x;
705     ulong v1 = 297321415603;
706     ulong v2 = 331681153971;
707     x[v1] = "aa";
708     printf( "%llx %llx\n", v1, v2 );
709     assert(!(v2 in x));
710 }
711 
712 
713 /***********************************/
714 
715 void test38()
716 {
717     int n = atoi("1");
718     static char flags[8192 + 1];
719     long i, k;
720     int count = 0;
721 
722     try
723     {
724        while (n--)
725        {
726           count = 0;
727 
728           for (i = 2; i <= 8192; i++)
729              flags[cast(size_t)i] = 1;
730 
731           for (i = 2; i <= 8192; i++)
732           {
733              if (flags[cast(size_t)i])
734              {
735                 for (k = i+i; k <= 8192; k += i)
736                    flags[cast(size_t)k] = 0;
737 
738                 count++;
739              }
740           }
741        }
742 
743        printf("Count: %d\n", count);
744 	assert(count == 1028);
745     }
746     catch
747     {
748        printf("Exception: %d\n", k);
749 	assert(0);
750     }
751 }
752 
753 
754 /***********************************/
755 
756 interface I39
757 {
758 }
759 
760 class C39 : I39
761 {
762     int x = 432;
763 }
764 
765 void test39()
766 {
767     C39 c = new C39;
768 
769     printf("%p %d\n", c, c.x);
770     assert(c.x == 432);
771     printf("%p\n", cast(I39) c);
772     c = cast(C39) cast(I39) c;
773     printf("%p\n", c);
774     assert(c !is null);
775 }
776 
777 
778 /***********************************/
779 
780 void test40()
781 {
782        Object x;
783 
784        x = null;
785        x = 0 ? x : null;
786        x = 0 ? null : x;
787 }
788 
789 /***********************************/
790 
791 int foo42(const(char) *x, ...)
792 {
793     va_list ap;
794 
795     va_start!(typeof(x))(ap, x);
796     printf("&x = %p, ap = %p\n", &x, ap);
797 
798     int i;
799     i = va_arg!(typeof(i))(ap);
800     printf("i = %d\n", i);
801 
802     long l;
803     l = va_arg!(typeof(l))(ap);
804     printf("l = %lld\n", l);
805 
806     uint k;
807     k = va_arg!(typeof(k))(ap);
808     printf("k = %u\n", k);
809 
810     va_end(ap);
811 
812     return cast(int)(i + l + k);
813 }
814 
815 void test42()
816 {
817     int j;
818 
819     j = foo42("hello", 3, 23L, 4);
820     printf("j = %d\n", j);
821     assert(j == 30);
822 }
823 
824 /***********************************/
825 
826 void test43()
827 {
828     creal C,Cj;
829     real y1,x1;
830 
831     C = x1 + y1*1i + Cj;
832     C = 1i*y1 + x1 + Cj;
833     C = Cj + 1i*y1 + x1;
834     C = y1*1i + Cj + x1;
835     C = 1i*y1 + Cj;
836     C = Cj + 1i*y1;
837 }
838 
839 /***********************************/
840 
841 int x44;
842 
843 class A44 {
844      this() { printf("A44 ctor\n"); x44 += 1; }
845      ~this() { printf("A44 dtor\n"); x44 += 0x100; }
846 }
847 class B44 : A44 { }
848 
849 void foo44() { scope B44 b = new B44; }
850 
851 void test44()
852 {
853      printf("foo44...\n");
854      foo44();
855      printf("...foo44\n");
856      assert(x44 == 0x101);
857 }
858 
859 /***********************************/
860 
861 /*
862 import std.stdarg;
863 import std.utf;
864 
865 int unFormat( bool delegate( out dchar ) getc,
866 	bool delegate( dchar ) ungetc,
867 	TypeInfo[] arguments,
868 	void* argptr )
869 {
870     size_t  arg = 0;
871     dchar[] fmt;
872 
873     if( arguments[arg] is typeid( string ) )
874 	fmt = toUTF32( va_arg!(string)( argptr ) );
875     else if( arguments[arg] is typeid( wchar[] ) )
876 	fmt = toUTF32( va_arg!(wchar[])( argptr ) );
877     else if( arguments[arg] is typeid( dchar[] ) )
878 	fmt = va_arg!(dchar[])( argptr );
879     else
880 	return 0;
881 }
882 */
883 
884 void test45()
885 {
886 }
887 
888 /***********************************/
889 
890 int sreadf( ... )
891 {
892     va_arg!(string)( _argptr );
893     return 0;
894 }
895 
896 
897 void test46()
898 {
899     printf( "hello world\n" );
900 }
901 
902 /***********************************/
903 
904 void test48()
905 { 
906   try{ 
907   }finally{ 
908     debug(p48) { } 
909   } 
910 }
911 
912 /***********************************/
913 
914 void test49()
915 { 
916   int k = 1; 
917   if(k == 0) 
918     debug{printf("test");} 
919 } 
920 
921 /***********************************/
922 
923 void test50()
924 {	int x;
925 
926         if (x)
927              version (none)
928                  foo;
929 }
930 
931 /***********************************/
932 
933 /+
934 void foo51(creal a)
935 {
936   writeln(a);
937   assert(a == -8i);
938 }
939 
940 void test51()
941 {
942   cdouble a = (2-2i)*(2-2i);
943 
944   // This fails
945   writeln(a);
946   assert(a == -8i);
947 
948   // This works
949   writeln((2-2i)*(2-2i));
950 
951   // This fails
952   foo51((2-2i)*(2-2i));
953 }
954 +/
955 
956 void foo51(creal a)
957 {
958     assert(a == -8i);
959 }
960 
961 void test51()
962 {
963     assert((2-2i)*(2-2i) == -8i);
964 
965     cdouble a = (2-2i)*(2-2i);
966     assert(a == -8i);
967 
968     foo51((2-2i)*(2-2i));
969 }
970 
971 /***********************************/
972 
973 // Bug 391
974 void test52()
975 {
976     char[] a;
977     a = "\u3026\u2021\u3061\n".dup;
978     assert(a =="\u3026\u2021\u3061\n");
979     assert(a.sort == "\n\u2021\u3026\u3061");
980     assert(a.reverse =="\u3061\u3026\u2021\n");
981 }
982 
983 /***********************************/
984 
985 int main()
986 {
987     test1();
988     test2();
989     test3();
990     test4();
991     test5();
992     test6();
993     test7();
994     test8();
995     test9();
996     test10();
997     test11();
998     test12();
999     test13();
1000     test14();
1001     test15();
1002     test16();
1003     test17();
1004     test18();
1005     test19();
1006     test20();
1007     test21();
1008     test22();
1009     test24();
1010     test25();
1011     test26();
1012     test27();
1013     test28();
1014     test29();
1015     test30();
1016     test31();
1017     test32();
1018     test33();
1019     test34();
1020     test35();
1021     test36();
1022     test37();
1023     test38();
1024     test39();
1025     test40();
1026     test42();
1027     test43();
1028     test44();
1029     test45();
1030     test46();
1031     test48();
1032     test49();
1033     test50();
1034     test51();
1035     test52();
1036 
1037     printf("Success\n");
1038     return 0;
1039 }