Browse Source

Add more CBOR tests

pull/2154/head
Sami Vaarala 5 years ago
parent
commit
f0ff226374
  1. 154
      tests/ecmascript/test-bi-cbor-dec-fastint.js
  2. 2
      tests/ecmascript/test-dev-fastint-basic.js
  3. 41
      tests/perf/test-cbor-decode-fastints.js
  4. 30
      tests/perf/test-cbor-decode-strings.js
  5. 21
      tests/perf/test-cbor-encode-double.js
  6. 21
      tests/perf/test-cbor-encode-float.js
  7. 21
      tests/perf/test-cbor-encode-half-float.js

154
tests/ecmascript/test-bi-cbor-dec-fastint.js

@ -0,0 +1,154 @@
/*
* Current CBOR decode fastint behavior.
*/
/*===
- top level integer
17 fastint
255 fastint
4275878552 fastint
4294967295 fastint
4294967295 fastint
4294967296 fastint
139486488458836 fastint
140737488355327 fastint
140737488355328
141685511714388
-18 fastint
-256 fastint
-2147483648 fastint
-2147483649 fastint
-4275878553 fastint
-139486488458837 fastint
-140737488355328 fastint
-2147483648 fastint
-2147483649 fastint
-140737488355329
-141685511714389
- array wrapped integer
17 fastint
255 fastint
4275878552 fastint
4294967295 fastint
4294967295
4294967296
139486488458836
140737488355327
140737488355328
141685511714388
-18 fastint
-256 fastint
-2147483648 fastint
-2147483649
-4275878553
-2147483648
-2147483649
-140737488355329
-141685511714389
- integer-compatible double
17 fastint
17
- integer-compatible float
123 fastint
123
- integer-compatible half-float
123 fastint
123
===*/
function test() {
var inp, dec;
function t(arr) {
var dec = CBOR.decode(new Uint8Array(arr));
var t = Array.isArray(dec) ? dec[0] : dec;
var info_t = Duktape.info(t);
var info_double = Duktape.info(123.4);
if (info_t.itag === info_double.itag) {
print(t);
} else {
print(t, 'fastint');
}
//print(Duktape.enc('jx', Duktape.info(t)));
}
// If top level value is an integer, it gets coerced into a fastint
// because of return value fastint check for CBOR.decode() itself.
// This applies to the entire fastint range.
print('- top level integer');
t([ 0x11 ]);
t([ 0x18, 0xff ]);
t([ 0x1a, 0xfe, 0xdc, 0xba, 0x98 ]);
t([ 0x1a, 0xff, 0xff, 0xff, 0xff ]);
t([ 0x1b, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff ]);
t([ 0x1b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 ]);
t([ 0x1b, 0x00, 0x00, 0x7e, 0xdc, 0xba, 0x98, 0x76, 0x54 ]);
t([ 0x1b, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff ]);
t([ 0x1b, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00 ]); // no longer in fastint range
t([ 0x1b, 0x00, 0x00, 0x80, 0xdc, 0xba, 0x98, 0x76, 0x54 ]); // no longer in fastint range
t([ 0x31 ]);
t([ 0x38, 0xff ]);
t([ 0x3a, 0x7f, 0xff, 0xff, 0xff ]);
t([ 0x3a, 0x80, 0x00, 0x00, 0x00 ]);
t([ 0x3a, 0xfe, 0xdc, 0xba, 0x98 ]);
t([ 0x3b, 0x00, 0x00, 0x7e, 0xdc, 0xba, 0x98, 0x76, 0x54 ]);
t([ 0x3b, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff ]);
t([ 0x3b, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff ]);
t([ 0x3b, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00 ]);
t([ 0x3b, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00 ]); // no longer in fastint range
t([ 0x3b, 0x00, 0x00, 0x80, 0xdc, 0xba, 0x98, 0x76, 0x54 ]); // no longer in fastint range
// An integer deeper inside a structure won't get automatic return
// value fastint check, so it is up to the CBOR binding to ensure
// fastints are used (e.g. use duk_push_int() rather than
// duk_push_number() where applicable).
//
// At present fastints are used for [-0x80000000,0xffffffff] when
// they are encoded in shortest form (this is likely to change).
print('- array wrapped integer');
t([ 0x81, 0x11 ]);
t([ 0x81, 0x18, 0xff ]);
t([ 0x81, 0x1a, 0xfe, 0xdc, 0xba, 0x98 ]);
t([ 0x81, 0x1a, 0xff, 0xff, 0xff, 0xff ]);
t([ 0x81, 0x1b, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff ]); // in range, non-shortest encoding
t([ 0x81, 0x1b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 ]); // no longer in 32-bit range
t([ 0x81, 0x1b, 0x00, 0x00, 0x7e, 0xdc, 0xba, 0x98, 0x76, 0x54 ]);
t([ 0x81, 0x1b, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff ]);
t([ 0x81, 0x1b, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00 ]); // no longer in fastint range
t([ 0x81, 0x1b, 0x00, 0x00, 0x80, 0xdc, 0xba, 0x98, 0x76, 0x54 ]); // no longer in fastint range
t([ 0x81, 0x31 ]);
t([ 0x81, 0x38, 0xff ]);
t([ 0x81, 0x3a, 0x7f, 0xff, 0xff, 0xff ]);
t([ 0x81, 0x3a, 0x80, 0x00, 0x00, 0x00 ]);
t([ 0x81, 0x3a, 0xfe, 0xdc, 0xba, 0x98 ]);
t([ 0x81, 0x3b, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff ]); // in range, non-shortest encoding
t([ 0x81, 0x3b, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00 ]); // no longer in 32-bit range
t([ 0x81, 0x3b, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00 ]);
t([ 0x81, 0x3b, 0x00, 0x00, 0x80, 0xdc, 0xba, 0x98, 0x76, 0x54 ]);
// Integer-compatible double.
//
// At present no fastint check (except for top level value).
//
// >>> cbor.dumps(17.0).encode('hex')
// 'fb4031000000000000'
print('- integer-compatible double');
t([ 0xfb, 0x40, 0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ]);
t([ 0x81, 0xfb, 0x40, 0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ]);
// Integer-compatible float or half-float.
//
// At present no fastint check (except for top level value).
print('- integer-compatible float');
t([ 0xfa, 0x42, 0xf6, 0x00, 0x00 ]); // 123 as float
t([ 0x81, 0xfa, 0x42, 0xf6, 0x00, 0x00 ]);
print('- integer-compatible half-float');
t([ 0xf9, 0x57, 0xb0 ]); // 123 as half-float
t([ 0x81, 0xf9, 0x57, 0xb0 ]);
}
try {
test();
} catch (e) {
print(e.stack || e);
}

