Debian Codegraph @

gcc/testsuite/gdc.test/runnable/staticaa.d

raw
1 // https://issues.dlang.org/show_bug.cgi?id=23103
2 // Issue 23103 - static initialization of associative arrays is not implemented
3
4 nothrow @safe:
5
6 /////////////////////////////////////////////
7
8 int[int] globalAA = [1: 10, 2: 20];
9
10 void testSimple()
11 {
12 assert(globalAA[1] == 10);
13 assert(globalAA[2] == 20);
14 assert(!(30 in globalAA));
15
16 foreach (i; 0 .. 1000)
17 {
18 globalAA[i] = i * 10;
19 assert(globalAA[i] == i * 10);
20 }
21 }
22
23 /////////////////////////////////////////////
24
25 struct Composit
26 {
27 string[string][] aa;
28 }
29
30 auto getAA() { return ["a": "A"]; }
31
32 immutable Composit compositAA = Composit([getAA(), ["b": "B"]]);
33
34 void testComposit() pure
35 {
36 assert(compositAA.aa[0]["a"] == "A");
37 assert(compositAA.aa[1]["b"] == "B");
38 }
39
40 /////////////////////////////////////////////
41
42 struct Destructing
43 {
44 int v;
45 static int destructorsCalled = 0;
46
47 ~this() nothrow
48 {
49 // FIXME: the lowering to newaa calls the destructor at CTFE, so we can't modify globals in it
50 if (!__ctfe)
51 destructorsCalled++;
52 }
53 }
54
55 struct Key
56 {
57 int v;
58 bool opEquals(ref const Key o) const { return v == o.v; }
59 size_t toHash() const nothrow { return v; }
60 }
61
62 Destructing[Key] dAa = [Key(1): Destructing(10), Key(2): Destructing(20)];
63
64 void testDestructor()
65 {
66 assert(dAa[Key(1)].v == 10);
67 assert(dAa[Key(2)].v == 20);
68 assert(Destructing.destructorsCalled == 0);
69 dAa[Key(1)] = Destructing(100);
70 assert(dAa[Key(1)].v == 100);
71 assert(Destructing.destructorsCalled == 1);
72 }
73
74 /////////////////////////////////////////////
75
76 enum A
77 {
78 x, y, z
79 }
80
81 struct S
82 {
83 string[A] t = [A.x : "A.x", A.y : "A.y"];
84 }
85
86 void testStructInit()
87 {
88 S s;
89 assert(s.t[A.x] == "A.x");
90 assert(s.t[A.y] == "A.y");
91 }
92
93 struct S2
94 {
95 string[A] t = [A.x : "A.x", A.y : "A.y"];
96 }
97
98 bool testStructInitCTFE()
99 {
100 S2 s2;
101 assert(s2.t[A.x] == "A.x");
102 assert(s2.t[A.y] == "A.y");
103 return true;
104 }
105 static assert(testStructInitCTFE());
106
107 /////////////////////////////////////////////
108
109 class C
110 {
111 string[int] t = [0 : "zero"];
112 }
113
114 void testClassInit()
115 {
116 C c = new C();
117 assert(c.t[0] == "zero");
118 }
119
120 class C2
121 {
122 string[int] t = [0 : "zero"];
123 }
124
125 bool testClassInitCTFE()
126 {
127 C2 c2 = new C2();
128 assert(c2.t[0] == "zero");
129 return true;
130 }
131 static assert(testClassInitCTFE());
132
133 /////////////////////////////////////////////
134
135 immutable(string)[immutable(int)] immutableAA = [1: "one", 2: "two"];
136
137 void testImmutable()
138 {
139 assert(immutableAA[1] == "one");
140 assert(immutableAA[2] == "two");
141 }
142
143 /////////////////////////////////////////////
144
145 // https://issues.dlang.org/show_bug.cgi?id=24209
146 void testLocalStatic() @trusted
147 {
148 static int[int] aa0 = [1: 2];
149 __gshared string[string] aa1 = null;
150
151 assert(aa0[1] == 2);
152 assert(aa1.length == 0);
153 }
154
155 /////////////////////////////////////////////
156
157 // https://issues.dlang.org/show_bug.cgi?id=24311
158 enum E : int[int] { x = [123: 456] }
159
160 void testEnumInit()
161 {
162 E e = E.init;
163 assert(e[123] == 456);
164 }
165
166 /////////////////////////////////////////////
167
168 // https://issues.dlang.org/show_bug.cgi?id=24370
169 immutable uint[3][string] test = [
170 "oneTwoThree": [1,2,3],
171 "fourFiveSix": [4,5,6],
172 "sevenEightNine": [7,8,9],
173 ];
174
175 void testStaticArray()
176 {
177 assert(test["oneTwoThree"] == [1, 2, 3]);
178 assert(test["fourFiveSix"] == [4, 5, 6]);
179 assert(test["sevenEightNine"] == [7, 8, 9]);
180 }
181
182 /////////////////////////////////////////////
183
184 // https://issues.dlang.org/show_bug.cgi?id=24602
185
186 class Set
187 {
188 bool[string] aa;
189
190 this(bool[string] aa)
191 {
192 this.aa = aa;
193 }
194 }
195
196 class Bar
197 {
198 Set x = new Set(["a": 1, "b": 0]);
199 }
200
201 void testClassLiteral()
202 {
203 assert(new Bar().x.aa["a"] == 1);
204 assert(new Bar().x.aa["b"] == 0);
205 }
206
207 /////////////////////////////////////////////
208
209
210 void main()
211 {
212 testSimple();
213 testComposit();
214 testDestructor();
215 testStructInit();
216 testClassInit();
217 testImmutable();
218 testLocalStatic();
219 testEnumInit();
220 testStaticArray();
221 testClassLiteral();
222 }