aboutsummaryrefslogtreecommitdiff
path: root/compiler/tests/runtime/constexpr.test.lc
blob: 6e1c80d38acd5ea8dbd52064fafe9f6e6a4cdb63 (plain)
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

linkname "";

// Please keep synced with runtimeexpr.test.lc

int add1 = 1 + 2;
int add2 = 1 + -2;
int add3 = -1 + 2;
int add4 = -1 + -2;
int add5 = 0 + 9;
int add6 = 1 + 9;
int add7 = 2 + 9;
int add8 = 0xF + 3;
int add9 = 15 + 0xA;
int add10 = 0xF + 0xFF;
int add11 = +123;
int sub1 = 4 - 3;
int sub2 = 3 - 4;
int sub3 = -4 - 3;
int sub4 = -4 - -3;
int sub5 = -3 - 0;
int sub6 = 0 - -3;
int sub7 = -0 - -3;
int sub8 = 3 - -0;
int mul1 = 0 * 0;
int mul2 = 1 * 1;
int mul3 = 1 * 2;
int mul4 = 7 * 9;
int mul5 = 4679 * -6;
int mul6 = 0xFF * 99;
int mul7 = 0xFF * 0x7F;
int mul8 = -10 * 10;
int mul9 = -0xA * -0xF3;
int mul10 = -0x0 * 3;

uint bitand1 = 0x1 bitand 0x3;
uint bitand2 = 1 bitand 3;
uint bitor1 = 1 bitor 4;
uint8 bitxor1 = 1 bitxor 3;

bool and1 = :false and :false;
bool and2 = :false and :true;
bool and3 = :true and :false;
bool and4 = :true and :true;
bool or1 = :false or :false;
bool or2 = :false or :true;
bool or3 = :true or :false;
bool or4 = :true or :true;
bool xor1 = :false xor :false;
bool xor2 = :false xor :true;
bool xor3 = :true xor :false;
bool xor4 = :true xor :true;
bool not1 = not :false;
bool not2 = not :true;

int? opt1 = makeopt 123;
int opt2 = (makeopt 123)?;

typedef E1 = enum(A=12,B=34);
typedef E2 = int enum(A=23,B=45);
count enumbase1 = enumbase E1:B;
int enumbase2 = enumbase E2:B;
(byte a, int b) enum (A=(12,345),B=(34,567)) complexenum = :A;
byte enumbase3 = (enumbase complexenum).a;

uint shl1 = 1 << 2;
uint shl2 = 0xE << 1;
uint shl3 = 1 << 0xA;
uint shr1 = 2 >> 1;
uint shr2 = 0xE >> 1;
uint shr3 = 1 >> 0x2;

bool ieq1 = 0 == 0;
bool ieq2 = 1 == 3;
bool ieq3 = -1 == 1;
//bool ieq4 = -0 == 0; // FIXME the type of -0 is correctly determined as eint8, but then the unary minus operation is not allowed...
bool ieq5 = -1 == -1;
bool ieq6 = 12 == 123;
bool ieq7 = 0x7FFF == 32767;
bool ieq8 = 0xFFFF == 65535;
bool ieq9 = 101 == 111;
bool ieq10 = -1 == 100_000;
//bool ieq11 = 0 == -0;
bool ieq12 = -127 == 128;
bool ieq13 = -128 == 128;
bool ineq1 = 1 != 1;
bool ineq2 = 1 != 0;

int? noint = none;
int? hasint = makeopt 123;
bool noneinteq1 = noint == noint; // TODO this currently evaluates to true. should it evaluate to false instead?
bool noneinteq2 = noint == none;
bool noneinteq3 = none == noint;
bool noneinteq4 = hasint == noint;
bool noneinteq5 = noint == hasint;
bool noneinteq6 = hasint == hasint;
bool noneinteq7 = hasint == none;
bool noneinteq8 = none == hasint;
(bool b, bool c)? nostruct = none;
(bool b, bool c)? hasstruct = makeopt (:true, :false);
bool nonesteq1 = nostruct == nostruct;
bool nonesteq2 = nostruct == none;
bool nonesteq3 = none == nostruct;
bool nonesteq4 = hasstruct == nostruct;
bool nonesteq5 = nostruct == hasstruct;
bool nonesteq6 = hasstruct == hasstruct;
bool nonesteq7 = hasstruct == none;
bool nonesteq8 = none == hasstruct;
bool nonestneq1 = nostruct != nostruct;
bool nonestneq2 = nostruct != none;
bool nonestneq3 = nostruct != hasstruct;
bool nonestneq4 = hasstruct != none;

