CoolFace
Datasetpublic

basant307/AI_Governance_Project

sourceHugging Faceapache-2.0updated 2mo agoView on Hugging Face
0likes48downloads
index.js100 linesDownload Raw Back to tests
1var tape = require("tape");
2
3var float = require("..");
4
5tape.test("float", function(test) {
6
7    // default
8    test.test(test.name + " - typed array", function(test) {
9        runTest(float, test);
10    });
11
12    // ieee754
13    test.test(test.name + " - fallback", function(test) {
14        var F32 = global.Float32Array,
15            F64 = global.Float64Array;
16        delete global.Float32Array;
17        delete global.Float64Array;
18        runTest(float({}), test);
19        global.Float32Array = F32;
20        global.Float64Array = F64;
21    });
22});
23
24function runTest(float, test) {
25
26    var common = [
27        0,
28        -0,
29        Infinity,
30        -Infinity,
31        0.125,
32        1024.5,
33        -4096.5,
34        NaN
35    ];
36
37    test.test(test.name + " - using 32 bits", function(test) {
38        common.concat([
39            3.4028234663852886e+38,
40            1.1754943508222875e-38,
41            1.1754946310819804e-39
42        ])
43        .forEach(function(value) {
44            var strval = value === 0 && 1 / value < 0 ? "-0" : value.toString();
45            test.ok(
46                checkValue(value, 4, float.readFloatLE, float.writeFloatLE, Buffer.prototype.writeFloatLE),
47                "should write and read back " + strval + " (32 bit LE)"
48            );
49            test.ok(
50                checkValue(value, 4, float.readFloatBE, float.writeFloatBE, Buffer.prototype.writeFloatBE),
51                "should write and read back " + strval + " (32 bit BE)"
52            );
53        });
54        test.end();
55    });
56
57    test.test(test.name + " - using 64 bits", function(test) {
58        common.concat([
59            1.7976931348623157e+308,
60            2.2250738585072014e-308,
61            2.2250738585072014e-309
62        ])
63        .forEach(function(value) {
64            var strval = value === 0 && 1 / value < 0 ? "-0" : value.toString();
65            test.ok(
66                checkValue(value, 8, float.readDoubleLE, float.writeDoubleLE, Buffer.prototype.writeDoubleLE),
67                "should write and read back " + strval + " (64 bit LE)"
68            );
69            test.ok(
70                checkValue(value, 8, float.readDoubleBE, float.writeDoubleBE, Buffer.prototype.writeDoubleBE),
71                "should write and read back " + strval + " (64 bit BE)"
72            );
73        });
74        test.end();
75    });
76
77    test.end();
78}
79
80function checkValue(value, size, read, write, write_comp) {
81    var buffer = new Buffer(size);
82    write(value, buffer, 0);
83    var value_comp = read(buffer, 0);
84    var strval = value === 0 && 1 / value < 0 ? "-0" : value.toString();
85    if (value !== value) {
86        if (value_comp === value_comp)
87            return false;
88    } else if (value_comp !== value)
89        return false;
90
91    var buffer_comp = new Buffer(size);
92    write_comp.call(buffer_comp, value, 0);
93    for (var i = 0; i < size; ++i)
94        if (buffer[i] !== buffer_comp[i]) {
95            console.error(">", buffer, buffer_comp);
96            return false;
97        }
98
99    return true;
100}
basant307/AI_Governance_Project · CoolFace