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