|
| 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 Memory = require( '@stdlib/wasm/memory' ); |
| 25 | +var ModuleWrapper = require( '@stdlib/wasm/module-wrapper' ); |
| 26 | +var daxpy = require( './../lib' ); |
| 27 | + |
| 28 | + |
| 29 | +// TESTS // |
| 30 | + |
| 31 | +tape( 'main export is an object', function test( t ) { |
| 32 | + t.ok( true, __filename ); |
| 33 | + t.strictEqual( typeof daxpy, 'object', 'returns expected value' ); |
| 34 | + t.end(); |
| 35 | +}); |
| 36 | + |
| 37 | +tape( 'the `Module` method is a constructor', function test( t ) { |
| 38 | + var mem; |
| 39 | + var mod; |
| 40 | + |
| 41 | + mem = new Memory({ |
| 42 | + 'initial': 0 |
| 43 | + }); |
| 44 | + mod = new daxpy.Module( mem ); |
| 45 | + t.strictEqual( mod instanceof daxpy.Module, true, 'returns expected value' ); |
| 46 | + t.end(); |
| 47 | +}); |
| 48 | + |
| 49 | +tape( 'the `Module` method is a constructor which does not require `new`', function test( t ) { |
| 50 | + var mem; |
| 51 | + var mod; |
| 52 | + |
| 53 | + mem = new Memory({ |
| 54 | + 'initial': 0 |
| 55 | + }); |
| 56 | + mod = daxpy.Module( mem ); // eslint-disable-line new-cap |
| 57 | + t.strictEqual( mod instanceof daxpy.Module, true, 'returns expected value' ); |
| 58 | + t.end(); |
| 59 | +}); |
| 60 | + |
| 61 | +tape( 'the module constructor throws an error if provided a first argument which is not a WebAssembly memory instance (new)', function test( t ) { |
| 62 | + var values; |
| 63 | + var i; |
| 64 | + |
| 65 | + values = [ |
| 66 | + '5', |
| 67 | + 5, |
| 68 | + NaN, |
| 69 | + true, |
| 70 | + false, |
| 71 | + null, |
| 72 | + void 0, |
| 73 | + [], |
| 74 | + {}, |
| 75 | + function noop() {} |
| 76 | + ]; |
| 77 | + for ( i = 0; i < values.length; i++ ) { |
| 78 | + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); |
| 79 | + } |
| 80 | + t.end(); |
| 81 | + |
| 82 | + function badValue( value ) { |
| 83 | + return function badValue() { |
| 84 | + return new daxpy.Module( value ); |
| 85 | + }; |
| 86 | + } |
| 87 | +}); |
| 88 | + |
| 89 | +tape( 'the module constructor throws an error if provided a first argument which is not a WebAssembly memory instance (no new)', function test( t ) { |
| 90 | + var values; |
| 91 | + var i; |
| 92 | + |
| 93 | + values = [ |
| 94 | + '5', |
| 95 | + 5, |
| 96 | + NaN, |
| 97 | + true, |
| 98 | + false, |
| 99 | + null, |
| 100 | + void 0, |
| 101 | + [], |
| 102 | + {}, |
| 103 | + function noop() {} |
| 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 daxpy.Module( value ); // eslint-disable-line new-cap |
| 113 | + }; |
| 114 | + } |
| 115 | +}); |
| 116 | + |
| 117 | +tape( 'the module instance returned by the module constructor inherits from a module wrapper', function test( t ) { |
| 118 | + var mem; |
| 119 | + var mod; |
| 120 | + |
| 121 | + mem = new Memory({ |
| 122 | + 'initial': 0 |
| 123 | + }); |
| 124 | + mod = new daxpy.Module( mem ); |
| 125 | + |
| 126 | + t.strictEqual( mod instanceof ModuleWrapper, true, 'returns expected value' ); |
| 127 | + t.end(); |
| 128 | +}); |
| 129 | + |
| 130 | +tape( 'attached to a module instance is a `main` method', function test( t ) { |
| 131 | + var mem; |
| 132 | + var mod; |
| 133 | + |
| 134 | + mem = new Memory({ |
| 135 | + 'initial': 0 |
| 136 | + }); |
| 137 | + mod = new daxpy.Module( mem ); |
| 138 | + |
| 139 | + t.strictEqual( typeof mod.main, 'function', 'returns expected value' ); |
| 140 | + t.end(); |
| 141 | +}); |
| 142 | + |
| 143 | +tape( 'attached to a module instance is an `ndarray` method', function test( t ) { |
| 144 | + var mem; |
| 145 | + var mod; |
| 146 | + |
| 147 | + mem = new Memory({ |
| 148 | + 'initial': 0 |
| 149 | + }); |
| 150 | + mod = new daxpy.Module( mem ); |
| 151 | + |
| 152 | + t.strictEqual( typeof mod.ndarray, 'function', 'returns expected value' ); |
| 153 | + t.end(); |
| 154 | +}); |
0 commit comments