Add tests for DataView.prototype.getUint16

This commit is contained in:
Leonardo Balter 2016-05-11 17:55:05 -04:00 committed by Mike Pennisi
parent 673b63e83d
commit 5e767dc2bb
15 changed files with 715 additions and 0 deletions

View File

@ -0,0 +1,54 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-dataview.prototype.getuint16
es6id: 24.2.4.11
description: >
Throws a RangeError if numberIndex getIndex
info: |
24.2.4.11 DataView.prototype.getUint16 ( byteOffset [ , littleEndian ] )
1. Let v be the this value.
2. If littleEndian is not present, let littleEndian be false.
3. Return ? GetViewValue(v, byteOffset, littleEndian, "Uint16").
24.2.1.1 GetViewValue ( view, requestIndex, isLittleEndian, type )
...
4. Let numberIndex be ? ToNumber(requestIndex).
5. Let getIndex be ToInteger(numberIndex).
6. If numberIndex getIndex or getIndex < 0, throw a RangeError exception.
...
---*/
var buffer = new ArrayBuffer(12);
var sample = new DataView(buffer, 0);
assert.throws(RangeError, function() {
sample.getUint16();
}, "no args");
assert.throws(RangeError, function() {
sample.getUint16(undefined);
}, "undefined");
assert.throws(RangeError, function() {
sample.getUint16(1.1);
}, "floating number");
assert.throws(RangeError, function() {
sample.getUint16(0.1);
}, "0.1");
assert.throws(RangeError, function() {
sample.getUint16(NaN);
}, "NaN");
assert.throws(RangeError, function() {
sample.getUint16(-0.1);
}, "-0.1");
assert.throws(RangeError, function() {
sample.getUint16(-1.1);
}, "-1.1");

View File

@ -0,0 +1,34 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-dataview.prototype.getuint16
es6id: 24.2.4.11
description: >
Throws a RangeError if getIndex < 0
info: |
24.2.4.11 DataView.prototype.getUint16 ( byteOffset [ , littleEndian ] )
1. Let v be the this value.
2. If littleEndian is not present, let littleEndian be false.
3. Return ? GetViewValue(v, byteOffset, littleEndian, "Uint16").
24.2.1.1 GetViewValue ( view, requestIndex, isLittleEndian, type )
...
4. Let numberIndex be ? ToNumber(requestIndex).
5. Let getIndex be ToInteger(numberIndex).
6. If numberIndex getIndex or getIndex < 0, throw a RangeError exception.
...
---*/
var buffer = new ArrayBuffer(12);
var sample = new DataView(buffer, 0);
assert.throws(RangeError, function() {
sample.getUint16(-1);
}, "-1");
assert.throws(RangeError, function() {
sample.getUint16(-Infinity);
}, "-Infinity");

View File

@ -0,0 +1,38 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-dataview.prototype.getuint16
es6id: 24.2.4.11
description: >
Detached buffer is checked after checking If numberIndex getIndex or
getIndex < 0,
info: |
24.2.4.11 DataView.prototype.getUint16 ( byteOffset [ , littleEndian ] )
1. Let v be the this value.
2. If littleEndian is not present, let littleEndian be false.
3. Return ? GetViewValue(v, byteOffset, littleEndian, "Uint16").
24.2.1.1 GetViewValue ( view, requestIndex, isLittleEndian, type )
...
6. If numberIndex getIndex or getIndex < 0, throw a RangeError exception.
...
8. Let buffer be the value of view's [[ViewedArrayBuffer]] internal slot.
9. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
...
includes: [detachArrayBuffer.js]
---*/
var buffer = new ArrayBuffer(8);
var sample = new DataView(buffer, 0);
$DETACHBUFFER(buffer);
assert.throws(RangeError, function() {
sample.getUint16(1.1);
});
assert.throws(RangeError, function() {
sample.getUint16(-1);
});

View File

@ -0,0 +1,40 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-dataview.prototype.getuint16
es6id: 24.2.4.11
description: >
Detached buffer is checked before out of range byteOffset's value
info: |
24.2.4.11 DataView.prototype.getUint16 ( byteOffset [ , littleEndian ] )
1. Let v be the this value.
2. If littleEndian is not present, let littleEndian be false.
3. Return ? GetViewValue(v, byteOffset, littleEndian, "Uint16").
24.2.1.1 GetViewValue ( view, requestIndex, isLittleEndian, type )
...
8. Let buffer be the value of view's [[ViewedArrayBuffer]] internal slot.
9. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
...
13. If getIndex + elementSize > viewSize, throw a RangeError exception.
...
includes: [detachArrayBuffer.js]
---*/
var sample;
var buffer = new ArrayBuffer(12);
sample = new DataView(buffer, 0);
$DETACHBUFFER(buffer);
assert.throws(TypeError, function() {
sample.getUint16(Infinity);
}, "Infinity");
assert.throws(TypeError, function() {
sample.getUint16(13);
}, "13");

