|
1 | 1 | /**
|
2 | 2 | * @license Apache-2.0
|
3 | 3 | *
|
4 |
| -* Copyright (c) 2022 The Stdlib Authors. |
| 4 | +* Copyright (c) 2024 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.
|
|
23 | 23 | var tape = require( 'tape' );
|
24 | 24 | var Complex128Array = require( '@stdlib/array/complex128' );
|
25 | 25 | var Complex64Array = require( '@stdlib/array/complex64' );
|
| 26 | +var BooleanArray = require( '@stdlib/array/bool' ); |
26 | 27 | var Float64Array = require( '@stdlib/array/float64' );
|
27 | 28 | var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
|
28 | 29 | var isSameComplex128Array = require( '@stdlib/assert/is-same-complex128array' );
|
29 | 30 | var isSameComplex64Array = require( '@stdlib/assert/is-same-complex64array' );
|
| 31 | +var isSameBooleanArray = require( '@stdlib/assert/is-same-booleanarray' ); |
30 | 32 | var zeros = require( '@stdlib/array/zeros' );
|
31 | 33 | var take = require( './../lib/assign.js' );
|
32 | 34 |
|
@@ -173,6 +175,50 @@ tape( 'the function takes elements from an array (complex typed array)', functio
|
173 | 175 | t.end();
|
174 | 176 | });
|
175 | 177 |
|
| 178 | +tape( 'the function takes elements from an array (boolean array)', function test( t ) { |
| 179 | + var expected; |
| 180 | + var indices; |
| 181 | + var actual; |
| 182 | + var out; |
| 183 | + var x; |
| 184 | + |
| 185 | + x = new BooleanArray( [ true, false, false, true ] ); |
| 186 | + |
| 187 | + indices = [ 1, 3 ]; |
| 188 | + out = new BooleanArray( indices.length ); |
| 189 | + actual = take( x, indices, 'throw', out, 1, 0 ); |
| 190 | + expected = new BooleanArray( [ false, true ] ); |
| 191 | + |
| 192 | + t.strictEqual( actual, out, 'returns expected value' ); |
| 193 | + t.strictEqual( isSameBooleanArray( actual, expected ), true, 'returns expected value' ); |
| 194 | + |
| 195 | + indices = [ 1, 1, 3, 3 ]; |
| 196 | + out = new BooleanArray( indices.length*2 ); |
| 197 | + actual = take( x, indices, 'throw', out, 2, 0 ); |
| 198 | + expected = new BooleanArray( [ false, false, false, false, true, false, true, false ] ); // eslint-disable-line max-len |
| 199 | + |
| 200 | + t.strictEqual( actual, out, 'returns expected value' ); |
| 201 | + t.strictEqual( isSameBooleanArray( actual, expected ), true, 'returns expected value' ); |
| 202 | + |
| 203 | + indices = [ 3, 2, 1, 0 ]; |
| 204 | + out = new BooleanArray( indices.length ); |
| 205 | + actual = take( x, indices, 'throw', out, -1, out.length-1 ); |
| 206 | + expected = new BooleanArray( [ true, false, false, true ] ); |
| 207 | + |
| 208 | + t.strictEqual( actual, out, 'returns expected value' ); |
| 209 | + t.strictEqual( isSameBooleanArray( actual, expected ), true, 'returns expected value' ); |
| 210 | + |
| 211 | + indices = [ 1, 1, 1, 1 ]; |
| 212 | + out = new BooleanArray( indices.length+1 ); |
| 213 | + actual = take( x, indices, 'throw', out, 1, 1 ); |
| 214 | + expected = new BooleanArray( [ false, false, false, false, false ] ); |
| 215 | + |
| 216 | + t.strictEqual( actual, out, 'returns expected value' ); |
| 217 | + t.strictEqual( isSameBooleanArray( actual, expected ), true, 'returns expected value' ); |
| 218 | + |
| 219 | + t.end(); |
| 220 | +}); |
| 221 | + |
176 | 222 | tape( 'the function takes elements from an array (accessors)', function test( t ) {
|
177 | 223 | var expected;
|
178 | 224 | var indices;
|
@@ -254,6 +300,12 @@ tape( 'the function returns leaves an output array unchanged if provided a secon
|
254 | 300 | actual = take( x, [], 'throw', out, 1, 0 );
|
255 | 301 | t.strictEqual( isSameComplex128Array( actual, expected ), true, 'returns expected value' );
|
256 | 302 |
|
| 303 | + x = new BooleanArray( [ true, false, false, true ] ); |
| 304 | + out = new BooleanArray( [ false, false, false, false ] ); |
| 305 | + expected = new BooleanArray( [ false, false, false, false ] ); |
| 306 | + actual = take( x, [], 'throw', out, 1, 0 ); |
| 307 | + t.strictEqual( isSameBooleanArray( actual, expected ), true, 'returns expected value' ); |
| 308 | + |
257 | 309 | t.end();
|
258 | 310 | });
|
259 | 311 |
|
@@ -325,6 +377,23 @@ tape( 'when the "mode" is "throw", the function throws an error if provided an o
|
325 | 377 | }
|
326 | 378 | });
|
327 | 379 |
|
| 380 | +tape( 'when the "mode" is "throw", the function throws an error if provided an out-of-bounds index (boolean)', function test( t ) { |
| 381 | + var indices; |
| 382 | + var out; |
| 383 | + var x; |
| 384 | + |
| 385 | + x = new BooleanArray( [ true, false ] ); |
| 386 | + indices = [ 4, 5, 1, 2 ]; |
| 387 | + out = new BooleanArray( x.length ); |
| 388 | + |
| 389 | + t.throws( badValue, RangeError, 'throws an error' ); |
| 390 | + t.end(); |
| 391 | + |
| 392 | + function badValue() { |
| 393 | + take( x, indices, 'throw', out, 1, 0 ); |
| 394 | + } |
| 395 | +}); |
| 396 | + |
328 | 397 | tape( 'when the "mode" is "normalize", the function normalizes negative indices (generic)', function test( t ) {
|
329 | 398 | var expected;
|
330 | 399 | var indices;
|
|
0 commit comments