|
| 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 hasOwnProp = require( '@stdlib/assert/has-own-property' ); |
| 25 | +var isFunction = require( '@stdlib/assert/is-function' ); |
| 26 | +var instanceOf = require( '@stdlib/assert/instance-of' ); |
| 27 | +var Uint8Array = require( '@stdlib/array/uint8' ); |
| 28 | +var factory = require( './../lib' ); |
| 29 | + |
| 30 | + |
| 31 | +// TESTS // |
| 32 | + |
| 33 | +tape( 'main export is a function', function test( t ) { |
| 34 | + t.ok( true, __filename ); |
| 35 | + t.strictEqual( typeof factory, 'function', 'main export is a function' ); |
| 36 | + t.end(); |
| 37 | +}); |
| 38 | + |
| 39 | +tape( 'attached to the prototype of the main export is a `toReversed` method', function test( t ) { |
| 40 | + var ctor = factory( 'float64' ); |
| 41 | + t.strictEqual( hasOwnProp( ctor.prototype, 'toReversed' ), true, 'returns expected value' ); |
| 42 | + t.strictEqual( isFunction( ctor.prototype.toReversed ), true, 'returns expected value' ); |
| 43 | + t.end(); |
| 44 | +}); |
| 45 | + |
| 46 | +tape( 'the method throws an error if invoked with a `this` context which is not a typed array instance', function test( t ) { |
| 47 | + var values; |
| 48 | + var ctor; |
| 49 | + var arr; |
| 50 | + var i; |
| 51 | + |
| 52 | + ctor = factory( 'float64' ); |
| 53 | + arr = new ctor( 'little-endian', 5 ); |
| 54 | + |
| 55 | + values = [ |
| 56 | + '5', |
| 57 | + 5, |
| 58 | + NaN, |
| 59 | + true, |
| 60 | + false, |
| 61 | + null, |
| 62 | + void 0, |
| 63 | + {}, |
| 64 | + [], |
| 65 | + function noop() {} |
| 66 | + ]; |
| 67 | + for ( i = 0; i < values.length; i++ ) { |
| 68 | + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); |
| 69 | + } |
| 70 | + |
| 71 | + function badValue( value ) { |
| 72 | + return function badValue() { |
| 73 | + return arr.toReversed.call( value ); |
| 74 | + }; |
| 75 | + } |
| 76 | + t.end(); |
| 77 | +}); |
| 78 | + |
| 79 | +tape( 'the method returns an empty array if operating on an empty typed array', function test( t ) { |
| 80 | + var ctor; |
| 81 | + var arr; |
| 82 | + var out; |
| 83 | + |
| 84 | + ctor = factory( 'float64' ); |
| 85 | + arr = new ctor( 'little-endian' ); |
| 86 | + out = arr.toReversed(); |
| 87 | + |
| 88 | + t.strictEqual(out.length, 0, 'returns expected value'); |
| 89 | + t.end(); |
| 90 | +}); |
| 91 | + |
| 92 | +tape( 'the method returns a new typed array containing elements in reverse order', function test( t ) { |
| 93 | + var expected; |
| 94 | + var ctor; |
| 95 | + var arr; |
| 96 | + var out; |
| 97 | + |
| 98 | + ctor = factory( 'float64' ); |
| 99 | + arr = new ctor( 'little-endian', [ 1.0, 2.0, 3.0, 4.0 ] ); |
| 100 | + expected = new Uint8Array( [ 4.0, 3.0, 2.0, 1.0 ] ); |
| 101 | + out = arr.toReversed(); |
| 102 | + |
| 103 | + t.strictEqual( instanceOf( out, ctor ), true, 'returns expected value' ); |
| 104 | + t.notEqual( out, arr, 'returns a new instance' ); |
| 105 | + t.strictEqual( out.length, expected.length, 'returns expected value' ); |
| 106 | + t.end(); |
| 107 | +}); |
| 108 | + |
| 109 | +tape( 'the method does not change the array length', function test( t ) { |
| 110 | + var ctor; |
| 111 | + var arr; |
| 112 | + var out; |
| 113 | + |
| 114 | + ctor = factory( 'float64' ); |
| 115 | + arr = new ctor( 'little-endian', 10 ); |
| 116 | + out = arr.toReversed(); |
| 117 | + |
| 118 | + t.strictEqual( out.length, 10, 'returns expected value' ); |
| 119 | + t.end(); |
| 120 | +}); |
0 commit comments