dataview-accessors.js 15 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
// Copyright 2013 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// These tests are adapted from Khronos DataView tests

var intArray1 =
  [0, 1, 2, 3, 100, 101, 102, 103, 128, 129, 130, 131, 252, 253, 254, 255];
var intArray2 =
  [31, 32, 33, 0, 1, 2, 3, 100, 101, 102, 103, 128, 129, 130, 131, 252,
    253, 254, 255];
var initialArray =
  [204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204, 204,
    204, 204, 204];

var arayBuffer = null;
var view = null;
var viewStart = 0;
var viewLength = 0;

function getElementSize(func) {
  switch (func) {
    case "Int8":
    case "Uint8":
      return 1;
    case "Int16":
    case "Uint16":
      return 2;
    case "Int32":
    case "Uint32":
    case "Float32":
      return 4;
    case "Float64":
      return 8;
    default:
      assertUnreachable(func);
  }
}

function checkGet(func, index, expected, littleEndian) {
  function doGet() {
65 66 67 68
    if (littleEndian != undefined)
      return view["get" + func](index, littleEndian);
    else
      return view["get" + func](index);
69
  }
70
  if (index >=0 && index + getElementSize(func) - 1 < view.byteLength)
71 72 73 74 75 76 77
      assertSame(expected, doGet());
  else
      assertThrows(doGet, RangeError);
}

function checkSet(func, index, value, littleEndian) {
  function doSet() {
78 79 80 81
    if (littleEndian != undefined)
      view["set" + func](index, value, littleEndian);
    else
      view["set" + func](index, value);
82
  }
83 84
  if (index >= 0 &&
      index + getElementSize(func) - 1 < view.byteLength) {
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    assertSame(undefined, doSet());
    checkGet(func, index, value, littleEndian);
  } else {
    assertThrows(doSet, RangeError);
  }
}

function test(isTestingGet, func, index, value, littleEndian) {
  if (isTestingGet)
    checkGet(func, index, value, littleEndian);
  else
    checkSet(func, index, value, littleEndian);
}

function createDataView(
    array, frontPaddingNum, littleEndian, start, length) {
  if (!littleEndian)
    array.reverse();
  var paddingArray = new Array(frontPaddingNum);
  arrayBuffer = (new Uint8Array(paddingArray.concat(array))).buffer;
  view = new DataView(arrayBuffer, viewStart, viewLength);
  if (!littleEndian)
    array.reverse(); // restore the array.
}

function runIntegerTestCases(isTestingGet, array, start, length) {
  createDataView(array, 0, true, start, length);

  test(isTestingGet, "Int8", 0, 0);
114
  test(isTestingGet, "Int8", undefined, 0);
115 116 117 118
  test(isTestingGet, "Int8", 8, -128);
  test(isTestingGet, "Int8", 15, -1);

  test(isTestingGet, "Uint8", 0, 0);
119
  test(isTestingGet, "Uint8", undefined, 0);
120 121 122 123 124
  test(isTestingGet, "Uint8", 8, 128);
  test(isTestingGet, "Uint8", 15, 255);

  // Little endian.
  test(isTestingGet, "Int16", 0, 256, true);
125
  test(isTestingGet, "Int16", undefined, 256, true);
126 127 128 129 130 131
  test(isTestingGet, "Int16", 5, 26213, true);
  test(isTestingGet, "Int16", 9, -32127, true);
  test(isTestingGet, "Int16", 14, -2, true);

  // Big endian.
  test(isTestingGet, "Int16", 0, 1);
132
  test(isTestingGet, "Int16", undefined, 1);
133 134 135 136 137 138
  test(isTestingGet, "Int16", 5, 25958);
  test(isTestingGet, "Int16", 9, -32382);
  test(isTestingGet, "Int16", 14, -257);

  // Little endian.
  test(isTestingGet, "Uint16", 0, 256, true);
139
  test(isTestingGet, "Uint16", undefined, 256, true);
140 141 142 143 144 145
  test(isTestingGet, "Uint16", 5, 26213, true);
  test(isTestingGet, "Uint16", 9, 33409, true);
  test(isTestingGet, "Uint16", 14, 65534, true);

  // Big endian.
  test(isTestingGet, "Uint16", 0, 1);
146
  test(isTestingGet, "Uint16", undefined, 1);
147 148 149 150 151 152
  test(isTestingGet, "Uint16", 5, 25958);
  test(isTestingGet, "Uint16", 9, 33154);
  test(isTestingGet, "Uint16", 14, 65279);

  // Little endian.
  test(isTestingGet, "Int32", 0, 50462976, true);
153
  test(isTestingGet, "Int32", undefined, 50462976, true);
154 155 156 157 158 159 160
  test(isTestingGet, "Int32", 3, 1717920771, true);
  test(isTestingGet, "Int32", 6, -2122291354, true);
  test(isTestingGet, "Int32", 9, -58490239, true);
  test(isTestingGet, "Int32", 12,-66052, true);

  // Big endian.
  test(isTestingGet, "Int32", 0, 66051);
161
  test(isTestingGet, "Int32", undefined, 66051);
162 163 164 165 166 167 168
  test(isTestingGet, "Int32", 3, 56911206);
  test(isTestingGet, "Int32", 6, 1718059137);
  test(isTestingGet, "Int32", 9, -2122152964);
  test(isTestingGet, "Int32", 12, -50462977);

  // Little endian.
  test(isTestingGet, "Uint32", 0, 50462976, true);
169
  test(isTestingGet, "Uint32", undefined, 50462976, true);
170 171 172 173 174 175 176
  test(isTestingGet, "Uint32", 3, 1717920771, true);
  test(isTestingGet, "Uint32", 6, 2172675942, true);
  test(isTestingGet, "Uint32", 9, 4236477057, true);
  test(isTestingGet, "Uint32", 12,4294901244, true);

  // Big endian.
  test(isTestingGet, "Uint32", 0, 66051);
177
  test(isTestingGet, "Uint32", undefined, 66051);
178 179 180 181 182 183 184 185 186 187
  test(isTestingGet, "Uint32", 3, 56911206);
  test(isTestingGet, "Uint32", 6, 1718059137);
  test(isTestingGet, "Uint32", 9, 2172814332);
  test(isTestingGet, "Uint32", 12, 4244504319);
}

