1 // REQUIRED_ARGS: -o-
2 // PERMUTE_ARGS:
3 
TypeTuple(T...)4 template TypeTuple(T...) { alias TypeTuple = T; }
5 
startsWith(string s,string m)6 bool startsWith(string s, string m) { return s[0 .. m.length] == m; }
7 
main()8 void main()
9 {
10     enum string castPrefix = "cast(" ~ size_t.stringof ~ ")";
11 
12     // TypeSArray
13     static assert((int[10]).stringof == "int[10]", T.stringof);
14 
15     int[] arr;
16 
17     // IndexExp
18     {
19         // index == IntegerExp
20         static assert((arr[ 4  ]).stringof == "arr[4]");
21         static assert((arr[ 4U ]).stringof == "arr[4]");
22         static assert((arr[ 4L ]).stringof == "arr[4]");
23         static assert((arr[ 4LU]).stringof == "arr[4]");
24 
25         // index == UAddExp
26         static assert((arr[+4  ]).stringof == "arr[4]");
27         static assert((arr[+4U ]).stringof == "arr[4]");
28         static assert((arr[+4L ]).stringof == "arr[4]");
29         static assert((arr[+4LU]).stringof == "arr[4]");
30 
31         // index == NegExp
32         static assert((arr[-4  ]).stringof == "arr[" ~ castPrefix ~ "-4]");
33         static assert((arr[-4U ]).stringof == "arr[4294967292]");
34         static assert((arr[int.min] ).stringof == "arr[" ~ castPrefix ~ "-2147483648]");
35       static if (is(size_t == ulong))
36       {
37         static assert((arr[-4L ]).stringof == "arr[" ~ castPrefix ~ "-4L]");
38         static assert((arr[-4LU]).stringof == "arr[-4LU]");
39 
40         // IntegerLiteral needs suffix if the value is greater than long.max
41         static assert((arr[long.max + 0]).stringof == "arr[9223372036854775807]");
42         static assert((arr[long.max + 1]).stringof == "arr[" ~ castPrefix ~ "(9223372036854775807L + 1L)]");
43       }
44 
45         foreach (Int; TypeTuple!(byte, ubyte, short, ushort, int, uint, long, ulong))
46         {
47             enum Int p4 = +4;
48             enum string result1 = (arr[p4]).stringof;
49             static assert(result1 == "arr[4]");
50 
51             enum string result2 = (arr[cast(Int)+4]).stringof;
52             static assert(result2 == "arr[4]");
53         }
54         foreach (Int; TypeTuple!(byte, short, int, long))
55         {
56             // keep "cast(Type)" in the string representation
57 
58             enum Int m4 = -4;
59             static if (is(typeof({ size_t x = m4; })))
60             {
61                 enum string result1 = (arr[m4]).stringof;
62                 static assert(result1.startsWith("arr[" ~ castPrefix));
63             }
64             else
65                 static assert(!__traits(compiles, arr[m4]));
66 
67             enum string result2 = (arr[cast(Int)-4]).stringof;
68             static assert(result2.startsWith("arr[" ~ castPrefix));
69         }
70     }
71 
72     // SliceExp
73     {
74         // lwr,upr == IntegerExp
75         static assert((arr[4   .. 8  ]).stringof == "arr[4..8]");
76         static assert((arr[4U  .. 8U ]).stringof == "arr[4..8]");
77         static assert((arr[4L  .. 8L ]).stringof == "arr[4..8]");
78         static assert((arr[4LU .. 8LU]).stringof == "arr[4..8]");
79 
80         // lwr,upr == UAddExp
81         static assert((arr[+4   .. +8  ]).stringof == "arr[4..8]");
82         static assert((arr[+4U  .. +8U ]).stringof == "arr[4..8]");
83         static assert((arr[+4L  .. +8L ]).stringof == "arr[4..8]");
84         static assert((arr[+4LU .. +8LU]).stringof == "arr[4..8]");
85     }
86 }
87