Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc-cloned / node_modules / @protobufjs / float / tests / index.js
diff --git a/legacy-libs/grpc-cloned/node_modules/@protobufjs/float/tests/index.js b/legacy-libs/grpc-cloned/node_modules/@protobufjs/float/tests/index.js
new file mode 100644 (file)
index 0000000..324e85c
--- /dev/null
@@ -0,0 +1,100 @@
+var tape = require("tape");\r
+\r
+var float = require("..");\r
+\r
+tape.test("float", function(test) {\r
+\r
+    // default\r
+    test.test(test.name + " - typed array", function(test) {\r
+        runTest(float, test);\r
+    });\r
+\r
+    // ieee754\r
+    test.test(test.name + " - fallback", function(test) {\r
+        var F32 = global.Float32Array,\r
+            F64 = global.Float64Array;\r
+        delete global.Float32Array;\r
+        delete global.Float64Array;\r
+        runTest(float({}), test);\r
+        global.Float32Array = F32;\r
+        global.Float64Array = F64;\r
+    });\r
+});\r
+\r
+function runTest(float, test) {\r
+\r
+    var common = [\r
+        0,\r
+        -0,\r
+        Infinity,\r
+        -Infinity,\r
+        0.125,\r
+        1024.5,\r
+        -4096.5,\r
+        NaN\r
+    ];\r
+\r
+    test.test(test.name + " - using 32 bits", function(test) {\r
+        common.concat([\r
+            3.4028234663852886e+38,\r
+            1.1754943508222875e-38,\r
+            1.1754946310819804e-39\r
+        ])\r
+        .forEach(function(value) {\r
+            var strval = value === 0 && 1 / value < 0 ? "-0" : value.toString();\r
+            test.ok(\r
+                checkValue(value, 4, float.readFloatLE, float.writeFloatLE, Buffer.prototype.writeFloatLE),\r
+                "should write and read back " + strval + " (32 bit LE)"\r
+            );\r
+            test.ok(\r
+                checkValue(value, 4, float.readFloatBE, float.writeFloatBE, Buffer.prototype.writeFloatBE),\r
+                "should write and read back " + strval + " (32 bit BE)"\r
+            );\r
+        });\r
+        test.end();\r
+    });\r
+\r
+    test.test(test.name + " - using 64 bits", function(test) {\r
+        common.concat([\r
+            1.7976931348623157e+308,\r
+            2.2250738585072014e-308,\r
+            2.2250738585072014e-309\r
+        ])\r
+        .forEach(function(value) {\r
+            var strval = value === 0 && 1 / value < 0 ? "-0" : value.toString();\r
+            test.ok(\r
+                checkValue(value, 8, float.readDoubleLE, float.writeDoubleLE, Buffer.prototype.writeDoubleLE),\r
+                "should write and read back " + strval + " (64 bit LE)"\r
+            );\r
+            test.ok(\r
+                checkValue(value, 8, float.readDoubleBE, float.writeDoubleBE, Buffer.prototype.writeDoubleBE),\r
+                "should write and read back " + strval + " (64 bit BE)"\r
+            );\r
+        });\r
+        test.end();\r
+    });\r
+\r
+    test.end();\r
+}\r
+\r
+function checkValue(value, size, read, write, write_comp) {\r
+    var buffer = new Buffer(size);\r
+    write(value, buffer, 0);\r
+    var value_comp = read(buffer, 0);\r
+    var strval = value === 0 && 1 / value < 0 ? "-0" : value.toString();\r
+    if (value !== value) {\r
+        if (value_comp === value_comp)\r
+            return false;\r
+    } else if (value_comp !== value)\r
+        return false;\r
+\r
+    var buffer_comp = new Buffer(size);\r
+    write_comp.call(buffer_comp, value, 0);\r
+    for (var i = 0; i < size; ++i)\r
+        if (buffer[i] !== buffer_comp[i]) {\r
+            console.error(">", buffer, buffer_comp);\r
+            return false;\r
+        }\r
+\r
+    return true;\r
+}
\ No newline at end of file