-
-
Notifications
You must be signed in to change notification settings - Fork 813
/
Copy pathtest.js
144 lines (113 loc) · 3.99 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
'use strict';
// MODULES //
var tape = require( 'tape' );
var repeat = require( '@stdlib/string/repeat' );
var rpad = require( '@stdlib/string/right-pad' );
var PINF = require( '@stdlib/math/constants/float64-pinf' );
var NINF = require( '@stdlib/math/constants/float64-ninf' );
var toBinaryString = require( './../lib' );
// VARIABLES //
// TODO: consider moving to external constants
var NUM_EXPONENT_BITS = 11;
var NUM_SIGNIFICAND_BITS = 52;
// FIXTURES //
var small = require( './fixtures/julia/bits_1e-200_1e-308.json' );
var medium = require( './fixtures/julia/bits_-1e3_1e3.json' );
var large = require( './fixtures/julia/bits_1e200_1e308.json' );
var subnormal = require( './fixtures/julia/bits_1e-308_5e-324.json' );
// TESTS //
tape( 'main export is a function', function test( t ) {
t.ok( true, __filename );
t.equal( typeof toBinaryString, 'function', 'main export is a function' );
t.end();
});
tape( 'if provided `+0`, the function returns a string of all zeros', function test( t ) {
var expected = repeat( '0', 64 );
t.equal( toBinaryString( 0.0 ), expected, 'returns all 0s' );
t.end();
});
tape( 'if provided `-0`, the function returns a string of all zeros except for the sign bit', function test( t ) {
var expected = rpad( '1', 64, '0' );
t.equal( toBinaryString( -0.0 ), expected, 'returns all 0s except the sign bit' );
t.end();
});
tape( 'if provided `+infinity`, the function returns a string where all exponent bits are 1s and everything else are 0s', function test( t ) {
var expected;
expected = '0';
expected += repeat( '1', NUM_EXPONENT_BITS );
expected += repeat( '0', NUM_SIGNIFICAND_BITS );
t.equal( toBinaryString( PINF ), expected, 'returns bit string for +infinity' );
t.end();
});
tape( 'if provided `-infinity`, the function returns a string where the sign bit is 1, all exponent bits are 1s, and everything else are 0s', function test( t ) {
var expected;
expected = '1';
expected += repeat( '1', NUM_EXPONENT_BITS );
expected += repeat( '0', NUM_SIGNIFICAND_BITS );
t.equal( toBinaryString( NINF ), expected, 'returns bit string for -infinity' );
t.end();
});
tape( 'if provided `NaN`, the function returns a string where the sign bit may be either 1 or 0, all exponent bits are 1s, and the fraction cannot be all 0s', function test( t ) {
var actual;
var frac;
var exp;
exp = repeat( '1', NUM_EXPONENT_BITS );
frac = repeat( '0', NUM_SIGNIFICAND_BITS );
actual = toBinaryString( NaN );
t.ok( actual[0] === '0' || actual[1] === '1', 'sign bit is either 1 or 0' );
t.equal( actual.substring( 1, 12 ), exp, 'all 1s for exponent' );
t.notEqual( actual.substring( 12 ), frac, 'fraction does not equal all 0s' );
t.end();
});
tape( 'the function returns literal bit representations for small values', function test( t ) {
var expected;
var str;
var x;
var i;
x = small.x;
expected = small.expected;
for ( i = 0; i < x.length; i++ ) {
str = toBinaryString( x[ i ] );
t.equal( str, expected[ i ], 'returns bit literal for ' + x[ i ] );
}
t.end();
});
tape( 'the function returns literal bit representations for medium values', function test( t ) {
var expected;
var str;
var x;
var i;
x = medium.x;
expected = medium.expected;
for ( i = 0; i < x.length; i++ ) {
str = toBinaryString( x[ i ] );
t.equal( str, expected[ i ], 'returns bit literal for ' + x[ i ] );
}
t.end();
});
tape( 'the function returns literal bit representations for large values', function test( t ) {
var expected;
var str;
var x;
var i;
x = large.x;
expected = large.expected;
for ( i = 0; i < x.length; i++ ) {
str = toBinaryString( x[ i ] );
t.equal( str, expected[ i ], 'returns bit literal for ' + x[ i ] );
}
t.end();
});
tape( 'the function returns literal bit representations for subnormal values', function test( t ) {
var expected;
var str;
var x;
var i;
x = subnormal.x;
expected = subnormal.expected;
for ( i = 0; i < x.length; i++ ) {
str = toBinaryString( x[ i ] );
t.equal( str, expected[ i ], 'returns bit literal for ' + x[ i ] );
}
t.end();
});