function testFloat(isTestingGet, func, array, start, expected) {
  // Little endian.
  createDataView(array, 0, true, start);
  test(isTestingGet, func, 0, expected, true);
188
  test(isTestingGet, func, undefined, expected, true);
189 190 191 192 193 194 195 196 197 198
  createDataView(array, 3, true, start);
  test(isTestingGet, func, 3, expected, true);
  createDataView(array, 7, true, start);
  test(isTestingGet, func, 7, expected, true);
  createDataView(array, 10, true, start);
  test(isTestingGet, func, 10, expected, true);

  // Big endian.
  createDataView(array, 0, false);
  test(isTestingGet, func, 0, expected, false);
199
  test(isTestingGet, func, undefined, expected, false);
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
  createDataView(array, 3, false);
  test(isTestingGet, func, 3, expected, false);
  createDataView(array, 7, false);
  test(isTestingGet, func, 7, expected, false);
  createDataView(array, 10, false);
  test(isTestingGet, func, 10, expected, false);
}

function runFloatTestCases(isTestingGet, start) {
  testFloat(isTestingGet, "Float32",
    isTestingGet ? [0, 0, 32, 65] : initialArray, start, 10);

  testFloat(isTestingGet, "Float32",
    isTestingGet ? [164, 112, 157, 63] : initialArray,
      start, 1.2300000190734863);
  testFloat(isTestingGet, "Float32",
    isTestingGet ? [95, 53, 50, 199] : initialArray,
    start, -45621.37109375);
  testFloat(isTestingGet, "Float32",
    isTestingGet ? [255, 255, 255, 127] : initialArray,
    start, NaN);
  testFloat(isTestingGet, "Float32",
    isTestingGet ? [255, 255, 255, 255] : initialArray,
    start, -NaN);

  testFloat(isTestingGet, "Float64",
    isTestingGet ? [0, 0, 0, 0, 0, 0, 36, 64] : initialArray,
    start, 10);
  testFloat(isTestingGet, "Float64",
    isTestingGet ? [174, 71, 225, 122, 20, 174, 243, 63] : initialArray,
    start, 1.23);
  testFloat(isTestingGet, "Float64",
    isTestingGet ? [181, 55, 248, 30, 242, 179, 87, 193] : initialArray,
    start, -6213576.4839);
  testFloat(isTestingGet, "Float64",
    isTestingGet ? [255, 255, 255, 255, 255, 255, 255, 127] : initialArray,
    start, NaN);
  testFloat(isTestingGet, "Float64",
    isTestingGet ? [255, 255, 255, 255, 255, 255, 255, 255] : initialArray,
    start, -NaN);
}

