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 | ||
26 | void break1 () | |
27 | { | |
28 | } | |
29 | ||
30 | void break2 () | |
31 | { | |
32 | } | |
33 | ||
34 | void break3 () | |
35 | { | |
36 | } | |
37 | ||
38 | void break4 () | |
39 | { | |
40 | } | |
41 | ||
42 | void break5 () | |
43 | { | |
44 | } | |
45 | ||
46 | void break6 () | |
47 | { | |
48 | } | |
49 | ||
50 | void break7 () | |
51 | { | |
52 | } | |
53 | ||
54 | void break8 () | |
55 | { | |
56 | } | |
57 | ||
58 | void break9 () | |
59 | { | |
60 | } | |
61 | ||
62 | void break10 () | |
63 | { | |
64 | } | |
65 | ||
4a76eae5 DJ |
66 | struct container |
67 | { | |
68 | struct fields one; | |
69 | struct fields two; | |
70 | } container; | |
71 | ||
c906108c SS |
72 | /* This is used by bitfields.exp to determine if the target understands |
73 | signed bitfields. */ | |
74 | int i; | |
75 | ||
76 | int main () | |
77 | { | |
78 | /* For each member, set that member to 1, allow gdb to verify that the | |
79 | member (and only that member) is 1, and then reset it back to 0. */ | |
80 | ||
c906108c SS |
81 | flags.uc = 1; |
82 | break1 (); | |
83 | flags.uc = 0; | |
84 | ||
f038d31b | 85 | flags.s1 = -1; |
c906108c SS |
86 | break1 (); |
87 | flags.s1 = 0; | |
88 | ||
89 | flags.u1 = 1; | |
90 | break1 (); | |
91 | flags.u1 = 0; | |
92 | ||
93 | flags.s2 = 1; | |
94 | break1 (); | |
95 | flags.s2 = 0; | |
96 | ||
97 | flags.u2 = 1; | |
98 | break1 (); | |
99 | flags.u2 = 0; | |
100 | ||
101 | flags.s3 = 1; | |
102 | break1 (); | |
103 | flags.s3 = 0; | |
104 | ||
105 | flags.u3 = 1; | |
106 | break1 (); | |
107 | flags.u3 = 0; | |
108 | ||
109 | flags.s9 = 1; | |
110 | break1 (); | |
111 | flags.s9 = 0; | |
112 | ||
113 | flags.u9 = 1; | |
114 | break1 (); | |
115 | flags.u9 = 0; | |
116 | ||
117 | flags.sc = 1; | |
118 | break1 (); | |
119 | flags.sc = 0; | |
120 | ||
121 | /* Fill alternating fields with all 1's and verify that none of the bits | |
122 | "bleed over" to the other fields. */ | |
123 | ||
124 | flags.uc = 0xFF; | |
125 | flags.u1 = 0x1; | |
126 | flags.u2 = 0x3; | |
127 | flags.u3 = 0x7; | |
128 | flags.u9 = 0x1FF; | |
129 | break2 (); | |
130 | flags.uc = 0; | |
131 | flags.u1 = 0; | |
132 | flags.u2 = 0; | |
133 | flags.u3 = 0; | |
134 | flags.u9 = 0; | |
135 | ||
f038d31b DJ |
136 | flags.s1 = -1; |
137 | flags.s2 = -1; | |
138 | flags.s3 = -1; | |
139 | flags.s9 = -1; | |
c906108c SS |
140 | flags.sc = 0xFF; |
141 | break2 (); | |
142 | flags.s1 = 0; | |
143 | flags.s2 = 0; | |
144 | flags.s3 = 0; | |
145 | flags.s9 = 0; | |
146 | flags.sc = 0; | |
147 | ||
148 | /* Fill the unsigned fields with the maximum positive value and verify | |
149 | that the values are printed correctly. */ | |
150 | ||
151 | /* Maximum positive values */ | |
152 | flags.u1 = 0x1; | |
153 | flags.u2 = 0x3; | |
154 | flags.u3 = 0x7; | |
155 | flags.u9 = 0x1FF; | |
156 | break3 (); | |
157 | flags.u1 = 0; | |
158 | flags.u2 = 0; | |
159 | flags.u3 = 0; | |
160 | flags.u9 = 0; | |
161 | ||
162 | /* Fill the signed fields with the maximum positive value, then the maximally | |
163 | negative value, then -1, and verify in each case that the values are | |
164 | printed correctly. */ | |
165 | ||
166 | /* Maximum positive values */ | |
167 | flags.s1 = 0x0; | |
168 | flags.s2 = 0x1; | |
169 | flags.s3 = 0x3; | |
170 | flags.s9 = 0xFF; | |
171 | break4 (); | |
172 | ||
173 | /* Maximally negative values */ | |
f038d31b DJ |
174 | flags.s1 = -0x1; |
175 | flags.s2 = -0x2; | |
176 | flags.s3 = -0x4; | |
177 | flags.s9 = -0x100; | |
c906108c SS |
178 | /* Extract bitfield value so that bitfield.exp can check if the target |
179 | understands signed bitfields. */ | |
180 | i = flags.s9; | |
181 | break4 (); | |
182 | ||
183 | /* -1 */ | |
f038d31b DJ |
184 | flags.s1 = -1; |
185 | flags.s2 = -1; | |
186 | flags.s3 = -1; | |
187 | flags.s9 = -1; | |
c906108c SS |
188 | break4 (); |
189 | ||
190 | flags.s1 = 0; | |
191 | flags.s2 = 0; | |
192 | flags.s3 = 0; | |
193 | flags.s9 = 0; | |
194 | ||
4a76eae5 DJ |
195 | /* Bitfields at a non-zero offset in a containing structure. */ |
196 | container.one.u3 = 5; | |
197 | container.two.u3 = 3; | |
198 | break5 (); | |
199 | ||
c906108c SS |
200 | return 0; |
201 | } |