-
-
Notifications
You must be signed in to change notification settings - Fork 594
/
testconst.d
3097 lines (2515 loc) · 73.7 KB
/
testconst.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import std.c.stdio;
class C { }
int ctfe() { return 3; }
template TypeTuple(T...) { alias T TypeTuple; }
/************************************/
void showf(string f)
{
printf("%.*s\n", f.length, f.ptr);
}
/************************************/
void test1()
{
const int* p;
const(int)* cp;
cp = p;
}
/************************************/
void test2()
{
const int i = 3;
const(int) j = 0;
int k;
// j = i;
k = i;
k = j;
// assert(k == 3);
}
/************************************/
void test3()
{
char[3] p;
const(char)[] q;
q = p;
}
/************************************/
void test4()
{
char[] p;
const(char)[] q;
q = p;
}
/************************************/
void test5()
{
const(int**)* p;
const(int)*** cp;
p = cp;
}
/************************************/
void test6()
{
const(int***)[] p;
const(int)***[] cp;
p = cp;
}
/************************************/
class C8 { }
void foo8(const char[] s, const C8 c, const int x)
{
}
void test8()
{
auto p = &foo8;
showf(p.mangleof);
assert(typeof(p).mangleof == "PFxAaxC9testconst2C8xiZv");
assert(p.mangleof == "_D9testconst5test8FZv1pPFxAaxC9testconst2C8xiZv");
}
/************************************/
void test9()
{
int [ const (char[]) ] aa;
int [ char[] ] ab;
int [ const char[] ] ac;
aa["hello"] = 3;
ab["hello"] = 3;
ac["hello"] = 3;
}
/************************************/
void test10()
{
const(int) x = 3;
auto y = x;
//y++;
assert(is(typeof(y) == const(int)));
}
/************************************/
void foo11(in char[] a1)
{
char[3] c;
char[] a2 = c[];
a2[0..2] = a1;
a2[0..2] = 'c';
const char b = 'b';
a2[0..2] = b;
}
void test11()
{
}
/************************************/
void foo12(const char[] a1)
{
}
void test12()
{
foo12("hello");
}
/************************************/
immutable char[16] hexdigits1 = "0123456789ABCDE1";
immutable char[ ] hexdigits2 = "0123456789ABCDE2";
const char[16] hexdigits3 = "0123456789ABCDE3";
const char[ ] hexdigits4 = "0123456789ABCDE4";
void test13()
{
}
/************************************/
void test14()
{
string s;
s = s ~ "hello";
}
/************************************/
class Foo15
{
const string xxxx;
this(immutable char[] aaa)
{
this.xxxx = aaa;
}
}
void test15()
{
}
/************************************/
void test16()
{
auto a = "abc";
immutable char[3] b = "abc";
const char[3] c = "abc";
}
/************************************/
void test17()
{
const(char)[3][] a = (["abc", "def"]);
assert(a[0] == "abc");
assert(a[1] == "def");
}
/************************************/
class C18
{
const(char)[] foo() { return "abc"; }
}
class D18 : C18
{
override char[] foo() { return null; }
}
void test18()
{
}
/************************************/
void test19()
{
char[] s;
if (s == "abc")
s = null;
if (s < "abc")
s = null;
if (s is "abc")
s = null;
}
/************************************/
void test20()
{
string p;
immutable char[] q;
p = p ~ q;
p ~= q;
}
/************************************/
void test21()
{
string s;
char[] p;
p = s.dup;
}
/************************************/
void fill22(const(char)[] s)
{
}
void test22()
{
}
/************************************/
struct S23
{
int x;
int* p;
}
void foo23(const(S23) s, const(int) i)
{
immutable int j = 3;
// j = 4;
// i = 4;
// s.x = 3;
// *s.p = 4;
}
void test23()
{
}
/************************************/
void test24()
{
wchar[] r;
r ~= "\000";
}
/************************************/
void test25()
{
char* p;
if (p == cast(const(char)*)"abc")
{}
}
/************************************/
void test26()
{
struct S
{
char[3] a;
}
static S s = { "abc" };
}
/************************************/
class C27
{
int x;
void foo() { x = 3; }
}
void test27()
{
C27 d = new C27;
d.foo();
}
/************************************/
class C28
{
int x;
void foo() immutable { }
}
void test28()
{
immutable(C28) d = cast(immutable)new C28;
d.foo();
}
/************************************/
struct S29 { }
int foo29(const(S29)* s)
{
S29 s2;
return *s == s2;
}
void test29()
{
}
/************************************/
struct S30
{
int x;
void foo() { x = 3; }
void bar() const
{ //x = 4;
//this.x = 5;
}
}
class C30
{
int x;
void foo() { x = 3; }
void bar() const
{ //x = 4;
//this.x = 5;
}
}
void test30()
{ S30 s;
s.foo();
s.bar();
S30 t;
//t.foo();
t.bar();
C30 c = new C30;
c.foo();
c.bar();
C30 d = new C30;
d.foo();
d.bar();
}
/************************************/
class Foo31
{
int x;
immutable immutable(int)* geti()
{
return &x;
}
const const(int)* getc()
{
return &x;
}
}
void test31()
{
}
/************************************/
int bar32;
struct Foo32
{
int func() immutable
{
return bar32;
}
}
void test32()
{
immutable(Foo32) foo;
printf("%d", foo.func());
printf("%d", foo.func());
}
/************************************/
void test33()
{
string d = "a" ~ "b" ~ (1?"a":"");
assert(d == "aba");
}
/************************************/
struct S34
{
int value;
}
const S34 s34 = { 5 };
const S34 t34 = s34;
const S34 u34 = s34;
void test34()
{
assert(u34.value == 5);
}
/************************************/
const int i35 = 20;
template Foo35(alias bar)
{
}
alias Foo35!(i35) foo35;
void test35()
{
}
/************************************/
immutable char[10] digits = "0123456789"; /// 0..9
immutable char[] octdigits = digits[0 .. 8]; //// 0..7
void test36()
{
}
/************************************/
void test37()
{
int i = 3;
const int x = i;
i++;
assert(x == 3);
}
/************************************/
void test38()
{
static const string s = "hello"[1..$];
assert(s == "ello");
}
/************************************/
static const int x39;
const int y39;
static this()
{
x39 = 3;
y39 = 4;
}
void test39()
{
const int i;
assert(x39 == 3);
assert(y39 == 4);
const p = &x39;
// assert(*p == 3);
}
/************************************/
struct S40
{
int a;
const int b = 3; // shouldn't be allocated
}
void test40()
{
assert(S40.sizeof == 8);
assert(S40.init.b == 3);
}
/************************************/
struct S41
{
int a;
const int b;
static const int c = ctfe() + 1;
}
void test41()
{
assert(S41.sizeof == 8);
S41 s;
assert(s.b == 0);
assert(S41.c == 4);
const(int)*p;
p = &s.b;
assert(*p == 0);
p = &s.c;
assert(*p == 4);
}
/************************************/
class C42
{
int a = ctfe() - 2;
const int b;
const int c = ctfe();
static const int d;
static const int e = ctfe() + 2;
static this()
{
d = 4;
}
this()
{
b = 2;
}
}
void test42()
{
printf("%d\n", C42.classinfo.init.length);
assert(C42.classinfo.init.length == 12 + (void*).sizeof + (void*).sizeof);
C42 c = new C42;
assert(c.a == 1);
assert(c.b == 2);
assert(c.c == 3);
assert(c.d == 4);
assert(c.e == 5);
const(int)*p;
p = &c.b;
assert(*p == 2);
p = &c.c;
assert(*p == 3);
p = &c.d;
assert(*p == 4);
p = &c.e;
assert(*p == 5);
}
/************************************/
template Foo43(T)
{
alias T Foo43;
}
void test43()
{
{
int x;
alias Foo43!(typeof(x)) f;
showf(typeid(f).toString());
assert(is(typeof(x) == int));
assert(is(f == int));
}
{
const int x;
alias Foo43!(typeof(x)) f;
showf(typeid(f).toString());
assert(is(typeof(x) == const(int)));
assert(is(f == const(int)));
}
{
immutable int x;
alias Foo43!(typeof(x)) f;
showf(typeid(f).toString());
assert(is(typeof(x) == immutable(int)));
assert(is(f == immutable(int)));
}
}
/************************************/
template Foo44(T:T)
{
alias T Foo44;
}
void test44()
{
{
int x;
alias Foo44!(typeof(x)) f;
showf(typeid(f).toString());
assert(is(typeof(x) == int));
assert(is(f == int));
}
{
const int x;
alias Foo44!(typeof(x)) f;
showf(typeid(f).toString());
assert(is(typeof(x) == const(int)));
assert(is(f == const(int)));
}
{
immutable int x;
alias Foo44!(typeof(x)) f;
showf(typeid(f).toString());
assert(is(typeof(x) == immutable(int)));
assert(is(f == immutable(int)));
}
}
/************************************/
template Foo45(T:const(T))
{
alias T Foo45;
}
void test45()
{
{
int x;
alias Foo45!(typeof(x)) f;
showf(typeid(f).toString());
assert(is(typeof(x) == int));
assert(is(f == int));
}
{
const int x;
alias Foo45!(typeof(x)) f;
showf(typeid(f).toString());
assert(is(typeof(x) == const(int)));
assert(is(f == int));
}
{
immutable int x;
alias Foo45!(typeof(x)) f;
showf(typeid(f).toString());
assert(is(typeof(x) == immutable(int)));
assert(is(f == int));
}
}
/************************************/
template Foo46(T:immutable(T))
{
alias T Foo46;
}
void test46()
{
{
immutable int x;
alias Foo46!(typeof(x)) f;
showf(typeid(f).toString());
assert(is(typeof(x) == immutable(int)));
assert(is(f == int));
}
}
/************************************/
template Foo47(T:T) { const int Foo47 = 2; }
template Foo47(T:const(T)) { const int Foo47 = 3; }
template Foo47(T:immutable(T)) { const int Foo47 = 4; }
void test47()
{
int x2;
const int x3;
immutable int x4;
printf("%d\n", Foo47!(typeof(x2)));
printf("%d\n", Foo47!(typeof(x3)));
printf("%d\n", Foo47!(typeof(x4)));
assert(Foo47!(typeof(x2)) == 2);
assert(Foo47!(typeof(x3)) == 3);
assert(Foo47!(typeof(x4)) == 4);
}
/************************************/
int foo48(T)(const(T) t) { return 3; }
void test48()
{
const int x = 4;
assert(foo48(x) == 3);
}
/************************************/
void foo49(T)(T[] t)
{
showf(typeid(typeof(t)).toString());
assert(is(T == immutable(char)));
}
void bar49(T)(const T t)
{
showf(typeid(T).toString());
assert(is(T == const(int)) || is(T == immutable(int)) || is(T == int));
}
void test49()
{
string s;
foo49(s);
foo49("hello");
const int c = 1;
bar49(c);
immutable int i = 1;
bar49(i);
bar49(1);
}
/************************************/
void foo50(T)(T t)
{
showf(typeid(typeof(t)).toString());
assert(is(T == C));
}
void baz50(T)(T t)
{
showf(typeid(typeof(t)).toString());
assert(is(T == const(C)));
}
void bar50(T)(const T t)
{
showf(typeid(T).toString());
showf(typeid(typeof(t)).toString());
assert(is(T == C));
assert(is(typeof(t) == const(C)));
}
void abc50(T)(const T t)
{
showf(typeid(T).toString());
showf(typeid(typeof(t)).toString());
assert(is(T == C));
assert(is(typeof(t) == const(C)));
}
void test50()
{
C c = new C;
const(C) d = new C;
foo50(c);
baz50(d);
bar50(c);
abc50(d);
}
/************************************/
void test51()
{
const(C) d = new C;
//d = new C;
}
/************************************/
template isStaticArray(T)
{
const bool isStaticArray = false;
}
template isStaticArray(T : T[N], size_t N)
{
const bool isStaticArray = true;
}
template isDynamicArray(T, U = void)
{
static const isDynamicArray = false;
}
template isDynamicArray(T : U[], U)
{
static const isDynamicArray = !isStaticArray!(T);
}
void test52()
{
immutable(char[5])[int] aa = ([3:"hello", 4:"betty"]);
showf(typeid(typeof(aa.values)).toString());
static assert(isDynamicArray!(typeof(aa.values)));
}
/************************************/
void foo53(string n) { }
void read53(in string name)
{
foo53(name);
}
void test53()
{
read53("hello");
}
/************************************/
void bar54(const(wchar)[] s) { }
void test54()
{
const(wchar)[] fmt;
bar54("Orphan format specifier: %" ~ fmt);
}
/************************************/
struct S55
{
int foo() { return 1; }
int foo() const { return 2; }
int foo() immutable { return 3; }
}
void test55()
{
S55 s1;
auto i = s1.foo();
assert(i == 1);
const S55 s2;
i = s2.foo();
assert(i == 2);
immutable S55 s3;
i = s3.foo();
assert(i == 3);
}
/************************************/
const struct S56 { int a; }
void test56()
{
S56 s;
S56 t;
printf("S56.sizeof = %d\n", S56.sizeof);
//t = s;
}
/************************************/
struct S57
{
const void foo(this T)(int i)
{
showf(typeid(T).toString());
if (i == 1)
assert(is(T == const));
if (i == 2)
assert(!is(T == const));
if (i == 3)
assert(is(T == immutable));
}
}
void test57()
{
const(S57) s;
(&s).foo(1);
S57 s2;
s2.foo(2);
immutable(S57) s3;
s3.foo(3);
}
/************************************/
class C58
{
const(C58) c;
const C58 y;
this()
{
y = null;
c = null;
}
const void foo()
{
//c = null; // should fail
}
void bar()
{
//c = null;
}
}
void test58()
{
}
/************************************/
class A59
{
int[] a;
this() { a.length = 1; }
int* ptr() { return a.ptr; }
const const(int)* ptr() { return a.ptr; }
immutable immutable(int)* ptr() { return a.ptr; }
}
void test59()
{
auto a = new A59;
const b = cast(const)new A59;
immutable c = cast(immutable)new A59;
}
/************************************/