From 51406ec97d27c9973bb45a6b0bbad45b8cc9db29 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Tue, 19 Sep 2023 19:57:40 +0000 Subject: [PATCH] Auto-generated commit --- ctor/docs/types/index.d.ts | 6 +- fancy/README.md | 2 +- fancy/docs/repl.txt | 485 ++++++++++++ fancy/docs/types/index.d.ts | 160 ++++ fancy/docs/types/test.ts | 203 +++++ fancy/test/test.argument_validation.js | 1000 ++++++++++++++++++++++++ fancy/test/test.js | 215 +++++ 7 files changed, 2067 insertions(+), 4 deletions(-) create mode 100644 fancy/docs/repl.txt create mode 100644 fancy/docs/types/index.d.ts create mode 100644 fancy/docs/types/test.ts create mode 100644 fancy/test/test.argument_validation.js create mode 100644 fancy/test/test.js diff --git a/ctor/docs/types/index.d.ts b/ctor/docs/types/index.d.ts index d1067cf2..e497a3f9 100644 --- a/ctor/docs/types/index.d.ts +++ b/ctor/docs/types/index.d.ts @@ -20,7 +20,7 @@ /// -import { ArrayLike } from '@stdlib/types/array'; +import { Collection } from '@stdlib/types/array'; import { ndarray, DataType, Mode, Order, Shape, Strides } from '@stdlib/types/ndarray'; import { Buffer } from 'buffer'; @@ -81,7 +81,7 @@ interface Constructor { * * var out = new ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); */ - new( dtype: DataType, buffer: ArrayLike | Buffer, shape: Shape, strides: Strides, offset: number, order: Order, options?: Options ): ndarray; // tslint-disable-line max-line-length + new ( dtype: DataType, buffer: Collection | Buffer, shape: Shape, strides: Strides, offset: number, order: Order, options?: Options ): ndarray; /** * ndarray constructor. @@ -116,7 +116,7 @@ interface Constructor { * * var out = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); */ - ( dtype: DataType, buffer: ArrayLike | Buffer, shape: Shape, strides: Strides, offset: number, order: Order, options?: Options ): ndarray; // tslint-disable-line max-line-length + ( dtype: DataType, buffer: Collection | Buffer, shape: Shape, strides: Strides, offset: number, order: Order, options?: Options ): ndarray; } /** diff --git a/fancy/README.md b/fancy/README.md index 6a93e088..21daeeed 100644 --- a/fancy/README.md +++ b/fancy/README.md @@ -75,7 +75,7 @@ var FancyArray = require( '@stdlib/ndarray/fancy' ); -#### new FancyArray( dtype, buffer, shape, strides, offset, order\[, options] ) +#### FancyArray( dtype, buffer, shape, strides, offset, order\[, options] ) Returns a `FancyArray` instance. diff --git a/fancy/docs/repl.txt b/fancy/docs/repl.txt new file mode 100644 index 00000000..cee8acae --- /dev/null +++ b/fancy/docs/repl.txt @@ -0,0 +1,485 @@ + +{{alias}}( dtype, buffer, shape, strides, offset, order[, options] ) + Returns a FancyArray instance. + + A FancyArray is an ndarray which supports slicing via indexing expressions. + + A FancyArray is an ndarray instance and supports all ndarray options, + attributes, and methods. A FancyArray can be consumed by any API which + supports ndarray instances. + + Indexing expressions provide a convenient and powerful means for creating + and operating on ndarray views; however, their use does entail a performance + cost. Indexing expressions are best suited for interactive use and + scripting. For performance critical applications, prefer equivalent + functional APIs supporting ndarray instances. + + In older JavaScript environments which do not support Proxy objects, the use + of indexing expressions is not supported. + + Parameters + ---------- + dtype: string + Underlying data type. + + buffer: ArrayLikeObject|TypedArray|Buffer + Data buffer. A data buffer must be an array-like object (i.e., have a + `length` property). For data buffers which are not indexed collections + (i.e., collections which cannot support direct index access, such as + `buffer[ index ]`; e.g., Complex64Array, Complex128Array, etc), a data + buffer should provide `#.get( idx )` and `#.set( v[, idx] )` methods. + Note that, for `set` methods, the value to set should be the first + argument, followed by the linear index, similar to the native typed + array `set` method. + + shape: ArrayLikeObject + Array shape. + + strides: ArrayLikeObject + Array strides. + + offset: integer + Index offset. + + order: string + Specifies whether an array is row-major (C-style) or column-major + (Fortran-style). + + options: Object (optional) + Options. + + options.mode: string (optional) + Specifies how to handle indices which exceed array dimensions. If equal + to 'throw', an ndarray instance throws an error when an index exceeds + array dimensions. If equal to 'wrap', an ndarray instance wraps around + indices exceeding array dimensions using modulo arithmetic. If equal to + 'clamp', an ndarray instance sets an index exceeding array dimensions to + either `0` (minimum index) or the maximum index. Default: 'throw'. + + options.submode: Array (optional) + Specifies how to handle subscripts which exceed array dimensions. If a + mode for a corresponding dimension is equal to 'throw', an ndarray + instance throws an error when a subscript exceeds array dimensions. If + equal to 'wrap', an ndarray instance wraps around subscripts exceeding + array dimensions using modulo arithmetic. If equal to 'clamp', an + ndarray instance sets a subscript exceeding array dimensions to either + `0` (minimum index) or the maximum index. If the number of modes is + fewer than the number of dimensions, the function recycles modes using + modulo arithmetic. Default: [ options.mode ]. + + options.readonly: boolean (optional) + Boolean indicating whether an array should be read-only. Default: false. + + Returns + ------- + ndarray: FancyArray + FancyArray instance. + + Examples + -------- + // Create a new instance... + > var b = [ 1.0, 2.0, 3.0, 4.0 ]; // underlying data buffer + > var d = [ 2, 2 ]; // shape + > var s = [ 2, 1 ]; // strides + > var o = 0; // index offset + > var arr = {{alias}}( 'generic', b, d, s, o, 'row-major' ) + + + // Get an element using subscripts: + > var v = arr.get( 1, 1 ) + 4.0 + + // Get an element using a linear index: + > v = arr.iget( 3 ) + 4.0 + + // Set an element using subscripts: + > arr.set( 1, 1, 40.0 ); + > arr.get( 1, 1 ) + 40.0 + + // Set an element using a linear index: + > arr.iset( 3, 99.0 ); + > arr.get( 1, 1 ) + 99.0 + + +{{alias}}.prototype.byteLength + Size (in bytes) of the array (if known). + + Returns + ------- + size: integer|null + Size (in bytes) of the array. + + Examples + -------- + > var b = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + > var d = [ 2, 2 ]; + > var s = [ 2, 1 ]; + > var o = 0; + > var arr = {{alias}}( 'float64', b, d, s, o, 'row-major' ); + > var sz = arr.byteLength + 32 + + +{{alias}}.prototype.BYTES_PER_ELEMENT + Size (in bytes) of each array element (if known). + + Returns + ------- + size: integer|null + Size (in bytes) of each array element. + + Examples + -------- + > var b = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + > var d = [ 2, 2 ]; + > var s = [ 2, 1 ]; + > var o = 0; + > var arr = {{alias}}( 'float64', b, d, s, o, 'row-major' ); + > var sz = arr.BYTES_PER_ELEMENT + 8 + + +{{alias}}.prototype.data + Pointer to the underlying data buffer. + + Returns + ------- + buf: ArrayLikeObject|TypedArray|Buffer + Underlying data buffer. + + Examples + -------- + > var b = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + > var d = [ 2, 2 ]; + > var s = [ 2, 1 ]; + > var o = 0; + > var arr = {{alias}}( 'float64', b, d, s, o, 'row-major' ); + > var buf = arr.data + [ 1.0, 2.0, 3.0, 4.0 ] + + +{{alias}}.prototype.dtype + Underlying data type. + + Returns + ------- + dtype: string + Underlying data type. + + Examples + -------- + > var b = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + > var d = [ 2, 2 ]; + > var s = [ 2, 1 ]; + > var o = 0; + > var arr = {{alias}}( 'float64', b, d, s, o, 'row-major' ); + > var dt = arr.dtype + 'float64' + + +{{alias}}.prototype.flags + Meta information, such as information concerning the memory layout of the + array. + + Returns + ------- + flags: Object + Info object. + + Examples + -------- + > var b = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + > var d = [ 2, 2 ]; + > var s = [ 2, 1 ]; + > var o = 0; + > var arr = {{alias}}( 'float64', b, d, s, o, 'row-major' ); + > var fl = arr.flags + {...} + + +{{alias}}.prototype.length + Length of the array (i.e., number of elements). + + Returns + ------- + len: integer + Array length. + + Examples + -------- + > var b = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + > var d = [ 2, 2 ]; + > var s = [ 2, 1 ]; + > var o = 0; + > var arr = {{alias}}( 'float64', b, d, s, o, 'row-major' ); + > var len = arr.length + 4 + + +{{alias}}.prototype.ndims + Number of dimensions. + + Returns + ------- + ndims: integer + Number of dimensions. + + Examples + -------- + > var b = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + > var d = [ 2, 2 ]; + > var s = [ 2, 1 ]; + > var o = 0; + > var arr = {{alias}}( 'float64', b, d, s, o, 'row-major' ); + > var n = arr.ndims + 2 + + +{{alias}}.prototype.offset + Index offset which specifies the buffer index at which to start iterating + over array elements. + + Returns + ------- + offset: integer + Index offset. + + Examples + -------- + > var b = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + > var d = [ 2, 2 ]; + > var s = [ 2, 1 ]; + > var o = 0; + > var arr = {{alias}}( 'float64', b, d, s, o, 'row-major' ); + > var v = arr.offset + 0 + + +{{alias}}.prototype.order + Array order. + + The array order is either row-major (C-style) or column-major (Fortran- + style). + + Returns + ------- + order: string + Array order. + + Examples + -------- + > var b = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + > var d = [ 2, 2 ]; + > var s = [ 2, 1 ]; + > var o = 0; + > var arr = {{alias}}( 'float64', b, d, s, o, 'row-major' ); + > var ord = arr.order + 'row-major' + + +{{alias}}.prototype.shape + Array shape. + + Returns + ------- + shape: Array + Array shape. + + Examples + -------- + > var b = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + > var d = [ 2, 2 ]; + > var s = [ 2, 1 ]; + > var o = 0; + > var arr = {{alias}}( 'float64', b, d, s, o, 'row-major' ); + > var sh = arr.shape + [ 2, 2 ] + + +{{alias}}.prototype.strides + Index strides which specify how to access data along corresponding array + dimensions. + + Returns + ------- + strides: Array + Index strides. + + Examples + -------- + > var b = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + > var d = [ 2, 2 ]; + > var s = [ 2, 1 ]; + > var o = 0; + > var arr = {{alias}}( 'float64', b, d, s, o, 'row-major' ); + > var st = arr.strides + [ 2, 1 ] + + +{{alias}}.prototype.get( ...idx ) + Returns an array element specified according to provided subscripts. + + The number of provided subscripts should equal the number of dimensions. + + For zero-dimensional arrays, no indices should be provided. + + Parameters + ---------- + idx: ...integer + Subscripts. + + Returns + ------- + out: any + Array element. + + Examples + -------- + > var b = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + > var d = [ 2, 2 ]; + > var s = [ 2, 1 ]; + > var o = 0; + > var arr = {{alias}}( 'float64', b, d, s, o, 'row-major' ); + > var v = arr.get( 1, 1 ) + 4.0 + + +{{alias}}.prototype.iget( idx ) + Returns an array element located at a specified linear index. + + For zero-dimensional arrays, the input argument is ignored and, for clarity, + should not be provided. + + Parameters + ---------- + idx: integer + Linear index. + + Returns + ------- + out: any + Array element. + + Examples + -------- + > var b = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + > var d = [ 2, 2 ]; + > var s = [ 2, 1 ]; + > var o = 0; + > var arr = {{alias}}( 'float64', b, d, s, o, 'row-major' ); + > var v = arr.iget( 3 ) + 4.0 + + +{{alias}}.prototype.set( ...idx, v ) + Sets an array element specified according to provided subscripts. + + The number of provided subscripts should equal the number of dimensions. + + For zero-dimensional arrays, no indices should be provided. + + Parameters + ---------- + idx: ...integer + Subscripts. + + v: any + Value to set. + + Returns + ------- + out: FancyArray + FancyArray instance. + + Examples + -------- + > var b = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + > var d = [ 2, 2 ]; + > var s = [ 2, 1 ]; + > var o = 0; + > var arr = {{alias}}( 'float64', b, d, s, o, 'row-major' ); + > arr.set( 1, 1, -4.0 ); + > arr.get( 1, 1 ) + -4.0 + + +{{alias}}.prototype.iset( idx, v ) + Sets an array element located at a specified linear index. + + For zero-dimensional arrays, the first, and only, argument should be the + value to set. + + Parameters + ---------- + idx: integer + Linear index. + + v: any + Value to set. + + Returns + ------- + out: FancyArray + FancyArray instance. + + Examples + -------- + > var b = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + > var d = [ 2, 2 ]; + > var s = [ 2, 1 ]; + > var o = 0; + > var arr = {{alias}}( 'float64', b, d, s, o, 'row-major' ); + > arr.iset( 3, -4.0 ); + > arr.iget( 3 ) + -4.0 + + +{{alias}}.prototype.toString() + Serializes a FancyArray as a string. + + This method does **not** serialize data outside of the buffer region defined + by the array configuration. + + Returns + ------- + str: string + Serialized ndarray string. + + Examples + -------- + > var b = [ 1, 2, 3, 4 ]; + > var d = [ 2, 2 ]; + > var s = [ 2, 1 ]; + > var o = 0; + > var arr = {{alias}}( 'generic', b, d, s, o, 'row-major' ); + > arr.toString() + '...' + + +{{alias}}.prototype.toJSON() + Serializes a FancyArray as a JSON object. + + This method does **not** serialize data outside of the buffer region defined + by the array configuration. + + Returns + ------- + obj: Object + JSON object. + + Examples + -------- + > var b = [ 1, 2, 3, 4 ]; + > var d = [ 2, 2 ]; + > var s = [ 2, 1 ]; + > var o = 0; + > var arr = {{alias}}( 'generic', b, d, s, o, 'row-major' ); + > arr.toJSON() + {...} + + See Also + -------- + diff --git a/fancy/docs/types/index.d.ts b/fancy/docs/types/index.d.ts new file mode 100644 index 00000000..4096b768 --- /dev/null +++ b/fancy/docs/types/index.d.ts @@ -0,0 +1,160 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/// + +import { Collection } from '@stdlib/types/array'; +import { ndarray, DataType, Mode, Order, Shape, Strides } from '@stdlib/types/ndarray'; +import { Buffer } from 'buffer'; + +/** +* Interface defining function options. +*/ +interface Options { + /** + * Specifies how to handle a linear index which exceeds array dimensions (default: 'throw'). + */ + mode?: Mode; + + /** + * Specifies how to handle subscripts which exceed array dimensions on a per dimension basis (default: ['throw']). + */ + submode?: Array; + + /** + * Boolean indicating whether an array should be read-only (default: false). + */ + readonly?: boolean; +} + +/** +* Interface defining a FancyArray constructor which is both "newable" and "callable". +*/ +interface Constructor { + /** + * Fancy array constructor. + * + * @param dtype - data type + * @param buffer - data buffer + * @param shape - array shape + * @param strides - array strides + * @param offset - index offset + * @param order - specifies whether an array is row-major (C-style) or column-major (Fortran-style) + * @param options - function options + * @param options.mode - specifies how to handle indices which exceed array dimensions (default: 'throw') + * @param options.submode - specifies how to handle subscripts which exceed array dimensions on a per dimension basis (default: ['throw']) + * @param options.readonly - specifies whether an array should be read-only (default: false) + * @throws `buffer` argument `get` and `set` properties must be functions + * @throws `shape` argument must be an array-like object containing nonnegative integers + * @throws `shape` argument length must equal the number of dimensions + * @throws `strides` argument must be an array-like object containing integers + * @throws `strides` argument length must equal the number of dimensions (except for zero-dimensional arrays; in which case, the `strides` argument length must be equal to `1`) + * @throws for zero-dimensional ndarrays, the `strides` argument must contain a single element equal to `0` + * @throws `offset` argument must be a nonnegative integer + * @throws `buffer` argument must be compatible with specified meta data + * @throws must provide valid options + * @throws too many dimensions + * @returns an ndarray + * + * @example + * var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + * var shape = [ 3, 2 ]; + * var strides = [ 2, 1 ]; + * var offset = 0; + * + * var out = new FancyArray( 'generic', buffer, shape, strides, offset, 'row-major' ); + */ + new ( dtype: DataType, buffer: Collection | Buffer, shape: Shape, strides: Strides, offset: number, order: Order, options?: Options ): ndarray; + + /** + * Fancy array constructor. + * + * @param dtype - data type + * @param buffer - data buffer + * @param shape - array shape + * @param strides - array strides + * @param offset - index offset + * @param order - specifies whether an array is row-major (C-style) or column-major (Fortran-style) + * @param options - function options + * @param options.mode - specifies how to handle indices which exceed array dimensions (default: 'throw') + * @param options.submode - specifies how to handle subscripts which exceed array dimensions on a per dimension basis (default: ['throw']) + * @param options.readonly - specifies whether an array should be read-only (default: false) + * @throws `buffer` argument `get` and `set` properties must be functions + * @throws `shape` argument must be an array-like object containing nonnegative integers + * @throws `shape` argument length must equal the number of dimensions + * @throws `strides` argument must be an array-like object containing integers + * @throws `strides` argument length must equal the number of dimensions (except for zero-dimensional arrays; in which case, the `strides` argument length must be equal to `1`) + * @throws for zero-dimensional ndarrays, the `strides` argument must contain a single element equal to `0` + * @throws `offset` argument must be a nonnegative integer + * @throws `buffer` argument must be compatible with specified meta data + * @throws must provide valid options + * @throws too many dimensions + * @returns an ndarray + * + * @example + * var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + * var shape = [ 3, 2 ]; + * var strides = [ 2, 1 ]; + * var offset = 0; + * + * var out = FancyArray( 'generic', buffer, shape, strides, offset, 'row-major' ); + */ + ( dtype: DataType, buffer: Collection | Buffer, shape: Shape, strides: Strides, offset: number, order: Order, options?: Options ): ndarray; +} + +/** +* Fancy array constructor. +* +* @param dtype - data type +* @param buffer - data buffer +* @param shape - array shape +* @param strides - array strides +* @param offset - index offset +* @param order - specifies whether an array is row-major (C-style) or column-major (Fortran-style) +* @param options - function options +* @param options.mode - specifies how to handle indices which exceed array dimensions (default: 'throw') +* @param options.submode - specifies how to handle subscripts which exceed array dimensions on a per dimension basis (default: ['throw']) +* @param options.readonly - specifies whether an array should be read-only (default: false) +* @throws `buffer` argument `get` and `set` properties must be functions +* @throws `shape` argument must be an array-like object containing nonnegative integers +* @throws `shape` argument length must equal the number of dimensions +* @throws `strides` argument must be an array-like object containing integers +* @throws `strides` argument length must equal the number of dimensions (except for zero-dimensional arrays; in which case, the `strides` argument length must be equal to `1`) +* @throws for zero-dimensional ndarrays, the `strides` argument must contain a single element equal to `0` +* @throws `offset` argument must be a nonnegative integer +* @throws `buffer` argument must be compatible with specified meta data +* @throws must provide valid options +* @throws too many dimensions +* @returns an ndarray +* +* @example +* var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; +* var shape = [ 3, 2 ]; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var out = new FancyArray( 'generic', buffer, shape, strides, offset, 'row-major' ); +*/ +declare var ctor: Constructor; + + +// EXPORTS // + +export = ctor; diff --git a/fancy/docs/types/test.ts b/fancy/docs/types/test.ts new file mode 100644 index 00000000..506ce7bb --- /dev/null +++ b/fancy/docs/types/test.ts @@ -0,0 +1,203 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2021 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import FancyArray = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const shape = [ 2, 2 ]; + const strides = [ 2, 1 ]; + + let x = FancyArray( 'float64', [ 1, 2, 3, 4 ], shape, strides, 0, 'row-major' ); // $ExpectType ndarray + x = new FancyArray( 'float64', [ 1, 2, 3, 4 ], shape, strides, 0, 'row-major' ); // $ExpectType ndarray + + const buffer = new Int32Array( [ 1, 2, 3 ] ); + const order = 'column-major'; + + x = FancyArray( 'int32', buffer, shape, strides, 0, order ); // $ExpectType ndarray + x = new FancyArray( 'int32', buffer, shape, strides, 0, order ); // $ExpectType ndarray + + x = FancyArray( 'int32', buffer, shape, strides, 0, order, { 'mode': 'clamp' } ); // $ExpectType ndarray + x = new FancyArray( 'int32', buffer, shape, strides, 0, order, { 'mode': 'clamp' } ); // $ExpectType ndarray + + x = FancyArray( 'int32', buffer, shape, strides, 0, order, { 'submode': [ 'throw' ] } ); // $ExpectType ndarray + x = new FancyArray( 'int32', buffer, shape, strides, 0, order, { 'submode': [ 'throw' ] } ); // $ExpectType ndarray + + if ( x.shape[ 0 ] !== x.shape[ 0 ] ) { + throw new Error( 'unexpected error' ); + } +} + +// The compiler throws an error if the function is provided a first argument which is not a recognized data type... +{ + const buffer = [ 1, 2, 3, 4 ]; + const shape = [ 2, 2 ]; + const strides = [ 2, 1 ]; + const offset = 0; + const order = 'row-major'; + + FancyArray( 'abc', buffer, shape, strides, offset, order ); // $ExpectError + FancyArray( 123, buffer, shape, strides, offset, order ); // $ExpectError + FancyArray( true, buffer, shape, strides, offset, order ); // $ExpectError + FancyArray( false, buffer, shape, strides, offset, order ); // $ExpectError + FancyArray( null, buffer, shape, strides, offset, order ); // $ExpectError + FancyArray( undefined, buffer, shape, strides, offset, order ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not an array-like object or buffer... +{ + const shape = [ 2, 2 ]; + const strides = [ 2, 1 ]; + const offset = 0; + const order = 'row-major'; + FancyArray( 'float64', 123, shape, strides, offset, order ); // $ExpectError + FancyArray( 'float64', true, shape, strides, offset, order ); // $ExpectError + FancyArray( 'float64', false, shape, strides, offset, order ); // $ExpectError + FancyArray( 'float64', null, shape, strides, offset, order ); // $ExpectError + FancyArray( 'float64', undefined, shape, strides, offset, order ); // $ExpectError +} + +// The compiler throws an error if the function is provided a third argument which is not an array-like object containing numbers... +{ + const buffer = [ 1, 2, 3, 4 ]; + const strides = [ 2, 1 ]; + const offset = 0; + const order = 'row-major'; + FancyArray( 'float64', buffer, true, strides, offset, order ); // $ExpectError + FancyArray( 'float64', buffer, false, strides, offset, order ); // $ExpectError + FancyArray( 'float64', buffer, null, strides, offset, order ); // $ExpectError + FancyArray( 'float64', buffer, undefined, strides, offset, order ); // $ExpectError + FancyArray( 'float64', buffer, '5', strides, offset, order ); // $ExpectError + FancyArray( 'float64', buffer, [ '1', '2' ], strides, offset, order ); // $ExpectError + FancyArray( 'float64', buffer, {}, strides, offset, order ); // $ExpectError + FancyArray( 'float64', buffer, ( x: number ): number => x, strides, offset, order ); // $ExpectError +} + +// The compiler throws an error if the function is provided a fourth argument which is not an array-like object containing numbers... +{ + const buffer = [ 1, 2, 3, 4 ]; + const shape = [ 2, 2 ]; + const offset = 0; + const order = 'row-major'; + FancyArray( 'float32', buffer, shape, true, offset, order ); // $ExpectError + FancyArray( 'float32', buffer, shape, false, offset, order ); // $ExpectError + FancyArray( 'float32', buffer, shape, null, offset, order ); // $ExpectError + FancyArray( 'float32', buffer, shape, undefined, offset, order ); // $ExpectError + FancyArray( 'float32', buffer, shape, '5', offset, order ); // $ExpectError + FancyArray( 'float32', buffer, shape, [ '1', '2' ], offset, order ); // $ExpectError + FancyArray( 'float32', buffer, shape, {}, offset, order ); // $ExpectError + FancyArray( 'float32', buffer, shape, ( x: number ): number => x, offset, order ); // $ExpectError +} + +// The compiler throws an error if the function is provided a fifth argument which is not a number... +{ + const buffer = [ 1, 2, 3, 4 ]; + const shape = [ 2, 2 ]; + const strides = [ 2, 1 ]; + const order = 'row-major'; + FancyArray( 'int32', buffer, shape, strides, true, order ); // $ExpectError + FancyArray( 'int32', buffer, shape, strides, false, order ); // $ExpectError + FancyArray( 'int32', buffer, shape, strides, null, order ); // $ExpectError + FancyArray( 'int32', buffer, shape, strides, undefined, order ); // $ExpectError + FancyArray( 'int32', buffer, shape, strides, '5', order ); // $ExpectError + FancyArray( 'int32', buffer, shape, strides, [ '1', '2' ], order ); // $ExpectError + FancyArray( 'int32', buffer, shape, strides, {}, order ); // $ExpectError + FancyArray( 'int32', buffer, shape, strides, ( x: number ): number => x, order ); // $ExpectError +} + +// The compiler throws an error if the function is provided a sixth argument which is not a known array order... +{ + const buffer = [ 1, 2, 3, 4 ]; + const shape = [ 2, 2 ]; + const strides = [ 2, 1 ]; + const offset = 0; + FancyArray( 'int8', buffer, shape, strides, offset, true ); // $ExpectError + FancyArray( 'int8', buffer, shape, strides, offset, false ); // $ExpectError + FancyArray( 'int8', buffer, shape, strides, offset, null ); // $ExpectError + FancyArray( 'int8', buffer, shape, strides, offset, undefined ); // $ExpectError + FancyArray( 'int8', buffer, shape, strides, offset, '5' ); // $ExpectError + FancyArray( 'int8', buffer, shape, strides, offset, [ '1', '2' ] ); // $ExpectError + FancyArray( 'int8', buffer, shape, strides, offset, {} ); // $ExpectError + FancyArray( 'int8', buffer, shape, strides, offset, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided a `mode` option which is not a recognized mode... +{ + const buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + const shape = [ 2, 2 ]; + const strides = [ 2, 1 ]; + const offset = 0; + const order = 'row-major'; + FancyArray( 'float64', buffer, shape, strides, offset, order, { 'mode': 'abc' } ); // $ExpectError + FancyArray( 'float64', buffer, shape, strides, offset, order, { 'mode': 123 } ); // $ExpectError + FancyArray( 'float64', buffer, shape, strides, offset, order, { 'mode': true } ); // $ExpectError + FancyArray( 'float64', buffer, shape, strides, offset, order, { 'mode': false } ); // $ExpectError + FancyArray( 'float64', buffer, shape, strides, offset, order, { 'mode': null } ); // $ExpectError + FancyArray( 'float64', buffer, shape, strides, offset, order, { 'mode': [] } ); // $ExpectError + FancyArray( 'float64', buffer, shape, strides, offset, order, { 'mode': {} } ); // $ExpectError + FancyArray( 'float64', buffer, shape, strides, offset, order, { 'mode': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an `submode` option which is not an FancyArray of strings... +{ + const buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + const shape = [ 2, 2 ]; + const strides = [ 2, 1 ]; + const offset = 0; + const order = 'row-major'; + FancyArray( 'float64', buffer, shape, strides, offset, order, { 'submode': 'abc' } ); // $ExpectError + FancyArray( 'float64', buffer, shape, strides, offset, order, { 'submode': 123 } ); // $ExpectError + FancyArray( 'float64', buffer, shape, strides, offset, order, { 'submode': true } ); // $ExpectError + FancyArray( 'float64', buffer, shape, strides, offset, order, { 'submode': false } ); // $ExpectError + FancyArray( 'float64', buffer, shape, strides, offset, order, { 'submode': null } ); // $ExpectError + FancyArray( 'float64', buffer, shape, strides, offset, order, { 'submode': {} } ); // $ExpectError + FancyArray( 'float64', buffer, shape, strides, offset, order, { 'submode': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided a `readonly` option which is not a boolean... +{ + const buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); + const shape = [ 2, 2 ]; + const strides = [ 2, 1 ]; + const offset = 0; + const order = 'row-major'; + FancyArray( 'float64', buffer, shape, strides, offset, order, { 'readonly': 'abc' } ); // $ExpectError + FancyArray( 'float64', buffer, shape, strides, offset, order, { 'readonly': 123 } ); // $ExpectError + FancyArray( 'float64', buffer, shape, strides, offset, order, { 'readonly': [] } ); // $ExpectError + FancyArray( 'float64', buffer, shape, strides, offset, order, { 'readonly': null } ); // $ExpectError + FancyArray( 'float64', buffer, shape, strides, offset, order, { 'readonly': {} } ); // $ExpectError + FancyArray( 'float64', buffer, shape, strides, offset, order, { 'readonly': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided and invalid number of arguments... +{ + const buffer = [ 1, 2, 3, 4 ]; + const shape = [ 2, 2 ]; + const strides = [ 2, 1 ]; + const offset = 0; + FancyArray(); // $ExpectError + FancyArray( 'uint32' ); // $ExpectError + FancyArray( 'int8', buffer ); // $ExpectError + FancyArray( 'uint8c', buffer, shape ); // $ExpectError + FancyArray( 'uint8', buffer, shape, strides ); // $ExpectError + FancyArray( 'uint16', buffer, shape, strides, offset ); // $ExpectError + FancyArray( 'uint16', buffer, shape, strides, offset, 'row-major', {}, {} ); // $ExpectError +} diff --git a/fancy/test/test.argument_validation.js b/fancy/test/test.argument_validation.js new file mode 100644 index 00000000..57ecbb66 --- /dev/null +++ b/fancy/test/test.argument_validation.js @@ -0,0 +1,1000 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-new */ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Float64Array = require( '@stdlib/array/float64' ); +var FancyArray = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof FancyArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the constructor throws an error if not provided a valid `dtype` argument', function test( t ) { + var strides; + var values; + var offset; + var buffer; + var order; + var shape; + var i; + + buffer = [ 0.0, 1.0, 2.0, 3.0 ]; + shape = [ 2, 2 ]; + order = 'row-major'; + strides = [ 2, 1 ]; + offset = 0; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + new FancyArray( value, buffer, shape, strides, offset, order ); + }; + } +}); + +tape( 'the constructor throws an error if not provided a valid `dtype` argument (options)', function test( t ) { + var strides; + var values; + var offset; + var buffer; + var order; + var shape; + var i; + + buffer = [ 0.0, 1.0, 2.0, 3.0 ]; + shape = [ 2, 2 ]; + order = 'row-major'; + strides = [ 2, 1 ]; + offset = 0; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + new FancyArray( value, buffer, shape, strides, offset, order, {} ); + }; + } +}); + +tape( 'the constructor throws an error if not provided a valid `buffer` argument', function test( t ) { + var strides; + var values; + var offset; + var dtype; + var order; + var shape; + var i; + + dtype = 'generic'; + shape = [ 2, 2 ]; + order = 'row-major'; + strides = [ 2, 1 ]; + offset = 0; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + { + 'length': 10, + 'get': function noop() {}, + 'set': true + }, + { + 'length': 10, + 'get': true, + 'set': function noop() {} + }, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + new FancyArray( dtype, value, shape, strides, offset, order ); + }; + } +}); + +tape( 'the constructor throws an error if not provided a valid `buffer` argument (options)', function test( t ) { + var strides; + var values; + var offset; + var dtype; + var order; + var shape; + var i; + + dtype = 'generic'; + shape = [ 2, 2 ]; + order = 'row-major'; + strides = [ 2, 1 ]; + offset = 0; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + { + 'length': 10, + 'get': function noop() {}, + 'set': true + }, + { + 'length': 10, + 'get': true, + 'set': function noop() {} + }, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + new FancyArray( dtype, value, shape, strides, offset, order, {} ); + }; + } +}); + +tape( 'the constructor throws an error if not provided a valid `shape` argument', function test( t ) { + var strides; + var values; + var offset; + var buffer; + var dtype; + var order; + var i; + + dtype = 'generic'; + buffer = [ 0.0, 1.0, 2.0, 3.0 ]; + order = 'row-major'; + strides = [ 2, 1 ]; + offset = 0; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [ 1, 3.14 ], + [ -1, -2 ], + [ 1, '1' ], + [ 1, null ], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + new FancyArray( dtype, buffer, value, strides, offset, order ); + }; + } +}); + +tape( 'the constructor throws an error if not provided a valid `shape` argument (options)', function test( t ) { + var strides; + var values; + var offset; + var buffer; + var dtype; + var order; + var i; + + dtype = 'generic'; + buffer = [ 0.0, 1.0, 2.0, 3.0 ]; + order = 'row-major'; + strides = [ 2, 1 ]; + offset = 0; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [ 1, 3.14 ], + [ -1, -2 ], + [ 1, '1' ], + [ 1, null ], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + new FancyArray( dtype, buffer, value, strides, offset, order, {} ); + }; + } +}); + +tape( 'the constructor throws an error if the number of array dimensions may cause stack limits to be exceeded', function test( t ) { + var strides; + var values; + var offset; + var buffer; + var dtype; + var order; + var i; + + dtype = 'float64'; + buffer = new Float64Array( [ 0.0, 1.0, 2.0, 3.0 ] ); + order = 'row-major'; + strides = [ 2, 1 ]; + offset = 0; + + values = [ + 1e5, + 1e6 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var shape = new Float64Array( value ); + new FancyArray( dtype, buffer, shape, strides, offset, order ); + }; + } +}); + +tape( 'the constructor throws an error if the number of array dimensions may cause stack limits to be exceeded (options)', function test( t ) { + var strides; + var values; + var offset; + var buffer; + var dtype; + var order; + var i; + + dtype = 'float64'; + buffer = new Float64Array( [ 0.0, 1.0, 2.0, 3.0 ] ); + order = 'row-major'; + strides = [ 2, 1 ]; + offset = 0; + + values = [ + 1e5, + 1e6 + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var shape = new Float64Array( value ); + new FancyArray( dtype, buffer, shape, strides, offset, order, {} ); + }; + } +}); + +tape( 'the constructor throws an error if not provided a valid `strides` argument', function test( t ) { + var values; + var offset; + var buffer; + var shape; + var dtype; + var order; + var i; + + dtype = 'generic'; + shape = [ 2, 2 ]; + buffer = [ 0.0, 1.0, 2.0, 3.0 ]; + order = 'row-major'; + offset = 0; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + [ 1, 3.14 ], + [ -1, -3.14 ], + [ 1, '1' ], + [ 1, null ], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + new FancyArray( dtype, buffer, shape, value, offset, order ); + }; + } +}); + +tape( 'the constructor throws an error if not provided a valid `strides` argument (0d)', function test( t ) { + var values; + var offset; + var buffer; + var shape; + var dtype; + var order; + var i; + + dtype = 'generic'; + shape = []; + buffer = [ 0.0, 1.0, 2.0, 3.0 ]; + order = 'row-major'; + offset = 0; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + [ 1.1 ], + [ 1, 3.14 ], + [ -1, -3.14 ], + [ 1, '1' ], + [ 1, null ], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + new FancyArray( dtype, buffer, shape, value, offset, order ); + }; + } +}); + +tape( 'the constructor throws an error if not provided a valid `strides` argument (0d)', function test( t ) { + var values; + var offset; + var buffer; + var shape; + var dtype; + var order; + var i; + + dtype = 'generic'; + shape = []; + buffer = [ 0.0, 1.0, 2.0, 3.0 ]; + order = 'row-major'; + offset = 0; + + values = [ + [ 1 ], + [ 2 ], + [ 3 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + new FancyArray( dtype, buffer, shape, value, offset, order ); + }; + } +}); + +tape( 'the constructor throws an error if not provided a valid `strides` argument (options)', function test( t ) { + var values; + var offset; + var buffer; + var shape; + var dtype; + var order; + var i; + + dtype = 'generic'; + shape = [ 2, 2 ]; + buffer = [ 0.0, 1.0, 2.0, 3.0 ]; + order = 'row-major'; + offset = 0; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + [ 1, 3.14 ], + [ -1, -3.14 ], + [ 1, '1' ], + [ 1, null ], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + new FancyArray( dtype, buffer, shape, value, offset, order, {} ); + }; + } +}); + +tape( 'the constructor throws an error if not provided a valid `strides` argument (options; 0d)', function test( t ) { + var values; + var offset; + var buffer; + var shape; + var dtype; + var order; + var i; + + dtype = 'generic'; + shape = []; + buffer = [ 0.0, 1.0, 2.0, 3.0 ]; + order = 'row-major'; + offset = 0; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + [ 1.1 ], + [ 1, 3.14 ], + [ -1, -3.14 ], + [ 1, '1' ], + [ 1, null ], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + new FancyArray( dtype, buffer, shape, value, offset, order, {} ); + }; + } +}); + +tape( 'the constructor throws an error if not provided a valid `strides` argument (number of dimensions)', function test( t ) { + var values; + var offset; + var buffer; + var shape; + var dtype; + var order; + var i; + + dtype = 'generic'; + shape = [ 2, 2 ]; + buffer = [ 0.0, 1.0, 2.0, 3.0 ]; + order = 'row-major'; + offset = 0; + + values = [ + [ 1 ], + [ 4, 2, 1 ], + [ 4, 4, 2, 1 ], + [ 4, 4, 4, 2, 1 ], + [ 4, 4, 4, 4, 2, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + new FancyArray( dtype, buffer, shape, value, offset, order ); + }; + } +}); + +tape( 'the constructor throws an error if not provided a valid `strides` argument (number of dimensions; 0d)', function test( t ) { + var values; + var offset; + var buffer; + var shape; + var dtype; + var order; + var i; + + dtype = 'generic'; + shape = []; + buffer = [ 0.0, 1.0, 2.0, 3.0 ]; + order = 'row-major'; + offset = 0; + + values = [ + [], + [ 2, 1 ], + [ 4, 2, 1 ], + [ 4, 4, 2, 1 ], + [ 4, 4, 4, 2, 1 ], + [ 4, 4, 4, 4, 2, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + new FancyArray( dtype, buffer, shape, value, offset, order ); + }; + } +}); + +tape( 'the constructor throws an error if not provided a valid `strides` argument (number of dimensions; options)', function test( t ) { + var values; + var offset; + var buffer; + var shape; + var dtype; + var order; + var i; + + dtype = 'generic'; + shape = [ 2, 2 ]; + buffer = [ 0.0, 1.0, 2.0, 3.0 ]; + order = 'row-major'; + offset = 0; + + values = [ + [ 1 ], + [ 4, 2, 1 ], + [ 4, 4, 2, 1 ], + [ 4, 4, 4, 2, 1 ], + [ 4, 4, 4, 4, 2, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + new FancyArray( dtype, buffer, shape, value, offset, order, {} ); + }; + } +}); + +tape( 'the constructor throws an error if not provided a valid `strides` argument (number of dimensions; options; 0d)', function test( t ) { + var values; + var offset; + var buffer; + var shape; + var dtype; + var order; + var i; + + dtype = 'generic'; + shape = []; + buffer = [ 0.0, 1.0, 2.0, 3.0 ]; + order = 'row-major'; + offset = 0; + + values = [ + [], + [ 2, 1 ], + [ 4, 2, 1 ], + [ 4, 4, 2, 1 ], + [ 4, 4, 4, 2, 1 ], + [ 4, 4, 4, 4, 2, 1 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + new FancyArray( dtype, buffer, shape, value, offset, order, {} ); + }; + } +}); + +tape( 'the constructor throws an error if not provided a valid `offset` argument', function test( t ) { + var strides; + var values; + var buffer; + var shape; + var dtype; + var order; + var i; + + dtype = 'generic'; + buffer = [ 0.0, 1.0, 2.0, 3.0 ]; + shape = [ 2, 2 ]; + order = 'row-major'; + strides = [ 2, 1 ]; + + values = [ + '5', + -5, + 3.14, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + new FancyArray( dtype, buffer, shape, strides, value, order ); + }; + } +}); + +tape( 'the constructor throws an error if not provided a valid `offset` argument (options)', function test( t ) { + var strides; + var values; + var buffer; + var shape; + var dtype; + var order; + var i; + + dtype = 'generic'; + buffer = [ 0.0, 1.0, 2.0, 3.0 ]; + shape = [ 2, 2 ]; + order = 'row-major'; + strides = [ 2, 1 ]; + + values = [ + '5', + -5, + 3.14, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + new FancyArray( dtype, buffer, shape, strides, value, order, {} ); + }; + } +}); + +tape( 'the constructor throws an error if not provided a valid `order` argument', function test( t ) { + var strides; + var values; + var offset; + var buffer; + var dtype; + var shape; + var i; + + dtype = 'generic'; + buffer = [ 0.0, 1.0, 2.0, 3.0 ]; + shape = [ 2, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + + values = [ + 'c', + 'f', + 'c-style', + 'fortran', + 'fortran-style', + 'row', + 'column', + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + new FancyArray( dtype, buffer, shape, strides, offset, value ); + }; + } +}); + +tape( 'the constructor throws an error if not provided a valid `order` argument (options)', function test( t ) { + var strides; + var values; + var offset; + var buffer; + var dtype; + var shape; + var i; + + dtype = 'generic'; + buffer = [ 0.0, 1.0, 2.0, 3.0 ]; + shape = [ 2, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + + values = [ + 'c', + 'f', + 'c-style', + 'fortran', + 'fortran-style', + 'row', + 'column', + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + new FancyArray( dtype, buffer, shape, strides, offset, value, {} ); + }; + } +}); + +tape( 'the constructor throws an error if not provided a valid options argument', function test( t ) { + var strides; + var values; + var offset; + var buffer; + var dtype; + var shape; + var order; + var i; + + dtype = 'generic'; + buffer = [ 0.0, 1.0, 2.0, 3.0 ]; + shape = [ 2, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + new FancyArray( dtype, buffer, shape, strides, offset, order, value ); // eslint-disable-line max-len + }; + } +}); + +tape( 'the constructor throws an error if provided an invalid option', function test( t ) { + var strides; + var values; + var offset; + var buffer; + var dtype; + var shape; + var order; + var i; + + dtype = 'generic'; + buffer = [ 0.0, 1.0, 2.0, 3.0 ]; + shape = [ 2, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var opts = { + 'mode': value + }; + new FancyArray( dtype, buffer, shape, strides, offset, order, opts ); // eslint-disable-line max-len + }; + } +}); + +tape( 'the constructor throws an error if not provided compatible input arguments', function test( t ) { + var strides; + var values; + var offset; + var buffer; + var order; + var dtype; + var shape; + var i; + + dtype = 'generic'; + buffer = [ 0.0, 1.0, 2.0, 3.0 ]; + shape = [ 2, 2 ]; + order = 'row-major'; + strides = [ 2, 1 ]; + offset = 0; + + values = [ + [ dtype, [ 0.0 ], shape, strides, offset, order ], + [ dtype, buffer, [ 3, 2 ], strides, offset, order ], + [ dtype, buffer, shape, [ 2, 2 ], offset, order ], + [ dtype, buffer, shape, strides, 2, order ], + [ dtype, buffer, shape, strides, 20, order ], + [ dtype, buffer, shape, [ 2, -1 ], offset, order ], + [ dtype, buffer, shape, [ -2, 1 ], offset, order ], + [ dtype, buffer, shape, [ -2, 1 ], 1, order ], + [ dtype, buffer, shape, [ -2, -1 ], offset, order ], + [ dtype, buffer, shape, [ -2, -1 ], 1, order ], + [ dtype, buffer, shape, [ -2, -1 ], 2, order ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + JSON.stringify( values[ i ] ) ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + FancyArray.apply( null, value ); + }; + } +}); diff --git a/fancy/test/test.js b/fancy/test/test.js new file mode 100644 index 00000000..3962ccc7 --- /dev/null +++ b/fancy/test/test.js @@ -0,0 +1,215 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var instanceOf = require( '@stdlib/assert/instance-of' ); +var Complex64Array = require( '@stdlib/array/complex64' ); +var FancyArray = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof FancyArray, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function is a constructor', function test( t ) { + var strides; + var buffer; + var offset; + var dtype; + var order; + var shape; + var arr; + + dtype = 'generic'; + buffer = [ 1.0, 2.0, 3.0, 4.0 ]; + shape = [ 2, 2 ]; + order = 'row-major'; + strides = [ 2, 1 ]; + offset = 0; + + arr = new FancyArray( dtype, buffer, shape, strides, offset, order ); + + t.strictEqual( instanceOf( arr, FancyArray ), true, 'returns an instance' ); + t.end(); +}); + +tape( 'the function is a constructor (complex dtype)', function test( t ) { + var strides; + var buffer; + var offset; + var dtype; + var order; + var shape; + var arr; + + dtype = 'complex64'; + buffer = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] ); + shape = [ 2, 2 ]; + order = 'row-major'; + strides = [ 2, 1 ]; + offset = 0; + + arr = new FancyArray( dtype, buffer, shape, strides, offset, order ); + + t.strictEqual( instanceOf( arr, FancyArray ), true, 'returns an instance' ); + t.end(); +}); + +tape( 'the function is a constructor (0d)', function test( t ) { + var strides; + var buffer; + var offset; + var dtype; + var order; + var shape; + var arr; + + dtype = 'generic'; + buffer = [ 1.0 ]; + shape = []; + order = 'row-major'; + strides = [ 0 ]; + offset = 0; + + arr = new FancyArray( dtype, buffer, shape, strides, offset, order ); + + t.strictEqual( instanceOf( arr, FancyArray ), true, 'returns an instance' ); + t.end(); +}); + +tape( 'the function is a constructor (0d; complex dtype)', function test( t ) { + var strides; + var buffer; + var offset; + var dtype; + var order; + var shape; + var arr; + + dtype = 'complex64'; + buffer = new Complex64Array( [ 1.0, 1.0 ] ); + shape = []; + order = 'row-major'; + strides = [ 0 ]; + offset = 0; + + arr = new FancyArray( dtype, buffer, shape, strides, offset, order ); + + t.strictEqual( instanceOf( arr, FancyArray ), true, 'returns an instance' ); + t.end(); +}); + +tape( 'the function is a constructor (options)', function test( t ) { + var strides; + var buffer; + var offset; + var dtype; + var order; + var shape; + var arr; + + dtype = 'generic'; + buffer = [ 1.0, 2.0, 3.0, 4.0 ]; + shape = [ 2, 2 ]; + order = 'row-major'; + strides = [ 2, 1 ]; + offset = 0; + + arr = new FancyArray( dtype, buffer, shape, strides, offset, order, {} ); + + t.strictEqual( instanceOf( arr, FancyArray ), true, 'returns an instance' ); + t.end(); +}); + +tape( 'the function is a constructor (0d; options)', function test( t ) { + var strides; + var buffer; + var offset; + var dtype; + var order; + var shape; + var arr; + + dtype = 'generic'; + buffer = [ 1.0 ]; + shape = []; + order = 'row-major'; + strides = [ 0 ]; + offset = 0; + + arr = new FancyArray( dtype, buffer, shape, strides, offset, order, {} ); + + t.strictEqual( instanceOf( arr, FancyArray ), true, 'returns an instance' ); + t.end(); +}); + +tape( 'the constructor does not require the `new` keyword', function test( t ) { + var ndarray; + var strides; + var buffer; + var offset; + var dtype; + var order; + var shape; + var arr; + + dtype = 'generic'; + buffer = [ 1.0, 2.0, 3.0, 4.0 ]; + shape = [ 2, 2 ]; + order = 'row-major'; + strides = [ 2, 1 ]; + offset = 0; + + ndarray = FancyArray; + arr = ndarray( dtype, buffer, shape, strides, offset, order ); + + t.strictEqual( instanceOf( arr, FancyArray ), true, 'returns an instance' ); + t.end(); +}); + +tape( 'the constructor does not require the `new` keyword (options)', function test( t ) { + var ndarray; + var strides; + var buffer; + var offset; + var dtype; + var order; + var shape; + var arr; + + dtype = 'generic'; + buffer = [ 1.0, 2.0, 3.0, 4.0 ]; + shape = [ 2, 2 ]; + order = 'row-major'; + strides = [ 2, 1 ]; + offset = 0; + + ndarray = FancyArray; + arr = ndarray( dtype, buffer, shape, strides, offset, order, {} ); + + t.strictEqual( instanceOf( arr, FancyArray ), true, 'returns an instance' ); + t.end(); +});