|
| 1 | +/** |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2023 The Stdlib Authors. |
| 5 | +* |
| 6 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +* you may not use this file except in compliance with the License. |
| 8 | +* You may obtain a copy of the License at |
| 9 | +* |
| 10 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +* |
| 12 | +* Unless required by applicable law or agreed to in writing, software |
| 13 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +* See the License for the specific language governing permissions and |
| 16 | +* limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +'use strict'; |
| 20 | + |
| 21 | +// MODULES // |
| 22 | + |
| 23 | +var tape = require( 'tape' ); |
| 24 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 25 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 26 | +var random = require( './../lib/main.js' ); |
| 27 | + |
| 28 | + |
| 29 | +// TESTS // |
| 30 | + |
| 31 | +tape( 'main export is a function', function test( t ) { |
| 32 | + t.ok( true, __filename ); |
| 33 | + t.strictEqual( typeof random, 'function', 'main export is a function' ); |
| 34 | + t.end(); |
| 35 | +}); |
| 36 | + |
| 37 | +tape( 'the function throws an error if provided a first argument which is not a nonnegative integer', function test( t ) { |
| 38 | + var values; |
| 39 | + var i; |
| 40 | + |
| 41 | + values = [ |
| 42 | + '5', |
| 43 | + -5, |
| 44 | + null, |
| 45 | + true, |
| 46 | + false, |
| 47 | + void 0, |
| 48 | + NaN, |
| 49 | + [], |
| 50 | + {}, |
| 51 | + function noop() {} |
| 52 | + ]; |
| 53 | + |
| 54 | + for ( i = 0; i < values.length; i++ ) { |
| 55 | + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); |
| 56 | + } |
| 57 | + t.end(); |
| 58 | + |
| 59 | + function badValue( value ) { |
| 60 | + return function badValue() { |
| 61 | + random( value, 2.0, 5.0 ); |
| 62 | + }; |
| 63 | + } |
| 64 | +}); |
| 65 | + |
| 66 | +tape( 'the function throws an error if provided a first argument which is not a nonnegative integer (options)', function test( t ) { |
| 67 | + var values; |
| 68 | + var i; |
| 69 | + |
| 70 | + values = [ |
| 71 | + '5', |
| 72 | + -5, |
| 73 | + null, |
| 74 | + true, |
| 75 | + false, |
| 76 | + void 0, |
| 77 | + NaN, |
| 78 | + [], |
| 79 | + {}, |
| 80 | + function noop() {} |
| 81 | + ]; |
| 82 | + |
| 83 | + for ( i = 0; i < values.length; i++ ) { |
| 84 | + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); |
| 85 | + } |
| 86 | + t.end(); |
| 87 | + |
| 88 | + function badValue( value ) { |
| 89 | + return function badValue() { |
| 90 | + random( value, 2.0, 5.0, {} ); |
| 91 | + }; |
| 92 | + } |
| 93 | +}); |
| 94 | + |
| 95 | +tape( 'the function throws an error if provided an options argument which is not an object', function test( t ) { |
| 96 | + var values; |
| 97 | + var i; |
| 98 | + |
| 99 | + values = [ |
| 100 | + '5', |
| 101 | + 5, |
| 102 | + null, |
| 103 | + true, |
| 104 | + false, |
| 105 | + void 0, |
| 106 | + NaN, |
| 107 | + [], |
| 108 | + function noop() {} |
| 109 | + ]; |
| 110 | + |
| 111 | + for ( i = 0; i < values.length; i++ ) { |
| 112 | + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); |
| 113 | + } |
| 114 | + t.end(); |
| 115 | + |
| 116 | + function badValue( value ) { |
| 117 | + return function badValue() { |
| 118 | + random( 10, 2.0, 5.0, value ); |
| 119 | + }; |
| 120 | + } |
| 121 | +}); |
| 122 | + |
| 123 | +tape( 'the function throws an error if provided an invalid option', function test( t ) { |
| 124 | + var values; |
| 125 | + var i; |
| 126 | + |
| 127 | + values = [ |
| 128 | + '5', |
| 129 | + 5, |
| 130 | + null, |
| 131 | + true, |
| 132 | + false, |
| 133 | + void 0, |
| 134 | + NaN, |
| 135 | + [], |
| 136 | + {}, |
| 137 | + function noop() {} |
| 138 | + ]; |
| 139 | + |
| 140 | + for ( i = 0; i < values.length; i++ ) { |
| 141 | + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); |
| 142 | + } |
| 143 | + t.end(); |
| 144 | + |
| 145 | + function badValue( value ) { |
| 146 | + return function badValue() { |
| 147 | + random( 10, 2.0, 5.0, { |
| 148 | + 'dtype': value |
| 149 | + }); |
| 150 | + }; |
| 151 | + } |
| 152 | +}); |
| 153 | + |
| 154 | +tape( 'the function returns an array containing pseudorandom numbers (default)', function test( t ) { |
| 155 | + var actual; |
| 156 | + var i; |
| 157 | + |
| 158 | + actual = random( 10, 2.0, 5.0 ); |
| 159 | + |
| 160 | + t.strictEqual( actual instanceof Float64Array, true, 'returns expected value' ); |
| 161 | + t.strictEqual( actual.length, 10, 'returns expected value' ); |
| 162 | + |
| 163 | + for ( i = 0; i < actual.length; i++ ) { |
| 164 | + t.strictEqual( typeof actual[ i ], 'number', 'returns expected value for index '+i ); |
| 165 | + } |
| 166 | + t.end(); |
| 167 | +}); |
| 168 | + |
| 169 | +tape( 'the function supports specifying the output array data type (dtype=float64)', function test( t ) { |
| 170 | + var actual; |
| 171 | + var i; |
| 172 | + |
| 173 | + actual = random( 10, 2.0, 5.0, { |
| 174 | + 'dtype': 'float64' |
| 175 | + }); |
| 176 | + |
| 177 | + t.strictEqual( actual instanceof Float64Array, true, 'returns expected value' ); |
| 178 | + t.strictEqual( actual.length, 10, 'returns expected value' ); |
| 179 | + |
| 180 | + for ( i = 0; i < actual.length; i++ ) { |
| 181 | + t.strictEqual( typeof actual[ i ], 'number', 'returns expected value for index '+i ); |
| 182 | + } |
| 183 | + t.end(); |
| 184 | +}); |
| 185 | + |
| 186 | +tape( 'the function supports specifying the output array data type (dtype=float32)', function test( t ) { |
| 187 | + var actual; |
| 188 | + var i; |
| 189 | + |
| 190 | + actual = random( 10, 2.0, 5.0, { |
| 191 | + 'dtype': 'float32' |
| 192 | + }); |
| 193 | + |
| 194 | + t.strictEqual( actual instanceof Float32Array, true, 'returns expected value' ); |
| 195 | + t.strictEqual( actual.length, 10, 'returns expected value' ); |
| 196 | + |
| 197 | + for ( i = 0; i < actual.length; i++ ) { |
| 198 | + t.strictEqual( typeof actual[ i ], 'number', 'returns expected value for index '+i ); |
| 199 | + } |
| 200 | + t.end(); |
| 201 | +}); |
| 202 | + |
| 203 | +tape( 'the function supports specifying the output array data type (dtype=generic)', function test( t ) { |
| 204 | + var actual; |
| 205 | + var i; |
| 206 | + |
| 207 | + actual = random( 10, 2.0, 5.0, { |
| 208 | + 'dtype': 'generic' |
| 209 | + }); |
| 210 | + |
| 211 | + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); |
| 212 | + t.strictEqual( actual.length, 10, 'returns expected value' ); |
| 213 | + |
| 214 | + for ( i = 0; i < actual.length; i++ ) { |
| 215 | + t.strictEqual( typeof actual[ i ], 'number', 'returns expected value for index '+i ); |
| 216 | + } |
| 217 | + t.end(); |
| 218 | +}); |
| 219 | + |
| 220 | +tape( 'the function supports setting the generator state', function test( t ) { |
| 221 | + var state; |
| 222 | + var arr; |
| 223 | + var i; |
| 224 | + |
| 225 | + // Move to a future state... |
| 226 | + for ( i = 0; i < 100; i++ ) { |
| 227 | + random( 2, 2.0, 5.0 ); |
| 228 | + } |
| 229 | + // Capture the current state: |
| 230 | + state = random.state; |
| 231 | + |
| 232 | + // Move to a future state... |
| 233 | + arr = random( 10, 2.0, 5.0 ); |
| 234 | + for ( i = 0; i < 100; i++ ) { |
| 235 | + random( 2, 2.0, 5.0 ); |
| 236 | + } |
| 237 | + // Set the state: |
| 238 | + random.state = state; |
| 239 | + |
| 240 | + // Replay previously generated values: |
| 241 | + t.deepEqual( random( 10, 2.0, 5.0 ), arr, 'returns expected value' ); |
| 242 | + |
| 243 | + t.end(); |
| 244 | +}); |
0 commit comments