bool equalenum1 = E1:A == E1:B;
bool equalenum2 = E1:A == E1:A;
bool equalenum3 = E1:A == :B;
bool equalenum4 = E1:A == :A;
bool equalenum5 = :A == E1:B;
bool equalenum6 = :A == E1:A;
bool equalenum7 = :A != E1:B;
bool equalenum8 = :A != E1:A;
bool equalenum9 = bool:true == :false;
bool equalenum10 = bool:true == :true;
bool equalenum11 = bool:true != :false;
bool equalenum12 = bool:true != :true;

int#[3] arr3a = [1,2,3];
int#[3] arr3b = [1,2,3];
bool arreq1 = arr3a == arr3b;
bool arreq2 = arr3a == [1,2,3];
bool arreq3 = [1,2,3] == arr3a;
bool arrneq1 = arr3a != arr3b;
bool arrneq2 = arr3a != [1,2,3];
bool arrneq3 = [1,2,3] != arr3a;

int arrind1a = arr3a#[0];
int arrind1b = arr3a#[0x0];
int arrind2a = arr3a#[1];
int arrind2b = arr3a#[01];
int arrind2c = arr3a#[0_1];
int arrind3a = arr3a#[2];
int arrind3b = arr3a#[0x2];
int arrind3c = arr3a#[0x0_0_02];

(int a, bool b, (byte x, byte y) nested) struct1a = (12, :true, (33, 44));
(int a, bool b, (byte x, byte y) nested) struct1b = (12, :true, (33, 44));
(int a, bool b, (byte x, byte y) nested) struct1x = (12, :true, (33, 33));
bool eqstruct1 = struct1a == struct1a;
bool eqstruct2 = struct1a == struct1b;
bool eqstruct3 = struct1a == struct1x;
bool eqstruct4 = struct1a != struct1a;
bool eqstruct5 = struct1a != struct1b;
bool eqstruct6 = struct1a != struct1x;

int member1 = struct1a.a;
bool member2 = struct1a.b;
int member3 = struct1a.nested.x;
int member4 = struct1a.nested.y;

bool less1 = 1 < 2;
bool less2 = -3 < 1;
bool less3 = 3 < -4;
bool less4 = -3 < -4;
bool less5 = 0 < 0;
//bool less6 = -0 < 0; // FIXME
bool less7 = -1 < -1;
bool greater1 = 1 > 2;
bool greater2 = -3 > 1;
bool greater3 = 3 > -4;
bool greater4 = -3 > -4;
bool greater5 = 0 > 0;
//bool greater6 = -0 > 0; // FIXME
bool greater7 = -1 > -1;
bool lesseq1 = 1 <= 2;
bool lesseq2 = -3 <= 1;
bool lesseq3 = 3 <= -4;
bool lesseq4 = -3 <= -4;
bool lesseq5 = 0 <= 0;
//bool lesseq6 = -0 <= 0; // FIXME
bool lesseq7 = -1 <= -1;
bool greatereq1 = 1 >= 2;
bool greatereq2 = -3 >= 1;
bool greatereq3 = 3 >= -4;
bool greatereq4 = -3 >= -4;
bool greatereq5 = 0 >= 0;
//bool greatereq6 = -0 >= 0; // FIXME
bool greatereq7 = -1 >= -1;

int cond1 = :true then 12 else 34;
int cond2 = :false then 12 else 34;
(int x, int y) cond3 = 1==2 then (11,22) else (33,44);

bool eqnan1 = 1.2 as float == NaN;
bool eqnan2 = NaN == 1.2 as float;
bool eqnan3 = NaN as float == 1.2;
bool eqnan4 = NaN as float == NaN;
bool neqnan1 = 1.2 as float != NaN;
bool neqnan2 = NaN != 1.2 as float;
bool neqnan3 = NaN as float != 1.2;
bool neqnan4 = NaN as float != NaN;

