|
| 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 hasProxySupport = require( '@stdlib/assert/has-proxy-support' ); |
| 25 | +var Int32Array = require( '@stdlib/array/int32' ); |
| 26 | +var Uint8Array = require( '@stdlib/array/uint8' ); |
| 27 | +var array2fancy = require( './../lib' ); |
| 28 | + |
| 29 | + |
| 30 | +// VARIABLES // |
| 31 | + |
| 32 | +var opts = { |
| 33 | + 'skip': !hasProxySupport() |
| 34 | +}; |
| 35 | + |
| 36 | + |
| 37 | +// TESTS // |
| 38 | + |
| 39 | +tape( 'main export is a function', function test( t ) { |
| 40 | + t.ok( true, __filename ); |
| 41 | + t.strictEqual( typeof array2fancy, 'function', 'main export is a function' ); |
| 42 | + t.end(); |
| 43 | +}); |
| 44 | + |
| 45 | +tape( 'the function returns an array-like object supporting mask array indexing (generic)', opts, function test( t ) { |
| 46 | + var expected; |
| 47 | + var actual; |
| 48 | + var idx; |
| 49 | + var x; |
| 50 | + var y; |
| 51 | + |
| 52 | + x = [ 1, 2, 3, 4 ]; |
| 53 | + y = array2fancy( x ); |
| 54 | + |
| 55 | + idx = array2fancy.idx( new Uint8Array( [ 0, 0, 0, 0 ] ) ); |
| 56 | + expected = [ 1, 2, 3, 4 ]; |
| 57 | + actual = y[ idx ]; |
| 58 | + |
| 59 | + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); |
| 60 | + t.notEqual( actual, x, 'different reference' ); |
| 61 | + t.notEqual( actual, y, 'different reference' ); |
| 62 | + t.deepEqual( actual, expected, 'returns expected value' ); |
| 63 | + |
| 64 | + idx = array2fancy.idx( new Uint8Array( [ 1, 1, 1, 1 ] ) ); |
| 65 | + expected = []; |
| 66 | + actual = y[ idx ]; |
| 67 | + |
| 68 | + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); |
| 69 | + t.notEqual( actual, x, 'different reference' ); |
| 70 | + t.notEqual( actual, y, 'different reference' ); |
| 71 | + t.deepEqual( actual, expected, 'returns expected value' ); |
| 72 | + |
| 73 | + idx = array2fancy.idx( new Uint8Array( [ 0, 0, 1, 1 ] ) ); |
| 74 | + expected = [ 1, 2 ]; |
| 75 | + actual = y[ idx ]; |
| 76 | + |
| 77 | + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); |
| 78 | + t.notEqual( actual, x, 'different reference' ); |
| 79 | + t.notEqual( actual, y, 'different reference' ); |
| 80 | + t.deepEqual( actual, expected, 'returns expected value' ); |
| 81 | + |
| 82 | + idx = array2fancy.idx( new Uint8Array( [ 1, 0, 0, 1 ] ) ); |
| 83 | + expected = [ 2, 3 ]; |
| 84 | + actual = y[ idx ]; |
| 85 | + |
| 86 | + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); |
| 87 | + t.notEqual( actual, x, 'different reference' ); |
| 88 | + t.notEqual( actual, y, 'different reference' ); |
| 89 | + t.deepEqual( actual, expected, 'returns expected value' ); |
| 90 | + |
| 91 | + idx = array2fancy.idx( new Uint8Array( [ 0, 1, 0, 1 ] ) ); |
| 92 | + expected = [ 1, 3 ]; |
| 93 | + actual = y[ idx ]; |
| 94 | + |
| 95 | + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); |
| 96 | + t.notEqual( actual, x, 'different reference' ); |
| 97 | + t.notEqual( actual, y, 'different reference' ); |
| 98 | + t.deepEqual( actual, expected, 'returns expected value' ); |
| 99 | + |
| 100 | + t.end(); |
| 101 | +}); |
| 102 | + |
| 103 | +tape( 'the function returns an array-like object supporting mask array indexing (typed)', opts, function test( t ) { |
| 104 | + var expected; |
| 105 | + var actual; |
| 106 | + var idx; |
| 107 | + var x; |
| 108 | + var y; |
| 109 | + |
| 110 | + x = new Int32Array( [ 1, 2, 3, 4 ] ); |
| 111 | + y = array2fancy( x ); |
| 112 | + |
| 113 | + idx = array2fancy.idx( new Uint8Array( [ 0, 0, 0, 0 ] ) ); |
| 114 | + expected = new Int32Array( [ 1, 2, 3, 4 ] ); |
| 115 | + actual = y[ idx ]; |
| 116 | + |
| 117 | + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); |
| 118 | + t.notEqual( actual, x, 'different reference' ); |
| 119 | + t.notEqual( actual, y, 'different reference' ); |
| 120 | + t.deepEqual( actual, expected, 'returns expected value' ); |
| 121 | + |
| 122 | + idx = array2fancy.idx( new Uint8Array( [ 1, 1, 1, 1 ] ) ); |
| 123 | + expected = new Int32Array( [] ); |
| 124 | + actual = y[ idx ]; |
| 125 | + |
| 126 | + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); |
| 127 | + t.notEqual( actual, x, 'different reference' ); |
| 128 | + t.notEqual( actual, y, 'different reference' ); |
| 129 | + t.deepEqual( actual, expected, 'returns expected value' ); |
| 130 | + |
| 131 | + idx = array2fancy.idx( new Uint8Array( [ 0, 0, 1, 1 ] ) ); |
| 132 | + expected = new Int32Array( [ 1, 2 ] ); |
| 133 | + actual = y[ idx ]; |
| 134 | + |
| 135 | + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); |
| 136 | + t.notEqual( actual, x, 'different reference' ); |
| 137 | + t.notEqual( actual, y, 'different reference' ); |
| 138 | + t.deepEqual( actual, expected, 'returns expected value' ); |
| 139 | + |
| 140 | + idx = array2fancy.idx( new Uint8Array( [ 1, 0, 0, 1 ] ) ); |
| 141 | + expected = new Int32Array( [ 2, 3 ] ); |
| 142 | + actual = y[ idx ]; |
| 143 | + |
| 144 | + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); |
| 145 | + t.notEqual( actual, x, 'different reference' ); |
| 146 | + t.notEqual( actual, y, 'different reference' ); |
| 147 | + t.deepEqual( actual, expected, 'returns expected value' ); |
| 148 | + |
| 149 | + idx = array2fancy.idx( new Uint8Array( [ 0, 1, 0, 1 ] ) ); |
| 150 | + expected = new Int32Array( [ 1, 3 ] ); |
| 151 | + actual = y[ idx ]; |
| 152 | + |
| 153 | + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); |
| 154 | + t.notEqual( actual, x, 'different reference' ); |
| 155 | + t.notEqual( actual, y, 'different reference' ); |
| 156 | + t.deepEqual( actual, expected, 'returns expected value' ); |
| 157 | + |
| 158 | + t.end(); |
| 159 | +}); |
| 160 | + |
| 161 | +tape( 'the function returns an array-like object supporting mask array indexing and returning arrays which can themselves support mask arrays (generic)', opts, function test( t ) { |
| 162 | + var expected; |
| 163 | + var actual; |
| 164 | + var idx; |
| 165 | + var x; |
| 166 | + var y; |
| 167 | + var z; |
| 168 | + |
| 169 | + x = [ 1, 2, 3, 4 ]; |
| 170 | + y = array2fancy( x ); |
| 171 | + |
| 172 | + idx = array2fancy.idx( new Uint8Array( [ 0, 0, 0, 0 ] ) ); |
| 173 | + expected = [ 1, 2, 3, 4 ]; |
| 174 | + actual = y[ idx ]; |
| 175 | + |
| 176 | + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); |
| 177 | + t.notEqual( actual, x, 'different reference' ); |
| 178 | + t.notEqual( actual, y, 'different reference' ); |
| 179 | + t.deepEqual( actual, expected, 'returns expected value' ); |
| 180 | + |
| 181 | + z = actual; |
| 182 | + |
| 183 | + idx = array2fancy.idx( new Uint8Array( [ 0, 1, 0, 1 ] ) ); |
| 184 | + expected = [ 1, 3 ]; |
| 185 | + actual = z[ idx ]; |
| 186 | + |
| 187 | + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); |
| 188 | + t.notEqual( actual, x, 'different reference' ); |
| 189 | + t.notEqual( actual, y, 'different reference' ); |
| 190 | + t.notEqual( actual, z, 'different reference' ); |
| 191 | + t.deepEqual( actual, expected, 'returns expected value' ); |
| 192 | + |
| 193 | + z = actual; |
| 194 | + |
| 195 | + idx = array2fancy.idx( new Uint8Array( [ 1, 0 ] ) ); |
| 196 | + expected = [ 3 ]; |
| 197 | + actual = z[ idx ]; |
| 198 | + |
| 199 | + t.strictEqual( actual instanceof Array, true, 'returns expected value' ); |
| 200 | + t.notEqual( actual, x, 'different reference' ); |
| 201 | + t.notEqual( actual, y, 'different reference' ); |
| 202 | + t.notEqual( actual, z, 'different reference' ); |
| 203 | + t.deepEqual( actual, expected, 'returns expected value' ); |
| 204 | + |
| 205 | + t.end(); |
| 206 | +}); |
| 207 | + |
| 208 | +tape( 'the function returns an array-like object supporting mask array indexing and returning arrays which can themselves support mask arrays (typed)', opts, function test( t ) { |
| 209 | + var expected; |
| 210 | + var actual; |
| 211 | + var idx; |
| 212 | + var x; |
| 213 | + var y; |
| 214 | + var z; |
| 215 | + |
| 216 | + x = new Int32Array( [ 1, 2, 3, 4 ] ); |
| 217 | + y = array2fancy( x ); |
| 218 | + |
| 219 | + idx = array2fancy.idx( new Uint8Array( [ 0, 0, 0, 0 ] ) ); |
| 220 | + expected = new Int32Array( [ 1, 2, 3, 4 ] ); |
| 221 | + actual = y[ idx ]; |
| 222 | + |
| 223 | + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); |
| 224 | + t.notEqual( actual, x, 'different reference' ); |
| 225 | + t.notEqual( actual, y, 'different reference' ); |
| 226 | + t.deepEqual( actual, expected, 'returns expected value' ); |
| 227 | + |
| 228 | + z = actual; |
| 229 | + |
| 230 | + idx = array2fancy.idx( new Uint8Array( [ 0, 1, 0, 1 ] ) ); |
| 231 | + expected = new Int32Array( [ 1, 3 ] ); |
| 232 | + actual = z[ idx ]; |
| 233 | + |
| 234 | + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); |
| 235 | + t.notEqual( actual, x, 'different reference' ); |
| 236 | + t.notEqual( actual, y, 'different reference' ); |
| 237 | + t.notEqual( actual, z, 'different reference' ); |
| 238 | + t.deepEqual( actual, expected, 'returns expected value' ); |
| 239 | + |
| 240 | + z = actual; |
| 241 | + |
| 242 | + idx = array2fancy.idx( new Uint8Array( [ 1, 0 ] ) ); |
| 243 | + expected = new Int32Array( [ 3 ] ); |
| 244 | + actual = z[ idx ]; |
| 245 | + |
| 246 | + t.strictEqual( actual instanceof Int32Array, true, 'returns expected value' ); |
| 247 | + t.notEqual( actual, x, 'different reference' ); |
| 248 | + t.notEqual( actual, y, 'different reference' ); |
| 249 | + t.notEqual( actual, z, 'different reference' ); |
| 250 | + t.deepEqual( actual, expected, 'returns expected value' ); |
| 251 | + |
| 252 | + t.end(); |
| 253 | +}); |
| 254 | + |
| 255 | +tape( 'the function returns an array-like object which throws an error if provided a mask array index having an incompatible number of elements (generic)', opts, function test( t ) { |
| 256 | + var values; |
| 257 | + var x; |
| 258 | + var y; |
| 259 | + var i; |
| 260 | + |
| 261 | + x = [ 1, 2, 3, 4 ]; |
| 262 | + y = array2fancy( x ); |
| 263 | + |
| 264 | + values = [ |
| 265 | + array2fancy.idx( new Uint8Array( [ 0, 1 ] ) ), |
| 266 | + array2fancy.idx( new Uint8Array( [ 0, 0, 0 ] ) ), |
| 267 | + array2fancy.idx( new Uint8Array( [ 0, 1, 0, 1, 0 ] ) ) |
| 268 | + ]; |
| 269 | + for ( i = 0; i < values.length; i++ ) { |
| 270 | + t.throws( badValue( values[ i ] ), Error, 'throws an error' ); |
| 271 | + } |
| 272 | + t.end(); |
| 273 | + |
| 274 | + function badValue( value ) { |
| 275 | + return function badValue() { |
| 276 | + var v = y[ value ]; // eslint-disable-line no-unused-vars |
| 277 | + }; |
| 278 | + } |
| 279 | +}); |
| 280 | + |
| 281 | +tape( 'the function returns an array-like object which throws an error if provided a mask array index having an incompatible number of elements (typed)', opts, function test( t ) { |
| 282 | + var values; |
| 283 | + var x; |
| 284 | + var y; |
| 285 | + var i; |
| 286 | + |
| 287 | + x = new Int32Array( [ 1, 2, 3, 4 ] ); |
| 288 | + y = array2fancy( x ); |
| 289 | + |
| 290 | + values = [ |
| 291 | + array2fancy.idx( new Uint8Array( [ 0, 1 ] ) ), |
| 292 | + array2fancy.idx( new Uint8Array( [ 0, 0, 0 ] ) ), |
| 293 | + array2fancy.idx( new Uint8Array( [ 0, 1, 0, 1, 0 ] ) ) |
| 294 | + ]; |
| 295 | + for ( i = 0; i < values.length; i++ ) { |
| 296 | + t.throws( badValue( values[ i ] ), Error, 'throws an error' ); |
| 297 | + } |
| 298 | + t.end(); |
| 299 | + |
| 300 | + function badValue( value ) { |
| 301 | + return function badValue() { |
| 302 | + var v = y[ value ]; // eslint-disable-line no-unused-vars |
| 303 | + }; |
| 304 | + } |
| 305 | +}); |
0 commit comments