File: testassert.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 (380 lines) | stat: -rw-r--r-- 8,646 bytes parent folder | download | duplicates (5)
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
/*
REQUIRED_ARGS: -checkaction=context -preview=dip1000
PERMUTE_ARGS: -O -g -inline
*/

void test8765()
{
    string msg;
    try
    {
        int a = 0;
        assert(a);
    }
    catch (Throwable e)
    {
        // no-message -> assert expression
        msg = e.msg;
    }
    assert(msg == "0 != true");
}

 void test9255()
{
    string file;
    try
    {
        int x = 0;
        assert(x);
    }
    catch (Throwable e)
    {
        file = e.file;
    }

    version(Windows)
        assert(file && file == r"runnable\testassert.d");
    else
        assert(file && file == "runnable/testassert.d");
}

// https://issues.dlang.org/show_bug.cgi?id=20114
void test20114()
{
    // Function call returning simple type
    static int fun() {
        static int i = 0;
        assert(i++ == 0);
        return 3;
    }

    const a = getMessage(assert(fun() == 4));
    assert(a == "3 != 4");

    // Function call returning complex type with opEquals
    static struct S
    {
        bool opEquals(const int x) const
        {
            return false;
        }
    }

    static S bar()
    {
        static int i = 0;
        assert(i++ == 0);
        return S.init;
    }

    const b = getMessage(assert(bar() == 4));
    assert(b == "S() != 4");

    // Non-call expression with side effects
    int i = 0;
    const c = getMessage(assert(++i == 0));
    assert(c == "1 != 0");
}

version (DigitalMars) version (Win64) version = DMD_Win64;

void test20375() @safe
{
    static struct RefCounted
    {
        // Force temporary through "impure" generator function
        static RefCounted create() @trusted
        {
            __gshared int counter = 0;
            return RefCounted(++counter > 0);
        }

        static int instances;
        static int postblits;

        this(bool) @safe
        {
            instances++;
        }

        this(this) @safe
        {
            instances++;
            postblits++;
        }

        ~this() @safe
        {
            // make the dtor non-nothrow (we are tracking clean-ups during AssertError unwinding)
            if (postblits > 100)
                throw new Exception("");
            assert(instances > 0);
            instances--;
        }

        bool opEquals(RefCounted) @safe
        {
            return true;
        }
    }

    {
        auto a = RefCounted.create();
        RefCounted.instances++; // we're about to construct an instance below, increment manually
        assert(a == RefCounted()); // both operands are pure expressions => no temporaries
    }

    assert(RefCounted.instances == 0);
    assert(RefCounted.postblits == 0);

    {
        auto a = RefCounted.create();
        assert(a == RefCounted.create()); // impure rhs is promoted to a temporary lvalue => copy for a.opEquals(rhs)
    }

    assert(RefCounted.instances == 0);
    assert(RefCounted.postblits == 1);
    RefCounted.postblits = 0;

    {
        const msg = getMessage(assert(RefCounted.create() != RefCounted.create())); // both operands promoted to temporary lvalues
        assert(msg == "RefCounted() == RefCounted()");
    }

    version (DMD_Win64) // FIXME: temporaries apparently not destructed when unwinding via AssertError
    {
        assert(RefCounted.instances >= 0 && RefCounted.instances <= 2);
        RefCounted.instances = 0;
    }
    else
        assert(RefCounted.instances == 0);
    assert(RefCounted.postblits == 1);
    RefCounted.postblits = 0;

    static int numGetLvalImpureCalls = 0;
    ref RefCounted getLvalImpure() @trusted
    {
        numGetLvalImpureCalls++;
        __gshared lval = RefCounted(); // not incrementing RefCounted.instances
        return lval;
    }

    {
        const msg = getMessage(assert(getLvalImpure() != getLvalImpure())); // both operands promoted to ref temporaries
        assert(msg == "RefCounted() == RefCounted()");
    }

    assert(numGetLvalImpureCalls == 2);
    assert(RefCounted.instances == 0);
    assert(RefCounted.postblits == 1);
    RefCounted.postblits = 0;
}

// https://issues.dlang.org/show_bug.cgi?id=21471
void test21471()
{
    {
        static struct S
        {
            S get()() const { return this; }
        }

        static auto f(S s) { return s; }

        auto s = S();
        assert(f(s.get) == s);
    }
    {
        pragma(inline, true)
        real exp(real x) pure nothrow
        {
            return x;
        }

        bool isClose(int lhs, real rhs) pure nothrow
        {
            return false;
        }
        auto c = 0;
        assert(!isClose(c, exp(1)));
    }
}