int asexpr1 = 12 as byte;
int small = 123;
byte typeassert1 = small typeassert byte;

wuint8 wrap8_1 = 1 + 254;
wuint8 wrap8_2 = 1 + 255;
wuint8 wrap8_3 = 1 - 2;
wuint8 wrap8_4 = 255 + 255 + 255;
wuint8 wrap8_5 = 1 - 255 - 255 - 255;
wuint16 wrap16_1 = 1 + 65534;
wuint16 wrap16_2 = 1 + 65535;
wuint16 wrap16_3 = 1 - 2;
wuint16 wrap16_4 = 65535 + 65535 + 65535;
wuint16 wrap16_5 = 1 - 65535 - 65535 - 65535;
wuint32 wrap32_1 = 1 + 4294967294;
wuint32 wrap32_2 = 1 + 4294967295;
wuint32 wrap32_3 = 1 - 2;
wuint32 wrap32_4 = 4294967295 + 4294967295 + 4294967295;
wuint32 wrap32_5 = 1 - 4294967295 - 4294967295 - 4294967295;
wuint64 wrap64_1 = 1 + 18446744073709551614;
wuint64 wrap64_2 = 1 + 18446744073709551615;
wuint64 wrap64_3 = 1 - 2;
wuint64 wrap64_4 = 18446744073709551615 + 18446744073709551615 + 18446744073709551615;
wuint64 wrap64_5 = 1 - 18446744073709551615 - 18446744073709551615 - 18446744073709551615;
wuint128 wrap128_1 = 170141183460469231731687303715884105728 + 170141183460469231731687303715884105728;
wuint128 wrap128_2 = 170141183460469231731687303715884105728 + 170141183460469231731687303715884105729;
wuint128 wrap128_3 = 1 - 170141183460469231731687303715884105728 - 170141183460469231731687303715884105729;
wuint128 wrap128_4 = 1 - 170141183460469231731687303715884105728 - 170141183460469231731687303715884105728;
wuint128 wrap128_5 = 340282366920938463463374607431768211455 * 340282366920938463463374607431768211455;
wuint128 wrap128_6 = 340282366920938463463374607431768211455 * 340282366920938463463374607431768199999;

/* Unimplemented in constexpr:
  - bitwise complement operator
  - division and modulus
  - floating point (arithmetic, compare, etc)
  - wrap-around types with system-dependent size (wcount/wuint/wushort/...)
*/


