File: testenum.d

package info (click to toggle)
gcc-arm-none-eabi 15%3A12.2.rel1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 959,712 kB
  • sloc: cpp: 3,275,382; ansic: 2,061,766; ada: 840,956; f90: 208,513; makefile: 76,132; asm: 73,433; xml: 50,448; exp: 34,146; sh: 32,436; objc: 15,637; fortran: 14,012; python: 11,991; pascal: 6,787; awk: 4,779; perl: 3,054; yacc: 338; ml: 285; lex: 201; haskell: 122
file content (452 lines) | stat: -rw-r--r-- 8,025 bytes parent folder | download | duplicates (6)
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
/*
PERMUTE_ARGS:
RUN_OUTPUT:
---
Success
---
*/

extern(C) int printf(const char*, ...);

/**********************************************/

enum Bar
{
    bar2 = 2,
    bar3,
    bar4 = 0
}

void test1()
{
    Bar b;

    assert(b == 2);
}

/**********************************************/

void test2()
{
    enum E
    {
        a=-1
    }

    assert(E.min == -1);
    assert(E.max == -1);
}


/**********************************************/

void test3()
{
    enum E
    {
        a = 1,
        b = -1,
        c = 3,
        d = 2
    }

    assert(E.min == -1);
    assert(E.max == 3);
}

/**********************************************/

void test4()
{
    enum E
    {
        a = -1,
        b = -1,
        c = -3,
        d = -3
    }

    assert(E.min==-3);
    assert(E.max==-1);
}

/**********************************************/

enum Enum5
{
    A = 3,
    B = 10,
    E = -5,
}

void test5()
{
    assert(Enum5.init == Enum5.A);
    assert(Enum5.init == 3);
    Enum5 e;
    assert(e == Enum5.A);
    assert(e == 3);
}

/***********************************/

enum E6 : byte
{
    NORMAL_VALUE = 0,
    REFERRING_VALUE = NORMAL_VALUE + 1,
    OTHER_NORMAL_VALUE = 2
}

void foo6(E6 e)
{
}

void test6()
{
     foo6(E6.NORMAL_VALUE);
     foo6(E6.REFERRING_VALUE);
     foo6(E6.OTHER_NORMAL_VALUE);
}

/**********************************************/
// https://issues.dlang.org/show_bug.cgi?id=2407

int i2407;

void add2407() { ++i2407; }
void sub2407() { --i2407; }

enum EF2407f : void function()
{
    a = &add2407,
    s = &sub2407,
}

enum EF2407s
{
    a = &add2407,
    s = &sub2407,
}

enum
{
    a2407 = &add2407,
    s2407 = &sub2407,
}

enum : void function()
{
    at2407 = &add2407,
    st2407 = &sub2407,
}

enum EEF2407 : EF2407s
{
    a = EF2407s.a,
    s = EF2407s.s,
}

void test2407()
{
    alias i2407 i;

    EF2407f.a();
    assert(i == 1);
    EF2407f.s();
    assert(i == 0);

    EF2407s.a();
    assert(i == 1);
    EF2407s.s();
    assert(i == 0);

    a2407();
    assert(i == 1);
    s2407();
    assert(i == 0);

    at2407();
    assert(i == 1);
    st2407();
    assert(i == 0);

    EEF2407.a();
    assert(i == 1);
    EEF2407.s();
    assert(i == 0);

    EEF2407.init();
    assert(i == 1);

    struct S { int i; }
    enum ES : S
    {
        a = S(1),
        b = S(3),
        c = S(2),
    }
    static assert(ES.init == S(1));
    static assert(!__traits(compiles, ES.min));
    static assert(!__traits(compiles, ES.max));

    enum EES : ES
    {
        a = ES.a,
        b = ES.b,
        c = ES.c,
    }
    static assert(EES.init == S(1));
    static assert(!__traits(compiles, EES.min));
    static assert(!__traits(compiles, EES.max));

    ES es = ES.c;
    assert(es.i == 2);
    es = ES.b;
    assert(es.i == 3);

    class C { this(int i) { this.i = i; } int i; }
    enum EC : C
    {
        a = new C(42),
        b = null,
        c = new C(1),
        d = new C(33),
    }
    static assert(EC.init.i == (new C(42)).i);
    static assert(!__traits(compiles, EC.min));
    static assert(!__traits(compiles, EC.max));

    EC ec = EC.d;
    assert(ec.i == 33);
    ec = EC.b;
    assert(ec is null);
}

/**********************************************/
// https://issues.dlang.org/show_bug.cgi?id=3096

