nashorn/test/script/basic/NASHORN-377.js
author hannesw
Fri, 03 May 2013 22:48:53 +0200
changeset 17521 0522325da5ef
parent 16151 97c1e756ae1e
child 17763 f30b7aa0e736
permissions -rw-r--r--
8013873: Regexp regression for escaped dash in character class Reviewed-by: jlaskey
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     1
/*
16151
97c1e756ae1e 8005663: Update copyright year to 2013
jlaskey
parents: 16147
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     4
 * 
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     7
 * published by the Free Software Foundation.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     8
 * 
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    13
 * accompanied this code).
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    14
 * 
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    18
 * 
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    21
 * questions.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    22
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    23
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    24
/*
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    25
 * NASHORN-377: Typed arrays.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    26
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    27
 * @test
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    28
 * @run
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    29
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    30
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    31
var types = [Int8Array,Uint8Array,Uint8ClampedArray,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    32
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    33
//---------------------------------------------------------------------------
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    34
// utility functions
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    35
//---------------------------------------------------------------------------
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    36
function tohex(d, w) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    37
  var hex = Number(d).toString(16);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    38
  var pad = (w ? w : 8) - hex.length;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    39
  hex = "00000000".substr(0, pad) + hex;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    40
  return hex;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    41
}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    42
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    43
function arrstr(a, n, w) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    44
  var s = "";
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    45
  if (typeof n == "undefined") n = a.length;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    46
  if (typeof w == "undefined") w = a.BYTES_PER_ELEMENT * 2;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    47
  for (var i = 0; i < n; i++) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    48
    s += tohex(a[i], w);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    49
  }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    50
  return s;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    51
}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    52
function bufstr(b) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    53
  if (b.buffer !== undefined) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    54
    b = b.buffer;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    55
  }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    56
  return arrstr(new Uint8Array(b));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    57
}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    58
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    59
function assertFail(f) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    60
  try {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    61
    f();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    62
  } catch (e) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    63
    //print(e);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    64
    return;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    65
  }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    66
  throw "assertion failed: expected exception";
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    67
}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    68
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    69
function assertTrue(f) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    70
  if (f() !== true) throw "assertion failed: " + f;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    71
}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    72
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    73
function isUndefined(x) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    74
  return typeof x === "undefined";
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    75
}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    76
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    77
function fillArray(a, start) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    78
  if (typeof start == "undefined") start = 1;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    79
  for (var i = 0; i < a.length; i++) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    80
    a[i] = i + start;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    81
  }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    82
  return a;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    83
}
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    84
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    85
//---------------------------------------------------------------------------
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    86
// tests
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    87
//---------------------------------------------------------------------------
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    88
(function() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    89
  var b = new ArrayBuffer(8);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    90
  var i8 = new Int8Array(b);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    91
  print(i8.buffer.byteLength, b.byteLength, i8.buffer === b, b.length);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    92
  print(b, i8.buffer, i8);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    93
})();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    94
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    95
(function test_attributes() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    96
  var b = new ArrayBuffer(8);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    97
  for (var i in types) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    98
    var x = new types[i](b);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    99
    print(x.byteOffset, x.byteLength, x.length, x.BYTES_PER_ELEMENT);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   100
    assertTrue(function(){ return x.constructor === types[i] });
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   101
  }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   102
})();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   103
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   104
(function() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   105
  var b = new ArrayBuffer(8);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   106
  var i8 = new Int8Array(b);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   107
  fillArray(i8, 0x70);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   108
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   109
  var i8_2 = new Int8Array(b, 2);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   110
  var i8_2_4 = new Uint8Array(b, 2, 4);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   111
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   112
  i8_2_4[3] = 0x80;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   113
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   114
  print(arrstr(i8, 8, 2)  + " " + bufstr(i8));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   115
  print(arrstr(i8_2, 6)   + " " + i8_2.byteOffset   + " " + i8_2.byteLength);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   116
  print(arrstr(i8_2_4, 4) + " " + i8_2_4.byteOffset + " " + i8_2_4.byteLength);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   117
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   118
  var i8_1_5 = i8.subarray(1, 5);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   119
  i8_2_4.subarray(1, 5);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   120
  print(arrstr(i8_1_5, 4) + " " + i8_1_5.byteOffset + " " + i8_1_5.byteLength);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   121
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   122
  print(bufstr(b.slice(1,7)));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   123
})();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   124
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   125
(function() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   126
  var b = new ArrayBuffer(8);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   127
  fillArray(new Int8Array(b), 0x70);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   128
  new Int8Array(b)[5] = 0x80;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   129
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   130
  var i32 = new Int32Array(b);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   131
  var u32 = new Uint32Array(b);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   132
  print(arrstr(i32), i32[0], i32[1]);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   133
  i32[1] = 0xfefdfcfb;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   134
  print(arrstr(i32), i32[0], i32[1]);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   135
  print(arrstr(u32), u32[0], u32[1]);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   136
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   137
  var pi = 3.1415926;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   138
  var f32 = new Float32Array(b);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   139
  var f64 = new Float64Array(b);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   140
  f32[0] = pi;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   141
  print(bufstr(b), f32.length);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   142
  f64[0] = pi;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   143
  print(bufstr(b), f64.length);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   144
  print(arrstr(u32), u32[0], u32[1]);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   145
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   146
  var d = new Int32Array(3);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   147
  d.set(i32,1);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   148
  print(bufstr(d));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   149
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   150
  var s = new Int16Array(b);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   151
  var t = new Uint16Array(b);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   152
  print(arrstr(s), arrstr(t));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   153
  s[0] = -1; s[1] = 0x80;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   154
  print(arrstr(s), arrstr(t));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   155
})();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   156
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   157
(function enumerate_properties() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   158
  var i8 = new Int8Array(new ArrayBuffer(8));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   159
  var s = ""; for (var i in i8) { s += i + " "; } print(s.trim());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   160
})();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   161
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   162
// check that ScriptObject fallback is still working
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   163
// DISABLED because correct behavior is unclear
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   164
(function() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   165
  // NB: firefox will never set any out-of-bounds or non-array values although it does get both from prototype.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   166
  var z = new Uint8Array(4);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   167
  z["asdf"] = "asdf"; print(z["asdf"]);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   168
  z[0x100000000] = "asdf"; print(z[0x100000000]);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   169
  z[-1] = "asdf"; print(z[-1]);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   170
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   171
  // v8 and nashorn disagree on out-of-bounds uint32 indices: v8 won't go to the prototype.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   172
  z[0xf0000000] = "asdf"; print(z[0xf0000000]);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   173
  z[0xffffffff] = "asdf"; print(z[0xffffffff]);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   174
  z[0x70000000] = "asdf"; print(z[0x70000000]);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   175
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   176
  // this will work in firefox and nashorn (not in v8).
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   177
  Uint8Array.prototype[4] = "asdf"; print(z[4]);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   178
});
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   179
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   180
(function test_exceptions() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   181
  assertFail(function() { new Int32Array(new ArrayBuffer(7)); });
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   182
  assertFail(function() { new Int32Array(new ArrayBuffer(8), 0, 4); });
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   183
  assertFail(function() { new Int32Array(new ArrayBuffer(8),-1, 2); });
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   184
  assertFail(function() { new Int32Array(new ArrayBuffer(8), 0,-1); });
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   185
})();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   186
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   187
(function test_subarray() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   188
  var x = fillArray(new Int8Array(8));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   189
  print(arrstr(x));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   190
  print("subarray(2,4)=" + arrstr(x.subarray(2, 4)), "subarray(-6,-4)=" + arrstr(x.subarray(-6, -4))); // negative index refers from the end of the array
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   191
  print(arrstr(x.subarray(-10, -2))); // negative index clamped to 0
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   192
  assertTrue(function(){ return arrstr(x.subarray(6, 4)) === ""; }); // negative length clamped to 0
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   193
  print(arrstr(x.subarray(1,-1).subarray(1,-1)), arrstr(x.subarray(1,-1).subarray(1,-1).subarray(1,-1))); // subarray of subarray
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   194
})();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   195
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   196
(function test_slice() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   197
  var b = ArrayBuffer(16);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   198
  fillArray(new Int8Array(b));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   199
  print(bufstr(b));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   200
  print("slice(4,8)=" + bufstr(b.slice(4, 8)), "slice(-8,-4)=" + bufstr(b.slice(-8, -4))); // negative index refers from the end of the array
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   201
  print(bufstr(b.slice(-20, -4))); // negative index clamped to 0
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   202
  assertTrue(function(){ return bufstr(b.slice(8, 4)) === ""; }); // negative length clamped to 0
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   203
  print(arrstr(new Int16Array(b.slice(1,-1).slice(2,-1).slice(1,-2).slice(1,-1)))); // slice of slice
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   204
})();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   205
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   206
(function test_clamped() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   207
  var a = new Uint8ClampedArray(10);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   208
  a[0] = -17;       // clamped to 0
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   209
  a[1] = 4711;      // clamped to 255
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   210
  a[2] = 17.5;      // clamped to 18
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   211
  a[3] = 16.5;      // clamped to 16
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   212
  a[4] = 255.9;     // clamped to 255
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   213
  a[5] = Infinity;  // clamped to 255
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   214
  a[6] = -Infinity; // clamped to 0
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   215
  a[7] = NaN;       // 0
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   216
  assertTrue(function(){ return a[0] === 0 && a[1] === 255 && a[2] === 18 && a[3] === 16 && a[4] === 255 && a[5] === 255 && a[6] === 0 && a[7] === 0; });
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   217
})();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   218
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   219
(function test_out_of_bounds() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   220
  var a = new Int32Array(10);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   221
  a[10] = 10;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   222
  a[100] = 100;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   223
  a[1000] = 1000;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   224
  assertTrue(function(){ return isUndefined(a[10]) && isUndefined(a[11]) && isUndefined(a[100]) && isUndefined(a[123]) && isUndefined(a[1000]); });
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   225
})();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   226