2
tests/ecmascript/test-dev-fastint-basic.js

@ -86,7 +86,7 @@ function printFastint(v) {
}
// The internal type tag depends on the duk_tval unpacked/packed layout.
// XXX: add to util.
if (typeof Duktape !== 'object') {
isfast = ' NOT-DUKTAPE';
} else if (getValueInternalTag(true) >= 0xfff0) {

41
tests/perf/test-cbor-decode-fastints.js

@ -0,0 +1,41 @@
if (typeof print !== 'function') { print = console.log; }
function build() {
var obj = [];
for (var i = 0; i < 1000; i++) {
obj.push(123);
obj.push(12);
obj.push(123);
obj.push(12);
obj.push(123);
obj.push(12);
obj.push(-123);
obj.push(-12);
obj.push(-123);
obj.push(-12);
obj.push(-123);
obj.push(-12);
}
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;
}

30
tests/perf/test-cbor-decode-strings.js

@ -0,0 +1,30 @@
if (typeof print !== 'function') { print = console.log; }
function build() {
var obj = [];
for (var i = 0; i < 10000; i++) {
obj.push('foobar-' + i);
}
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;
}

21
tests/perf/test-cbor-encode-double.js

@ -0,0 +1,21 @@
if (typeof print !== 'function') { print = console.log; }
function test() {
var msg = [];
while (msg.length < 10000) {
msg[msg.length] = Math.PI;
}
// print(Duktape.enc('hex', CBOR.encode(msg)));
for (var i = 0; i < 1e3; i++) {
void CBOR.encode(msg);
}
}
try {
test();
} catch (e) {
print(e.stack || e);
throw e;
}

21
tests/perf/test-cbor-encode-float.js

@ -0,0 +1,21 @@
if (typeof print !== 'function') { print = console.log; }
function test() {
var msg = [];
while (msg.length < 10000) {
msg[msg.length] = 100000.5;
}
// print(Duktape.enc('hex', CBOR.encode(msg)));
for (var i = 0; i < 1e3; i++) {
void CBOR.encode(msg);
}
}
try {
test();
} catch (e) {
print(e.stack || e);
throw e;
}

21
tests/perf/test-cbor-encode-half-float.js

@ -0,0 +1,21 @@
if (typeof print !== 'function') { print = console.log; }
function test() {
var msg = [];
while (msg.length < 10000) {
msg[msg.length] = 1.5;
}
// print(Duktape.enc('hex', CBOR.encode(msg)));
for (var i = 0; i < 1e3; i++) {
void CBOR.encode(msg);
}
}
try {
test();
} catch (e) {
print(e.stack || e);
throw e;
}
Loading…
Cancel
Save