View File

@ -0,0 +1,31 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-dataview.prototype.getuint16
es6id: 24.2.4.11
description: >
Throws a TypeError if buffer is detached
info: |
24.2.4.11 DataView.prototype.getUint16 ( byteOffset [ , littleEndian ] )
1. Let v be the this value.
2. If littleEndian is not present, let littleEndian be false.
3. Return ? GetViewValue(v, byteOffset, littleEndian, "Uint16").
24.2.1.1 GetViewValue ( view, requestIndex, isLittleEndian, type )
...
8. Let buffer be the value of view's [[ViewedArrayBuffer]] internal slot.
9. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
...
includes: [detachArrayBuffer.js]
---*/
var buffer = new ArrayBuffer(1);
var sample = new DataView(buffer, 0);
$DETACHBUFFER(buffer);
assert.throws(TypeError, function() {
sample.getUint16(0);
});

View File

@ -0,0 +1,76 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-dataview.prototype.getuint16
es6id: 24.2.4.11
description: >
Throws a RangeError if getIndex + elementSize > viewSize
info: |
24.2.4.11 DataView.prototype.getUint16 ( byteOffset [ , littleEndian ] )
1. Let v be the this value.
2. If littleEndian is not present, let littleEndian be false.
3. Return ? GetViewValue(v, byteOffset, littleEndian, "Uint16").
24.2.1.1 GetViewValue ( view, requestIndex, isLittleEndian, type )
...
10. Let viewOffset be the value of view's [[ByteOffset]] internal slot.
11. Let viewSize be the value of view's [[ByteLength]] internal slot.
12. Let elementSize be the Number value of the Element Size value specified in
Table 50 for Element Type type.
13. If getIndex + elementSize > viewSize, throw a RangeError exception.
...
---*/
var sample;
var buffer = new ArrayBuffer(12);
sample = new DataView(buffer, 0);
assert.throws(RangeError, function() {
sample.getUint16(Infinity);
}, "getIndex == Infinity");
assert.throws(RangeError, function() {
sample.getUint16(13);
}, "13 + 2 > 12");
assert.throws(RangeError, function() {
sample.getUint16(12);
}, "12 + 2 > 12");
assert.throws(RangeError, function() {
sample.getUint16(11);
}, "11 + 2 > 12");
sample = new DataView(buffer, 10);
assert.throws(RangeError, function() {
sample.getUint16(1);
}, "1 + 2 > 2 (offset)");
sample = new DataView(buffer, 11);
assert.throws(RangeError, function() {
sample.getUint16(0);
}, "0 + 2 > 1 (offset)");
sample = new DataView(buffer, 0, 2);
assert.throws(RangeError, function() {
sample.getUint16(1);
}, "1 + 2 > 2 (length)");
sample = new DataView(buffer, 0, 1);
assert.throws(RangeError, function() {
sample.getUint16(0);
}, "0 + 2 > 1 (length)");
sample = new DataView(buffer, 4, 2);
assert.throws(RangeError, function() {
sample.getUint16(1);
}, "1 + 2 > 2 (offset+length)");
sample = new DataView(buffer, 4, 1);
assert.throws(RangeError, function() {
sample.getUint16(0);
}, "0 + 2 > 1 (offset+length)");

View File

@ -0,0 +1,31 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-dataview.prototype.getuint16
es6id: 24.2.4.11
description: >
Return abrupt from ToNumber(symbol byteOffset)
info: |
24.2.4.11 DataView.prototype.getUint16 ( byteOffset [ , littleEndian ] )
1. Let v be the this value.
2. If littleEndian is not present, let littleEndian be false.
3. Return ? GetViewValue(v, byteOffset, littleEndian, "Uint16").
24.2.1.1 GetViewValue ( view, requestIndex, isLittleEndian, type )
...
4. Let numberIndex be ? ToNumber(requestIndex).
...
features: [Symbol]
---*/
var buffer = new ArrayBuffer(1);
var sample = new DataView(buffer, 0);
var s = Symbol("1");
assert.throws(TypeError, function() {
sample.getUint16(s);
});

View File