// https://issues.dlang.org/show_bug.cgi?id=20581
void test20581() @safe
{
    static auto retro(return scope int[] s) @safe
    {
        static struct R
        {
            int[] source;
        }
        return R(s);
    }

    int[5] a = [ 1, 2, 3, 4, 5 ];
    // Creates ref temporary __assertTmpXXXX for source
    // Error: address of variable a assigned to __assertOp27 with longer lifetime
    assert(retro(a[]).source is a[]);
}

string getMessage(T)(lazy T expr) @trusted
{
    try
    {
        expr();
        return null;
    }
    catch (Throwable t)
    {
        return t.msg;
    }
}

void testMixinExpression() @safe
{
    static struct S
    {
        bool opEquals(S) @safe { return true; }
    }

    const msg = getMessage(assert(mixin("S() != S()")));
    assert(msg == "S() == S()");
}

void testUnaryFormat()
{
    int zero = 0, one = 1;
    assert(getMessage(assert(zero)) == "0 != true");
    assert(getMessage(assert(!one)) == "1 == true");

    assert(getMessage(assert(!cast(int) 1.5)) == "1 == true");

    assert(getMessage(assert(!!__ctfe)) == "assert(__ctfe) failed!");

    static struct S
    {
        int i;
        bool opCast() const
        {
            return i < 2;
        }
    }

    assert(getMessage(assert(S(4))) == "S(4) != true");

    S s = S(4);
    assert(getMessage(assert(*&s)) == "S(4) != true");

    assert(getMessage(assert(--(++zero))) == "0 != true");
}

void testAssignments()
{
    int a = 1;
    int b = 2;
    assert(getMessage(assert(a -= --b)) == "0 != true");

    static ref int c()
    {
        static int counter;
        counter++;
        return counter;
    }

    assert(getMessage(assert(--c())) == "0 != true");
}

/// https://issues.dlang.org/show_bug.cgi?id=21472
void testTupleFormat()
{
    alias AliasSeq(T...) = T;

    // Default usage
    {
        alias a = AliasSeq!(1, "ab");
        alias b = AliasSeq!(false, "xyz");
        assert(getMessage(assert(a == b)) == `(1, "ab") != (false, "xyz")`);
    }

    // Single elements work but are not formatted as tuples
    // Is this acceptable? (a workaround would probably require a
    // different name for the tuple formatting hook)
    {
        alias a = AliasSeq!(1, "ab", []);
        alias b = AliasSeq!(false, "xyz", [1]);
        assert(getMessage(assert(a == b)) == `(1, "ab", []) != (false, "xyz", [1])`);
    }

    // Also works with tupleof (as taken from the bug report)
    {
        static struct Var { int a, b; }
        const a = Var(1, 2);
        const b = Var(3, 4);
        const msg = getMessage(assert(a.tupleof == b.tupleof));
        assert(msg == `(1, 2) != (3, 4)`);
    }

    // Also works when creating temporaries for the TupleExp
    {
        static struct S
        {
            int a, b;
        }

        static S get()
        {
            static int i;
            return S(i++, i++);
        }

        auto a = get().tupleof;
        auto b = get().tupleof;

        string msg = getMessage(assert(a == b));
        assert(msg == `(0, 1) != (2, 3)`);

        msg = getMessage(assert(get().tupleof == AliasSeq!(2, 3)));
        assert(msg == `(4, 5) != (2, 3)`);

        msg = getMessage(assert(get().tupleof == get().tupleof));
        assert(msg == `(6, 7) != (8, 9)`);
    }
}

// https://issues.dlang.org/show_bug.cgi?id=21682
void testStaticOperators()
{
    static class environment {
        static bool opCmp(scope const(char)[] name)
        {
            return false;
        }

        static bool opBinaryRight(string op : "in")(scope const(char)[] name)
        {
            return false;
        }
    }

    string msg = getMessage(assert(environment < "Hello"));
    assert(msg == `"environment" >= "Hello"`);

    msg = getMessage(assert("Hello" in environment));
    assert(msg == `"Hello" !in "environment"`);
}

void main()
{
    test8765();
    test9255();
    test20114();
    test20375();
    test21471();
    test20581();
    testMixinExpression();
    testUnaryFormat();
    testAssignments();
    testTupleFormat();
    testStaticOperators();
}