Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(353)

Side by Side Diff: frog/tests/frog/src/NodeTypedArrayTest.dart

Issue 9034014: Add support for the node net module. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: new version of minfrog Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011, 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 #library('NodeTypedArrayTest');
6
7 #import('unittest_node.dart');
8 #import('../../../lib/node/node.dart');
9
10 void compareLists(List a, List b) {
11 Expect.equals(a.length, b.length, 'length of lists');
12 for (int i = 0; i < a.length; i++) {
13 Expect.equals(a[i], b[i]);
14 }
15 }
16
17 List listFromList(ctor, List a) {
18 int length = a.length;
19 List b = ctor(length);
20 for (int i = 0; i < a.length; i++) {
21 b[i] = a[i];
22 }
23 return b;
24 }
25
26 void listTest(List ctor(int length)) {
27 group('List', (){
28 test('length', () {
29 List a = ctor(10);
30 Expect.equals(10, a.length);
31 });
32 test('operator[]', () {
33 List src = [0,1,2,3];
34 List a = listFromList(ctor, src);
35 compareLists(src, a);
36 });
37 test('operator[]=', () {
38 List src = [0,1,2,3];
39 int length = src.length;
40 List a = ctor(length);
41 for (int i = 0; i < length; i++) {
42 a[i] = src[i];
43 }
44 compareLists(src, a);
45 });
46 test('indexOf', () {
47 List a = listFromList(ctor, [10,11,12,11]);
48 Expect.equals(-1, a.indexOf(17));
49 Expect.equals(1, a.indexOf(11));
50 Expect.equals(3, a.indexOf(11, 2));
51 });
52 test('lastIndexOf', () {
53 List a = listFromList(ctor, [10,11,12,11]);
54 Expect.equals(-1, a.lastIndexOf(17));
55 Expect.equals(3, a.lastIndexOf(11));
56 Expect.equals(1, a.lastIndexOf(11, 2));
57 });
58 test('last', () {
59 List a = listFromList(ctor, [10,11,12,13]);
60 Expect.equals(13, a.last());
61 });
62 test('getRange', () {
63 List a = listFromList(ctor, [10,11,12,13]);
64 List b = a.getRange(1,2);
65 a[1] = 15;
66 a[2] = 17;
67 compareLists(listFromList(ctor, [11,12]), b);
68 });
69 test('sort', () {
70 List a = listFromList(ctor, [13,11,11,12]);
71 List b = listFromList(ctor, [11,11,12,13]);
72 a.sort((a,b) => a < b ? -1 : ((a == b) ? 0 : 1));
73 compareLists(b, a);
74 });
75 test('forEach', () {
76 List a = listFromList(ctor, [10, 11, 12, 13]);
77 int expected = 10;
78 int count = 0;
79 a.forEach((e) {
80 Expect.equals(expected, e);
81 expected++;
82 count++;
83 });
84 Expect.equals(a.length, count);
85 });
86 test('filter', () {
87 List a = listFromList(ctor, [10, 11, 12, 13]);
88 int expected = 10;
89 int count = 0;
90 List b = a.filter((e) {
91 Expect.equals(expected, e);
92 expected++;
93 count++;
94 return (e & 1) == 0;
95 });
96 Expect.equals(a.length, count);
97 compareLists(listFromList(ctor, [10, 12]), b);
98 });
99 test('map', () {
100 List a = listFromList(ctor, [10, 11, 12, 13]);
101 int expected = 10;
102 int count = 0;
103 List b = a.map((e) {
104 Expect.equals(expected, e);
105 expected++;
106 count++;
107 return e * 2;
108 });
109 Expect.equals(a.length, count);
110 compareLists(listFromList(ctor, [20, 22, 24, 26]), b);
111 });
112 test('every', () {
113 List a = listFromList(ctor, [10, 11, 12, 13]);
114 Expect.equals(true, a.every((e) => e >= 10));
115 Expect.equals(false, a.every((e) => e < 13));
116 });
117 test('some', () {
118 List a = listFromList(ctor, [10, 11, 12, 13]);
119 Expect.equals(true, a.some((e) => e == 13));
120 Expect.equals(false, a.some((e) => e == 14));
121 });
122 test('isEmpty', () {
123 Expect.equals(true, ctor(0).isEmpty());
124 Expect.equals(false, ctor(1).isEmpty());
125 });
126 test('iterator', () {
127 List a = listFromList(ctor, [10, 11, 12, 13]);
128 int expected = 10;
129 int count = 0;
130 Iterator i = a.iterator();
131 while (i.hasNext()) {
132 var e = i.next();
133 Expect.equals(expected, e);
134 expected++;
135 count++;
136 }
137 Expect.equals(a.length, count);
138 });
139 });
140 }
141
142 main() {
143 test('ArrayBuffer', () {
144 ArrayBuffer a = new ArrayBuffer(100);
145 Expect.equals(a.byteLength, 100);
146 });
147
148 void basicTest(TypedArrayBufferView a, int length,
149 int bytesPerElement) {
150 a[0] = 12;
151 Expect.equals(12, a[0]);
152 Expect.equals(bytesPerElement, a.BYTES_PER_ELEMENT);
153 Expect.equals(length, a.length);
154 Expect.equals(0, a.byteOffset);
155 Expect.equals(length * a.BYTES_PER_ELEMENT, a.byteLength);
156 Expect.equals(a.byteLength, a.buffer.byteLength);
157
158 var sub = a.subarray(75);
159 Expect.equals(length - 75, sub.length, 'subarray length');
160 sub = a.subarray(2, 10);
161 Expect.equals(8, sub.length, 'subarray length 8');
162
163 sub[0] = 13;
164 a.set(sub, 1);
165 Expect.equals(13, a[1], 'polymorphic set 2 args');
166
167 sub[0] = 14;
168 a.set(sub, 0);
169 Expect.equals(14, a[0], 'polymorphic set 1 arg');
170 }
171
172 group('Int8Array', () {
173 test('basic', () {
174 basicTest(new Int8Array(100), 100, 1);
175 });
176 listTest((length) => new Int8Array(length));
177 test('fromArray', () {
178 Int8Array a = new Int8Array(100);
179 a[0] = 12;
180 Int8Array b = new Int8Array.fromArray(a);
181 Expect.equals(a[0], b[0]);
182 });
183 test('fromList', () {
184 List<int> a = [12];
185 Int8Array b = new Int8Array.fromList(a);
186 Expect.equals(a[0], b[0]);
187 });
188 test('fromArrayBuffer 1 arg', () {
189 ArrayBuffer a = new ArrayBuffer(128);
190 Int8Array b = new Int8Array.fromArrayBuffer(a);
191 Expect.equals(b.buffer, a);
192 Expect.equals(128, b.length * b.BYTES_PER_ELEMENT);
193 });
194 test('fromArrayBuffer 2 args', () {
195 ArrayBuffer a = new ArrayBuffer(128);
196 Int8Array b = new Int8Array.fromArrayBuffer(a, 16);
197 Expect.equals(a, b.buffer);
198 Expect.equals(112, b.length * b.BYTES_PER_ELEMENT);
199 });
200 test('fromArrayBuffer 3 args', () {
201 ArrayBuffer a = new ArrayBuffer(128);
202 Int8Array b = new Int8Array.fromArrayBuffer(a, 16, 3);
203 Expect.equals(a, b.buffer);
204 Expect.equals(3, b.length);
205 });
206 });
207
208 group('Uint8Array', () {
209 test('basic', () {
210 basicTest(new Uint8Array(100), 100, 1);
211 });
212 listTest((length) => new Uint8Array(length));
213 test('fromArray', () {
214 Uint8Array a = new Uint8Array(100);
215 a[0] = 12;
216 Uint8Array b = new Uint8Array.fromArray(a);
217 Expect.equals(a[0], b[0]);
218 });
219 test('fromList', () {
220 List<int> a = [12];
221 Uint8Array b = new Uint8Array.fromList(a);
222 Expect.equals(a[0], b[0]);
223 });
224 test('fromArrayBuffer 1 arg', () {
225 ArrayBuffer a = new ArrayBuffer(128);
226 Uint8Array b = new Uint8Array.fromArrayBuffer(a);
227 Expect.equals(b.buffer, a);
228 Expect.equals(128, b.length * b.BYTES_PER_ELEMENT);
229 });
230 test('fromArrayBuffer 2 args', () {
231 ArrayBuffer a = new ArrayBuffer(128);
232 Uint8Array b = new Uint8Array.fromArrayBuffer(a, 16);
233 Expect.equals(a, b.buffer);
234 Expect.equals(112, b.length * b.BYTES_PER_ELEMENT);
235 });
236 test('fromArrayBuffer 3 args', () {
237 ArrayBuffer a = new ArrayBuffer(128);
238 Uint8Array b = new Uint8Array.fromArrayBuffer(a, 16, 3);
239 Expect.equals(a, b.buffer);
240 Expect.equals(3, b.length);
241 });
242 });
243
244 group('Int16Array', () {
245 test('basic', () {
246 basicTest(new Int16Array(100), 100, 2);
247 });
248 listTest((length) => new Int16Array(length));
249 test('fromArray', () {
250 Int16Array a = new Int16Array(100);
251 a[0] = 12;
252 Int16Array b = new Int16Array.fromArray(a);
253 Expect.equals(a[0], b[0]);
254 });
255 test('fromList', () {
256 List<int> a = [12];
257 Int16Array b = new Int16Array.fromList(a);
258 Expect.equals(a[0], b[0]);
259 });
260 test('fromArrayBuffer 1 arg', () {
261 ArrayBuffer a = new ArrayBuffer(128);
262 Int16Array b = new Int16Array.fromArrayBuffer(a);
263 Expect.equals(b.buffer, a);
264 Expect.equals(128, b.length * b.BYTES_PER_ELEMENT);
265 });
266 test('fromArrayBuffer 2 args', () {
267 ArrayBuffer a = new ArrayBuffer(128);
268 Int16Array b = new Int16Array.fromArrayBuffer(a, 16);
269 Expect.equals(a, b.buffer);
270 Expect.equals(112, b.length * b.BYTES_PER_ELEMENT);
271 });
272 test('fromArrayBuffer 3 args', () {
273 ArrayBuffer a = new ArrayBuffer(128);
274 Int16Array b = new Int16Array.fromArrayBuffer(a, 16, 3);
275 Expect.equals(a, b.buffer);
276 Expect.equals(3, b.length);
277 });
278 });
279
280 group('Uint16Array', () {
281 test('basic', () {
282 basicTest(new Uint16Array(100), 100, 2);
283 });
284 listTest((length) => new Uint16Array(length));
285 test('fromArray', () {
286 Uint16Array a = new Uint16Array(100);
287 a[0] = 12;
288 Uint16Array b = new Uint16Array.fromArray(a);
289 Expect.equals(a[0], b[0]);
290 });
291 test('fromList', () {
292 List<int> a = [12];
293 Uint16Array b = new Uint16Array.fromList(a);
294 Expect.equals(a[0], b[0]);
295 });
296 test('fromArrayBuffer 1 arg', () {
297 ArrayBuffer a = new ArrayBuffer(128);
298 Uint16Array b = new Uint16Array.fromArrayBuffer(a);
299 Expect.equals(b.buffer, a);
300 Expect.equals(128, b.length * b.BYTES_PER_ELEMENT);
301 });
302 test('fromArrayBuffer 2 args', () {
303 ArrayBuffer a = new ArrayBuffer(128);
304 Uint16Array b = new Uint16Array.fromArrayBuffer(a, 16);
305 Expect.equals(a, b.buffer);
306 Expect.equals(112, b.length * b.BYTES_PER_ELEMENT);
307 });
308 test('fromArrayBuffer 3 args', () {
309 ArrayBuffer a = new ArrayBuffer(128);
310 Uint16Array b = new Uint16Array.fromArrayBuffer(a, 16, 3);
311 Expect.equals(a, b.buffer);
312 Expect.equals(3, b.length);
313 });
314 });
315
316 group('Int32Array', () {
317 test('basic', () {
318 basicTest(new Int32Array(100), 100, 4);
319 });
320 listTest((length) => new Int32Array(length));
321 test('fromArray', () {
322 Int32Array a = new Int32Array(100);
323 a[0] = 12;
324 Int32Array b = new Int32Array.fromArray(a);
325 Expect.equals(a[0], b[0]);
326 });
327 test('fromList', () {
328 List<int> a = [12];
329 Int32Array b = new Int32Array.fromList(a);
330 Expect.equals(a[0], b[0]);
331 });
332 test('fromArrayBuffer 1 arg', () {
333 ArrayBuffer a = new ArrayBuffer(128);
334 Int32Array b = new Int32Array.fromArrayBuffer(a);
335 Expect.equals(b.buffer, a);
336 Expect.equals(128, b.length * b.BYTES_PER_ELEMENT);
337 });
338 test('fromArrayBuffer 2 args', () {
339 ArrayBuffer a = new ArrayBuffer(128);
340 Int32Array b = new Int32Array.fromArrayBuffer(a, 16);
341 Expect.equals(a, b.buffer);
342 Expect.equals(112, b.length * b.BYTES_PER_ELEMENT);
343 });
344 test('fromArrayBuffer 3 args', () {
345 ArrayBuffer a = new ArrayBuffer(128);
346 Int32Array b = new Int32Array.fromArrayBuffer(a, 16, 3);
347 Expect.equals(a, b.buffer);
348 Expect.equals(3, b.length);
349 });
350 });
351
352 group('Uint32Array', () {
353 test('basic', () {
354 basicTest(new Uint32Array(100), 100, 4);
355 });
356 listTest((length) => new Uint32Array(length));
357 test('fromArray', () {
358 Uint32Array a = new Uint32Array(100);
359 a[0] = 12;
360 Uint32Array b = new Uint32Array.fromArray(a);
361 Expect.equals(a[0], b[0]);
362 });
363 test('fromList', () {
364 List<int> a = [12];
365 Uint32Array b = new Uint32Array.fromList(a);
366 Expect.equals(a[0], b[0]);
367 });
368 test('fromArrayBuffer 1 arg', () {
369 ArrayBuffer a = new ArrayBuffer(128);
370 Uint32Array b = new Uint32Array.fromArrayBuffer(a);
371 Expect.equals(b.buffer, a);
372 Expect.equals(128, b.length * b.BYTES_PER_ELEMENT);
373 });
374 test('fromArrayBuffer 2 args', () {
375 ArrayBuffer a = new ArrayBuffer(128);
376 Uint32Array b = new Uint32Array.fromArrayBuffer(a, 16);
377 Expect.equals(a, b.buffer);
378 Expect.equals(112, b.length * b.BYTES_PER_ELEMENT);
379 });
380 test('fromArrayBuffer 3 args', () {
381 ArrayBuffer a = new ArrayBuffer(128);
382 Uint32Array b = new Uint32Array.fromArrayBuffer(a, 16, 3);
383 Expect.equals(a, b.buffer);
384 Expect.equals(3, b.length);
385 });
386 });
387
388 group('Float32Array', () {
389 test('basic', () {
390 basicTest(new Float32Array(100), 100, 4);
391 });
392 listTest((length) => new Float32Array(length));
393 test('fromArray', () {
394 Float32Array a = new Float32Array(100);
395 a[0] = 0.5;
396 Float32Array b = new Float32Array.fromArray(a);
397 Expect.equals(a[0], b[0]);
398 });
399 test('fromList', () {
400 List<double> a = [0.5];
401 Float32Array b = new Float32Array.fromList(a);
402 Expect.equals(a[0], b[0]);
403 });
404 test('fromArrayBuffer 1 arg', () {
405 ArrayBuffer a = new ArrayBuffer(128);
406 Float32Array b = new Float32Array.fromArrayBuffer(a);
407 Expect.equals(b.buffer, a);
408 Expect.equals(128, b.length * b.BYTES_PER_ELEMENT);
409 });
410 test('fromArrayBuffer 2 args', () {
411 ArrayBuffer a = new ArrayBuffer(128);
412 Float32Array b = new Float32Array.fromArrayBuffer(a, 16);
413 Expect.equals(a, b.buffer);
414 Expect.equals(112, b.length * b.BYTES_PER_ELEMENT);
415 });
416 test('fromArrayBuffer 3 args', () {
417 ArrayBuffer a = new ArrayBuffer(128);
418 Float32Array b = new Float32Array.fromArrayBuffer(a, 16, 3);
419 Expect.equals(a, b.buffer);
420 Expect.equals(3, b.length);
421 });
422 });
423
424 group('Float64Array', () {
425 test('basic', () {
426 basicTest(new Float64Array(100), 100, 8);
427 });
428 listTest((length) => new Float64Array(length));
429 test('fromArray', () {
430 Float64Array a = new Float64Array(100);
431 a[0] = 0.5;
432 Float64Array b = new Float64Array.fromArray(a);
433 Expect.equals(a[0], b[0]);
434 });
435 test('fromList', () {
436 List<double> a = [0.5];
437 Float64Array b = new Float64Array.fromList(a);
438 Expect.equals(a[0], b[0]);
439 });
440 test('fromArrayBuffer 1 arg', () {
441 ArrayBuffer a = new ArrayBuffer(128);
442 Float64Array b = new Float64Array.fromArrayBuffer(a);
443 Expect.equals(b.buffer, a);
444 Expect.equals(128, b.length * b.BYTES_PER_ELEMENT);
445 });
446 test('fromArrayBuffer 2 args', () {
447 ArrayBuffer a = new ArrayBuffer(128);
448 Float64Array b = new Float64Array.fromArrayBuffer(a, 16);
449 Expect.equals(a, b.buffer);
450 Expect.equals(112, b.length * b.BYTES_PER_ELEMENT);
451 });
452 test('fromArrayBuffer 3 args', () {
453 ArrayBuffer a = new ArrayBuffer(128);
454 Float64Array b = new Float64Array.fromArrayBuffer(a, 16, 3);
455 Expect.equals(a, b.buffer);
456 Expect.equals(3, b.length);
457 });
458 });
459
460 test('DataView', () {
461 Int8Array bytes = new Int8Array.fromList([0, -1, 2, 3, 4, 5]);
462 DataView d = new DataView.fromArray(bytes.buffer);
463 Expect.equals(bytes.buffer, d.buffer);
464 Expect.equals(0, d.byteOffset);
465 Expect.equals(6, d.byteLength);
466
467 Expect.equals(-1, d.getInt8(1));
468
469 Expect.equals(0xff, d.getUint8(1));
470
471 Expect.equals(0x0203, d.getInt16(2));
472 Expect.equals(0x0203, d.getInt16(2, false));
473 Expect.equals(0x0302, d.getInt16(2, true));
474
475 Expect.equals(0x0203, d.getUint16(2));
476 Expect.equals(0x0203, d.getUint16(2, false));
477 Expect.equals(0x0302, d.getUint16(2, true));
478
479 Expect.equals(0x02030405, d.getInt32(2));
480 Expect.equals(0x02030405, d.getInt32(2, false));
481 Expect.equals(0x05040302, d.getInt32(2, true));
482
483 Expect.equals(0x02030405, d.getUint32(2));
484 Expect.equals(0x02030405, d.getUint32(2, false));
485 Expect.equals(0x05040302, d.getUint32(2, true));
486
487 Int8Array floatBytes = new Int8Array.fromList([0x3f, 0x80, 0x00, 0x00]);
488 d = new DataView.fromArray(floatBytes.buffer);
489 Expect.equals(1.0, d.getFloat32(0));
490
491 Int8Array doubleBytes = new Int8Array.fromList(
492 [0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
493 d = new DataView.fromArray(doubleBytes.buffer);
494 Expect.equals(1.0, d.getFloat64(0));
495
496 d = new DataView.fromArray(new ArrayBuffer(8));
497 d.setInt8(0, -5);
498 Expect.equals(-5, d.getInt8(0));
499 d.setUint8(0, -5);
500 Expect.equals(0xfb, d.getUint8(0));
501 d.setInt16(0, -5);
502 Expect.equals(-5, d.getInt16(0));
503 d.setUint16(0, -5);
504 Expect.equals(0xfffb, d.getUint16(0));
505 d.setInt32(0, -5);
506 Expect.equals(-5, d.getInt32(0));
507 d.setUint32(0, -5);
508 Expect.equals(0xfffffffb, d.getUint32(0));
509 d.setFloat32(0, 2.0);
510 Expect.equals(2.0, d.getFloat32(0));
511 d.setFloat64(0, 2.0);
512 Expect.equals(2.0, d.getFloat64(0));
513 });
514 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698