Debian Codegraph @

gcc/testsuite/g++.dg/cpp0x/constexpr-array19.C

raw
1 // PR c++/89833
2 // Test to verify that constant array elements initialized to zero
3 // evaluate to zero regardless of the form of their initilizer,
4 // and irrespective whether it's explicit or implicit.
5
6 // { dg-do compile { target c++11 } }
7 // { dg-options "-Wall" }
8
9 static const char all_zero[1024] = { };
10
11 namespace test_int
12 {
13 constexpr int a[][3] = { { 0, 0 }, { 0 }, { } };
14
15 static_assert (sizeof a == sizeof (int) * 3 * 3);
16
17 static_assert ( a[0][0] == 0 && a[0][1] == 0 && a[0][2] == 0
18 && a[1][0] == 0 && a[1][1] == 0 && a[1][2] == 0
19 && a[2][0] == 0 && a[2][1] == 0 && a[2][2] == 0);
20
21 constexpr int b[3][3] = { { 0, 0 }, { 0 } };
22
23 static_assert (sizeof b == sizeof (int) * 3 * 3);
24
25 static_assert ( b[0][0] == 0 && b[0][1] == 0 && b[0][2] == 0
26 && b[1][0] == 0 && b[1][1] == 0 && b[1][2] == 0
27 && b[2][0] == 0 && b[2][1] == 0 && b[2][2] == 0);
28
29 constexpr int c[3][3] = { { } };
30
31 static_assert (sizeof c == sizeof (int) * 3 * 3);
32
33 static_assert ( c[0][0] == 0 && c[0][1] == 0 && c[0][2] == 0
34 && c[1][0] == 0 && c[1][1] == 0 && c[1][2] == 0
35 && c[2][0] == 0 && c[2][1] == 0 && c[2][2] == 0);
36
37 }
38
39 namespace test_char
40 {
41 constexpr char a[][3] = { { 0, 0 }, { 0 }, { } };
42
43 static_assert (sizeof a == sizeof (char) * 3 * 3);
44
45 static_assert ( a[0][0] == 0 && a[0][1] == 0 && a[0][2] == 0
46 && a[1][0] == 0 && a[1][1] == 0 && a[1][2] == 0
47 && a[2][0] == 0 && a[2][1] == 0 && a[2][2] == 0);
48
49 constexpr char b[3][3] = { { 0, 0 }, { 0 } };
50
51 static_assert (sizeof b == sizeof (char) * 3 * 3);
52
53 static_assert ( b[0][0] == 0 && b[0][1] == 0 && b[0][2] == 0
54 && b[1][0] == 0 && b[1][1] == 0 && b[1][2] == 0
55 && b[2][0] == 0 && b[2][1] == 0 && b[2][2] == 0);
56
57 constexpr char c[3][3] = { { } };
58
59 static_assert (sizeof c == sizeof (char) * 3 * 3);
60
61 static_assert ( c[0][0] == 0 && c[0][1] == 0 && c[0][2] == 0
62 && c[1][0] == 0 && c[1][1] == 0 && c[1][2] == 0
63 && c[2][0] == 0 && c[2][1] == 0 && c[2][2] == 0);
64 }
65
66 namespace test_string
67 {
68 constexpr char a[][3] = { "\0", "", { } };
69
70 static_assert (sizeof a == sizeof (char) * 3 * 3);
71
72 static_assert ( a[0][0] == 0 && a[0][1] == 0 && a[0][2] == 0
73 && a[1][0] == 0 && a[1][1] == 0 && a[1][2] == 0
74 && a[2][0] == 0 && a[2][1] == 0 && a[2][2] == 0);
75
76 constexpr char b[3][3] = { "\0", "" };
77
78 static_assert (sizeof b == sizeof (char) * 3 * 3);
79
80 static_assert ( b[0][0] == 0 && b[0][1] == 0 && b[0][2] == 0
81 && b[1][0] == 0 && b[1][1] == 0 && b[1][2] == 0
82 && b[2][0] == 0 && b[2][1] == 0 && b[2][2] == 0);
83
84 constexpr char c[3][3] = { };
85
86 static_assert (sizeof c == sizeof (char) * 3 * 3);
87
88 static_assert ( c[0][0] == 0 && c[0][1] == 0 && c[0][2] == 0
89 && c[1][0] == 0 && c[1][1] == 0 && c[1][2] == 0
90 && c[2][0] == 0 && c[2][1] == 0 && c[2][2] == 0);
91 }
92
93 namespace test_string_member
94 {
95 struct B { struct A { char a[5]; } a[2]; };
96
97 constexpr B b[3] =
98 {
99 /* [0] */
100 {
101 /* a = */
102 {
103 /* a[0] */ { { 0, 0, 0, 0, 0 } },
104 /* a[1] */ { { 0, 0 } }
105 }
106 },
107 /* [1] */
108 {
109 /* a */
110 {
111 /* a[0] */ { "\0\0\0\0" },
112 /* a[0] */ { "" }
113 }
114 },
115 };
116
117 static_assert ( b[0].a[0].a[0] == 0
118 && b[0].a[0].a[1] == 0
119 && b[0].a[0].a[2] == 0
120 && b[0].a[0].a[3] == 0
121 && b[0].a[0].a[4] == 0
122 && b[0].a[1].a[0] == 0
123 && b[0].a[1].a[1] == 0
124 && b[0].a[1].a[2] == 0
125 && b[0].a[1].a[3] == 0
126 && b[0].a[1].a[4] == 0
127 && b[1].a[0].a[0] == 0
128 && b[1].a[0].a[1] == 0
129 && b[1].a[0].a[2] == 0
130 && b[1].a[0].a[3] == 0
131 && b[1].a[0].a[4] == 0
132 && b[2].a[0].a[0] == 0
133 && b[2].a[0].a[1] == 0
134 && b[2].a[0].a[2] == 0
135 && b[2].a[0].a[3] == 0
136 && b[2].a[0].a[4] == 0);
137 }