summaryrefslogtreecommitdiff
path: root/tests/test_bjson.js
diff options
context:
space:
mode:
authorbellard <6490144+bellard@users.noreply.github.com>2020-09-06 18:53:08 +0200
committerbellard <6490144+bellard@users.noreply.github.com>2020-09-06 18:53:08 +0200
commit91459fb6723e29e923380cec0023af93819ae69d (patch)
tree6a1aff8d9b290ed184d1481da50d0e6b4a9a324c /tests/test_bjson.js
parent9096e544ba2357eeadc6f09fc6e5cf58db7751bc (diff)
downloadquickjs-91459fb6723e29e923380cec0023af93819ae69d.tar.gz
quickjs-91459fb6723e29e923380cec0023af93819ae69d.zip
2020-01-05 release
Diffstat (limited to 'tests/test_bjson.js')
-rw-r--r--tests/test_bjson.js125
1 files changed, 125 insertions, 0 deletions
diff --git a/tests/test_bjson.js b/tests/test_bjson.js
new file mode 100644
index 0000000..a6196df
--- /dev/null
+++ b/tests/test_bjson.js
@@ -0,0 +1,125 @@
+import * as bjson from "./bjson.so";
+
+function assert(b, str)
+{
+ if (b) {
+ return;
+ } else {
+ throw Error("assertion failed: " + str);
+ }
+}
+
+function toHex(a)
+{
+ var i, s = "", tab, v;
+ tab = new Uint8Array(a);
+ for(i = 0; i < tab.length; i++) {
+ v = tab[i].toString(16);
+ if (v.length < 2)
+ v = "0" + v;
+ if (i !== 0)
+ s += " ";
+ s += v;
+ }
+ return s;
+}
+
+function toStr(a)
+{
+ var s, i, props, prop;
+
+ switch(typeof(a)) {
+ case "object":
+ if (a === null)
+ return "null";
+ if (Array.isArray(a)) {
+ s = "[";
+ for(i = 0; i < a.length; i++) {
+ if (i != 0)
+ s += ",";
+ s += toStr(a[i]);
+ }
+ s += "]";
+ } else {
+ props = Object.keys(a);
+ s = "{";
+ for(i = 0; i < props.length; i++) {
+ if (i != 0)
+ s += ",";
+ prop = props[i];
+ s += prop + ":" + toStr(a[prop]);
+ }
+ s += "}";
+ }
+ return s;
+ case "undefined":
+ return "undefined";
+ case "string":
+ return a.__quote();
+ case "number":
+ case "bigfloat":
+ if (a == 0 && 1 / a < 0)
+ return "-0";
+ else
+ return a.toString();
+ break;
+ default:
+ return a.toString();
+ }
+}
+
+function bjson_test(a)
+{
+ var buf, r, a_str, r_str;
+ a_str = toStr(a);
+ buf = bjson.write(a);
+ if (0) {
+ print(a_str, "->", toHex(buf));
+ }
+ r = bjson.read(buf, 0, buf.byteLength);
+ r_str = toStr(r);
+ if (a_str != r_str) {
+ print(a_str);
+ print(r_str);
+ assert(false);
+ }
+}
+
+function bjson_test_all()
+{
+ var obj;
+
+ bjson_test({x:1, y:2, if:3});
+ bjson_test([1, 2, 3]);
+ bjson_test([1.0, "aa", true, false, undefined, null, NaN, -Infinity, -0.0]);
+ if (typeof BigInt !== "undefined") {
+ bjson_test([BigInt("1"), -BigInt("0x123456789"),
+ BigInt("0x123456789abcdef123456789abcdef")]);
+ }
+ if (typeof BigFloat !== "undefined") {
+ BigFloatEnv.setPrec(function () {
+ bjson_test([BigFloat("0.1"), BigFloat("-1e30"), BigFloat("0"),
+ BigFloat("-0"), BigFloat("Infinity"), BigFloat("-Infinity"),
+ 0.0 / BigFloat("0"), BigFloat.MAX_VALUE,
+ BigFloat.MIN_VALUE]);
+ }, 113, 15);
+ }
+ if (typeof BigDecimal !== "undefined") {
+ bjson_test([BigDecimal("0"),
+ BigDecimal("0.8"), BigDecimal("123321312321321e100"),
+ BigDecimal("-1233213123213214332333223332e100"),
+ BigDecimal("1.233e-1000")]);
+ }
+
+ /* tested with a circular reference */
+ obj = {};
+ obj.x = obj;
+ try {
+ bjson.write(obj);
+ assert(false);
+ } catch(e) {
+ assert(e instanceof TypeError);
+ }
+}
+
+bjson_test_all();