int main()
{
    assert add1 == 3;
    assert add2 == -1;
    assert add3 == 1;
    assert add4 == -3;
    assert add5 == 9;
    assert add6 == 10;
    assert add7 == 11;
    assert add8 == 18;
    assert add9 == 25;
    assert add10 == 270;
    assert add11 == 123;
    assert sub1 == 1;
    assert sub2 == -1;
    assert sub3 == -7;
    assert sub4 == -1;
    assert sub5 == -3;
    assert sub6 == 3;
    assert sub7 == 3;
    assert sub8 == 3;
    assert mul1 == 0;
    assert mul2 == 1;
    assert mul3 == 2;
    assert mul4 == 63;
    assert mul5 == -28074;
    assert mul6 == 25245;
    assert mul7 == 32385;
    assert mul8 == -100;
    assert mul9 == 2430;
    assert mul10 == 0;

    assert bitand1 == 1;
    assert bitand2 == 1;
    assert bitor1 == 5;
    assert bitxor1 == 2;
    assert not and1;
    assert not and2;
    assert not and3;
    assert and4;
    assert not or1;
    assert or2;
    assert or3;
    assert or4;
    assert not xor1;
    assert xor2;
    assert xor3;
    assert not xor4;
    assert not1;
    assert not not2;
    assert opt1? == 123;
    assert opt1 == makeopt 123;
    assert opt2 == 123;
    assert enumbase1 == 34;
    assert enumbase2 == 45;
    assert enumbase3 == 12;
    assert shl1 == 4;
    assert shl2 == 28;
    assert shl3 == 1024;
    assert shr1 == 1;
    assert shr2 == 7;
    assert shr3 == 0;
    assert ieq1;
    assert not ieq2;
    assert not ieq3;
    //assert ieq4;
    assert ieq5;
    assert not ieq6;
    assert ieq7;
    assert ieq8;
    assert not ieq9;
    assert not ieq10;
    //assert ieq11;
    assert not ieq12;
    assert not ieq13;
    assert not ineq1;
    assert ineq2;

    assert noneinteq1;
    assert noneinteq2;
    assert noneinteq3;
    assert not noneinteq4;
    assert not noneinteq5;
    assert noneinteq6;
    assert not noneinteq7;
    assert not noneinteq8;
    assert nonesteq1;
    assert nonesteq2;
    assert nonesteq3;
    assert not nonesteq4;
    assert not nonesteq5;
    assert nonesteq6;
    assert not nonesteq7;
    assert not nonesteq8;
    assert not nonestneq1;
    assert not nonestneq2;
    assert nostruct != hasstruct;
    assert hasstruct != none;

    assert not equalenum1;
    assert equalenum2;
    assert not equalenum3;
    assert equalenum4;
    assert not equalenum5;
    assert equalenum6;
    assert equalenum7;
    assert not equalenum8;
    assert not equalenum9;
    assert equalenum10;
    assert equalenum11;
    assert not equalenum12;

    assert arreq1;
    assert arreq2;
    assert arreq3;
    assert not arrneq1;
    assert not arrneq2;
    assert not arrneq3;
    assert arrind1a == 1;
    assert arrind1b == 1;
    assert arrind2a == 2;
    assert arrind2b == 2;
    assert arrind2c == 2;
    assert arrind3a == 3;
    assert arrind3b == 3;
    assert arrind3c == 3;

    assert eqstruct1;
    assert eqstruct2;
    assert not eqstruct3;
    assert not eqstruct4;
    assert not eqstruct5;
    assert eqstruct6;

    assert member1 == 12;
    assert member2;
    assert member3 == 33;
    assert member4 == 44;

    assert less1;
    assert less2;
    assert not less3;
    assert not less4;
    assert not less5;
    //assert not less6;
    assert not less7;
    assert not greater1;
    assert not greater2;
    assert greater3;
    assert greater4;
    assert not greater5;
    //assert not greater6;
    assert not greater7;
    assert lesseq1;
    assert lesseq2;
    assert not lesseq3;
    assert not lesseq4;
    assert lesseq5;
    //assert lesseq6;
    assert lesseq7;
    assert not greatereq1;
    assert not greatereq2;
    assert greatereq3;
    assert greatereq4;
    assert greatereq5;
    //assert greatereq6;
    assert greatereq7;

    assert cond1 == 12;
    assert cond2 == 34;
    assert cond3 == (33,44);

    assert not eqnan1;
    assert not eqnan2;
    assert not eqnan3;
    assert not eqnan4;
    assert neqnan1;
    assert neqnan2;
    assert neqnan3;
    assert neqnan4;

    assert asexpr1 == 12;
    assert typeassert1 == 123;

    assert wrap8_1 == 255;
    assert wrap8_2 == 0;
    assert wrap8_3 == 255;
    assert wrap8_4 == 253;
    assert wrap8_5 == 4;
    assert wrap16_1 == 65535;
    assert wrap16_2 == 0;
    assert wrap16_3 == 65535;
    assert wrap16_4 == 65533;
    assert wrap16_5 == 4;
    assert wrap32_1 == 4294967295;
    assert wrap32_2 == 0;
    assert wrap32_3 == 4294967295;
    assert wrap32_4 == 4294967293;
    assert wrap32_5 == 4;
    assert wrap64_1 == 18446744073709551615;
    assert wrap64_2 == 0;
    assert wrap64_3 == 18446744073709551615;
    assert wrap64_4 == 18446744073709551613;
    assert wrap64_5 == 4;
    assert wrap128_1 == 0;
    assert wrap128_2 == 1;
    assert wrap128_3 == 0;
    assert wrap128_4 == 1;
    assert wrap128_5 == 1;
    assert wrap128_6 == 11457;

    return 0;
}