@ -0,0 +1,44 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-dataview.prototype.getuint16
es6id: 24.2.4.11
description: >
Return abrupt from ToNumber(byteOffset)
info: |
24.2.4.11 DataView.prototype.getUint16 ( byteOffset [ , littleEndian ] )
1. Let v be the this value.
2. If littleEndian is not present, let littleEndian be false.
3. Return ? GetViewValue(v, byteOffset, littleEndian, "Uint16").
24.2.1.1 GetViewValue ( view, requestIndex, isLittleEndian, type )
...
4. Let numberIndex be ? ToNumber(requestIndex).
...
---*/
var buffer = new ArrayBuffer(1);
var sample = new DataView(buffer, 0);
var bo1 = {
valueOf: function() {
throw new Test262Error();
}
};
var bo2 = {
toString: function() {
throw new Test262Error();
}
};
assert.throws(Test262Error, function() {
sample.getUint16(bo1);
}, "valueOf");
assert.throws(Test262Error, function() {
sample.getUint16(bo2);
}, "toString");

View File

@ -0,0 +1,43 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-dataview.prototype.getuint16
es6id: 24.2.4.11
description: >
Return value from Buffer using a clean ArrayBuffer
info: |
24.2.4.11 DataView.prototype.getUint16 ( byteOffset [ , littleEndian ] )
1. Let v be the this value.
2. If littleEndian is not present, let littleEndian be false.
3. Return ? GetViewValue(v, byteOffset, littleEndian, "Uint16").
24.2.1.1 GetViewValue ( view, requestIndex, isLittleEndian, type )
...
14. Let bufferIndex be getIndex + viewOffset.
15. Return GetValueFromBuffer(buffer, bufferIndex, type, isLittleEndian).
...
24.1.1.5 GetValueFromBuffer ( arrayBuffer, byteIndex, type [ , isLittleEndian
] )
...
8. If isLittleEndian is false, reverse the order of the elements of rawValue.
...
---*/
var buffer = new ArrayBuffer(6);
var sample = new DataView(buffer, 0);
assert.sameValue(sample.getUint16(0, true), 0, "sample.getUint16(0, true)");
assert.sameValue(sample.getUint16(1, true), 0, "sample.getUint16(1, true)");
assert.sameValue(sample.getUint16(2, true), 0, "sample.getUint16(2, true)");
assert.sameValue(sample.getUint16(3, true), 0, "sample.getUint16(3, true)");
assert.sameValue(sample.getUint16(4, true), 0, "sample.getUint16(4, true)");
assert.sameValue(sample.getUint16(0, false), 0, "sample.getUint16(0, false)");
assert.sameValue(sample.getUint16(1, false), 0, "sample.getUint16(1, false)");
assert.sameValue(sample.getUint16(2, false), 0, "sample.getUint16(2, false)");
assert.sameValue(sample.getUint16(3, false), 0, "sample.getUint16(3, false)");
assert.sameValue(sample.getUint16(4, false), 0, "sample.getUint16(4, false)");

View File

@ -0,0 +1,71 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-dataview.prototype.getuint16
es6id: 24.2.4.11
description: >
Return values using coerced ToInteger byteOffset values
info: |
24.2.4.11 DataView.prototype.getUint16 ( byteOffset [ , littleEndian ] )
1. Let v be the this value.
2. If littleEndian is not present, let littleEndian be false.
3. Return ? GetViewValue(v, byteOffset, littleEndian, "Uint16").
24.2.1.1 GetViewValue ( view, requestIndex, isLittleEndian, type )
...
14. Let bufferIndex be getIndex + viewOffset.
15. Return GetValueFromBuffer(buffer, bufferIndex, type, isLittleEndian).
...
24.1.1.5 GetValueFromBuffer ( arrayBuffer, byteIndex, type [ , isLittleEndian
] )
...
8. If isLittleEndian is false, reverse the order of the elements of rawValue.
...
features: [DataView.prototype.setUint8]
---*/
var buffer = new ArrayBuffer(3);
var sample = new DataView(buffer, 0);
sample.setUint8(0, 127);
sample.setUint8(1, 255);
sample.setUint8(2, 1);
assert.sameValue(sample.getUint16("", false), 32767, "'', false");
assert.sameValue(sample.getUint16("", true), 65407, "'', true");
assert.sameValue(sample.getUint16("0", false), 32767, "'0', false");
assert.sameValue(sample.getUint16("0", true), 65407, "'0', true");
assert.sameValue(sample.getUint16("1", false), 65281, "'1', false");
assert.sameValue(sample.getUint16("1", true), 511, "'1', true");
var obj1 = {
valueOf: function() {
return 1;
}
};
assert.sameValue(sample.getUint16(obj1, false), 65281, "{}.valueOf, false");
assert.sameValue(sample.getUint16(obj1, true), 511, "{}.valueOf, true");
var obj2 = {
toString: function() {
return 1;
}
};
assert.sameValue(sample.getUint16(obj2, false), 65281, "{}.toString, false");
assert.sameValue(sample.getUint16(obj2, true), 511, "{}.toString, true");
assert.sameValue(sample.getUint16(true, false), 65281, "true, false");
assert.sameValue(sample.getUint16(true, true), 511, "true, true");
assert.sameValue(sample.getUint16(false, false), 32767, "false, false");
assert.sameValue(sample.getUint16(false, true), 65407, "false, true");
assert.sameValue(sample.getUint16(null, false), 32767, "null, false");
assert.sameValue(sample.getUint16(null, true), 65407, "null, true");