function runNegativeIndexTests(isTestingGet) {
  createDataView(intArray1, 0, true, 0, 16);

  test(isTestingGet, "Int8", -1, 0);
  test(isTestingGet, "Int8", -2, 0);

  test(isTestingGet, "Uint8", -1, 0);
  test(isTestingGet, "Uint8", -2, 0);

  test(isTestingGet, "Int16", -1, 1);
  test(isTestingGet, "Int16", -2, 1);
  test(isTestingGet, "Int16", -3, 1);

  test(isTestingGet, "Uint16", -1, 1);
  test(isTestingGet, "Uint16", -2, 1);
  test(isTestingGet, "Uint16", -3, 1);

  test(isTestingGet, "Int32", -1, 66051);
  test(isTestingGet, "Int32", -3, 66051);
  test(isTestingGet, "Int32", -5, 66051);

  test(isTestingGet, "Uint32", -1, 66051);
  test(isTestingGet, "Uint32", -3, 66051);
  test(isTestingGet, "Uint32", -5, 66051);

  createDataView([0, 0, 0, 0, 0, 0, 0, 0], 0, true, 0, 8);

  test(isTestingGet, "Float32", -1, 0);
  test(isTestingGet, "Float32", -3, 0);
  test(isTestingGet, "Float32", -5, 0);

  test(isTestingGet, "Float64", -1, 0);
  test(isTestingGet, "Float64", -5, 0);
  test(isTestingGet, "Float64", -9, 0);
}


function TestGetters() {
  runIntegerTestCases(true, intArray1, 0, 16);
  runFloatTestCases(true, 0);

  runIntegerTestCases(true, intArray2, 3, 2);
  runFloatTestCases(true, 3);

  runNegativeIndexTests(true);
}

function TestSetters() {
  runIntegerTestCases(false, initialArray, 0, 16);
  runFloatTestCases(false);

  runIntegerTestCases(false, initialArray, 3, 2);
  runFloatTestCases(false, 7);

  runNegativeIndexTests(false);
}

TestGetters();
TestSetters();

302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
function CheckOutOfRangeInt8(value, expected) {
  var view = new DataView(new ArrayBuffer(100));
  assertSame(undefined, view.setInt8(0, value));
  assertSame(expected, view.getInt8(0));
  assertSame(undefined, view.setInt8(0, value, true));
  assertSame(expected, view.getInt8(0, true));
}

function CheckOutOfRangeUint8(value, expected) {
  var view = new DataView(new ArrayBuffer(100));
  assertSame(undefined, view.setUint8(0, value));
  assertSame(expected, view.getUint8(0));
  assertSame(undefined, view.setUint8(0, value, true));
  assertSame(expected, view.getUint8(0, true));
}

function CheckOutOfRangeInt16(value, expected) {
  var view = new DataView(new ArrayBuffer(100));
  assertSame(undefined, view.setInt16(0, value));
  assertSame(expected, view.getInt16(0));
  assertSame(undefined, view.setInt16(0, value, true));
  assertSame(expected, view.getInt16(0, true));
}

function CheckOutOfRangeUint16(value, expected) {
  var view = new DataView(new ArrayBuffer(100));
  assertSame(undefined, view.setUint16(0, value));
  assertSame(expected, view.getUint16(0));
  assertSame(undefined, view.setUint16(0, value, true));
  assertSame(expected, view.getUint16(0, true));
}

function CheckOutOfRangeInt32(value, expected) {
  var view = new DataView(new ArrayBuffer(100));
  assertSame(undefined, view.setInt32(0, value));
  assertSame(expected, view.getInt32(0));
  assertSame(undefined, view.setInt32(0, value, true));
  assertSame(expected, view.getInt32(0, true));
}

function CheckOutOfRangeUint32(value, expected) {
  var view = new DataView(new ArrayBuffer(100));
  assertSame(undefined, view.setUint32(0, value));
  assertSame(expected, view.getUint32(0));
  assertSame(undefined, view.setUint32(0, value, true));
  assertSame(expected, view.getUint32(0, true));
}

