OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 // | |
5 // Dart test program for testing typed data. | |
6 | |
7 // Library tag to be able to run in html test framework. | |
8 library ByteDataTest; | |
9 | |
10 import "package:expect/expect.dart"; | |
11 import 'dart:typed_data'; | |
12 | |
13 testGetters() { | |
14 bool host_is_little_endian = | |
15 (new Uint8List.view(new Uint16List.fromList([1]).buffer))[0] == 1; | |
16 | |
17 var list = new Uint8List(8); | |
18 list[0] = 0xf1; | |
19 list[1] = 0xf2; | |
20 list[2] = 0xf3; | |
21 list[3] = 0xf4; | |
22 list[4] = 0xf5; | |
23 list[5] = 0xf6; | |
24 list[6] = 0xf7; | |
25 list[7] = 0xf8; | |
26 var ba = list.buffer; | |
27 | |
28 ByteData bd = new ByteData.view(ba); | |
29 var value; | |
30 int expected_value_be = -3598; | |
31 int expected_value_le = -3343; | |
32 | |
33 value = bd.getInt16(0); // Default is big endian access. | |
34 Expect.equals(expected_value_be, value); | |
35 value = bd.getInt16(0, Endianness.BIG_ENDIAN); | |
36 Expect.equals(expected_value_be, value); | |
37 value = bd.getInt16(0, Endianness.LITTLE_ENDIAN); | |
38 Expect.equals(expected_value_le, value); | |
39 value = bd.getInt16(0, Endianness.HOST_ENDIAN); | |
40 if (host_is_little_endian) { | |
41 Expect.isTrue(identical(Endianness.HOST_ENDIAN, Endianness.LITTLE_ENDIAN)); | |
42 Expect.equals(expected_value_le, value); | |
43 } else { | |
44 Expect.isTrue(identical(Endianness.HOST_ENDIAN, Endianness.BIG_ENDIAN)); | |
45 Expect.equals(expected_value_be, value); | |
46 } | |
47 | |
48 value = bd.getUint16(0); // Default is big endian access. | |
49 Expect.equals(0xf1f2, value); | |
50 value = bd.getUint16(0, Endianness.BIG_ENDIAN); | |
51 Expect.equals(0xf1f2, value); | |
52 value = bd.getUint16(0, Endianness.LITTLE_ENDIAN); | |
53 Expect.equals(0xf2f1, value); | |
54 if (host_is_little_endian) { | |
55 Expect.isTrue(identical(Endianness.HOST_ENDIAN, Endianness.LITTLE_ENDIAN)); | |
56 Expect.equals(0xf2f1, value); | |
57 } else { | |
58 Expect.isTrue(identical(Endianness.HOST_ENDIAN, Endianness.BIG_ENDIAN)); | |
59 Expect.equals(0xf1f2, value); | |
60 } | |
61 | |
62 expected_value_be = -235736076; | |
63 expected_value_le = -185339151; | |
64 | |
65 value = bd.getInt32(0); // Default is big endian access. | |
66 Expect.equals(expected_value_be, value); | |
67 value = bd.getInt32(0, Endianness.BIG_ENDIAN); | |
68 Expect.equals(expected_value_be, value); | |
69 value = bd.getInt32(0, Endianness.LITTLE_ENDIAN); | |
70 Expect.equals(expected_value_le, value); | |
71 if (host_is_little_endian) { | |
72 Expect.isTrue(identical(Endianness.HOST_ENDIAN, Endianness.LITTLE_ENDIAN)); | |
73 Expect.equals(expected_value_le, value); | |
74 } else { | |
75 Expect.isTrue(identical(Endianness.HOST_ENDIAN, Endianness.BIG_ENDIAN)); | |
76 Expect.equals(expected_value_be, value); | |
77 } | |
78 | |
79 value = bd.getUint32(0); // Default is big endian access. | |
80 Expect.equals(0xf1f2f3f4, value); | |
81 value = bd.getUint32(0, Endianness.BIG_ENDIAN); | |
82 Expect.equals(0xf1f2f3f4, value); | |
83 value = bd.getUint32(0, Endianness.LITTLE_ENDIAN); | |
84 Expect.equals(0xf4f3f2f1, value); | |
85 if (host_is_little_endian) { | |
86 Expect.isTrue(identical(Endianness.HOST_ENDIAN, Endianness.LITTLE_ENDIAN)); | |
87 Expect.equals(0xf4f3f2f1, value); | |
88 } else { | |
89 Expect.isTrue(identical(Endianness.HOST_ENDIAN, Endianness.BIG_ENDIAN)); | |
90 Expect.equals(0xf1f2f3f4, value); | |
91 } | |
92 | |
93 expected_value_be = -1012478732780767240; | |
94 expected_value_le = -506664896818842895; | |
95 | |
96 value = bd.getInt64(0); // Default is big endian access. | |
97 Expect.equals(expected_value_be, value); | |
98 value = bd.getInt64(0, Endianness.BIG_ENDIAN); | |
99 Expect.equals(expected_value_be, value); | |
100 value = bd.getInt64(0, Endianness.LITTLE_ENDIAN); | |
101 Expect.equals(expected_value_le, value); | |
102 if (host_is_little_endian) { | |
103 Expect.isTrue(identical(Endianness.HOST_ENDIAN, Endianness.LITTLE_ENDIAN)); | |
104 Expect.equals(expected_value_le, value); | |
105 } else { | |
106 Expect.isTrue(identical(Endianness.HOST_ENDIAN, Endianness.BIG_ENDIAN)); | |
107 Expect.equals(expected_value_be, value); | |
108 } | |
109 | |
110 value = bd.getUint64(0); // Default is big endian access. | |
111 Expect.equals(0xf1f2f3f4f5f6f7f8, value); | |
112 value = bd.getUint64(0, Endianness.BIG_ENDIAN); | |
113 Expect.equals(0xf1f2f3f4f5f6f7f8, value); | |
114 value = bd.getUint64(0, Endianness.LITTLE_ENDIAN); | |
115 Expect.equals(0xf8f7f6f5f4f3f2f1, value); | |
116 if (host_is_little_endian) { | |
117 Expect.isTrue(identical(Endianness.HOST_ENDIAN, Endianness.LITTLE_ENDIAN)); | |
118 Expect.equals(0xf8f7f6f5f4f3f2f1, value); | |
119 } else { | |
120 Expect.isTrue(identical(Endianness.HOST_ENDIAN, Endianness.BIG_ENDIAN)); | |
121 Expect.equals(0xf1f2f3f4f5f6f7f8, value); | |
122 } | |
123 | |
124 double expected_be_value = -2.4060893954673178e+30; | |
125 double expected_le_value = -1.5462104171572421e+32; | |
126 value = bd.getFloat32(0); // Default is big endian access. | |
127 Expect.equals(expected_be_value, value); | |
128 value = bd.getFloat32(0, Endianness.BIG_ENDIAN); | |
129 Expect.equals(expected_be_value, value); | |
130 value = bd.getFloat32(0, Endianness.LITTLE_ENDIAN); | |
131 Expect.equals(expected_le_value, value); | |
132 if (host_is_little_endian) { | |
133 Expect.isTrue(identical(Endianness.HOST_ENDIAN, Endianness.LITTLE_ENDIAN)); | |
134 Expect.equals(expected_le_value, value); | |
135 } else { | |
136 Expect.isTrue(identical(Endianness.HOST_ENDIAN, Endianness.BIG_ENDIAN)); | |
137 Expect.equals(expected_be_value, value); | |
138 } | |
139 | |
140 expected_be_value = -7.898661740976602e+240; | |
141 expected_le_value = -5.185705956736366e+274; | |
142 value = bd.getFloat64(0); // Default is big endian access. | |
143 Expect.equals(expected_be_value, value); | |
144 value = bd.getFloat64(0, Endianness.BIG_ENDIAN); | |
145 Expect.equals(expected_be_value, value); | |
146 value = bd.getFloat64(0, Endianness.LITTLE_ENDIAN); | |
147 Expect.equals(expected_le_value, value); | |
148 if (host_is_little_endian) { | |
149 Expect.isTrue(identical(Endianness.HOST_ENDIAN, Endianness.LITTLE_ENDIAN)); | |
150 Expect.equals(expected_le_value, value); | |
151 } else { | |
152 Expect.isTrue(identical(Endianness.HOST_ENDIAN, Endianness.BIG_ENDIAN)); | |
153 Expect.equals(expected_be_value, value); | |
154 } | |
155 } | |
156 | |
157 validate16be(var list) { | |
158 Expect.equals(0xf1, list[0]); | |
159 Expect.equals(0xf2, list[1]); | |
160 } | |
161 | |
162 validate16le(var list) { | |
163 Expect.equals(0xf2, list[0]); | |
164 Expect.equals(0xf1, list[1]); | |
165 } | |
166 | |
167 validate32be(var list) { | |
168 Expect.equals(0xf1, list[0]); | |
169 Expect.equals(0xf2, list[1]); | |
170 Expect.equals(0xf3, list[2]); | |
171 Expect.equals(0xf4, list[3]); | |
172 } | |
173 | |
174 validate32le(var list) { | |
175 Expect.equals(0xf4, list[0]); | |
176 Expect.equals(0xf3, list[1]); | |
177 Expect.equals(0xf2, list[2]); | |
178 Expect.equals(0xf1, list[3]); | |
179 } | |
180 | |
181 validate64be(var list) { | |
182 Expect.equals(0xf1, list[0]); | |
183 Expect.equals(0xf2, list[1]); | |
184 Expect.equals(0xf3, list[2]); | |
185 Expect.equals(0xf4, list[3]); | |
186 Expect.equals(0xf5, list[4]); | |
187 Expect.equals(0xf6, list[5]); | |
188 Expect.equals(0xf7, list[6]); | |
189 Expect.equals(0xf8, list[7]); | |
190 } | |
191 | |
192 validate64le(var list) { | |
193 Expect.equals(0xf8, list[0]); | |
194 Expect.equals(0xf7, list[1]); | |
195 Expect.equals(0xf6, list[2]); | |
196 Expect.equals(0xf5, list[3]); | |
197 Expect.equals(0xf4, list[4]); | |
198 Expect.equals(0xf3, list[5]); | |
199 Expect.equals(0xf2, list[6]); | |
200 Expect.equals(0xf1, list[7]); | |
201 } | |
202 | |
203 testSetters() { | |
204 bool host_is_little_endian = | |
205 (new Uint8List.view(new Uint16List.fromList([1]).buffer))[0] == 1; | |
206 | |
207 var list = new Uint8List(8); | |
208 for (int i = 0; i < list.length; i++) { | |
209 list[i] = 0; | |
210 } | |
211 var ba = list.buffer; | |
212 ByteData bd = new ByteData.view(ba); | |
213 | |
214 bd.setInt16(0, 0xf1f2); // Default is big endian access. | |
215 validate16be(list); | |
216 bd.setInt16(0, 0xf1f2, Endianness.BIG_ENDIAN); | |
217 validate16be(list); | |
218 bd.setInt16(0, 0xf1f2, Endianness.LITTLE_ENDIAN); | |
219 validate16le(list); | |
220 bd.setInt16(0, 0xf1f2, Endianness.HOST_ENDIAN); | |
221 if (host_is_little_endian) { | |
222 Expect.isTrue(identical(Endianness.HOST_ENDIAN, Endianness.LITTLE_ENDIAN)); | |
223 validate16le(list); | |
224 } else { | |
225 Expect.isTrue(identical(Endianness.HOST_ENDIAN, Endianness.BIG_ENDIAN)); | |
226 validate16be(list); | |
227 } | |
228 | |
229 bd.setUint16(0, 0xf1f2); // Default is big endian access. | |
230 validate16be(list); | |
231 bd.setUint16(0, 0xf1f2, Endianness.BIG_ENDIAN); | |
232 validate16be(list); | |
233 bd.setUint16(0, 0xf1f2, Endianness.LITTLE_ENDIAN); | |
234 validate16le(list); | |
235 bd.setUint16(0, 0xf1f2, Endianness.HOST_ENDIAN); | |
236 if (host_is_little_endian) { | |
237 Expect.isTrue(identical(Endianness.HOST_ENDIAN, Endianness.LITTLE_ENDIAN)); | |
238 validate16le(list); | |
239 } else { | |
240 Expect.isTrue(identical(Endianness.HOST_ENDIAN, Endianness.BIG_ENDIAN)); | |
241 validate16be(list); | |
242 } | |
243 | |
244 bd.setInt32(0, 0xf1f2f3f4); // Default is big endian access. | |
245 validate32be(list); | |
246 bd.setInt32(0, 0xf1f2f3f4, Endianness.BIG_ENDIAN); | |
247 validate32be(list); | |
248 bd.setInt32(0, 0xf1f2f3f4, Endianness.LITTLE_ENDIAN); | |
249 validate32le(list); | |
250 bd.setInt32(0, 0xf1f2f3f4, Endianness.HOST_ENDIAN); | |
251 if (host_is_little_endian) { | |
252 Expect.isTrue(identical(Endianness.HOST_ENDIAN, Endianness.LITTLE_ENDIAN)); | |
253 validate32le(list); | |
254 } else { | |
255 Expect.isTrue(identical(Endianness.HOST_ENDIAN, Endianness.BIG_ENDIAN)); | |
256 validate32be(list); | |
257 } | |
258 | |
259 bd.setUint32(0, 0xf1f2f3f4); // Default is big endian access. | |
260 validate32be(list); | |
261 bd.setUint32(0, 0xf1f2f3f4, Endianness.BIG_ENDIAN); | |
262 validate32be(list); | |
263 bd.setUint32(0, 0xf1f2f3f4, Endianness.LITTLE_ENDIAN); | |
264 validate32le(list); | |
265 bd.setUint32(0, 0xf1f2f3f4, Endianness.HOST_ENDIAN); | |
266 if (host_is_little_endian) { | |
267 Expect.isTrue(identical(Endianness.HOST_ENDIAN, Endianness.LITTLE_ENDIAN)); | |
268 validate32le(list); | |
269 } else { | |
270 Expect.isTrue(identical(Endianness.HOST_ENDIAN, Endianness.BIG_ENDIAN)); | |
271 validate32be(list); | |
272 } | |
273 | |
274 bd.setInt64(0, 0xf1f2f3f4f5f6f7f8); // Default is big endian access. | |
275 validate64be(list); | |
276 bd.setInt64(0, 0xf1f2f3f4f5f6f7f8, Endianness.BIG_ENDIAN); | |
277 validate64be(list); | |
278 bd.setInt64(0, 0xf1f2f3f4f5f6f7f8, Endianness.LITTLE_ENDIAN); | |
279 validate64le(list); | |
280 bd.setInt64(0, 0xf1f2f3f4f5f6f7f8, Endianness.HOST_ENDIAN); | |
281 if (host_is_little_endian) { | |
282 Expect.isTrue(identical(Endianness.HOST_ENDIAN, Endianness.LITTLE_ENDIAN)); | |
283 validate64le(list); | |
284 } else { | |
285 Expect.isTrue(identical(Endianness.HOST_ENDIAN, Endianness.BIG_ENDIAN)); | |
286 validate64be(list); | |
287 } | |
288 | |
289 bd.setUint64(0, 0xf1f2f3f4f5f6f7f8); // Default is big endian access. | |
290 validate64be(list); | |
291 bd.setUint64(0, 0xf1f2f3f4f5f6f7f8, Endianness.BIG_ENDIAN); | |
292 validate64be(list); | |
293 bd.setUint64(0, 0xf1f2f3f4f5f6f7f8, Endianness.LITTLE_ENDIAN); | |
294 validate64le(list); | |
295 bd.setUint64(0, 0xf1f2f3f4f5f6f7f8, Endianness.HOST_ENDIAN); | |
296 if (host_is_little_endian) { | |
297 Expect.isTrue(identical(Endianness.HOST_ENDIAN, Endianness.LITTLE_ENDIAN)); | |
298 validate64le(list); | |
299 } else { | |
300 Expect.isTrue(identical(Endianness.HOST_ENDIAN, Endianness.BIG_ENDIAN)); | |
301 validate64be(list); | |
302 } | |
303 | |
304 bd.setFloat32(0, -2.4060893954673178e+30); // Default is big endian access. | |
305 validate32be(list); | |
306 bd.setFloat32(0, -2.4060893954673178e+30, Endianness.BIG_ENDIAN); | |
307 validate32be(list); | |
308 bd.setFloat32(0, -2.4060893954673178e+30, Endianness.LITTLE_ENDIAN); | |
309 validate32le(list); | |
310 bd.setFloat32(0, -2.4060893954673178e+30, Endianness.HOST_ENDIAN); | |
311 if (host_is_little_endian) { | |
312 Expect.isTrue(identical(Endianness.HOST_ENDIAN, Endianness.LITTLE_ENDIAN)); | |
313 validate32le(list); | |
314 } else { | |
315 Expect.isTrue(identical(Endianness.HOST_ENDIAN, Endianness.BIG_ENDIAN)); | |
316 validate32be(list); | |
317 } | |
318 | |
319 bd.setFloat64(0, -7.898661740976602e+240); // Default is big endian access. | |
320 validate64be(list); | |
321 bd.setFloat64(0, -7.898661740976602e+240, Endianness.BIG_ENDIAN); | |
322 validate64be(list); | |
323 bd.setFloat64(0, -7.898661740976602e+240, Endianness.LITTLE_ENDIAN); | |
324 validate64le(list); | |
325 bd.setFloat64(0, -7.898661740976602e+240, Endianness.HOST_ENDIAN); | |
326 if (host_is_little_endian) { | |
327 Expect.isTrue(identical(Endianness.HOST_ENDIAN, Endianness.LITTLE_ENDIAN)); | |
328 validate64le(list); | |
329 } else { | |
330 Expect.isTrue(identical(Endianness.HOST_ENDIAN, Endianness.BIG_ENDIAN)); | |
331 validate64be(list); | |
332 } | |
333 } | |
334 | |
335 main() { | |
336 testGetters(); | |
337 testSetters(); | |
338 } | |
OLD | NEW |