View File

@ -0,0 +1,52 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-dataview.prototype.getuint16
es6id: 24.2.4.11
description: >
Return values from Buffer using a custom offset
info: |
24.2.4.11 DataView.prototype.getUint16 ( byteOffset [ , littleEndian ] )
1. Let v be the this value.
2. If littleEndian is not present, let littleEndian be false.
3. Return ? GetViewValue(v, byteOffset, littleEndian, "Uint16").
24.2.1.1 GetViewValue ( view, requestIndex, isLittleEndian, type )
...
14. Let bufferIndex be getIndex + viewOffset.
15. Return GetValueFromBuffer(buffer, bufferIndex, type, isLittleEndian).
...
24.1.1.5 GetValueFromBuffer ( arrayBuffer, byteIndex, type [ , isLittleEndian
] )
...
8. If isLittleEndian is false, reverse the order of the elements of rawValue.
...
features: [DataView.prototype.setUint8]
---*/
var buffer = new ArrayBuffer(8);
var sample = new DataView(buffer, 0);
sample.setUint8(0, 0);
sample.setUint8(1, 2);
sample.setUint8(2, 6);
sample.setUint8(3, 2);
sample.setUint8(4, 128);
sample.setUint8(5, 42);
sample.setUint8(6, 128);
sample.setUint8(7, 39);
sample = new DataView(buffer, 4);
assert.sameValue(sample.getUint16(0, false), 32810, "0, false");
assert.sameValue(sample.getUint16(1, false), 10880, "1, false");
assert.sameValue(sample.getUint16(2, false), 32807, "2, false");
assert.sameValue(sample.getUint16(0, true), 10880, "0, true");
assert.sameValue(sample.getUint16(1, true), 32810, "1, true");
assert.sameValue(sample.getUint16(2, true), 10112, "2, true");

View File

@ -0,0 +1,58 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-dataview.prototype.getuint16
es6id: 24.2.4.11
description: >
Return values from Buffer
info: |
24.2.4.11 DataView.prototype.getUint16 ( byteOffset [ , littleEndian ] )
1. Let v be the this value.
2. If littleEndian is not present, let littleEndian be false.
3. Return ? GetViewValue(v, byteOffset, littleEndian, "Uint16").
24.2.1.1 GetViewValue ( view, requestIndex, isLittleEndian, type )
...
14. Let bufferIndex be getIndex + viewOffset.
15. Return GetValueFromBuffer(buffer, bufferIndex, type, isLittleEndian).
...
24.1.1.5 GetValueFromBuffer ( arrayBuffer, byteIndex, type [ , isLittleEndian
] )
...
8. If isLittleEndian is false, reverse the order of the elements of rawValue.
...
features: [DataView.prototype.setUint8]
---*/
var buffer = new ArrayBuffer(8);
var sample = new DataView(buffer, 0);
sample.setUint8(0, 127);
sample.setUint8(1, 255);
sample.setUint8(2, 255);
sample.setUint8(3, 255);
sample.setUint8(4, 128);
sample.setUint8(5, 0);
sample.setUint8(6, 1);
sample.setUint8(7, 0);
assert.sameValue(sample.getUint16(0, false), 32767, "0, false");
assert.sameValue(sample.getUint16(1, false), 65535, "1, false");
assert.sameValue(sample.getUint16(2, false), 65535, "2, false");
assert.sameValue(sample.getUint16(3, false), 65408, "3, false");
assert.sameValue(sample.getUint16(4, false), 32768, "4, false");
assert.sameValue(sample.getUint16(5, false), 1, "5, false");
assert.sameValue(sample.getUint16(6, false), 256, "8, false");
assert.sameValue(sample.getUint16(0, true), 65407, "0, true");
assert.sameValue(sample.getUint16(1, true), 65535, "1, true");
assert.sameValue(sample.getUint16(2, true), 65535, "2, true");
assert.sameValue(sample.getUint16(3, true), 33023, "3, true");
assert.sameValue(sample.getUint16(4, true), 128, "4, true");
assert.sameValue(sample.getUint16(5, true), 256, "5, true");
assert.sameValue(sample.getUint16(6, true), 1, "6, true");

