File: testbounds.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 (559 lines) | stat: -rw-r--r-- 17,449 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
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/*
RUN_OUTPUT:
---
Success
---
*/
// REQUIRED_ARGS:

// Test array bounds checking

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

template TypeTuple(T...) { alias T TypeTuple; }

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

const int[10] foos = [1,2,3,4,5,6,7,8,9,10];
const int[] food   = [21,22,23,24,25,26,27,28,29,30];
const int *foop    = cast(int*) foos;

static int x = 2;

int index()
{
    return x++;
}

int tests(int i)
{
    return foos[index()];
}

int testd(int i)
{
    return food[index()];
}

int testp(int i)
{
    return foop[i];
}

const(int)[] slices(int lwr, int upr)
{
    return foos[lwr .. upr];
}

const(int)[] sliced(int lwr, int upr)
{
    return food[lwr .. upr];
}

const(int)[] slicep(int lwr, int upr)
{
    return foop[lwr .. upr];
}

void test1()
{
    int i;

    i = tests(0);
    assert(i == 3);

    i = testd(0);
    assert(i == 24);

    i = testp(1);
    assert(i == 2);

    x = 10;
    try
    {
        i = tests(0);
    }
    catch (RangeError a)
    {
        i = 73;
    }
    assert(i == 73);

    x = -1;
    try
    {
        i = testd(0);
    }
    catch (RangeError a)
    {
        i = 37;
    }
    assert(i == 37);

    const(int)[] r;

    r = slices(3,5);
    assert(r[0] == foos[3]);
    assert(r[1] == foos[4]);

    r = sliced(3,5);
    assert(r[0] == food[3]);
    assert(r[1] == food[4]);

    r = slicep(3,5);
    assert(r[0] == foos[3]);
    assert(r[1] == foos[4]);

    try
    {
        i = 7;
        r = slices(5,3);
    }
    catch (RangeError a)
    {
        i = 53;
    }
    assert(i == 53);

    try
    {
        i = 7;
        r = slices(5,11);
    }
    catch (RangeError a)
    {
        i = 53;
    }
    assert(i == 53);

    try
    {
        i = 7;
        r = sliced(5,11);
    }
    catch (RangeError a)
    {
        i = 53;
    }
    assert(i == 53);

    try
    {
        i = 7;
        r = slicep(5,3);
    }
    catch (RangeError a)
    {
        i = 53;
    }
    assert(i == 53);

    // Take side effects into account
    x = 1;
    r = foos[index() .. 3];
    assert(x == 2);
    assert(r[0] == foos[1]);
    assert(r[1] == foos[2]);

    r = foos[1 .. index()];
    assert(r.length == 1);
    assert(x == 3);
    assert(r[0] == foos[1]);

    x = 1;
    r = food[index() .. 3];
    assert(x == 2);
    assert(r[0] == food[1]);
    assert(r[1] == food[2]);

    r = food[1 .. index()];
    assert(r.length == 1);
    assert(x == 3);
    assert(r[0] == food[1]);

    x = 1;
    r = foop[index() .. 3];
    assert(x == 2);
    assert(r[0] == foop[1]);
    assert(r[1] == foop[2]);

    r = foop[1 .. index()];
    assert(r.length == 1);
    assert(x == 3);
    assert(r[0] == foop[1]);
}

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

