quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

test_bjson.js (6095B)


      1 import * as bjson from "./bjson.so";
      2 
      3 function assert(actual, expected, message) {
      4     if (arguments.length == 1)
      5         expected = true;
      6 
      7     if (actual === expected)
      8         return;
      9 
     10     if (actual !== null && expected !== null
     11     &&  typeof actual == 'object' && typeof expected == 'object'
     12     &&  actual.toString() === expected.toString())
     13         return;
     14 
     15     throw Error("assertion failed: got |" + actual + "|" +
     16                 ", expected |" + expected + "|" +
     17                 (message ? " (" + message + ")" : ""));
     18 }
     19 
     20 function toHex(a)
     21 {
     22     var i, s = "", tab, v;
     23     tab = new Uint8Array(a);
     24     for(i = 0; i < tab.length; i++) {
     25         v = tab[i].toString(16);
     26         if (v.length < 2)
     27             v = "0" + v;
     28         if (i !== 0)
     29             s += " ";
     30         s += v;
     31     }
     32     return s;
     33 }
     34 
     35 function isArrayLike(a)
     36 {
     37     return Array.isArray(a) ||
     38         (a instanceof Uint8ClampedArray) ||
     39         (a instanceof Uint8Array) ||
     40         (a instanceof Uint16Array) ||
     41         (a instanceof Uint32Array) ||
     42         (a instanceof Int8Array) ||
     43         (a instanceof Int16Array) ||
     44         (a instanceof Int32Array) ||
     45         (a instanceof Float16Array) ||
     46         (a instanceof Float32Array) ||
     47         (a instanceof Float64Array);
     48 }
     49 
     50 function toStr(a)
     51 {
     52     var s, i, props, prop;
     53 
     54     switch(typeof(a)) {
     55     case "object":
     56         if (a === null)
     57             return "null";
     58         if (a instanceof Date) {
     59             s = "Date(" + toStr(a.valueOf()) + ")";
     60         } else if (a instanceof Number) {
     61             s = "Number(" + toStr(a.valueOf()) + ")";
     62         } else if (a instanceof String) {
     63             s = "String(" + toStr(a.valueOf()) + ")";
     64         } else if (a instanceof Boolean) {
     65             s = "Boolean(" + toStr(a.valueOf()) + ")";
     66         } else if (isArrayLike(a)) {
     67             s = "[";
     68             for(i = 0; i < a.length; i++) {
     69                 if (i != 0)
     70                     s += ",";
     71                 s += toStr(a[i]);
     72             }
     73             s += "]";
     74         } else {
     75             props = Object.keys(a);
     76             s = "{";
     77             for(i = 0; i < props.length; i++) {
     78                 if (i != 0)
     79                     s += ",";
     80                 prop = props[i];
     81                 s += prop + ":" + toStr(a[prop]);
     82             }
     83             s += "}";
     84         }
     85         return s;
     86     case "undefined":
     87         return "undefined";
     88     case "string":
     89         return JSON.stringify(a);
     90     case "number":
     91         if (a == 0 && 1 / a < 0)
     92             return "-0";
     93         else
     94             return a.toString();
     95         break;
     96     default:
     97         return a.toString();
     98     }
     99 }
    100 
    101 function bjson_test(a)
    102 {
    103     var buf, r, a_str, r_str;
    104     a_str = toStr(a);
    105     buf = bjson.write(a);
    106     if (0) {
    107         print(a_str, "->", toHex(buf));
    108     }
    109     r = bjson.read(buf, 0, buf.byteLength);
    110     r_str = toStr(r);
    111     if (a_str != r_str) {
    112         print(a_str);
    113         print(r_str);
    114         assert(false);
    115     }
    116 }
    117 
    118 function bjson_test_arraybuffer()
    119 {
    120     var buf, array_buffer;
    121 
    122     array_buffer = new ArrayBuffer(4);
    123     assert(array_buffer.byteLength, 4);
    124     assert(array_buffer.maxByteLength, 4);
    125     assert(array_buffer.resizable, false);
    126     buf = bjson.write(array_buffer);
    127     array_buffer = bjson.read(buf, 0, buf.byteLength);
    128     assert(array_buffer.byteLength, 4);
    129     assert(array_buffer.maxByteLength, 4);
    130     assert(array_buffer.resizable, false);
    131 
    132     array_buffer = new ArrayBuffer(4, {maxByteLength: 4});
    133     assert(array_buffer.byteLength, 4);
    134     assert(array_buffer.maxByteLength, 4);
    135     assert(array_buffer.resizable, true);
    136     buf = bjson.write(array_buffer);
    137     array_buffer = bjson.read(buf, 0, buf.byteLength);
    138     assert(array_buffer.byteLength, 4);
    139     assert(array_buffer.maxByteLength, 4);
    140     assert(array_buffer.resizable, true);
    141 
    142     array_buffer = new ArrayBuffer(4, {maxByteLength: 8});
    143     assert(array_buffer.byteLength, 4);
    144     assert(array_buffer.maxByteLength, 8);
    145     assert(array_buffer.resizable, true);
    146     buf = bjson.write(array_buffer);
    147     array_buffer = bjson.read(buf, 0, buf.byteLength);
    148     assert(array_buffer.byteLength, 4);
    149     assert(array_buffer.maxByteLength, 8);
    150     assert(array_buffer.resizable, true);
    151 }
    152 
    153 /* test multiple references to an object including circular
    154    references */
    155 function bjson_test_reference()
    156 {
    157     var array, buf, i, n, array_buffer;
    158     n = 16;
    159     array = [];
    160     for(i = 0; i < n; i++)
    161         array[i] = {};
    162     array_buffer = new ArrayBuffer(n);
    163     for(i = 0; i < n; i++) {
    164         array[i].next = array[(i + 1) % n];
    165         array[i].idx = i;
    166         array[i].typed_array = new Uint8Array(array_buffer, i, 1);
    167     }
    168     buf = bjson.write(array, true);
    169 
    170     array = bjson.read(buf, 0, buf.byteLength, true);
    171 
    172     /* check the result */
    173     for(i = 0; i < n; i++) {
    174         assert(array[i].next, array[(i + 1) % n]);
    175         assert(array[i].idx, i);
    176         assert(array[i].typed_array.buffer, array_buffer);
    177         assert(array[i].typed_array.length, 1);
    178         assert(array[i].typed_array.byteOffset, i);
    179     }
    180 }
    181 
    182 function bjson_test_all()
    183 {
    184     var obj;
    185 
    186     bjson_test({x:1, y:2, if:3});
    187 
    188     bjson_test([1, 2, 3]);
    189 
    190     /* array with holes */
    191     bjson_test([1, , 2, , 3]); 
    192 
    193     /* fast array with hole */
    194     obj = new Array(5);
    195     obj[0] = 1;
    196     obj[1] = 2;
    197     bjson_test(obj);
    198 
    199     bjson_test([1.0, "aa", true, false, undefined, null, NaN, -Infinity, -0.0]);
    200     if (typeof BigInt !== "undefined") {
    201         bjson_test([BigInt("1"), -BigInt("0x123456789"),
    202                BigInt("0x123456789abcdef123456789abcdef")]);
    203     }
    204     bjson_test([new Date(1234), new String("abc"), new Number(-12.1), new Boolean(true)]);
    205 
    206     bjson_test(new Int32Array([123123, 222111, -32222]));
    207     bjson_test(new Float16Array([1024, 1024.5]));
    208     bjson_test(new Float64Array([123123, 222111.5]));
    209 
    210     /* tested with a circular reference */
    211     obj = {};
    212     obj.x = obj;
    213     try {
    214         bjson.write(obj);
    215         assert(false);
    216     } catch(e) {
    217         assert(e instanceof TypeError);
    218     }
    219 
    220     bjson_test_arraybuffer();
    221     bjson_test_reference();
    222 }
    223 
    224 bjson_test_all();