| 1 |
/* REQUIRED_ARGS: -preview=bitfields |
| 2 |
*/ |
| 3 |
|
| 4 |
struct S |
| 5 |
{ |
| 6 |
int a:2, b:4; |
| 7 |
} |
| 8 |
|
| 9 |
static assert(S.sizeof == 4); |
| 10 |
|
| 11 |
void test1() |
| 12 |
{ |
| 13 |
S s; |
| 14 |
s.a = 3; |
| 15 |
assert(s.a == -1); |
| 16 |
|
| 17 |
s.b = 4; |
| 18 |
assert(s.b == 4); |
| 19 |
} |
| 20 |
|
| 21 |
/******************************************/ |
| 22 |
|
| 23 |
struct S2 |
| 24 |
{ |
| 25 |
uint a:2, b:4; |
| 26 |
} |
| 27 |
|
| 28 |
S2 foo() |
| 29 |
{ |
| 30 |
S2 s = { 7, 8 }; // test struct literal expressions |
| 31 |
return s; |
| 32 |
} |
| 33 |
|
| 34 |
void test2() |
| 35 |
{ |
| 36 |
S2 s = foo(); |
| 37 |
|
| 38 |
assert(s.a == 3); |
| 39 |
assert(s.b == 8); |
| 40 |
} |
| 41 |
|
| 42 |
/******************************************/ |
| 43 |
|
| 44 |
struct S3 |
| 45 |
{ |
| 46 |
int i1; |
| 47 |
uint a:2, b:4, c:6; |
| 48 |
int i2; |
| 49 |
} |
| 50 |
|
| 51 |
static assert(S3.sizeof == 12); |
| 52 |
|
| 53 |
S3 s3 = { 63, 7, 8 }; |
| 54 |
|
| 55 |
void test3() |
| 56 |
{ |
| 57 |
assert(s3.i1 == 63); |
| 58 |
assert(s3.a == 3); |
| 59 |
assert(s3.b == 8); |
| 60 |
assert(s3.c == 0); |
| 61 |
assert(s3.i2 == 0); |
| 62 |
} |
| 63 |
|
| 64 |
/******************************************/ |
| 65 |
|
| 66 |
struct S4 |
| 67 |
{ |
| 68 |
int i1; |
| 69 |
uint a:2, b:31; |
| 70 |
} |
| 71 |
|
| 72 |
static assert(S4.sizeof == 12); |
| 73 |
|
| 74 |
S4 s4 = { 63, 7, 8 }; |
| 75 |
|
| 76 |
void test4() |
| 77 |
{ |
| 78 |
assert(s4.i1 == 63); |
| 79 |
assert(s4.a == 3); |
| 80 |
assert(s4.b == 8); |
| 81 |
} |
| 82 |
|
| 83 |
/******************************************/ |
| 84 |
|
| 85 |
struct S5 |
| 86 |
{ |
| 87 |
int i1; |
| 88 |
uint a:2, :0, b:5; |
| 89 |
} |
| 90 |
|
| 91 |
static assert(S5.sizeof == 12); |
| 92 |
|
| 93 |
S5 s5 = { 63, 7, 8 }; |
| 94 |
|
| 95 |
void test5() |
| 96 |
{ |
| 97 |
assert(s5.i1 == 63); |
| 98 |
assert(s5.a == 3); |
| 99 |
assert(s5.b == 8); |
| 100 |
} |
| 101 |
|
| 102 |
/******************************************/ |
| 103 |
|
| 104 |
// https://issues.dlang.org/show_bug.cgi?id=22710 |
| 105 |
|
| 106 |
struct S6 |
| 107 |
{ |
| 108 |
uint a:2, b:2; |
| 109 |
} |
| 110 |
|
| 111 |
int boo6() |
| 112 |
{ |
| 113 |
S s; |
| 114 |
s.a = 3; |
| 115 |
s.b = 1; |
| 116 |
s.a += 2; |
| 117 |
return s.a; |
| 118 |
} |
| 119 |
|
| 120 |
void test6() |
| 121 |
{ |
| 122 |
//printf("res: %d\n", test()); |
| 123 |
assert(boo6() == 1); |
| 124 |
} |
| 125 |
|
| 126 |
/******************************************/ |
| 127 |
|
| 128 |
// https://issues.dlang.org/show_bug.cgi?id=22710 |
| 129 |
|
| 130 |
struct S7 |
| 131 |
{ |
| 132 |
uint a:2, b:2; |
| 133 |
int c:2, d:2; |
| 134 |
} |
| 135 |
|
| 136 |
int test7u() |
| 137 |
{ |
| 138 |
S7 s; |
| 139 |
s.a = 7; |
| 140 |
s.b = 1; |
| 141 |
s.a += 2; |
| 142 |
return s.a; |
| 143 |
} |
| 144 |
|
| 145 |
int test7s() |
| 146 |
{ |
| 147 |
S7 s; |
| 148 |
s.c = 7; |
| 149 |
s.d = 1; |
| 150 |
s.c += 4; |
| 151 |
return s.c; |
| 152 |
} |
| 153 |
|
| 154 |
int test7s2() |
| 155 |
{ |
| 156 |
S7 s; |
| 157 |
s.c = 7; |
| 158 |
s.d = 2; |
| 159 |
s.c += 4; |
| 160 |
return s.d; |
| 161 |
} |
| 162 |
|
| 163 |
void test7() |
| 164 |
{ |
| 165 |
//printf("uns: %d\n", test7u()); |
| 166 |
assert(test7u() == 1); |
| 167 |
//printf("sig: %d\n", test7s()); |
| 168 |
assert(test7s() == -1); |
| 169 |
assert(test7s2() == -2); |
| 170 |
} |
| 171 |
|
| 172 |
static assert(test7u() == 1); |
| 173 |
static assert(test7s() == -1); |
| 174 |
static assert(test7s2() == -2); |
| 175 |
|
| 176 |
/******************************************/ |
| 177 |
// https://issues.dlang.org/show_bug.cgi?id=24257 |
| 178 |
|
| 179 |
struct S24257 |
| 180 |
{ |
| 181 |
uint : 15; |
| 182 |
bool done : 1; |
| 183 |
} |
| 184 |
|
| 185 |
bool advance() |
| 186 |
{ |
| 187 |
S24257 n; |
| 188 |
n.done = false; |
| 189 |
n.done = true; |
| 190 |
return n.done; |
| 191 |
} |
| 192 |
|
| 193 |
bool retard() |
| 194 |
{ |
| 195 |
S24257 n; |
| 196 |
n.done = true; |
| 197 |
n.done = false; |
| 198 |
return n.done; |
| 199 |
} |
| 200 |
|
| 201 |
static assert(advance() == true); |
| 202 |
|
| 203 |
void test24257() |
| 204 |
{ |
| 205 |
assert(advance() == true); |
| 206 |
assert(retard() == false); |
| 207 |
} |
| 208 |
|
| 209 |
/******************************************/ |
| 210 |
|
| 211 |
int main() |
| 212 |
{ |
| 213 |
test1(); |
| 214 |
test2(); |
| 215 |
test3(); |
| 216 |
test4(); |
| 217 |
test5(); |
| 218 |
test6(); |
| 219 |
test7(); |
| 220 |
test24257(); |
| 221 |
|
| 222 |
return 0; |
| 223 |
} |