| 1 |
/* REQUIRED_ARGS: -preview=bitfields |
| 2 |
*/ |
| 3 |
|
| 4 |
struct T |
| 5 |
{ |
| 6 |
uint x : 2, y : 3, :0; |
| 7 |
int :0; |
| 8 |
} |
| 9 |
|
| 10 |
uint foo(T s) |
| 11 |
{ |
| 12 |
return s.x + s.y; |
| 13 |
} |
| 14 |
|
| 15 |
void test1() |
| 16 |
{ |
| 17 |
T s; |
| 18 |
s.x = 2; |
| 19 |
s.y = 4; |
| 20 |
uint u = foo(s); |
| 21 |
assert(u == 6); |
| 22 |
} |
| 23 |
|
| 24 |
/********************************************/ |
| 25 |
|
| 26 |
struct S |
| 27 |
{ |
| 28 |
uint a:3; |
| 29 |
uint b:1; |
| 30 |
ulong c:64; |
| 31 |
|
| 32 |
int d:3; |
| 33 |
int e:1; |
| 34 |
long f:64; |
| 35 |
|
| 36 |
int i; |
| 37 |
alias f this; |
| 38 |
} |
| 39 |
|
| 40 |
static assert(S.a.min == 0); |
| 41 |
static assert(S.a.max == 7); |
| 42 |
|
| 43 |
static assert(S.b.min == 0); |
| 44 |
static assert(S.b.max == 1); |
| 45 |
|
| 46 |
static assert(S.c.min == 0); |
| 47 |
static assert(S.c.max == ulong.max); |
| 48 |
|
| 49 |
static assert(S.d.min == -4); |
| 50 |
static assert(S.d.max == 3); |
| 51 |
|
| 52 |
static assert(S.e.min == -1); |
| 53 |
static assert(S.e.max == 0); |
| 54 |
|
| 55 |
static assert(S.f.min == long.min); |
| 56 |
static assert(S.f.max == long.max); |
| 57 |
static assert(S.max == S.f.max); |
| 58 |
|
| 59 |
void test2() |
| 60 |
{ |
| 61 |
int x; |
| 62 |
S effect() |
| 63 |
{ |
| 64 |
++x; |
| 65 |
return S(); |
| 66 |
} |
| 67 |
assert(effect().a.max == 7); |
| 68 |
assert(effect().i.max == int.max); |
| 69 |
assert(x == 0); // ensure effect() was not executed |
| 70 |
} |
| 71 |
|
| 72 |
/********************************************/ |
| 73 |
|
| 74 |
struct U |
| 75 |
{ |
| 76 |
int a; |
| 77 |
int b:3, c:4; |
| 78 |
this(this) |
| 79 |
{ |
| 80 |
b = 2; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
static assert(U.b.offsetof == 4); |
| 85 |
static assert(U.b.sizeof == 4); |
| 86 |
|
| 87 |
void test3() |
| 88 |
{ |
| 89 |
U u; |
| 90 |
u.c = 4; |
| 91 |
U v = u; |
| 92 |
assert(v.c == 4); |
| 93 |
u = v; |
| 94 |
assert(u.b == 2); |
| 95 |
assert(__traits(getMember, u, "b") == 2); |
| 96 |
} |
| 97 |
|
| 98 |
/********************************************/ |
| 99 |
|
| 100 |
int main() |
| 101 |
{ |
| 102 |
test1(); |
| 103 |
test2(); |
| 104 |
test3(); |
| 105 |
return 0; |
| 106 |
} |