|
| 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 Complex128 = require( '@stdlib/complex/float64/ctor' ); |
| 29 | +var real = require( '@stdlib/complex/float64/real' ); |
| 30 | +var imag = require( '@stdlib/complex/float64/imag' ); |
| 31 | +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); |
| 32 | +var unary = 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 unary, 'function', 'main export is a function'); |
| 40 | + t.end(); |
| 41 | +}); |
| 42 | + |
| 43 | +tape( 'the function applies a unary callback to each indexed element of a 0-dimensional ndarray', function test( t ) { |
| 44 | + var expected; |
| 45 | + var x; |
| 46 | + var y; |
| 47 | + |
| 48 | + x = scalar2ndarray( 5.0, { |
| 49 | + 'dtype': 'float64' |
| 50 | + }); |
| 51 | + |
| 52 | + y = scalar2ndarray( 0.0, { |
| 53 | + 'dtype': 'float64' |
| 54 | + }); |
| 55 | + |
| 56 | + unary( [ x, y ], scale ); |
| 57 | + |
| 58 | + expected = new Float64Array( [ 50.0 ] ); |
| 59 | + t.strictEqual( isSameFloat64Array( y.data, expected ), true, 'returns expected value' ); |
| 60 | + t.end(); |
| 61 | + |
| 62 | + function scale( x ) { |
| 63 | + return x * 10.0; |
| 64 | + } |
| 65 | +}); |
| 66 | + |
| 67 | +tape( 'the function applies a unary callback to each indexed element of a 0-dimensional ndarray (accessors)', function test( t ) { |
| 68 | + var expected; |
| 69 | + var x; |
| 70 | + var y; |
| 71 | + |
| 72 | + x = scalar2ndarray( new Complex128( 5.0, 5.0 ), { |
| 73 | + 'dtype': 'complex128' |
| 74 | + }); |
| 75 | + |
| 76 | + y = scalar2ndarray( new Complex128( 0.0, 0.0 ), { |
| 77 | + 'dtype': 'complex128' |
| 78 | + }); |
| 79 | + |
| 80 | + unary( [ x, y ], scale ); |
| 81 | + |
| 82 | + expected = new Complex128Array( [ 50.0, 50.0 ] ); |
| 83 | + t.strictEqual( isSameComplex128Array( y.data, expected ), true, 'returns expected value' ); |
| 84 | + t.end(); |
| 85 | + |
| 86 | + function scale( z ) { |
| 87 | + return new Complex128( real(z)*10.0, imag(z)*10.0 ); |
| 88 | + } |
| 89 | +}); |
0 commit comments