| 1 |
/* REQUIRED_ARGS: -preview=bitfields |
| 2 |
*/ |
| 3 |
|
| 4 |
/***************************************************/ |
| 5 |
|
| 6 |
class C |
| 7 |
{ |
| 8 |
uint a:3; |
| 9 |
uint b:1; |
| 10 |
ulong c:64; |
| 11 |
|
| 12 |
int d:3; |
| 13 |
int e:1; |
| 14 |
long f:64; |
| 15 |
|
| 16 |
int i; |
| 17 |
} |
| 18 |
|
| 19 |
static assert(C.a.min == 0); |
| 20 |
static assert(C.a.max == 7); |
| 21 |
|
| 22 |
static assert(C.b.min == 0); |
| 23 |
static assert(C.b.max == 1); |
| 24 |
|
| 25 |
static assert(C.c.min == 0); |
| 26 |
static assert(C.c.max == ulong.max); |
| 27 |
|
| 28 |
static assert(C.d.min == -4); |
| 29 |
static assert(C.d.max == 3); |
| 30 |
|
| 31 |
static assert(C.e.min == -1); |
| 32 |
static assert(C.e.max == 0); |
| 33 |
|
| 34 |
static assert(C.f.min == long.min); |
| 35 |
static assert(C.f.max == long.max); |
| 36 |
|
| 37 |
int testc() |
| 38 |
{ |
| 39 |
scope c = new C(); |
| 40 |
c.d = 9; |
| 41 |
return c.d; |
| 42 |
} |
| 43 |
|
| 44 |
static assert(testc() == 1); |
| 45 |
|
| 46 |
/***************************************************/ |
| 47 |
|
| 48 |
union U |
| 49 |
{ |
| 50 |
uint a:3; |
| 51 |
uint b:1; |
| 52 |
ulong c:64; |
| 53 |
|
| 54 |
int d:3; |
| 55 |
int e:1; |
| 56 |
long f:64; |
| 57 |
|
| 58 |
int i; |
| 59 |
} |
| 60 |
|
| 61 |
static assert(U.sizeof == 8); |
| 62 |
|
| 63 |
static assert(U.a.min == 0); |
| 64 |
static assert(U.a.max == 7); |
| 65 |
|
| 66 |
static assert(U.b.min == 0); |
| 67 |
static assert(U.b.max == 1); |
| 68 |
|
| 69 |
static assert(U.c.min == 0); |
| 70 |
static assert(U.c.max == ulong.max); |
| 71 |
|
| 72 |
static assert(U.d.min == -4); |
| 73 |
static assert(U.d.max == 3); |
| 74 |
|
| 75 |
static assert(U.e.min == -1); |
| 76 |
static assert(U.e.max == 0); |
| 77 |
|
| 78 |
static assert(U.f.min == long.min); |
| 79 |
static assert(U.f.max == long.max); |
| 80 |
|
| 81 |
int testu() |
| 82 |
{ |
| 83 |
U u; |
| 84 |
u.d = 9; |
| 85 |
return u.d; |
| 86 |
} |
| 87 |
|
| 88 |
static assert(testu() == 1); |
| 89 |
|
| 90 |
/***************************************************/ |
| 91 |
// https://issues.dlang.org/show_bug.cgi?id=24193 |
| 92 |
|
| 93 |
union U24193 { |
| 94 |
struct S { |
| 95 |
int aa,bb; |
| 96 |
} |
| 97 |
S ss; |
| 98 |
int tt:8; |
| 99 |
} |
| 100 |
|
| 101 |
static assert(U24193.sizeof == 8); |