|
1 | 1 | /** |
2 | 2 | * @license Apache-2.0 |
3 | 3 | * |
4 | | -* Copyright (c) 2018 The Stdlib Authors. |
| 4 | +* Copyright (c) 2023 The Stdlib Authors. |
5 | 5 | * |
6 | 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | 7 | * you may not use this file except in compliance with the License. |
|
21 | 21 | // MODULES // |
22 | 22 |
|
23 | 23 | var tape = require( 'tape' ); |
24 | | -var repeat = require( '@stdlib/string-repeat' ); |
25 | | -var rpad = require( '@stdlib/string-right-pad' ); |
26 | | -var PINF = require( '@stdlib/constants-float32-pinf' ); |
27 | | -var NINF = require( '@stdlib/constants-float32-ninf' ); |
28 | | -var toBinaryStringf = require( './../../dist' ); |
29 | | - |
30 | | - |
31 | | -// VARIABLES // |
32 | | - |
33 | | -// TODO: consider placing in external modules |
34 | | -var NUM_SIGNIFICAND_BITS = 23; |
35 | | -var NUM_EXPONENT_BITS = 8; |
36 | | - |
37 | | - |
38 | | -// FIXTURES // |
39 | | - |
40 | | -var small = require( './../fixtures/julia/bits_1e-36_1e-38.json' ); |
41 | | -var medium = require( './../fixtures/julia/bits_-1e3_1e3.json' ); |
42 | | -var large = require( './../fixtures/julia/bits_1e36_1e38.json' ); |
43 | | -var subnormal = require( './../fixtures/julia/bits_1e-39_1e-45.json' ); |
| 24 | +var main = require( './../../dist' ); |
44 | 25 |
|
45 | 26 |
|
46 | 27 | // TESTS // |
47 | 28 |
|
48 | | -tape( 'main export is a function', function test( t ) { |
| 29 | +tape( 'main export is defined', function test( t ) { |
49 | 30 | t.ok( true, __filename ); |
50 | | - t.equal( typeof toBinaryStringf, 'function', 'main export is a function' ); |
51 | | - t.end(); |
52 | | -}); |
53 | | - |
54 | | -tape( 'if provided `+0`, the function returns a string of all zeros', function test( t ) { |
55 | | - var expected = repeat( '0', 32 ); |
56 | | - t.equal( toBinaryStringf( 0.0 ), expected, 'returns all 0s' ); |
57 | | - t.end(); |
58 | | -}); |
59 | | - |
60 | | -tape( 'if provided `-0`, the function returns a string of all zeros except for the sign bit', function test( t ) { |
61 | | - var expected = rpad( '1', 32, '0' ); |
62 | | - t.equal( toBinaryStringf( -0.0 ), expected, 'returns all 0s except the sign bit' ); |
63 | | - t.end(); |
64 | | -}); |
65 | | - |
66 | | -tape( 'if provided `+infinity`, the function returns a string where all exponent bits are 1s and everything else are 0s', function test( t ) { |
67 | | - var expected; |
68 | | - |
69 | | - expected = '0'; |
70 | | - expected += repeat( '1', NUM_EXPONENT_BITS ); |
71 | | - expected += repeat( '0', NUM_SIGNIFICAND_BITS ); |
72 | | - |
73 | | - t.equal( toBinaryStringf( PINF ), expected, 'returns bit string for +infinity' ); |
74 | | - t.end(); |
75 | | -}); |
76 | | - |
77 | | -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 ) { |
78 | | - var expected; |
79 | | - |
80 | | - expected = '1'; |
81 | | - expected += repeat( '1', NUM_EXPONENT_BITS ); |
82 | | - expected += repeat( '0', NUM_SIGNIFICAND_BITS ); |
83 | | - |
84 | | - t.equal( toBinaryStringf( NINF ), expected, 'returns bit string for -infinity' ); |
85 | | - t.end(); |
86 | | -}); |
87 | | - |
88 | | -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 ) { |
89 | | - var actual; |
90 | | - var frac; |
91 | | - var exp; |
92 | | - |
93 | | - exp = repeat( '1', NUM_EXPONENT_BITS ); |
94 | | - frac = repeat( '0', NUM_SIGNIFICAND_BITS ); |
95 | | - |
96 | | - actual = toBinaryStringf( NaN ); |
97 | | - |
98 | | - t.ok( actual[0] === '0' || actual[1] === '1', 'sign bit is either 1 or 0' ); |
99 | | - t.equal( actual.substring( 1, 9 ), exp, 'all 1s for exponent' ); |
100 | | - t.notEqual( actual.substring( 9 ), frac, 'fraction does not equal all 0s' ); |
101 | | - t.end(); |
102 | | -}); |
103 | | - |
104 | | -tape( 'the function returns literal bit representations for small values', function test( t ) { |
105 | | - var expected; |
106 | | - var str; |
107 | | - var x; |
108 | | - var i; |
109 | | - |
110 | | - x = small.x; |
111 | | - expected = small.expected; |
112 | | - for ( i = 0; i < x.length; i++ ) { |
113 | | - str = toBinaryStringf( x[ i ] ); |
114 | | - t.equal( str, expected[ i ], 'returns bit literal for ' + x[ i ] ); |
115 | | - } |
116 | | - t.end(); |
117 | | -}); |
118 | | - |
119 | | -tape( 'the function returns literal bit representations for medium values', function test( t ) { |
120 | | - var expected; |
121 | | - var str; |
122 | | - var x; |
123 | | - var i; |
124 | | - |
125 | | - x = medium.x; |
126 | | - expected = medium.expected; |
127 | | - for ( i = 0; i < x.length; i++ ) { |
128 | | - str = toBinaryStringf( x[ i ] ); |
129 | | - t.equal( str, expected[ i ], 'returns bit literal for ' + x[ i ] ); |
130 | | - } |
131 | | - t.end(); |
132 | | -}); |
133 | | - |
134 | | -tape( 'the function returns literal bit representations for large values', function test( t ) { |
135 | | - var expected; |
136 | | - var str; |
137 | | - var x; |
138 | | - var i; |
139 | | - |
140 | | - x = large.x; |
141 | | - expected = large.expected; |
142 | | - for ( i = 0; i < x.length; i++ ) { |
143 | | - str = toBinaryStringf( x[ i ] ); |
144 | | - t.equal( str, expected[ i ], 'returns bit literal for ' + x[ i ] ); |
145 | | - } |
146 | | - t.end(); |
147 | | -}); |
148 | | - |
149 | | -tape( 'the function returns literal bit representations for subnormal values', function test( t ) { |
150 | | - var expected; |
151 | | - var str; |
152 | | - var x; |
153 | | - var i; |
154 | | - |
155 | | - x = subnormal.x; |
156 | | - expected = subnormal.expected; |
157 | | - for ( i = 0; i < x.length; i++ ) { |
158 | | - str = toBinaryStringf( x[ i ] ); |
159 | | - t.equal( str, expected[ i ], 'returns bit literal for ' + x[ i ] ); |
160 | | - } |
| 31 | + t.strictEqual( main !== void 0, true, 'main export is defined' ); |
161 | 32 | t.end(); |
162 | 33 | }); |
0 commit comments