void test3096()
{
    template Tuple(T...) { alias Tuple = T; }

    template Base(E)
    {
        static if(is(E B == enum))
            alias Base = B;
    }

    template GetEnum(T)
    {
        enum GetEnum { v = T.init }
    }

    struct S { }
    class C { }

    foreach (Type; Tuple!(char, wchar, dchar, byte, ubyte,
                          short, ushort, int, uint, long,
                          ulong, float, double, real, S, C))
    {
        static assert(is(Base!(GetEnum!Type) == Type));
    }
}

/**********************************************/
// https://issues.dlang.org/show_bug.cgi?id=7719

enum foo7719 = bar7719;
enum { bar7719 = 1 }

/**********************************************/
// https://issues.dlang.org/show_bug.cgi?id=9845

enum { A9845 = B9845 }
enum { B9845 = 1 }

/**********************************************/
// https://issues.dlang.org/show_bug.cgi?id=9846

const int A9846 = B9846;
enum { B9846 = 1 }

/**********************************************/
// https://issues.dlang.org/show_bug.cgi?id=10105

enum E10105 : char[1] { a = "a" }

/**********************************************/
// https://issues.dlang.org/show_bug.cgi?id=10113

enum E10113 : string
{
    a = "a",
    b = "b",
    abc = "abc"
}

void test10113()
{
    E10113 v = E10113.b;
    bool check = false;

    final switch (v) {
    case E10113.a: assert(false);
    case E10113.b: check = true; break;
    case E10113.abc: assert(false);
    }

    assert(check);
}

/**********************************************/
// https://issues.dlang.org/show_bug.cgi?id=10503

@property int octal10503(string num)()
{
    return num.length;
}

enum
{
    A10503 = octal10503!"2000000",
    B10503 = octal10503!"4000",
}

/**********************************************/
// https://issues.dlang.org/show_bug.cgi?id=10505

enum
{
    a10505 = true,
    b10505 = 10.0f,
    c10505 = false,
    d10505 = 10,
    e10505 = null
}

static assert(is(typeof(a10505) == bool));
static assert(is(typeof(b10505) == float));
static assert(is(typeof(c10505) == bool));
static assert(is(typeof(d10505) == int));
static assert(is(typeof(e10505) == typeof(null)));

/**********************************************/
// https://issues.dlang.org/show_bug.cgi?id=10561

void test10561()
{
    template Tuple(T...) { alias Tuple = T; }

    foreach (Type; Tuple!(char, wchar, dchar, byte, ubyte,
                          short, ushort, int, uint, long,
                          ulong, float, double, real))
    {
        enum : Type { v = 0, w = 0, x, y = x }
        static assert(is(typeof(v) == Type));
        static assert(is(typeof(w) == Type));
        static assert(is(typeof(x) == Type));
        static assert(is(typeof(y) == Type));
    }

    class B { }
    class D : B { }
    enum : B { a = new D, b = new B, c = null }
    static assert(is(typeof(a) == B));
    static assert(is(typeof(b) == B));
    static assert(is(typeof(c) == B));

    struct S { this(int) { } }
    enum : S { d = S(1), e = S(2) }
    static assert(is(typeof(d) == S));
    static assert(is(typeof(e) == S));

    enum : float[] { f = [], g = [1.0, 2.0], h = [1.0f] }
    static assert(is(typeof(f) == float[]));
    static assert(is(typeof(g) == float[]));
    static assert(is(typeof(h) == float[]));
}

/**********************************************/
// https://issues.dlang.org/show_bug.cgi?id=10612

int[E10612] ie10612;
E10612[int] ei10612;
E10612[E10612] ee10612;

enum E10612 { a }

/**********************************************/
// https://issues.dlang.org/show_bug.cgi?id=10788

enum v10788 = e10788;
enum : int { e10788 }

/**********************************************/

class C7
{
    enum Policy
    {
        PREFER_READERS,
        PREFER_WRITERS
    }

    void foo1( Policy policy = Policy.PREFER_READERS ) { }
    void foo2( Policy policy = Policy.PREFER_WRITERS ) { }
}

/**********************************************/

void test8()
{
    enum E
    {
        A = B,
        E = D + 7,
        B = 3,
        C,
        D,
    }

    assert(E.A == 3);
    assert(E.B == 3);
    assert(E.C == 4);
    assert(E.D == 5);
    assert(E.E == 12);
    assert(E.max == 12);
}

/**********************************************/
// https://issues.dlang.org/show_bug.cgi?id=13220

enum E13220a;
@(1) enum E13220b;

void test13220()
{
    auto prot = __traits(getProtection, E13220a);
    assert(prot == "public");

    auto udas = __traits(getAttributes, E13220b);
    assert(udas[0] == 1);
}

/**********************************************/

int main()
{
    test1();
    test2();
    test3();
    test4();
    test5();
    test6();
    test2407();
    test10113();
    test10561();
    test8();
    test13220();

    printf("Success\n");
    return 0;
}