void test13976()
{
    int[] da = new int[](10);
    int[10] sa;
    enum size_t two = 2;
    enum size_t five = 5;
    size_t lb = 0;                    // upperInRange
    size_t ub = 9;                    // | lowerLessThan
                                      // | |  check code
    { auto s = da[lb .. ub];        } // 0 0  (ub   <= $  && lb <= ub  )
    { auto s = da[1 .. ub];         } // 0 0  (ub   <= $  && 1  <= ub  )
    { auto s = da[lb .. 10];        } // 0 0  (10   <= $  && lb <= 10  )
    { auto s = da[1 .. ub%5];       } // 0 0  (ub%5 <= $  && 1  <= ub%5)

    { auto s = da[lb .. ub];        } // 0 0  (ub   <= $  && lb <= ub  )
    { auto s = da[0 .. ub];         } // 0 1  (ub   <= $               )
    { auto s = da[lb .. 10];        } // 0 0  (10   <= $  && lb <= 10  )
    { auto s = da[0 .. ub%5];       } // 0 1  (ub%5 <= $               )

    { auto s = da[0 .. 0];          } // 1 1  NULL
    { auto s = da[0 .. $];          } // 1 1  NULL
    { auto s = da[1 .. $];          } // 1 0  (              1   <= $  )
    { auto s = da[$ .. $];          } // 1 0  (              $   <= $  )
    { auto s = da[0 .. $/two];      } // 0 1  ($/2  <= $               )
    { auto s = da[$/two .. $];      } // 1 0  (              $/2 <= $  )
    { auto s = da[$/five .. $/two]; } // 0 0  ($/2  <= $  && $/5 <= $/2)

    { auto s = sa[lb .. ub];        } // 0 0  (ub   <= 10 && lb <= ub  )
    { auto s = sa[1 .. ub];         } // 0 0  (ub   <= 10 && 1  <= ub  )
    { auto s = sa[lb .. 10];        } // 1 0  (              lb <= 10  )
    { auto s = sa[1 .. ub%5];       } // 1 0  (              1  <= ub%5)

    { auto s = sa[lb .. ub];        } // 0 0  (ub   <= 10 && lb <= ub  )
    { auto s = sa[0 .. ub];         } // 0 1  (ub   <= 10              )
    { auto s = sa[lb .. 10];        } // 1 0  (              lb <= 10  )
    { auto s = sa[0 .. ub%5];       } // 1 1  NULL

    { auto s = sa[0 .. 0];          } // 1 1  NULL
    { auto s = sa[0 .. $];          } // 1 1  NULL
    { auto s = sa[1 .. $];          } // 1 1  NULL
    { auto s = sa[$ .. $];          } // 1 1  NULL
    { auto s = sa[0 .. $/two];      } // 1 1  NULL
    { auto s = sa[$/two .. $];      } // 1 1  NULL
    { auto s = sa[$/five .. $/two]; } // 1 1  NULL

    int* p = new int[](10).ptr;
    { auto s = p[0 .. ub];          } // 1 1  NULL
    { auto s = p[lb .. ub];         } // 1 0  (lb <= ub  )
    { auto s = p[0 .. ub%5];        } // 1 1  NULL
    { auto s = p[1 .. ub%5];        } // 1 0  (1  <= ub%5)
    { auto s = p[0 .. 0];           } // 1 1  NULL
}

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

void test3652()
{
    int foo(int[4] x)
    {
        return x[0] + x[1] * x[2] - x[3];
    }

    int[] xs = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];

    // simple case
    foo(xs[0 .. 4]);

  version(none)
  {
    // Need deformation of formula and detection of base point
    int x = 0;
    int y = 0;
    foreach (i; 0 .. 4)
    {
        x += foo(xs[i .. i + 4]);
        y += foo(xs[(i*4+10)/2 .. (i*8>>1)/2+9]);
        // lwr = (i*4 + 10)/2 = i*4/2 + 10/2            = (i*2+5)
        // upr = (i*8>>1)/2 + 5 = (i*4/2) + 5 = i*2 + 9 = (i*2+5) + 4
    }
    assert(x == (0,1,2,3) + (1,2,3, 4) + (2, 3, 4, 5) + ( 3, 4, 5, 6));
    assert(y == (5,6,7,8) + (7,8,9,10) + (9,10,11,12) + (11,12,13,14));
  }
}