View File

@ -0,0 +1,43 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-dataview.prototype.getuint16
es6id: 24.2.4.11
description: >
Throws a TypeError if this does not have a [[DataView]] internal slot
info: |
24.2.4.11 DataView.prototype.getUint16 ( byteOffset [ , littleEndian ] )
1. Let v be the this value.
2. If littleEndian is not present, let littleEndian be false.
3. Return ? GetViewValue(v, byteOffset, littleEndian, "Uint16").
24.2.1.1 GetViewValue ( view, requestIndex, isLittleEndian, type )
1. If Type(view) is not Object, throw a TypeError exception.
2. If view does not have a [[DataView]] internal slot, throw a TypeError
exception.
...
features: [Int8Array]
---*/
var getUint16 = DataView.prototype.getUint16;
assert.throws(TypeError, function() {
getUint16.call({});
}, "{}");
assert.throws(TypeError, function() {
getUint16.call([]);
}, "[]");
var ab = new ArrayBuffer(1);
assert.throws(TypeError, function() {
getUint16.call(ab);
}, "ArrayBuffer");
var ta = new Int8Array();
assert.throws(TypeError, function() {
getUint16.call(ta);
}, "TypedArray");

View File

@ -0,0 +1,51 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-dataview.prototype.getuint16
es6id: 24.2.4.11
description: Throws a TypeError if this is not Object
info: |
24.2.4.11 DataView.prototype.getUint16 ( byteOffset [ , littleEndian ] )
1. Let v be the this value.
2. If littleEndian is not present, let littleEndian be false.
3. Return ? GetViewValue(v, byteOffset, littleEndian, "Uint16").
24.2.1.1 GetViewValue ( view, requestIndex, isLittleEndian, type )
1. If Type(view) is not Object, throw a TypeError exception.
...
features: [Symbol]
---*/
var getUint16 = DataView.prototype.getUint16;
assert.throws(TypeError, function() {
getUint16.call(undefined);
}, "undefined");
assert.throws(TypeError, function() {
getUint16.call(null);
}, "null");
assert.throws(TypeError, function() {
getUint16.call(1);
}, "1");
assert.throws(TypeError, function() {
getUint16.call("string");
}, "string");
assert.throws(TypeError, function() {
getUint16.call(true);
}, "true");
assert.throws(TypeError, function() {
getUint16.call(false);
}, "false");
var s = Symbol("1");
assert.throws(TypeError, function() {
getUint16.call(s);
}, "symbol");

View File

@ -0,0 +1,49 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-dataview.prototype.getuint16
es6id: 24.2.4.11
description: >
Boolean littleEndian argument coerced in ToBoolean
info: |
24.2.4.11 DataView.prototype.getUint16 ( byteOffset [ , littleEndian ] )
1. Let v be the this value.
2. If littleEndian is not present, let littleEndian be false.
3. Return ? GetViewValue(v, byteOffset, littleEndian, "Uint16").
24.2.1.1 GetViewValue ( view, requestIndex, isLittleEndian, type )
...
14. Let bufferIndex be getIndex + viewOffset.
15. Return GetValueFromBuffer(buffer, bufferIndex, type, isLittleEndian).
...
24.1.1.5 GetValueFromBuffer ( arrayBuffer, byteIndex, type [ , isLittleEndian
] )
...
8. If isLittleEndian is false, reverse the order of the elements of rawValue.
...
features: [DataView.prototype.setUint8, Symbol]
---*/
var buffer = new ArrayBuffer(2);
var sample = new DataView(buffer, 0);
sample.setUint8(0, 0);
sample.setUint8(1, 42);
// False
assert.sameValue(sample.getUint16(0), 42, "no arg");
assert.sameValue(sample.getUint16(0, undefined), 42, "undefined");
assert.sameValue(sample.getUint16(0, null), 42, "null");
assert.sameValue(sample.getUint16(0, 0), 42, "0");
assert.sameValue(sample.getUint16(0, ""), 42, "the empty string");
// True
assert.sameValue(sample.getUint16(0, {}), 10752, "{}");
assert.sameValue(sample.getUint16(0, Symbol("1")), 10752, "symbol");
assert.sameValue(sample.getUint16(0, 1), 10752, "1");
assert.sameValue(sample.getUint16(0, "string"), 10752, "string");