|
| 1 | +/** |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2025 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 isSameComplex128Array = require( '@stdlib/assert/is-same-complex128array' ); |
| 25 | +var isSameFloat64Array = require( '@stdlib/assert/is-same-float64array' ); |
| 26 | +var Complex128Array = require( '@stdlib/array/complex128' ); |
| 27 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 28 | +var zeros = require( '@stdlib/array/zeros' ); |
| 29 | +var ndarray = require( '@stdlib/ndarray/ctor' ); |
| 30 | +var add = require( '@stdlib/number/float64/base/add' ); |
| 31 | +var zadd = require( '@stdlib/complex/float64/base/add' ); |
| 32 | +var binary = require( './../lib' ); |
| 33 | + |
| 34 | + |
| 35 | +// TESTS // |
| 36 | + |
| 37 | +tape( 'main export is a function', function test( t ) { |
| 38 | + t.ok( true, __filename ); |
| 39 | + t.strictEqual( typeof binary, 'function', 'main export is a function'); |
| 40 | + t.end(); |
| 41 | +}); |
| 42 | + |
| 43 | +tape( 'the function applies a binary callback to indexed elements of two 1-dimensional ndarrays', function test( t ) { |
| 44 | + var expected; |
| 45 | + var x; |
| 46 | + var y; |
| 47 | + |
| 48 | + x = ndarray( 'float64', new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ), [ 4 ], [ 1 ], 0, 'row-major' ); |
| 49 | + y = ndarray( 'float64', zeros( 4, 'float64' ), [ 4 ], [ 1 ], 0, 'row-major' ); |
| 50 | + |
| 51 | + binary( [ x, x, y ], add ); |
| 52 | + |
| 53 | + expected = new Float64Array([ |
| 54 | + 2.0, |
| 55 | + 4.0, |
| 56 | + 6.0, |
| 57 | + 8.0 |
| 58 | + ]); |
| 59 | + t.strictEqual( isSameFloat64Array( y.data, expected ), true, 'returns expected value' ); |
| 60 | + t.end(); |
| 61 | +}); |
| 62 | + |
| 63 | +tape( 'the function applies a binary callback to indexed elements of two 1-dimensional ndarrays (empty arrays)', function test( t ) { |
| 64 | + var expected; |
| 65 | + var x; |
| 66 | + var y; |
| 67 | + |
| 68 | + x = ndarray( 'float64', new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ), [ 0 ], [ 1 ], 0, 'row-major' ); |
| 69 | + y = ndarray( 'float64', zeros( 4, 'float64' ), [ 0 ], [ 1 ], 0, 'row-major' ); |
| 70 | + |
| 71 | + binary( [ x, x, y ], add ); |
| 72 | + |
| 73 | + expected = new Float64Array([ |
| 74 | + 0.0, |
| 75 | + 0.0, |
| 76 | + 0.0, |
| 77 | + 0.0 |
| 78 | + ]); |
| 79 | + t.strictEqual( isSameFloat64Array( y.data, expected ), true, 'returns expected value' ); |
| 80 | + t.end(); |
| 81 | +}); |
| 82 | + |
| 83 | +tape( 'the function applies a binary callback to indexed elements of two 1-dimensional ndarrays (accessors)', function test( t ) { |
| 84 | + var expected; |
| 85 | + var x; |
| 86 | + var y; |
| 87 | + |
| 88 | + x = ndarray( 'complex128', new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ), [ 4 ], [ 1 ], 0, 'row-major' ); |
| 89 | + y = ndarray( 'complex128', zeros( 4, 'complex128' ), [ 4 ], [ 1 ], 0, 'row-major' ); |
| 90 | + |
| 91 | + binary( [ x, x, y ], zadd ); |
| 92 | + |
| 93 | + expected = new Complex128Array([ |
| 94 | + 2.0, |
| 95 | + 4.0, |
| 96 | + 6.0, |
| 97 | + 8.0, |
| 98 | + 10.0, |
| 99 | + 12.0, |
| 100 | + 14.0, |
| 101 | + 16.0 |
| 102 | + ]); |
| 103 | + t.strictEqual( isSameComplex128Array( y.data, expected ), true, 'returns expected value' ); |
| 104 | + t.end(); |
| 105 | +}); |
0 commit comments