void test3652a() @safe
{
    string str = "aaaabbbbccccdddd";
    //printf("str.ptr = %p\n", str.ptr);

    void foo(ref const(char)[16] buf)
    {
        //printf("buf.ptr = %p\n", buf.ptr);
        assert(buf.ptr is str.ptr);
    }

    // can check length at runtime
    assert(str.length == 16);

    // compiler can check the length of string literal, so
    // conversion from immutable(char)[] to ref const(char)[16] is allowed;
    static assert(__traits(compiles, foo("aaaabbbbccccdddd")));

    // OK, correctly rejected by the compiler.
    static assert(!__traits(compiles, foo(str[])));

    // Ugly, furthermore does not work in safe code!
    //foo(*cast(const(char)[16]*)(str[0..16].ptr));

    // New: compiler can check the length of slice, but currently it is not allowed.
    enum size_t m = 0;
    size_t calc(){ return 0; }
    foo(str[0 .. 16]);
    foo(str[m .. 16]);
  //foo(str[calc() .. 16]); // with CTFE

    // If boundaries cannot be calculated in compile time, it's rejected.
    size_t n;
    size_t calc2(){ return n; }
    static assert(!__traits(compiles, foo(str[n .. 16])));
    static assert(!__traits(compiles, foo(str[calc2() .. 16])));

    void hoo1(size_t dim)(char[dim]) { static assert(dim == 2); }
    void hoo2(char[2]) {}
    void hoo3(size_t dim)(ref char[dim]) {}
    void hoo4(ref char[2]) {}
    hoo1(str[0 .. 2]);
    hoo2(str[0 .. 2]);
    static assert(!__traits(compiles, hoo3(str[0 .. 2])));
    static assert(!__traits(compiles, hoo4(str[0 .. 2])));
}
void test3652b() @safe
{
    int[] da = [1,2,3,4,5];

    void bar(int[3] sa1, ref int[3] sa2)
    {
        assert(sa1 == [1,2,3] && sa1.ptr !is da.ptr);
        assert(sa2 == [1,2,3] && sa2.ptr  is da.ptr);
    }
    bar(da[0..3], da[0..3]);
    static assert(!__traits(compiles, bar(da[0..4], da[0..4])));

    void baz1(T)(T[3] sa1, ref T[3] sa2)
    {
        assert(sa1 == [1,2,3] && sa1.ptr !is da.ptr);
        assert(sa2 == [1,2,3] && sa2.ptr  is da.ptr);
    }
    void baz2(T, size_t dim)(T[dim] sa1, ref T[dim] sa2, size_t result)
    {
        assert(dim == result);
        static if (dim == 3)
        {
            assert(sa1 == [1,2,3] && sa1.ptr !is da.ptr);
            assert(sa2 == [1,2,3] && sa2.ptr  is da.ptr);
        }
        else
        {
            assert(sa1 == [1,2,3,4] && sa1.ptr !is da.ptr);
            assert(sa2 == [1,2,3,4] && sa2.ptr  is da.ptr);
        }
    }
    baz1(da[0..3], da[0..3]);
    static assert(!__traits(compiles, baz1(da[0..4], da[0..4])));
    baz2(da[0..3], da[0..3], 3);
    baz2(da[0..4], da[0..4], 4);

    void hoo1(size_t dim)(int[dim]) { static assert(dim == 2); }
    void hoo2(int[2]) {}
    void hoo3(size_t dim)(ref int[dim]) {}
    void hoo4(ref int[2]) {}
    hoo1(da.idup[0 .. 2]);
    hoo2(da.idup[0 .. 2]);
    static assert(!__traits(compiles, hoo3(da.idup[0 .. 2])));
    static assert(!__traits(compiles, hoo4(da.idup[0 .. 2])));
}

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

auto foo9654a(ref           char[8] str) { return str; }
auto foo9654b(ref     const char[8] str) { return str; }
auto foo9654c(ref immutable char[8] str) { return str; }
static assert(!is(typeof(foo9654a("testinfo"))));
static assert( is(typeof(foo9654b("testinfo")) ==     const char[8]));
static assert( is(typeof(foo9654c("testinfo")) == immutable char[8]));

auto bar9654a(T)(ref           T[8] str) { return str; static assert(is(T == immutable char)); }
auto bar9654b(T)(ref     const T[8] str) { return str; static assert(is(T ==           char)); }
auto bar9654c(T)(ref immutable T[8] str) { return str; static assert(is(T ==           char)); }
static assert( is(typeof(bar9654a("testinfo")) == immutable char[8]));
static assert( is(typeof(bar9654b("testinfo")) ==     const char[8]));
static assert( is(typeof(bar9654c("testinfo")) == immutable char[8]));

