summaryrefslogtreecommitdiff
path: root/gcc/testsuite/gdc.test/runnable/test19.d
blob: b9360eeeabb3fcfcad97d071943474840bc3145f (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
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
// REQUIRED_ARGS: -unittest

import std.algorithm: cmp;

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

/* ================================ */

class Foo
{
    int foo(int x) { return x + 3; }
}

class Bar : Foo
{
    override int foo(int y) { return y + 4; }
}

void test1()
{
    Bar e;

    assert(e is null);
    e = new Bar();
    assert(e.foo(5) == 9);
}

/* ================================ */

class Foo2
{
    int foo(int x)
    {
        return x + 3;
    }
}

class Bar2 : Foo2
{
    override int foo(int y)
    {
        assert(Foo2.foo(2) == 5);
        return y + 4;
    }
}

void test2()
{
    Bar2 e;

    assert(e is null);
    e = new Bar2();
    assert(e.foo(5) == 9);
    assert(e.Foo2.foo(10) == 13);
}

/* ================================ */

void test3()
{
    debug printf("debug\n");
    debug(1) printf("debug(1)\n");
    debug(2) printf("debug(2)\n");
    debug(3) printf("debug(3)\n");
    debug(bar) printf("debug(bar)\n");
    debug(10) assert(0);

    debug(1)
    {
        int d1 = 3;

        printf("debug(1) { }\n");
    }
    debug(2)
    {
        printf("debug(2): d1 = %d\n", d1);
    }
}

/* ================================ */

int x1;
int x2;

class Foo4
{
    static  this() { x1 = 3; printf("Foo4 ctor()\n"); }
    static ~this() { x1 = 4; printf("Foo4 dtor()\n"); }
}

static  this() { x2 = 5; printf("ctor()\n"); }
static ~this() { x2 = 6; printf("dtor()\n"); }

void test4()
{
    printf("x1 = %d, x2 = %d\n", x1, x2);
    assert(x1 == 3);
    assert(x2 == 5);
}

/* ================================ */

void test5()
{
    version (D_Bits)
    {
    printf("test5()\n");
    static uint foo;
    static uint x = 3;
    static uint len = 32;

    bool[] bools;

    bools = (cast(bool *)&foo)[0..len];
    bools[6] = true;
    assert(foo == (1 << 6));
    }
}

/* ================================ */

int[] test6_1(int[] a)
{
    a.length = 6;
    return a;
}

void test6()
{
    printf("test6()\n");
    int b[3];
    int a[];

    b[0] = 0;
    b[1] = 1;
    b[2] = 2;
    assert(b.length == 3);
    a = test6_1(b);
    a[2] = 2;
    assert(a.length == 6);
}

/* ================================ */

class OutBuffer7
{
    char data[];
    uint offset;

    void write(const(char) *p, uint nbytes)
    {
        data[offset .. offset + nbytes] = (cast(char *)p)[0 .. nbytes];
    }
}


void test7()
{
    printf("test7()\n");
    int i;
    OutBuffer7 ob = new OutBuffer7;

    ob.data = new char[10];
    printf("ob.data.length = %d\n", ob.data.length);
    assert(ob.data.length == 10);
    for (i = 0; i < 10; i++)
        assert(ob.data[i] == char.init);

printf("test7.1()\n");
    ob.data[] = '-';
printf("test7.2()\n");
    printf("ob.data[] = '%.*s'\n", ob.data.length, ob.data.ptr);
    for (i = 0; i < 10; i++)
        assert(ob.data[i] == '-');

    ob.offset = 3;
    ob.write("foo", 3);
    printf("ob.data.length = %d\n", ob.data.length);
    printf("ob.data[] = '%.*s'\n", ob.data.length, ob.data.ptr);
    for (i = 0; i < 10; i++)
    {
        if (i < 3 || i >= 6)
            assert(ob.data[i] == '-');
    }
    assert(ob.data[3] == 'f');
    assert(ob.data[4] == 'o');
    assert(ob.data[5] == 'o');
}

/* ================================ */

class A8
{
    enum { bar = 8, baz }
    int foo;
}

void test8()
{
    printf("test8()\n");
    A8 a;
    a = new A8();
    a.foo = A8.bar;
    assert(a.foo == 8);
}

/* ================================ */


int z9;

unittest
{
    printf("module unittest 9\n");
    z9 = 3;
}


void test9()
{
    assert(z9 == 3);
}

/* ================================ */

void test10()
{
    printf("test10()\n");
    const int i = 8000;
    assert(i == 8000);
    static int j = 78;
    assert(j == 78);
}

/* ================================ */

Object test11_a()
{
    return null;
}

void test11()
{
    assert(test11_a() is null);
}

/* ================================ */

class A12 { }
class B12 { }

int testx(A12 a) { return 1; }

int testx(B12 b) { return 2; }

void test12()
{
    A12 a = new A12();
    B12 b = new B12();

    assert(testx(a) == 1);
    assert(testx(b) == 2);
}

/* ================================ */

char[] tolower13(ref char[] s)
{
    int i;

    for (i = 0; i < s.length; i++)
    {
        char c = s[i];
        if ('A' <= c && c <= 'Z')
            s[i] = cast(char)(c + (cast(char)'a' - 'A'));
    }
    return s;
}

void test13()
{
    char[] s1 = "FoL".dup;
    char[] s2;

    s1 = s1.dup;
    s2 = tolower13(s1);
    assert(cmp(s2, "fol") == 0);
    assert(s2 == s1);
}

/* ================================ */

alias ABC14* LPABC14;
class ABC14 { }

alias DEF14* LPDEF14;
DEF14[3] foo;
struct DEF14 { int x; }

void test14()
{
    assert(foo.sizeof == int.sizeof * 3);
}

/* ================================ */

class bools15
{
    bool a = true, b = true, c = true;
    void dump()
    {
        printf("%d %d %d\n", a, b, c);
    }
}

void test15()
{
     bools15 k = new bools15;
     k.a = true; k.dump();
     k.b = true; k.dump();
     k.c = true; k.dump();
     assert(k.a == true);
     assert(k.b == true);
     assert(k.c == true);
}


/* ================================ */

align(4) struct foo16
{
    short s;
    int i;
}

void test16()
{
    assert(foo16.sizeof == 8);
}

/* ================================ */

enum Color { red, blue, green };
int[Color.max+1] colors1 = [ Color.blue:6, Color.green:2, Color.red:5 ];


enum { red, blue, green };
int[3] colors2 = [ blue:6, green:2, red:5 ];

void test17()
{
    assert(colors1.length == 3);
    assert(colors1[0] == 5);
    assert(colors1[1] == 6);
    assert(colors1[2] == 2);
    assert(colors2[0] == 5);
    assert(colors2[1] == 6);
    assert(colors2[2] == 2);
}

/* ================================ */


alias void* HANDLE18;

HANDLE18 testx18()
{
    return null;
}

void test18()
{
    assert(testx18() is null);
}

/* ================================ */

class Test19 { struct { int a, b, c; } }

void test19()
{
    Test19 t = new Test19();

    t.a = 3;
    assert(t.a == 3);
}

/* ================================ */

bool tested20;

struct S20
{
    unittest
    {
        assert(!tested20);
        tested20 = true;
    }
}

void test20()
{
    assert(tested20);
}

/* ================================ */
// 7848

@safe pure nothrow void func7848() {}

@safe pure nothrow unittest
{
    func7848();
}

/* ================================ */
// 8128

int flag8128 = 0;

interface I8128
{
    unittest
    {
        printf("utest, flag8128 = %d\n", flag8128);
        flag8128 = 1;
    }
}

void test8128()
{
    printf("main, flag8128 = %d\n", flag8128);
    assert(flag8128 == 1);
}

/* ================================ */

class C8635{
        int x;
        this(int x)
        {
                this.x = x;
        }
}
void test8635()
{
        assert(new C8635(2).x==2);
        assert(new C8635(3).x==3);
}

/* ================================ */


int main()
{
    test1();
    test2();
    test3();
    test4();
    test5();
    test6();
    test7();
    test8();
    test9();
    test10();
    test11();
    test12();
    test13();
    test14();
    test15();
    test16();
    test17();
    test18();
    test19();
    test20();
    test8128();
    test8635();
    printf("Success\n");
    return 0;
}