Home | History | Annotate | Download | only in es6
      1 // Copyright 2015 the V8 project authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 var typedArrayConstructors = [
      6   Uint8Array,
      7   Int8Array,
      8   Uint16Array,
      9   Int16Array,
     10   Uint32Array,
     11   Int32Array,
     12   Uint8ClampedArray,
     13   Float32Array,
     14   Float64Array
     15 ];
     16 
     17 function assertArrayLikeEquals(value, expected, type) {
     18   assertEquals(value.__proto__, type.prototype);
     19   // Don't test value.length because we mess with that;
     20   // instead in certain callsites we check that length
     21   // is set appropriately.
     22   for (var i = 0; i < expected.length; ++i) {
     23     // Use Object.is to differentiate between +-0
     24     assertSame(expected[i], value[i]);
     25   }
     26 }
     27 
     28 for (var constructor of typedArrayConstructors) {
     29   // Test default numerical sorting order
     30   var a = new constructor([100, 7, 45])
     31   assertEquals(a.sort(), a);
     32   assertArrayLikeEquals(a, [7, 45, 100], constructor);
     33   assertEquals(a.length, 3);
     34 
     35   // For arrays of floats, certain handling of +-0/NaN
     36   if (constructor === Float32Array || constructor === Float64Array) {
     37     var b = new constructor([+0, -0, NaN, -0, NaN, +0])
     38     b.sort();
     39     assertArrayLikeEquals(b, [-0, -0, +0, +0, NaN, NaN], constructor);
     40     assertEquals(b.length, 6);
     41   }
     42 
     43   // Custom sort--backwards
     44   a.sort(function(x, y) { return y - x; });
     45   assertArrayLikeEquals(a, [100, 45, 7], constructor);
     46 
     47   // Basic TypedArray method properties:
     48   // Length field is ignored
     49   Object.defineProperty(a, 'length', {value: 1});
     50   assertEquals(a.sort(), a);
     51   assertArrayLikeEquals(a, [7, 45, 100], constructor);
     52   assertEquals(a.length, 1);
     53   // Method doesn't work on other objects
     54   assertThrows(function() { a.sort.call([]); }, TypeError);
     55 }
     56