auto baz9654a(T, size_t dim)(ref           T[dim] str) { return str; static assert(is(T == immutable char)); }
auto baz9654b(T, size_t dim)(ref     const T[dim] str) { return str; static assert(is(T ==           char)); }
auto baz9654c(T, size_t dim)(ref immutable T[dim] str) { return str; static assert(is(T ==           char)); }
static assert( is(typeof(baz9654a("testinfo")) == immutable char[8]));
static assert( is(typeof(baz9654b("testinfo")) ==     const char[8]));
static assert( is(typeof(baz9654c("testinfo")) == immutable char[8]));

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

auto func9712(T)(T[2] arg) { return arg; }
static assert(is(typeof(func9712([1,2])) == int[2]));

auto deduceLength9712(T,size_t n)(T[n] a) { return a; }
static assert(is(typeof(deduceLength9712([1,2,3])) == int[3]));

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

void test9743()
{
    //    +-Char
    //    |+-Immutable or Const or Mutable
    //    ||+-Value or Ref
    //    |||+-Function                           or +-Template
    void fCIVF(    immutable  char[4]) {}   void fCIVT()(    immutable  char[4]) {}
    void fCCVF(        const  char[4]) {}   void fCCVT()(        const  char[4]) {}
    void fCMVF(               char[4]) {}   void fCMVT()(               char[4]) {}
    void fCIRF(ref immutable  char[4]) {}   void fCIRT()(ref immutable  char[4]) {}
    void fCCRF(ref     const  char[4]) {}   void fCCRT()(ref     const  char[4]) {}
    void fCMRF(ref            char[4]) {}   void fCMRT()(ref            char[4]) {}
    alias fcOK = TypeTuple!(fCIVF, fCIVT, fCCVF, fCCVT, fCMVF, fCMVT, fCIRF, fCIRT, fCCRF, fCCRT);
    foreach (f; fcOK)                                   f("1234" )   ;
    foreach (f; fcOK)                                   f("1234"c)   ;
    foreach (f; fcOK) static assert(!__traits(compiles, f("1234"w) ));
    foreach (f; fcOK) static assert(!__traits(compiles, f("1234"d) ));
    alias fcNG = TypeTuple!(fCMRF, fCMRT);  // cannot hold immutable data by mutable ref
    foreach (f; fcNG) static assert(!__traits(compiles, f("1234" ) ));
    foreach (f; fcNG) static assert(!__traits(compiles, f("1234"c) ));
    foreach (f; fcNG) static assert(!__traits(compiles, f("1234"w) ));
    foreach (f; fcNG) static assert(!__traits(compiles, f("1234"d) ));

    //    +-Wchar
    void fWIVF(    immutable wchar[4]) {}   void fWIVT()(    immutable wchar[4]) {}
    void fWCVF(        const wchar[4]) {}   void fWCVT()(        const wchar[4]) {}
    void fWMVF(              wchar[4]) {}   void fWMVT()(              wchar[4]) {}
    void fWIRF(ref immutable wchar[4]) {}   void fWIRT()(ref immutable wchar[4]) {}
    void fWCRF(ref     const wchar[4]) {}   void fWCRT()(ref     const wchar[4]) {}
    void fWMRF(ref           wchar[4]) {}   void fWMRT()(ref           wchar[4]) {}
    alias fwOK = TypeTuple!(fWIVF, fWIVT, fWCVF, fWCVT, fWMVF, fWMVT, fWIRF, fWIRT, fWCRF, fWCRT);
    foreach (f; fwOK)                                   f("1234" )   ;
    foreach (f; fwOK) static assert(!__traits(compiles, f("1234"c) ));
    foreach (f; fwOK)                                   f("1234"w)   ;
    foreach (f; fwOK) static assert(!__traits(compiles, f("1234"d) ));
    alias fwNG = TypeTuple!(fWMRF, fWMRT);  // cannot hold immutable data by mutable ref
    foreach (f; fwNG) static assert(!__traits(compiles, f("1234" ) ));
    foreach (f; fwNG) static assert(!__traits(compiles, f("1234"c) ));
    foreach (f; fwNG) static assert(!__traits(compiles, f("1234"w) ));
    foreach (f; fwNG) static assert(!__traits(compiles, f("1234"d) ));

    //    +-Dchar
    void fDIVF(    immutable dchar[4]) {}   void fDIVT()(    immutable dchar[4]) {}
    void fDCVF(        const dchar[4]) {}   void fDCVT()(        const dchar[4]) {}
    void fDMVF(              dchar[4]) {}   void fDMVT()(              dchar[4]) {}
    void fDIRF(ref immutable dchar[4]) {}   void fDIRT()(ref immutable dchar[4]) {}
    void fDCRF(ref     const dchar[4]) {}   void fDCRT()(ref     const dchar[4]) {}
    void fDMRF(ref           dchar[4]) {}   void fDMRT()(ref           dchar[4]) {}
    alias fdOK = TypeTuple!(fDIVF, fDIVT, fDCVF, fDCVT, fDMVF, fDMVT, fDIRF, fDIRT, fDCRF, fDCRT);
    foreach (f; fdOK)                                   f("1234" )   ;
    foreach (f; fdOK) static assert(!__traits(compiles, f("1234"c) ));
    foreach (f; fdOK) static assert(!__traits(compiles, f("1234"w) ));
    foreach (f; fdOK)                                   f("1234"d)   ;
    alias fdNG = TypeTuple!(fDMRF, fDMRT);  // cannot hold immutable data by mutable ref
    foreach (f; fdNG) static assert(!__traits(compiles, f("1234" ) ));
    foreach (f; fdNG) static assert(!__traits(compiles, f("1234"c) ));
    foreach (f; fdNG) static assert(!__traits(compiles, f("1234"w) ));
    foreach (f; fdNG) static assert(!__traits(compiles, f("1234"d) ));
}

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