function TestOutOfRange() {
  CheckOutOfRangeInt8(0x80,   -0x80);
  CheckOutOfRangeInt8(0x1000, 0);
  CheckOutOfRangeInt8(-0x81,  0x7F);

  CheckOutOfRangeUint8(0x100,  0);
  CheckOutOfRangeUint8(0x1000, 0);
  CheckOutOfRangeUint8(-0x80,  0x80);
  CheckOutOfRangeUint8(-1,     0xFF);
  CheckOutOfRangeUint8(-0xFF,  1);

  CheckOutOfRangeInt16(0x8000,  -0x8000);
  CheckOutOfRangeInt16(0x10000, 0);
  CheckOutOfRangeInt16(-0x8001, 0x7FFF);

  CheckOutOfRangeUint16(0x10000,  0);
  CheckOutOfRangeUint16(0x100000, 0);
  CheckOutOfRangeUint16(-0x8000,  0x8000);
  CheckOutOfRangeUint16(-1,       0xFFFF);
  CheckOutOfRangeUint16(-0xFFFF,  1);

  CheckOutOfRangeInt32(0x80000000,  -0x80000000);
  CheckOutOfRangeInt32(0x100000000, 0);
  CheckOutOfRangeInt32(-0x80000001, 0x7FFFFFFF);

  CheckOutOfRangeUint32(0x100000000,  0);
  CheckOutOfRangeUint32(0x1000000000, 0);
  CheckOutOfRangeUint32(-0x80000000,  0x80000000);
  CheckOutOfRangeUint32(-1,           0xFFFFFFFF);
  CheckOutOfRangeUint32(-0xFFFFFFFF,  1);
}

TestOutOfRange();

384 385 386 387
function TestGeneralAccessors() {
  var a = new DataView(new ArrayBuffer(256));
  function CheckAccessor(name) {
    var f = a[name];
388
    assertThrows(function() { f(); }, TypeError);
389 390
    f.call(a, 0, 0); // should not throw
    assertThrows(function() { f.call({}, 0, 0); }, TypeError);
391
    assertThrows(function() { f.call(a); }, TypeError);
392 393 394 395 396
    if (name.indexOf("set") == 0) {
      assertThrows(function() { f.call(a, 1); }, TypeError);
    } else {
      f.call(a, 1); // should not throw
    }
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
  }
  CheckAccessor("getUint8");
  CheckAccessor("setUint8");
  CheckAccessor("getInt8");
  CheckAccessor("setInt8");
  CheckAccessor("getUint16");
  CheckAccessor("setUint16");
  CheckAccessor("getInt16");
  CheckAccessor("setInt16");
  CheckAccessor("getUint32");
  CheckAccessor("setUint32");
  CheckAccessor("getInt32");
  CheckAccessor("setInt32");
  CheckAccessor("getFloat32");
  CheckAccessor("setFloat32");
  CheckAccessor("getFloat64");
  CheckAccessor("setFloat64");
}

TestGeneralAccessors();
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449

function TestInsufficientArguments() {
  var a = new DataView(new ArrayBuffer(256));

  assertThrows(function() { a.getUint8(); }, TypeError);
  assertThrows(function() { a.getInt8(); }, TypeError);
  assertThrows(function() { a.getUint16(); }, TypeError);
  assertThrows(function() { a.getInt16(); }, TypeError);
  assertThrows(function() { a.getUint32(); }, TypeError);
  assertThrows(function() { a.getInt32(); }, TypeError);
  assertThrows(function() { a.getFloat32(); }, TypeError);
  assertThrows(function() { a.getFloat64(); }, TypeError);

  assertThrows(function() { a.setUint8(); }, TypeError);
  assertThrows(function() { a.setInt8(); }, TypeError);
  assertThrows(function() { a.setUint16(); }, TypeError);
  assertThrows(function() { a.setInt16(); }, TypeError);
  assertThrows(function() { a.setUint32(); }, TypeError);
  assertThrows(function() { a.setInt32(); }, TypeError);
  assertThrows(function() { a.setFloat32(); }, TypeError);
  assertThrows(function() { a.setFloat64(); }, TypeError);

  assertThrows(function() { a.setUint8(1) }, TypeError);
  assertThrows(function() { a.setInt8(1) }, TypeError);
  assertThrows(function() { a.setUint16(1) }, TypeError);
  assertThrows(function() { a.setInt16(1) }, TypeError);
  assertThrows(function() { a.setUint32(1) }, TypeError);
  assertThrows(function() { a.setInt32(1) }, TypeError);
  assertThrows(function() { a.setFloat32(1) }, TypeError);
  assertThrows(function() { a.setFloat64(1) }, TypeError);
}

TestInsufficientArguments();