|
| 1 | +/** |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2024 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 hasOwnProp = require( '@stdlib/assert/has-own-property' ); |
| 25 | +var isFunction = require( '@stdlib/assert/is-function' ); |
| 26 | +var factory = require( './../lib' ); |
| 27 | + |
| 28 | + |
| 29 | +// TESTS // |
| 30 | + |
| 31 | +tape( 'main export is a function', function test( t ) { |
| 32 | + t.ok( true, __filename ); |
| 33 | + t.strictEqual( typeof factory, 'function', 'main export is a function' ); |
| 34 | + t.end(); |
| 35 | +}); |
| 36 | + |
| 37 | +tape( 'the function returns a function', function test( t ) { |
| 38 | + var ctor = factory( 'float64' ); |
| 39 | + t.strictEqual( isFunction( ctor ), true, 'returns expected value' ); |
| 40 | + t.end(); |
| 41 | +}); |
| 42 | + |
| 43 | +tape( 'attached to the prototype of the returned function is a `with` method', function test( t ) { |
| 44 | + var ctor = factory( 'float64' ); |
| 45 | + t.strictEqual( hasOwnProp( ctor.prototype, 'with' ), true, 'returns expected value' ); |
| 46 | + t.strictEqual( isFunction( ctor.prototype.with ), true, 'returns expected value' ); |
| 47 | + t.end(); |
| 48 | +}); |
| 49 | + |
| 50 | +tape( 'the method throws an error if invoked with a `this` context which is not a typed array instance', function test( t ) { |
| 51 | + var values; |
| 52 | + var ctor; |
| 53 | + var arr; |
| 54 | + var i; |
| 55 | + |
| 56 | + ctor = factory( 'float64' ); |
| 57 | + arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); |
| 58 | + |
| 59 | + values = [ |
| 60 | + '5', |
| 61 | + 5, |
| 62 | + NaN, |
| 63 | + true, |
| 64 | + false, |
| 65 | + null, |
| 66 | + void 0, |
| 67 | + {}, |
| 68 | + [], |
| 69 | + function noop() {} |
| 70 | + ]; |
| 71 | + for ( i = 0; i < values.length; i++ ) { |
| 72 | + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); |
| 73 | + } |
| 74 | + t.end(); |
| 75 | + |
| 76 | + function badValue( value ) { |
| 77 | + return function badValue() { |
| 78 | + return arr.with.call( value, 0, 0.0 ); |
| 79 | + }; |
| 80 | + } |
| 81 | +}); |
| 82 | + |
| 83 | +tape( 'the method throws an error if provided a first argument which is not an integer', function test( t ) { |
| 84 | + var values; |
| 85 | + var ctor; |
| 86 | + var arr; |
| 87 | + var i; |
| 88 | + |
| 89 | + ctor = factory( 'float64' ); |
| 90 | + arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); |
| 91 | + |
| 92 | + values = [ |
| 93 | + '5', |
| 94 | + 3.14, |
| 95 | + NaN, |
| 96 | + true, |
| 97 | + false, |
| 98 | + null, |
| 99 | + void 0, |
| 100 | + {}, |
| 101 | + [], |
| 102 | + function noop() {} |
| 103 | + ]; |
| 104 | + |
| 105 | + for ( i = 0; i < values.length; i++ ) { |
| 106 | + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); |
| 107 | + } |
| 108 | + t.end(); |
| 109 | + |
| 110 | + function badValue( value ) { |
| 111 | + return function badValue() { |
| 112 | + return arr.with( value, 0.0 ); |
| 113 | + }; |
| 114 | + } |
| 115 | +}); |
| 116 | + |
| 117 | +tape( 'the method throws an error if provided a first argument which is not in bounds', function test( t ) { |
| 118 | + var values; |
| 119 | + var ctor; |
| 120 | + var arr; |
| 121 | + var i; |
| 122 | + |
| 123 | + ctor = factory( 'float64' ); |
| 124 | + arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); |
| 125 | + |
| 126 | + values = [ |
| 127 | + -6, |
| 128 | + -9, |
| 129 | + 10, |
| 130 | + 20 |
| 131 | + ]; |
| 132 | + |
| 133 | + for ( i = 0; i < values.length; i++ ) { |
| 134 | + t.throws( badValue( values[i] ), RangeError, 'throws an error when provided '+values[i] ); |
| 135 | + } |
| 136 | + t.end(); |
| 137 | + |
| 138 | + function badValue( value ) { |
| 139 | + return function badValue() { |
| 140 | + return arr.with( value, 0.0 ); |
| 141 | + }; |
| 142 | + } |
| 143 | +}); |
| 144 | + |
| 145 | +tape( 'the method throws an error if provided a second argument which is not a number', function test( t ) { |
| 146 | + var values; |
| 147 | + var ctor; |
| 148 | + var arr; |
| 149 | + var i; |
| 150 | + |
| 151 | + ctor = factory( 'float64' ); |
| 152 | + arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); |
| 153 | + |
| 154 | + values = [ |
| 155 | + '5', |
| 156 | + true, |
| 157 | + false, |
| 158 | + null, |
| 159 | + void 0, |
| 160 | + {}, |
| 161 | + [], |
| 162 | + function noop() {} |
| 163 | + ]; |
| 164 | + |
| 165 | + for ( i = 0; i < values.length; i++ ) { |
| 166 | + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); |
| 167 | + } |
| 168 | + t.end(); |
| 169 | + |
| 170 | + function badValue( value ) { |
| 171 | + return function badValue() { |
| 172 | + return arr.with( 0, value ); |
| 173 | + }; |
| 174 | + } |
| 175 | +}); |
| 176 | + |
| 177 | +tape( 'the method does not change the array length', function test( t ) { |
| 178 | + var ctor; |
| 179 | + var arr; |
| 180 | + var out; |
| 181 | + |
| 182 | + ctor = factory( 'float64' ); |
| 183 | + arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); |
| 184 | + out = arr.with( 0, 4.0 ); |
| 185 | + |
| 186 | + t.strictEqual( out.length, 5, 'returns expected value' ); |
| 187 | + t.end(); |
| 188 | +}); |
| 189 | + |
| 190 | +tape( 'the method returns a new boolean array with the element at a provided index replaced with a provided value', function test( t ) { |
| 191 | + var expected; |
| 192 | + var actual; |
| 193 | + var ctor; |
| 194 | + var arr; |
| 195 | + |
| 196 | + ctor = factory( 'float64' ); |
| 197 | + arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); |
| 198 | + expected = new ctor( 'little-endian', [ 0.0, 2.0, 3.0, 4.0, 5.0 ] ); |
| 199 | + actual = arr.with( 0, 0.0 ); |
| 200 | + t.strictEqual( actual instanceof ctor, true, 'returns expected value' ); |
| 201 | + t.deepEqual( actual, expected, 'returns expected value' ); |
| 202 | + t.notEqual( actual, arr, 'returns new instance' ); |
| 203 | + t.end(); |
| 204 | +}); |
| 205 | + |
| 206 | +tape( 'the method supports negative indices', function test( t ) { |
| 207 | + var expected; |
| 208 | + var actual; |
| 209 | + var ctor; |
| 210 | + var arr; |
| 211 | + |
| 212 | + ctor = factory( 'float64' ); |
| 213 | + arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); |
| 214 | + expected = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0, 0.0 ] ); |
| 215 | + actual = arr.with( -5, 0.0 ); |
| 216 | + t.strictEqual( actual instanceof ctor, true, 'returns expected value' ); |
| 217 | + t.deepEqual( actual, expected, 'returns expected value' ); |
| 218 | + t.notEqual( actual, arr, 'returns new instance' ); |
| 219 | + t.end(); |
| 220 | +}); |
0 commit comments