File: dbitfields.d

package info (click to toggle)
gcc-arm-none-eabi 15%3A14.2.rel1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,099,328 kB
  • sloc: cpp: 3,627,108; ansic: 2,571,498; ada: 834,230; f90: 235,082; makefile: 79,231; asm: 74,984; xml: 51,692; exp: 39,736; sh: 33,298; objc: 15,629; python: 15,069; fortran: 14,429; pascal: 7,003; awk: 5,070; perl: 3,106; ml: 285; lisp: 253; lex: 204; haskell: 135
file content (223 lines) | stat: -rw-r--r-- 2,831 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* REQUIRED_ARGS: -preview=bitfields
 */

struct S
{
    int a:2, b:4;
}

static assert(S.sizeof == 4);

void test1()
{
    S s;
    s.a = 3;
    assert(s.a == -1);

    s.b = 4;
    assert(s.b == 4);
}

/******************************************/

struct S2
{
    uint a:2, b:4;
}

S2 foo()
{
    S2 s = { 7, 8 };     // test struct literal expressions
    return s;
}

void test2()
{
    S2 s = foo();

    assert(s.a == 3);
    assert(s.b == 8);
}

/******************************************/

struct S3
{
    int i1;
    uint a:2, b:4, c:6;
    int i2;
}

static assert(S3.sizeof == 12);

S3 s3 = { 63, 7, 8 };

void test3()
{
    assert(s3.i1 == 63);
    assert(s3.a == 3);
    assert(s3.b == 8);
    assert(s3.c == 0);
    assert(s3.i2 == 0);
}

/******************************************/

struct S4
{
    int i1;
    uint a:2, b:31;
}

static assert(S4.sizeof == 12);

S4 s4 = { 63, 7, 8 };

void test4()
{
    assert(s4.i1 == 63);
    assert(s4.a == 3);
    assert(s4.b == 8);
}

/******************************************/

struct S5
{
    int i1;
    uint a:2, :0, b:5;
}

static assert(S5.sizeof == 12);

S5 s5 = { 63, 7, 8 };

void test5()
{
    assert(s5.i1 == 63);
    assert(s5.a == 3);
    assert(s5.b == 8);
}

/******************************************/

// https://issues.dlang.org/show_bug.cgi?id=22710

struct S6
{
    uint a:2, b:2;
}

int boo6()
{
    S s;
    s.a = 3;
    s.b = 1;
    s.a += 2;
    return s.a;
}

void test6()
{
    //printf("res: %d\n", test());
    assert(boo6() == 1);
}

/******************************************/

// https://issues.dlang.org/show_bug.cgi?id=22710

struct S7
{
    uint a:2, b:2;
    int c:2, d:2;
}

int test7u()
{
    S7 s;
    s.a = 7;
    s.b = 1;
    s.a += 2;
    return s.a;
}

int test7s()
{
    S7 s;
    s.c = 7;
    s.d = 1;
    s.c += 4;
    return s.c;
}

int test7s2()
{
    S7 s;
    s.c = 7;
    s.d = 2;
    s.c += 4;
    return s.d;
}

void test7()
{
    //printf("uns: %d\n", test7u());
    assert(test7u() == 1);
    //printf("sig: %d\n", test7s());
    assert(test7s() == -1);
    assert(test7s2() == -2);
}

static assert(test7u() ==  1);
static assert(test7s() == -1);
static assert(test7s2() == -2);

/******************************************/
// https://issues.dlang.org/show_bug.cgi?id=24257

struct S24257
{
    uint : 15;
    bool done : 1;
}

bool advance()
{
    S24257 n;
    n.done = false;
    n.done = true;
    return n.done;
}

bool retard()
{
    S24257 n;
    n.done = true;
    n.done = false;
    return n.done;
}

static assert(advance() == true);

void test24257()
{
    assert(advance() == true);
    assert(retard() == false);
}

/******************************************/

int main()
{
    test1();
    test2();
    test3();
    test4();
    test5();
    test6();
    test7();
    test24257();

    return 0;
}