mirror of https://github.com/svaarala/duktape.git
Sami Vaarala
5 years ago
committed by
GitHub
26 changed files with 2094 additions and 21 deletions
@ -1,15 +0,0 @@ |
|||
/*--- |
|||
{ |
|||
"skip": true |
|||
} |
|||
---*/ |
|||
|
|||
/*=== |
|||
[1,2,3] |
|||
done |
|||
===*/ |
|||
|
|||
var t = CBOR.decode(new Uint8Array([ 0xdb, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x83, 0x01, 0x02, 0x03 ])); |
|||
print(JSON.stringify(t)); |
|||
|
|||
print('done'); |
@ -0,0 +1,19 @@ |
|||
/*--- |
|||
{ |
|||
"skip": true |
|||
} |
|||
---*/ |
|||
|
|||
/*=== |
|||
[1,2,3] |
|||
done |
|||
===*/ |
|||
|
|||
// While 64-bit integers might not be supported, a 64-bit tag must still be
|
|||
// at least ignored.
|
|||
var t = CBOR.decode(new Uint8Array([ |
|||
0xdb, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x83, 0x01, 0x02, 0x03 |
|||
])); |
|||
print(JSON.stringify(t)); |
|||
|
|||
print('done'); |
@ -0,0 +1,19 @@ |
|||
/*--- |
|||
{ |
|||
"skip": true |
|||
} |
|||
---*/ |
|||
|
|||
/*=== |
|||
TypeError |
|||
===*/ |
|||
|
|||
// Empty input is a decode error, TypeError used now. cbor-js triggers a
|
|||
// RangeError from reading out of bounds of a DataView.
|
|||
try { |
|||
CBOR.decode(new Uint8Array([]).buffer); |
|||
print('never here'); |
|||
} catch (e) { |
|||
print(e.name); |
|||
//print(e.stack || e);
|
|||
} |
@ -0,0 +1,188 @@ |
|||
/* |
|||
* Full coverage for decoding all half-floats. Compared against cbor-js. |
|||
*/ |
|||
|
|||
/*--- |
|||
{ |
|||
"skip": true |
|||
} |
|||
---*/ |
|||
|
|||
/*=== |
|||
f90000 0 |
|||
f90001 5.960464477539063e-8 |
|||
diff: 5.960464477539063e-8 |
|||
f90801 0.00012218952178955078 |
|||
diff: 1.1920928955078125e-7 |
|||
f90c01 0.00024437904357910156 |
|||
diff: 2.384185791015625e-7 |
|||
f91001 0.0004887580871582031 |
|||
diff: 4.76837158203125e-7 |
|||
f91401 0.0009775161743164063 |
|||
diff: 9.5367431640625e-7 |
|||
f91801 0.0019550323486328125 |
|||
diff: 0.0000019073486328125 |
|||
f91c01 0.003910064697265625 |
|||
diff: 0.000003814697265625 |
|||
f92001 0.00782012939453125 |
|||
diff: 0.00000762939453125 |
|||
f92401 0.0156402587890625 |
|||
diff: 0.0000152587890625 |
|||
f92801 0.031280517578125 |
|||
diff: 0.000030517578125 |
|||
f92c01 0.06256103515625 |
|||
diff: 0.00006103515625 |
|||
f93001 0.1251220703125 |
|||
diff: 0.0001220703125 |
|||
f93401 0.250244140625 |
|||
diff: 0.000244140625 |
|||
f93801 0.50048828125 |
|||
diff: 0.00048828125 |
|||
f93c01 1.0009765625 |
|||
diff: 0.0009765625 |
|||
f94001 2.001953125 |
|||
diff: 0.001953125 |
|||
f94401 4.00390625 |
|||
diff: 0.00390625 |
|||
f94801 8.0078125 |
|||
diff: 0.0078125 |
|||
f94c01 16.015625 |
|||
diff: 0.015625 |
|||
f95001 32.03125 |
|||
diff: 0.03125 |
|||
f95401 64.0625 |
|||
diff: 0.0625 |
|||
f95801 128.125 |
|||
diff: 0.125 |
|||
f95c01 256.25 |
|||
diff: 0.25 |
|||
f96001 512.5 |
|||
diff: 0.5 |
|||
f96401 1025 |
|||
diff: 1 |
|||
f96801 2050 |
|||
diff: 2 |
|||
f96c01 4100 |
|||
diff: 4 |
|||
f97001 8200 |
|||
diff: 8 |
|||
f97401 16400 |
|||
diff: 16 |
|||
f97801 32800 |
|||
diff: 32 |
|||
f97c00 Infinity |
|||
f97c01 NaN |
|||
f97c02 NaN |
|||
diff: 0 |
|||
f98000 -0 |
|||
f98001 -5.960464477539063e-8 |
|||
diff: -5.960464477539063e-8 |
|||
f98801 -0.00012218952178955078 |
|||
diff: -1.1920928955078125e-7 |
|||
f98c01 -0.00024437904357910156 |
|||
diff: -2.384185791015625e-7 |
|||
f99001 -0.0004887580871582031 |
|||
diff: -4.76837158203125e-7 |
|||
f99401 -0.0009775161743164063 |
|||
diff: -9.5367431640625e-7 |
|||
f99801 -0.0019550323486328125 |
|||
diff: -0.0000019073486328125 |
|||
f99c01 -0.003910064697265625 |
|||
diff: -0.000003814697265625 |
|||
f9a001 -0.00782012939453125 |
|||
diff: -0.00000762939453125 |
|||
f9a401 -0.0156402587890625 |
|||
diff: -0.0000152587890625 |
|||
f9a801 -0.031280517578125 |
|||
diff: -0.000030517578125 |
|||
f9ac01 -0.06256103515625 |
|||
diff: -0.00006103515625 |
|||
f9b001 -0.1251220703125 |
|||
diff: -0.0001220703125 |
|||
f9b401 -0.250244140625 |
|||
diff: -0.000244140625 |
|||
f9b801 -0.50048828125 |
|||
diff: -0.00048828125 |
|||
f9bc01 -1.0009765625 |
|||
diff: -0.0009765625 |
|||
f9c001 -2.001953125 |
|||
diff: -0.001953125 |
|||
f9c401 -4.00390625 |
|||
diff: -0.00390625 |
|||
f9c801 -8.0078125 |
|||
diff: -0.0078125 |
|||
f9cc01 -16.015625 |
|||
diff: -0.015625 |
|||
f9d001 -32.03125 |
|||
diff: -0.03125 |
|||
f9d401 -64.0625 |
|||
diff: -0.0625 |
|||
f9d801 -128.125 |
|||
diff: -0.125 |
|||
f9dc01 -256.25 |
|||
diff: -0.25 |
|||
f9e001 -512.5 |
|||
diff: -0.5 |
|||
f9e401 -1025 |
|||
diff: -1 |
|||
f9e801 -2050 |
|||
diff: -2 |
|||
f9ec01 -4100 |
|||
diff: -4 |
|||
f9f001 -8200 |
|||
diff: -8 |
|||
f9f401 -16400 |
|||
diff: -16 |
|||
f9f801 -32800 |
|||
diff: -32 |
|||
f9fc00 -Infinity |
|||
f9fc01 NaN |
|||
f9fc02 NaN |
|||
diff: 0 |
|||
done |
|||
===*/ |
|||
|
|||
function test() { |
|||
var b1, b2, prev = null, prevdiff = null; |
|||
|
|||
var res = []; |
|||
|
|||
for (b1 = 0; b1 < 256; b1++) { |
|||
for (b2 = 0; b2 < 256; b2++) { |
|||
var data = new Uint8Array([ 0xf9, b1, b2 ]).buffer; |
|||
var val = CBOR.decode(data); |
|||
var obj = { input: Duktape.enc('hex', data), value: val }; |
|||
if (prev !== null && Number.isNaN(prev) && Number.isNaN(val)) { |
|||
obj.diff = 0; |
|||
} else if (prev !== null && Number.isFinite(prev) && Number.isFinite(val)) { |
|||
obj.diff = val - prev; |
|||
} |
|||
prev = val; |
|||
res.push(obj); |
|||
} |
|||
} |
|||
|
|||
var diff_printed = false; |
|||
for (i = 0; i < res.length; i++) { |
|||
var prev = (i > 0 ? res[i - 1] : {}); |
|||
var curr = res[i]; |
|||
if (prev.diff === curr.diff && typeof curr.diff === 'number') { |
|||
if (diff_printed) { |
|||
} else { |
|||
print(' diff: ' + prev.diff); |
|||
diff_printed = true; |
|||
} |
|||
} else { |
|||
print(curr.input, Duktape.enc('jx', curr.value)); |
|||
diff_printed = false; |
|||
} |
|||
} |
|||
} |
|||
|
|||
try { |
|||
test(); |
|||
} catch (e) { |
|||
print(e.stack || e); |
|||
} |
|||
|
|||
print('done'); |
@ -0,0 +1,34 @@ |
|||
/* |
|||
* Decoding non-BMP, valid UTF-8 strings. |
|||
*/ |
|||
|
|||
/*--- |
|||
{ |
|||
"skip": true |
|||
} |
|||
---*/ |
|||
|
|||
/*=== |
|||
"foo" |
|||
"A\U0010fedcB" |
|||
done |
|||
===*/ |
|||
|
|||
function test(hexInput) { |
|||
try { |
|||
print(Duktape.enc('jx', CBOR.decode(Duktape.dec('hex', hexInput)))); |
|||
} catch (e) { |
|||
print(e.name); |
|||
} |
|||
} |
|||
|
|||
// Valid CBOR text string.
|
|||
test('63666f6f'); |
|||
|
|||
// Codepoint U+10FEDC. Input is valid UTF-8 which should decode to a
|
|||
// surrogate pair for ECMAScript compatibility.
|
|||
//
|
|||
// Currently decodes to Duktape specific non-BMP codepoint as is.
|
|||
test('6641f48fbb9c42'); |
|||
|
|||
print('done'); |
@ -0,0 +1,56 @@ |
|||
/* |
|||
* Test for current behavior when decoding non-UTF-8 CBOR text strings |
|||
* (technically invalid CBOR). |
|||
*/ |
|||
|
|||
/*--- |
|||
{ |
|||
"skip": true |
|||
} |
|||
---*/ |
|||
|
|||
/*=== |
|||
"foo" |
|||
"foo\ud800" |
|||
"foo\ud800bar" |
|||
"\x00" |
|||
TypeError |
|||
"\ud83d\udca9" |
|||
===*/ |
|||
|
|||
function test(hexInput) { |
|||
try { |
|||
print(Duktape.enc('jx', CBOR.decode(Duktape.dec('hex', hexInput)))); |
|||
} catch (e) { |
|||
print(e.name); |
|||
} |
|||
} |
|||
|
|||
// Valid CBOR text string.
|
|||
test('63666f6f'); |
|||
|
|||
// CESU-8 string, invalid UTF-8 and thus invalid CBOR: U-D800 in string.
|
|||
//
|
|||
// Current behavior: decode into an ECMAScript string as is.
|
|||
// Could also reject, or use U+FFFD.
|
|||
test('66666f6feda080'); |
|||
test('69666f6feda080626172'); |
|||
|
|||
// Invalid UTF-8 and CESU-8.
|
|||
//
|
|||
// Current behavior: decode as is, except if would map into a Duktape
|
|||
// Symbol which is rejected.
|
|||
test('62c080'); // "long form" U+0000
|
|||
test('6482112233'); |
|||
|
|||
// Input has valid surrogate pair (CESU-8), which is still invalid
|
|||
// UTF-8.
|
|||
//
|
|||
// Current behavior: accepted as is.
|
|||
/* |
|||
>>> u'\ud83d'.encode('utf-8') |
|||
'\xed\xa0\xbd' |
|||
>>> u'\udca9'.encode('utf-8') |
|||
'\xed\xb2\xa9' |
|||
*/ |
|||
test('66eda0bdedb2a9'); |
@ -0,0 +1,25 @@ |
|||
/* Previous versions of CBOR extra would encode Symbols (represented |
|||
* internally as strings) as is: |
|||
* |
|||
* duk> CBOR.encode(Symbol('foo')) |
|||
* = |6881666f6fff302d31| |
|||
* |
|||
* Check that decoded strings representing any kind of Symbol are |
|||
* rejected on decode, at least for now. |
|||
*/ |
|||
|
|||
/*--- |
|||
{ |
|||
"skip": true |
|||
} |
|||
---*/ |
|||
|
|||
/*=== |
|||
TypeError |
|||
===*/ |
|||
|
|||
try { |
|||
print(String(CBOR.decode(new Uint8Array([ 0x68, 0x81, 0x66, 0x6f, 0x6f, 0xff, 0x30, 0x2d, 0x31 ])))); |
|||
} catch (e) { |
|||
print(e.name); |
|||
} |
File diff suppressed because it is too large
@ -0,0 +1,49 @@ |
|||
/* |
|||
* Test for default string encoding behavior: valid UTF-8 strings are |
|||
* encoded as CBOR text strings, other strings as CBOR byte strings. |
|||
*/ |
|||
|
|||
/*--- |
|||
{ |
|||
"skip": true |
|||
} |
|||
---*/ |
|||
|
|||
/*=== |
|||
66666f6f626172 |
|||
"foobar" |
|||
69666f6fe188b4626172 |
|||
"foo\u1234bar" |
|||
49666f6feda080626172 |
|||
|666f6feda080626172| |
|||
49666f6fedb080626172 |
|||
|666f6fedb080626172| |
|||
4d666f6fedb08064383030626172 |
|||
|666f6fedb08064383030626172| |
|||
4c666f6feda0bdedb2a9626172 |
|||
|666f6feda0bdedb2a9626172| |
|||
done |
|||
===*/ |
|||
|
|||
function test(val) { |
|||
var t = CBOR.encode(val); |
|||
print(Duktape.enc('hex', t)); |
|||
print(Duktape.enc('jx', CBOR.decode(t))); |
|||
} |
|||
|
|||
// Plain ASCII.
|
|||
test('foobar'); |
|||
|
|||
// BMP.
|
|||
test('foo\u1234bar'); |
|||
|
|||
// Unpaired surrogate.
|
|||
test('foo\ud800bar'); |
|||
test('foo\udc00bar'); |
|||
test('foo\udc00d800bar'); |
|||
|
|||
// Paired surrogate. Currently CBOR.encode() does not combine the
|
|||
// surrogate.
|
|||
test('foo\ud83d\udca9bar'); |
|||
|
|||
print('done'); |
@ -0,0 +1,48 @@ |
|||
/*--- |
|||
{ |
|||
"skip": true |
|||
} |
|||
---*/ |
|||
|
|||
/*=== |
|||
ownKeys |
|||
get: foo |
|||
get: bar |
|||
a263666f6f63464f4f6362617263424152 |
|||
{"foo":"FOO","bar":"BAR"} |
|||
done |
|||
===*/ |
|||
|
|||
function test() { |
|||
var O = { foo: 1, bar: 2 }; // no 'quux' in target
|
|||
var P = new Proxy(O, { |
|||
ownKeys: function (targ) { |
|||
print('ownKeys'); |
|||
return [ 'foo', 'bar', 'quux' ]; |
|||
}, |
|||
get: function (targ, key, recv) { |
|||
print('get:', key); |
|||
if (key === 'foo') { |
|||
return 'FOO'; |
|||
} else if (key === 'bar') { |
|||
return 'BAR'; |
|||
} else if (key === 'quux') { |
|||
return 'QUUX'; |
|||
} else { |
|||
return void 0; |
|||
} |
|||
} |
|||
}); |
|||
|
|||
var res = CBOR.encode(P); |
|||
print(Duktape.enc('hex', res)); |
|||
print(JSON.stringify(CBOR.decode(res))); |
|||
} |
|||
|
|||
try { |
|||
test(); |
|||
} catch (e) { |
|||
print(e.stack || e); |
|||
} |
|||
|
|||
print('done'); |
@ -0,0 +1,19 @@ |
|||
/* |
|||
* Test for Symbol encoding behavior; for now encoded as an empty object. |
|||
*/ |
|||
|
|||
/*--- |
|||
{ |
|||
"skip": true |
|||
} |
|||
---*/ |
|||
|
|||
/*=== |
|||
a0 |
|||
===*/ |
|||
|
|||
try { |
|||
print(Duktape.enc('hex', CBOR.encode(Symbol('foo')))); |
|||
} catch (e) { |
|||
print(e.stack || e); |
|||
} |
@ -0,0 +1,211 @@ |
|||
/* |
|||
* Encode test for all major ECMAScript types. |
|||
* |
|||
* Encoding is not unique, so this demonstrates current output. |
|||
*/ |
|||
|
|||
/*--- |
|||
{ |
|||
"skip": true |
|||
} |
|||
---*/ |
|||
|
|||
/*=== |
|||
done |
|||
===*/ |
|||
|
|||
function test(val, expect) { |
|||
var res = Duktape.enc('hex', CBOR.encode(val)); |
|||
// print(res);
|
|||
if (res !== expect) { |
|||
print('MISMATCH, got ' + res + ', expected ' + expect); |
|||
} |
|||
} |
|||
|
|||
// Primitive types.
|
|||
test(void 0, 'f7'); |
|||
test(null, 'f6'); |
|||
test(true, 'f5'); |
|||
test(false, 'f4'); |
|||
test(123, '187b'); |
|||
/* |
|||
>>> cbor.dumps(123.1).encode('hex') |
|||
'fb405ec66666666666' |
|||
*/ |
|||
test(123.1, 'fb405ec66666666666'); |
|||
/* |
|||
>>> cbor.dumps(u'foo\u07fe\ucafebar').encode('hex') |
|||
'6b666f6fdfbeecabbe626172' |
|||
*/ |
|||
test('foo\u07fe\ucafebar', '6b666f6fdfbeecabbe626172'); |
|||
|
|||
// Object types.
|
|||
test([ 'foo', 'bar', 1, 2, 3, [ 4, 5 ], 'quux' ], '8763666f6f636261720102038204056471757578'); |
|||
test({ foo: 'bar', quux: { baz: true } }, 'a263666f6f636261726471757578a16362617af5'); |
|||
test(new Uint8Array([ 1, 2, 3 ]), '43010203'); |
|||
test(function foo() {}, 'a0'); // ECMAScript function
|
|||
test(Math.cos, 'a0'); // native function
|
|||
test(Math.cos.bind(123, 234), 'a0'); // bound function
|
|||
test(new Date(2019, 1, 2, 3, 4, 5), 'a0'); |
|||
test(/foobar/g, 'a0'); |
|||
test(new Proxy({}, {}), 'a0'); |
|||
// Cover null pointer only, encodes to "(null)" now.
|
|||
// XXX: non-null pointers
|
|||
test(Duktape.Pointer(null), '66286e756c6c29'); |
|||
// XXX: lightfunc
|
|||
|
|||
// Buffer type details.
|
|||
// For > 1-byte types expect string is little endian.
|
|||
var buf = Uint8Array.allocPlain(4); |
|||
buf[2] = 0xfe; |
|||
test(buf, '440000fe00'); |
|||
test(new Uint8Array([ 1, 2, 3, 4 ]).buffer, '4401020304'); |
|||
test(new Uint8Array([ 0xfe, 1, 2, 3 ]), '44fe010203'); |
|||
test(new Uint8ClampedArray([ 0x10fe, -100, 1, 2 ]), '44ff000102'); |
|||
test(new Int8Array([ 0xfe, 1, 2, 3 ]), '44fe010203'); |
|||
test(new Uint16Array([ 0xfe, 1, 2, 3 ]), '48fe00010002000300'); |
|||
test(new Int16Array([ 0xfe, 1, 2, 3 ]), '48fe00010002000300'); |
|||
test(new Uint32Array([ 0xfe, 1, 2, 3 ]), '50fe000000010000000200000003000000'); |
|||
test(new Int32Array([ 0xfe, 1, 2, 3 ]), '50fe000000010000000200000003000000'); |
|||
test(new Float32Array([ 0xfe, 1, 2, 3 ]), '5000007e430000803f0000004000004040'); |
|||
test(new Float64Array([ 0xfe, 1, 2, 3 ]), '58200000000000c06f40000000000000f03f00000000000000400000000000000840'); |
|||
var dv = new DataView(new Uint8Array([ 1, 2, 3, 4, 5, 6 ]).buffer); |
|||
test(dv, '46010203040506'); |
|||
|
|||
// Gap in array, similar to undefined, now encodes the same.
|
|||
var arr = new Array(1); |
|||
test(arr, '81f7'); |
|||
var arr = [ void 0 ]; |
|||
test(arr, '81f7'); |
|||
|
|||
// Integer tests.
|
|||
/* |
|||
>>> cbor.loads('f98000'.decode('hex')) |
|||
-0.0 |
|||
*/ |
|||
test(-0, 'f98000'); |
|||
test(+0, '00'); |
|||
test(1, '01'); |
|||
test(23, '17'); |
|||
test(24, '1818'); |
|||
test(255, '18ff'); |
|||
test(256, '190100'); |
|||
test(65534, '19fffe'); |
|||
test(65535, '19ffff'); |
|||
test(65536, '1a00010000'); |
|||
test(0xfedcab98, '1afedcab98'); |
|||
test(0xffffffff, '1affffffff'); |
|||
// Currently 64-bit integers are never used, shortest float
|
|||
// maintaining precision is used. No integers encode to
|
|||
// half-float because 16-bit integer encodings are better
|
|||
// and same size.
|
|||
/* |
|||
>>> cbor.loads('fa4f800000'.decode('hex')) |
|||
4294967296.0 |
|||
>>> cbor.loads('fb41f0000000100000'.decode('hex')) |
|||
4294967297.0 |
|||
>>> cbor.loads('fb41f0000000200000'.decode('hex')) |
|||
4294967298.0 |
|||
>>> cbor.loads('fb41f0000000300000'.decode('hex')) |
|||
4294967299.0 |
|||
>>> cbor.loads('fb41f0000000400000'.decode('hex')) |
|||
4294967300.0 |
|||
>>> cbor.loads('fb41f0000000500000'.decode('hex')) |
|||
4294967301.0 |
|||
>>> cbor.loads('fb41f0000000600000'.decode('hex')) |
|||
4294967302.0 |
|||
*/ |
|||
test(0x100000000, 'fa4f800000'); // encodes as float
|
|||
test(0x100000001, 'fb41f0000000100000'); // encodes as double
|
|||
test(0x100000002, 'fb41f0000000200000'); |
|||
test(0x100000003, 'fb41f0000000300000'); |
|||
test(0x100000004, 'fb41f0000000400000'); |
|||
test(0x100000005, 'fb41f0000000500000'); |
|||
test(0x100000006, 'fb41f0000000600000'); |
|||
test(-1, '20'); |
|||
test(-2, '21'); |
|||
test(-23, '36'); |
|||
test(-24, '37'); |
|||
test(-25, '3818'); |
|||
test(-255, '38fe'); |
|||
test(-256, '38ff'); |
|||
test(-257, '390100'); |
|||
test(-65535, '39fffe'); |
|||
test(-65536, '39ffff'); |
|||
test(-65537, '3a00010000'); |
|||
test(-0xfedcab98, '3afedcab97'); |
|||
test(-0xffffffff, '3afffffffe'); |
|||
test(-0x100000000, '3affffffff'); |
|||
// Here 3b0000000100000000 would be fine, but Duktape CBOR uses floats.
|
|||
test(-0x100000001, 'fbc1f0000000100000'); |
|||
test(-0x100000002, 'fbc1f0000000200000'); |
|||
test(-0x100000003, 'fbc1f0000000300000'); |
|||
test(-0x100000004, 'fbc1f0000000400000'); |
|||
test(-0x100000005, 'fbc1f0000000500000'); |
|||
test(-0x100000006, 'fbc1f0000000600000'); |
|||
|
|||
// Other number tests.
|
|||
/* |
|||
>>> cbor.loads('f93800'.decode('hex')) |
|||
0.5 |
|||
>>> cbor.loads('f93e00'.decode('hex')) |
|||
1.5 |
|||
>>> cbor.loads('fa47800040'.decode('hex')) |
|||
65536.5 |
|||
>>> cbor.loads('fb400921fb54442d18'.decode('hex')) |
|||
3.141592653589793 |
|||
>>> cbor.loads('fbc00921fb54442d18'.decode('hex')) |
|||
-3.141592653589793 |
|||
>>> cbor.loads('f97c00'.decode('hex')) |
|||
inf |
|||
>>> cbor.loads('f97e00'.decode('hex')) |
|||
nan |
|||
>>> cbor.loads('f9fc00'.decode('hex')) |
|||
-inf |
|||
*/ |
|||
test(0.5, 'f93800'); // half-float
|
|||
test(1.5, 'f93e00'); // half-float
|
|||
test(65536.5, 'fa47800040'); // single float
|
|||
test(Math.PI, 'fb400921fb54442d18'); |
|||
test(-Math.PI, 'fbc00921fb54442d18'); |
|||
test(1 / 0, 'f97c00'); // half-float
|
|||
test(-1 / 0, 'f9fc00'); // half-float
|
|||
test(0 / 0, 'f97e00'); // half-float
|
|||
|
|||
// Number tests from CBOR spec Appendix A.
|
|||
//test(1.0, 'f93c00'); // Encoded as 01.
|
|||
test(1.0, '01'); |
|||
test(1.1, 'fb3ff199999999999a'); |
|||
test(1.5, 'f93e00'); |
|||
//test(65504.0, 'f97bff'); // Encoded as 19ffe0.
|
|||
test(65504.0, '19ffe0'); |
|||
//test(100000.0, 'fa47c35000'); // Encoded as 1a000186a0.
|
|||
test(100000.0, '1a000186a0'); |
|||
test(3.4028234663852886e+38, 'fa7f7fffff'); |
|||
test(1.0e+300, 'fb7e37e43c8800759c'); |
|||
//test(5.960464477539063e-8, 'f90001'); // Encoded as fa33800000, no denormal encoding support yet.
|
|||
test(5.960464477539063e-8, 'fa33800000'); |
|||
test(0.00006103515625, 'f90400'); |
|||
//test(-4.0, 'f9c400'); // Encoded as 23.
|
|||
test(-4.0, '23'); |
|||
test(-4.1, 'fbc010666666666666'); |
|||
|
|||
// String tests, surrogates etc.
|
|||
test('', '60'); |
|||
test('foo', '63666f6f'); |
|||
/* |
|||
>>> u'\ucafe'.encode('utf-8') |
|||
'\xec\xab\xbe' |
|||
*/ |
|||
test('foo\ucafebar', '69666f6fecabbe626172'); |
|||
test('12345678901234567890123', '773132333435363738393031323334353637383930313233'); |
|||
test('123456789012345678901234', '7818313233343536373839303132333435363738393031323334'); |
|||
test('A'.repeat(255), '78ff' + '41'.repeat(255)); |
|||
test('A'.repeat(256), '790100' + '41'.repeat(256)); |
|||
test('B'.repeat(65535), '79ffff' + '42'.repeat(65535)); |
|||
test('B'.repeat(65536), '7a00010000' + '42'.repeat(65536)); |
|||
test('C'.repeat(655360), '7a000a0000' + '43'.repeat(655360)); |
|||
|
|||
// Non-UTF-8 strings covered in separate tests.
|
|||
|
|||
print('done'); |
@ -0,0 +1,72 @@ |
|||
/*--- |
|||
{ |
|||
"skip": true |
|||
} |
|||
---*/ |
|||
|
|||
/*=== |
|||
,,,foo,,,,,bar, |
|||
10 |
|||
0: false |
|||
1: false |
|||
2: false |
|||
3: true |
|||
4: false |
|||
5: false |
|||
6: false |
|||
7: false |
|||
8: true |
|||
9: false |
|||
10: false |
|||
11: false |
|||
8af7f7f763666f6ff7f7f7f763626172f7 |
|||
,,,foo,,,,,bar, |
|||
10 |
|||
0: true |
|||
1: true |
|||
2: true |
|||
3: true |
|||
4: true |
|||
5: true |
|||
6: true |
|||
7: true |
|||
8: true |
|||
9: true |
|||
10: false |
|||
11: false |
|||
===*/ |
|||
|
|||
function test() { |
|||
var i; |
|||
|
|||
var arr = new Array(10); |
|||
arr[3] = 'foo'; |
|||
arr[8] = 'bar'; |
|||
|
|||
print(String(arr)); |
|||
print(arr.length); |
|||
for (i = 0; i < 12; i++) { |
|||
print(i + ': ' + (i in arr)); |
|||
} |
|||
|
|||
// CBOR does not distinguish between an 'undefined' and a missing
|
|||
// value (gap). So the gaps get encoded as 'undefined' up to the
|
|||
// array length (10). When decoded, the arrays comes out with length
|
|||
// 10 and gaps replaced with 'undefined'.
|
|||
|
|||
var enc = CBOR.encode(arr); |
|||
print(Duktape.enc('hex', enc)); |
|||
arr = CBOR.decode(enc); |
|||
|
|||
print(String(arr)); |
|||
print(arr.length); |
|||
for (i = 0; i < 12; i++) { |
|||
print(i + ': ' + (i in arr)); |
|||
} |
|||
} |
|||
|
|||
try { |
|||
test(); |
|||
} catch (e) { |
|||
print(e.stack || e); |
|||
} |
@ -0,0 +1,26 @@ |
|||
/* |
|||
* Handling of half-float denormals. |
|||
* |
|||
* Decoding is covered by full coverage decode test, so only encode needs |
|||
* testing here. |
|||
*/ |
|||
|
|||
/*--- |
|||
{ |
|||
"skip": true |
|||
} |
|||
---*/ |
|||
|
|||
/*=== |
|||
5.960464477539063e-8 |
|||
fa33800000 |
|||
done |
|||
===*/ |
|||
|
|||
// Current behavior is not to support half-float subnormals. It's not
|
|||
// required, but would be nice for completeness.
|
|||
|
|||
print(CBOR.decode(new Uint8Array([ 0xf9, 0x00, 0x01 ]))); |
|||
print(Duktape.enc('hex', CBOR.encode(5.960464477539063e-8))); |
|||
|
|||
print('done'); |
@ -0,0 +1,69 @@ |
|||
/*--- |
|||
{ |
|||
"skip": true |
|||
} |
|||
---*/ |
|||
|
|||
/*=== |
|||
foo |
|||
foo |
|||
===*/ |
|||
|
|||
// CBOR.decode() accepts any buffer type now; cbor-js only accepts an
|
|||
// ArrayBuffer.
|
|||
|
|||
var t; |
|||
|
|||
try { |
|||
t = new Uint8Array([ 0x63, 0x66, 0x6f, 0x6f ]).buffer; |
|||
print(CBOR.decode(t)); |
|||
} catch (e) { |
|||
print(e.name); |
|||
} |
|||
|
|||
try { |
|||
t = new Uint8Array([ 0x63, 0x66, 0x6f, 0x6f ]); |
|||
print(CBOR.decode(t)); |
|||
} catch (e) { |
|||
print(e.name); |
|||
} |
|||
|
|||
/*=== |
|||
object |
|||
true |
|||
false |
|||
===*/ |
|||
|
|||
// CBOR.encode() results in an ArrayBuffer.
|
|||
try { |
|||
t = CBOR.encode('foo'); |
|||
print(typeof t); |
|||
print(t instanceof ArrayBuffer); |
|||
print(t instanceof Uint8Array); |
|||
} catch (e) { |
|||
print(e.name); |
|||
} |
|||
|
|||
/*=== |
|||
object |
|||
false |
|||
true |
|||
===*/ |
|||
|
|||
// While ArrayBuffer is the preferred input/output type, buffer values still
|
|||
// decode as Uint8Array in cbor-js and Duktape.
|
|||
|
|||
try { |
|||
t = CBOR.decode(new Uint8Array([ 0x43, 0x11, 0x22, 0x33 ]).buffer); |
|||
print(typeof t); |
|||
print(t instanceof ArrayBuffer); |
|||
print(t instanceof Uint8Array); |
|||
} catch (e) { |
|||
print(e.name); |
|||
} |
|||
|
|||
/*=== |
|||
done |
|||
===*/ |
|||
|
|||
print('done'); |
@ -0,0 +1,47 @@ |
|||
/*--- |
|||
{ |
|||
"skip": true |
|||
} |
|||
---*/ |
|||
|
|||
/*=== |
|||
get: toJSON |
|||
ownKeys |
|||
get: foo |
|||
get: bar |
|||
{"foo":"FOO","bar":"BAR"} |
|||
done |
|||
===*/ |
|||
|
|||
function test() { |
|||
var O = { foo: 1, bar: 2 }; // no 'quux' in target
|
|||
var P = new Proxy(O, { |
|||
ownKeys: function (targ) { |
|||
print('ownKeys'); |
|||
return [ 'foo', 'bar', 'quux' ]; |
|||
}, |
|||
get: function (targ, key, recv) { |
|||
print('get:', key); |
|||
if (key === 'foo') { |
|||
return 'FOO'; |
|||
} else if (key === 'bar') { |
|||
return 'BAR'; |
|||
} else if (key === 'quux') { |
|||
return 'QUUX'; |
|||
} else { |
|||
return void 0; |
|||
} |
|||
} |
|||
}); |
|||
|
|||
var res = JSON.stringify(P); |
|||
print(res); |
|||
} |
|||
|
|||
try { |
|||
test(); |
|||
} catch (e) { |
|||
print(e.stack || e); |
|||
} |
|||
|
|||
print('done'); |
@ -0,0 +1,37 @@ |
|||
if (typeof print !== 'function') { print = console.log; } |
|||
|
|||
function test() { |
|||
// Encoded data from test-cbor-encode-1.js.
|
|||
var u8 = new Uint8Array([ |
|||
0xa3, 0x67, 0x6a, 0x73, 0x6f, 0x6e, 0x72, 0x70, |
|||
0x63, 0x63, 0x32, 0x2e, 0x30, 0x66, 0x6d, 0x65, |
|||
0x74, 0x68, 0x6f, 0x64, 0x66, 0x46, 0x6f, 0x6f, |
|||
0x42, 0x61, 0x72, 0x66, 0x70, 0x61, 0x72, 0x61, |
|||
0x6d, 0x73, 0xa3, 0x63, 0x66, 0x6f, 0x6f, 0x18, |
|||
0x7b, 0x63, 0x62, 0x61, 0x72, 0x83, 0xf5, 0xf4, |
|||
0xfb, 0x40, 0x5e, 0xdd, 0x2f, 0x1a, 0x9f, 0xbe, |
|||
0x77, 0x64, 0x71, 0x75, 0x75, 0x78, 0xa2, 0x63, |
|||
0x62, 0x61, 0x7a, 0xf5, 0x65, 0x71, 0x75, 0x75, |
|||
0x75, 0x78, 0xf4 ]); |
|||
var buf = u8.buffer; |
|||
|
|||
for (var i = 0; i < 1e5; i++) { |
|||
void CBOR.decode(buf); |
|||
void CBOR.decode(buf); |
|||
void CBOR.decode(buf); |
|||
void CBOR.decode(buf); |
|||
void CBOR.decode(buf); |
|||
void CBOR.decode(buf); |
|||
void CBOR.decode(buf); |
|||
void CBOR.decode(buf); |
|||
void CBOR.decode(buf); |
|||
void CBOR.decode(buf); |
|||
} |
|||
} |
|||
|
|||
try { |
|||
test(); |
|||
} catch (e) { |
|||
print(e.stack || e); |
|||
throw e; |
|||
} |
@ -0,0 +1,69 @@ |
|||
if (typeof print !== 'function') { print = console.log; } |
|||
|
|||
function test() { |
|||
// Encoded data from test-cbor-encode-2.js.
|
|||
var u8 = new Uint8Array([ |
|||
0xa3, 0x67, 0x6a, 0x73, 0x6f, 0x6e, 0x72, 0x70, |
|||
0x63, 0x63, 0x32, 0x2e, 0x30, 0x66, 0x6d, 0x65, |
|||
0x74, 0x68, 0x6f, 0x64, 0x66, 0x46, 0x6f, 0x6f, |
|||
0x42, 0x61, 0x72, 0x66, 0x70, 0x61, 0x72, 0x61, |
|||
0x6d, 0x73, 0xab, 0x64, 0x61, 0x72, 0x67, 0x31, |
|||
0x6a, 0x78, 0x78, 0x78, 0x78, 0x78, 0x79, 0x79, |
|||
0x79, 0x79, 0x79, 0x64, 0x61, 0x72, 0x67, 0x32, |
|||
0x74, 0x78, 0x78, 0x78, 0x78, 0x78, 0x79, 0x79, |
|||
0x79, 0x79, 0x79, 0x7a, 0x7a, 0x7a, 0x7a, 0x7a, |
|||
0x77, 0x77, 0x77, 0x77, 0x77, 0x64, 0x61, 0x72, |
|||
0x67, 0x33, 0x6a, 0x78, 0x78, 0x78, 0x78, 0x78, |
|||
0x79, 0x79, 0x79, 0x79, 0x79, 0x64, 0x61, 0x72, |
|||
0x67, 0x34, 0x74, 0x78, 0x78, 0x78, 0x78, 0x78, |
|||
0x79, 0x79, 0x79, 0x79, 0x79, 0x7a, 0x7a, 0x7a, |
|||
0x7a, 0x7a, 0x77, 0x77, 0x77, 0x77, 0x77, 0x64, |
|||
0x61, 0x72, 0x67, 0x35, 0x6a, 0x78, 0x78, 0x78, |
|||
0x78, 0x78, 0x79, 0x79, 0x79, 0x79, 0x79, 0x64, |
|||
0x61, 0x72, 0x67, 0x36, 0x74, 0x78, 0x78, 0x78, |
|||
0x78, 0x78, 0x79, 0x79, 0x79, 0x79, 0x79, 0x7a, |
|||
0x7a, 0x7a, 0x7a, 0x7a, 0x77, 0x77, 0x77, 0x77, |
|||
0x77, 0x64, 0x61, 0x72, 0x67, 0x37, 0x6a, 0x78, |
|||
0x78, 0x78, 0x78, 0x78, 0x79, 0x79, 0x79, 0x79, |
|||
0x79, 0x64, 0x61, 0x72, 0x67, 0x38, 0x74, 0x78, |
|||
0x78, 0x78, 0x78, 0x78, 0x79, 0x79, 0x79, 0x79, |
|||
0x79, 0x7a, 0x7a, 0x7a, 0x7a, 0x7a, 0x77, 0x77, |
|||
0x77, 0x77, 0x77, 0x64, 0x61, 0x72, 0x67, 0x39, |
|||
0x6a, 0x78, 0x78, 0x78, 0x78, 0x78, 0x79, 0x79, |
|||
0x79, 0x79, 0x79, 0x65, 0x61, 0x72, 0x67, 0x31, |
|||
0x30, 0x74, 0x78, 0x78, 0x78, 0x78, 0x78, 0x79, |
|||
0x79, 0x79, 0x79, 0x79, 0x7a, 0x7a, 0x7a, 0x7a, |
|||
0x7a, 0x77, 0x77, 0x77, 0x77, 0x77, 0x65, 0x61, |
|||
0x72, 0x67, 0x31, 0x31, 0x8b, 0x01, 0xfb, 0x3f, |
|||
0xf1, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a, 0xfb, |
|||
0x3f, 0xf3, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, |
|||
0xfb, 0x3f, 0xf4, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, |
|||
0xcd, 0xfb, 0x3f, 0xf6, 0x66, 0x66, 0x66, 0x66, |
|||
0x66, 0x66, 0xf9, 0x3e, 0x00, 0xfb, 0x3f, 0xf9, |
|||
0x99, 0x99, 0x99, 0x99, 0x99, 0x9a, 0xfb, 0x3f, |
|||
0xfb, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0xfb, |
|||
0x3f, 0xfc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcd, |
|||
0xfb, 0x3f, 0xfe, 0x66, 0x66, 0x66, 0x66, 0x66, |
|||
0x66, 0x02 ]); |
|||
var buf = u8.buffer; |
|||
|
|||
for (var i = 0; i < 1e5; i++) { |
|||
void CBOR.decode(buf); |
|||
void CBOR.decode(buf); |
|||
void CBOR.decode(buf); |
|||
void CBOR.decode(buf); |
|||
void CBOR.decode(buf); |
|||
void CBOR.decode(buf); |
|||
void CBOR.decode(buf); |
|||
void CBOR.decode(buf); |
|||
void CBOR.decode(buf); |
|||
void CBOR.decode(buf); |
|||
} |
|||
} |
|||
|
|||
try { |
|||
test(); |
|||
} catch (e) { |
|||
print(e.stack || e); |
|||
throw e; |
|||
} |
@ -0,0 +1,38 @@ |
|||
if (typeof print !== 'function') { print = console.log; } |
|||
|
|||
function build() { |
|||
var obj = {}; |
|||
|
|||
for (var i = 0; i < 3000; i++) { |
|||
var k = 'key' + i; |
|||
var v; |
|||
switch (i % 4) { |
|||
case 0: v = true; break; |
|||
case 1: v = 123.4; break; |
|||
case 2: v = { foo: 1, bar: 2, quux: 3, baz: 4, quuux: 5 }; break; |
|||
case 3: v = [ 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.12345 ]; break; |
|||
} |
|||
obj[k] = v; |
|||
} |
|||
return obj; |
|||
} |
|||
|
|||
function test() { |
|||
var obj; |
|||
var buf; |
|||
var i; |
|||
|
|||
obj = build(); |
|||
buf = CBOR.encode(obj); |
|||
|
|||
for (i = 0; i < 1e3; i++) { |
|||
void CBOR.decode(buf); |
|||
} |
|||
} |
|||
|
|||
try { |
|||
test(); |
|||
} catch (e) { |
|||
print(e.stack || e); |
|||
throw e; |
|||
} |
@ -0,0 +1,36 @@ |
|||
if (typeof print !== 'function') { print = console.log; } |
|||
|
|||
function build() { |
|||
var obj = {}; |
|||
|
|||
for (var i = 0; i < 3000; i++) { |
|||
var k = 'key' + i; |
|||
var v; |
|||
switch (i % 4) { |
|||
case 0: v = true; break; |
|||
case 1: v = 123.4; break; |
|||
case 2: v = { foo: 1, bar: 2, quux: 3, baz: 4, quuux: 5 }; break; |
|||
case 3: v = [ 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.12345 ]; break; |
|||
} |
|||
obj[k] = v; |
|||
} |
|||
return obj; |
|||
} |
|||
|
|||
function test() { |
|||
var obj; |
|||
var i; |
|||
var ignore; |
|||
|
|||
obj = build(); |
|||
for (i = 0; i < 1e3; i++) { |
|||
ignore = JSON.stringify(obj); |
|||
} |
|||
} |
|||
|
|||
try { |
|||
test(); |
|||
} catch (e) { |
|||
print(e.stack || e); |
|||
throw e; |
|||
} |
Loading…
Reference in new issue