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