void foo9747A(T)(T[4]) {}
void foo9747C(size_t dim)(char[dim]) {}
void foo9747W(size_t dim)(wchar[dim]) {}
void foo9747D(size_t dim)(dchar[dim]) {}

void test9747()
{
    foo9747A("abcd"c);
    foo9747A("abcd"w);
    foo9747A("abcd"d);
    foo9747C("abcd"c);
    foo9747W("abcd"w);
    foo9747D("abcd"d);
}

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

void test12876()
{
    void foo(int[4] b) {}
    void bar(size_t n)(int[n] c) { static assert(n == 4); }

    int[5] a;
    foo(a[1 .. $]); // OK
    bar(a[1 .. $]); // OK <- Error
}

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

void test13775()
{
    ubyte[4] ubytes = [1,2,3,4];

    // CT-known slicing (https://issues.dlang.org/show_bug.cgi?id=3652)
    auto ok1 = cast(ubyte[2]) ubytes[0 .. 2];
    assert(ok1 == [1, 2]);

    // CT-known slicing with implicit conversion of SliceExp::e1 (https://issues.dlang.org/show_bug.cgi?id=13154)
    enum double[] arr = [1.0, 2.0, 3.0];
    auto ok2 = cast(float[2]) [1.0, 2.0, 3.0][0..2];
    auto ok3 = cast(float[2]) arr[1..3];    // currently this is accepted
    assert(ok2 == [1f, 2f]);
    assert(ok3 == [2f, 3f]);

    // CT-known slicing with type coercing (https://issues.dlang.org/show_bug.cgi?id=13775)
    auto ok4 = cast( byte[2]) ubytes[0 .. 2];   // CT-known slicing + type coercing
    auto ok5 = cast(short[1]) ubytes[0 .. 2];   // CT-known slicing + type coercing
    assert(ok4 == [1, 2]);
    version(LittleEndian) assert(ok5 == [0x0201]);
    version(   BigEndian) assert(ok5 == [0x0102]);
}

/******************************************/
// https://issues.dlang.org/show_bug.cgi?id=15889
// Array bounds check should report index and length
void test15889()
{
    int[] a = new int[2];

    try {
        a[3] = 40;
    } catch (ArrayIndexError e) {
        assert(e.index == 3);
        assert(e.length == 2);
    }

    try {
        a[1 .. 4] = 50;
    } catch (ArraySliceError e) {
        assert(e.lower == 1);
        assert(e.upper == 4);
        assert(e.length == 2);
    }
}

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

int main()
{
    test1();
    test13976();
    test3652();
    test3652a();
    test3652b();
    test9743();
    test9747();
    test13775();
    test15889();

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