Commit | Line | Data |
---|---|---|
c906108c SS |
1 | /* Test program to test bit field operations */ |
2 | ||
3 | /* For non-ANSI compilers, use plain ints for the signed bit fields. However, | |
4 | whether they actually end up signed or not is implementation defined, so | |
5 | this may cause some tests to fail. But at least we can still compile | |
6 | the test program and run the tests... */ | |
7 | ||
085dd6e6 | 8 | #if !defined(__STDC__) && !defined(__cplusplus) |
c906108c SS |
9 | #define signed /**/ |
10 | #endif | |
11 | ||
12 | struct fields | |
13 | { | |
14 | unsigned char uc ; | |
15 | signed int s1 : 1; | |
16 | unsigned int u1 : 1; | |
17 | signed int s2 : 2; | |
18 | unsigned int u2 : 2; | |
19 | signed int s3 : 3; | |
20 | unsigned int u3 : 3; | |
21 | signed int s9 : 9; | |
22 | unsigned int u9 : 9; | |
23 | signed char sc ; | |
24 | } flags; | |
25 | ||
d9e98382 SDJ |
26 | struct internalvartest |
27 | { | |
28 | unsigned int a : 1; | |
29 | struct | |
30 | { | |
31 | unsigned int b : 1; | |
32 | struct | |
33 | { | |
34 | unsigned int c : 1; | |
35 | signed int d : 1; | |
36 | } deep; | |
37 | signed int e : 1; | |
38 | } inner; | |
39 | signed int f : 1; | |
40 | } dummy_internalvartest; | |
41 | ||
c906108c SS |
42 | void break1 () |
43 | { | |
44 | } | |
45 | ||
46 | void break2 () | |
47 | { | |
48 | } | |
49 | ||
50 | void break3 () | |
51 | { | |
52 | } | |
53 | ||
54 | void break4 () | |
55 | { | |
56 | } | |
57 | ||
58 | void break5 () | |
59 | { | |
60 | } | |
61 | ||
62 | void break6 () | |
63 | { | |
64 | } | |
65 | ||
66 | void break7 () | |
67 | { | |
68 | } | |
69 | ||
70 | void break8 () | |
71 | { | |
72 | } | |
73 | ||
74 | void break9 () | |
75 | { | |
76 | } | |
77 | ||
78 | void break10 () | |
79 | { | |
80 | } | |
81 | ||
4a76eae5 DJ |
82 | struct container |
83 | { | |
84 | struct fields one; | |
85 | struct fields two; | |
86 | } container; | |
87 | ||
c906108c SS |
88 | /* This is used by bitfields.exp to determine if the target understands |
89 | signed bitfields. */ | |
90 | int i; | |
91 | ||
92 | int main () | |
93 | { | |
94 | /* For each member, set that member to 1, allow gdb to verify that the | |
95 | member (and only that member) is 1, and then reset it back to 0. */ | |
96 | ||
c906108c SS |
97 | flags.uc = 1; |
98 | break1 (); | |
99 | flags.uc = 0; | |
100 | ||
f038d31b | 101 | flags.s1 = -1; |
c906108c SS |
102 | break1 (); |
103 | flags.s1 = 0; | |
104 | ||
105 | flags.u1 = 1; | |
106 | break1 (); | |
107 | flags.u1 = 0; | |
108 | ||
109 | flags.s2 = 1; | |
110 | break1 (); | |
111 | flags.s2 = 0; | |
112 | ||
113 | flags.u2 = 1; | |
114 | break1 (); | |
115 | flags.u2 = 0; | |
116 | ||
117 | flags.s3 = 1; | |
118 | break1 (); | |
119 | flags.s3 = 0; | |
120 | ||
121 | flags.u3 = 1; | |
122 | break1 (); | |
123 | flags.u3 = 0; | |
124 | ||
125 | flags.s9 = 1; | |
126 | break1 (); | |
127 | flags.s9 = 0; | |
128 | ||
129 | flags.u9 = 1; | |
130 | break1 (); | |
131 | flags.u9 = 0; | |
132 | ||
133 | flags.sc = 1; | |
134 | break1 (); | |
135 | flags.sc = 0; | |
136 | ||
137 | /* Fill alternating fields with all 1's and verify that none of the bits | |
138 | "bleed over" to the other fields. */ | |
139 | ||
140 | flags.uc = 0xFF; | |
141 | flags.u1 = 0x1; | |
142 | flags.u2 = 0x3; | |
143 | flags.u3 = 0x7; | |
144 | flags.u9 = 0x1FF; | |
145 | break2 (); | |
146 | flags.uc = 0; | |
147 | flags.u1 = 0; | |
148 | flags.u2 = 0; | |
149 | flags.u3 = 0; | |
150 | flags.u9 = 0; | |
151 | ||
f038d31b DJ |
152 | flags.s1 = -1; |
153 | flags.s2 = -1; | |
154 | flags.s3 = -1; | |
155 | flags.s9 = -1; | |
c906108c SS |
156 | flags.sc = 0xFF; |
157 | break2 (); | |
158 | flags.s1 = 0; | |
159 | flags.s2 = 0; | |
160 | flags.s3 = 0; | |
161 | flags.s9 = 0; | |
162 | flags.sc = 0; | |
163 | ||
164 | /* Fill the unsigned fields with the maximum positive value and verify | |
165 | that the values are printed correctly. */ | |
166 | ||
167 | /* Maximum positive values */ | |
168 | flags.u1 = 0x1; | |
169 | flags.u2 = 0x3; | |
170 | flags.u3 = 0x7; | |
171 | flags.u9 = 0x1FF; | |
172 | break3 (); | |
173 | flags.u1 = 0; | |
174 | flags.u2 = 0; | |
175 | flags.u3 = 0; | |
176 | flags.u9 = 0; | |
177 | ||
178 | /* Fill the signed fields with the maximum positive value, then the maximally | |
179 | negative value, then -1, and verify in each case that the values are | |
180 | printed correctly. */ | |
181 | ||
182 | /* Maximum positive values */ | |
183 | flags.s1 = 0x0; | |
184 | flags.s2 = 0x1; | |
185 | flags.s3 = 0x3; | |
186 | flags.s9 = 0xFF; | |
187 | break4 (); | |
188 | ||
189 | /* Maximally negative values */ | |
f038d31b DJ |
190 | flags.s1 = -0x1; |
191 | flags.s2 = -0x2; | |
192 | flags.s3 = -0x4; | |
193 | flags.s9 = -0x100; | |
c906108c SS |
194 | /* Extract bitfield value so that bitfield.exp can check if the target |
195 | understands signed bitfields. */ | |
196 | i = flags.s9; | |
197 | break4 (); | |
198 | ||
199 | /* -1 */ | |
f038d31b DJ |
200 | flags.s1 = -1; |
201 | flags.s2 = -1; | |
202 | flags.s3 = -1; | |
203 | flags.s9 = -1; | |
c906108c SS |
204 | break4 (); |
205 | ||
206 | flags.s1 = 0; | |
207 | flags.s2 = 0; | |
208 | flags.s3 = 0; | |
209 | flags.s9 = 0; | |
210 | ||
4a76eae5 DJ |
211 | /* Bitfields at a non-zero offset in a containing structure. */ |
212 | container.one.u3 = 5; | |
213 | container.two.u3 = 3; | |
214 | break5 (); | |
215 | ||
c906108c SS |
216 | return 0; |
217 | } |