Bug 1504181 - Force scientific notation in CanonicalJSON r=glasserc
authorMathieu Leplatre <mathieu@mozilla.com>
Mon, 12 Nov 2018 14:12:58 +0000
changeset 445881 233f5ec7592438d70f43524dd7f628d717ae4f81
parent 445880 744ccc0fb73dfa5147ea696227c05b5931e2c2a3
child 445882 99d830166ae2b3862953b999b42f9294d26211b9
push id35028
push usercsabou@mozilla.com
push dateMon, 12 Nov 2018 21:54:15 +0000
treeherdermozilla-central@05331fb8f533 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersglasserc
bugs1504181
milestone65.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1504181 - Force scientific notation in CanonicalJSON r=glasserc Mimic python for small float numbers Differential Revision: https://phabricator.services.mozilla.com/D10734
toolkit/modules/tests/xpcshell/test_CanonicalJSON.js
--- a/toolkit/modules/tests/xpcshell/test_CanonicalJSON.js
+++ b/toolkit/modules/tests/xpcshell/test_CanonicalJSON.js
@@ -134,8 +134,27 @@ add_task(async function test_canonicalJS
     "id": "1"}];
   const expected =
     '[{"a":{"a":"a","b":"b","c":{"a":"a","b":"b","c":["b","a","c"],' +
     '"d":{"a":"a","b":"b"},"e":1,"f":[2,3,1],"g":{' +
     '"1":{"a":"a","b":"b","c":"c"},"2":2,"3":3},"id":"1"}},"id":"1"}]';
 
   Assert.equal(CanonicalJSON.stringify(records), expected);
 });
+
+add_task(async function test_canonicalJSON_should_use_scientific_notation() {
+  /*
+  We globally follow the Python float representation, except for exponents < 10
+  where we drop the leading zero
+  */
+  Assert.equal(CanonicalJSON.stringify(.00099), "0.00099");
+  Assert.equal(CanonicalJSON.stringify(.000011), "0.000011");
+  Assert.equal(CanonicalJSON.stringify(.0000011), "0.0000011");
+  Assert.equal(CanonicalJSON.stringify(.000001), "0.000001");
+  Assert.equal(CanonicalJSON.stringify(.00000099), "9.9e-7");
+  Assert.equal(CanonicalJSON.stringify(.0000001), "1e-7");
+  Assert.equal(CanonicalJSON.stringify(.000000930258908), "9.30258908e-7");
+  Assert.equal(CanonicalJSON.stringify(.00000000000068272), "6.8272e-13");
+  Assert.equal(CanonicalJSON.stringify(Math.pow(10, 20)), "100000000000000000000");
+  Assert.equal(CanonicalJSON.stringify(Math.pow(10, 21)), "1e+21");
+  Assert.equal(CanonicalJSON.stringify(Math.pow(10, 15) + 0.1), "1000000000000000.1");
+  Assert.equal(CanonicalJSON.stringify(Math.pow(10, 16) * 1.1), "11000000000000000");
+});