time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

test.ts (12797B)


      1 /*
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2019 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 /// <reference types="@stdlib/types"/>
     20 
     21 import * as array from '@stdlib/types/array';
     22 import * as iter from '@stdlib/types/iter';
     23 import * as ndarray from '@stdlib/types/ndarray';
     24 import * as obj from '@stdlib/types/object';
     25 import * as random from '@stdlib/types/random';
     26 
     27 /**
     28 * Returns an iterator protocol-compliant object.
     29 *
     30 * @returns iterator protocol-compliant object
     31 */
     32 function createIterator1(): iter.Iterator {
     33 	return {
     34 		'next': next1
     35 	};
     36 }
     37 
     38 /**
     39 * Implements the iterator protocol `next` method.
     40 *
     41 * @returns iterator protocol-compliant object
     42 */
     43 function next1(): iter.IteratorResult {
     44 	return {
     45 		'value': true,
     46 		'done': false
     47 	};
     48 }
     49 
     50 /**
     51 * Returns an iterator protocol-compliant object.
     52 *
     53 * @returns iterator protocol-compliant object
     54 */
     55 function createIterator2(): iter.Iterator {
     56 	return {
     57 		'next': next2
     58 	};
     59 }
     60 
     61 /**
     62 * Implements the iterator protocol `next` method.
     63 *
     64 * @returns iterator protocol-compliant object
     65 */
     66 function next2(): iter.IteratorResult {
     67 	return {
     68 		'done': true
     69 	};
     70 }
     71 
     72 /**
     73 * Returns an iterable iterator protocol-compliant object.
     74 *
     75 * @returns iterable iterator protocol-compliant object
     76 */
     77 function createIterableIterator(): iter.IterableIterator {
     78 	return {
     79 		'next': next3,
     80 		[ Symbol.iterator ]: factory
     81 	};
     82 }
     83 
     84 /**
     85 * Implements the iterator protocol `next` method.
     86 *
     87 * @returns iterator protocol-compliant object
     88 */
     89 function next3(): iter.IteratorResult {
     90 	return {
     91 		'done': true
     92 	};
     93 }
     94 
     95 /**
     96 * Returns an iterable iterator protocol-compliant object.
     97 *
     98 * @returns iterable iterator protocol-compliant object
     99 */
    100 function factory(): iter.IterableIterator {
    101 	return createIterableIterator();
    102 }
    103 
    104 /**
    105 * Returns a complex number array-like object.
    106 *
    107 * @returns complex number array-like object
    108 */
    109 function cmplxArray(): array.ComplexArrayLike {
    110 	const buf: array.TypedArray = new Float64Array( 8 );
    111 	const obj: array.ComplexArrayLike = {
    112 		'byteLength': 64,
    113 		'byteOffset': 0,
    114 		'BYTES_PER_ELEMENT': 8,
    115 		'length': 8,
    116 		'get': ( i: number ): obj.ComplexLike => {
    117 			return {
    118 				're': i * 10,
    119 				'im': i * 10
    120 			};
    121 		},
    122 		'set': ( value: obj.ComplexLike, i?: number ) => {
    123 			i = ( i ) ? i : 0;
    124 			buf[ i ] = value.re;
    125 			buf[ i + 1 ] = value.im;
    126 		}
    127 	};
    128 	return obj;
    129 }
    130 
    131 /**
    132 * Returns a 64-bit complex number array.
    133 *
    134 * @returns 64-bit complex number array
    135 */
    136 function cmplx64Array(): array.Complex64Array {
    137 	const buf: array.TypedArray = new Float64Array( 8 );
    138 	const obj: array.Complex64Array = {
    139 		'byteLength': 64,
    140 		'byteOffset': 0,
    141 		'BYTES_PER_ELEMENT': 8,
    142 		'length': 8,
    143 		'get': ( i: number ): obj.Complex64 => {
    144 			return {
    145 				're': i * 10,
    146 				'im': i * 10,
    147 				'byteLength': 8,
    148 				'BYTES_PER_ELEMENT': 4
    149 			};
    150 		},
    151 		'set': ( value: obj.Complex64, i?: number ) => {
    152 			i = ( i ) ? i : 0;
    153 			buf[ i ] = value.re;
    154 			buf[ i + 1 ] = value.im;
    155 		}
    156 	};
    157 	return obj;
    158 }
    159 
    160 /**
    161 * Returns a 128-bit complex number array.
    162 *
    163 * @returns 128-bit complex number array
    164 */
    165 function cmplx128Array(): array.Complex128Array {
    166 	const buf: array.TypedArray = new Float64Array( 16 );
    167 	const obj: array.Complex128Array = {
    168 		'byteLength': 128,
    169 		'byteOffset': 0,
    170 		'BYTES_PER_ELEMENT': 16,
    171 		'length': 8,
    172 		'get': ( i: number ): obj.Complex128 => {
    173 			return {
    174 				're': i * 10,
    175 				'im': i * 10,
    176 				'byteLength': 16,
    177 				'BYTES_PER_ELEMENT': 8
    178 			};
    179 		},
    180 		'set': ( value: obj.Complex128, i?: number ) => {
    181 			i = ( i ) ? i : 0;
    182 			buf[ i ] = value.re;
    183 			buf[ i + 1 ] = value.im;
    184 		}
    185 	};
    186 	return obj;
    187 }
    188 
    189 
    190 // TESTS //
    191 
    192 // The compiler should not throw an error when using array type aliases...
    193 {
    194 	const x: array.TypedArray = new Float64Array( 10 );
    195 	if ( x[ 0 ] !== 0.0 ) {
    196 		throw new Error( 'something went wrong' );
    197 	}
    198 
    199 	const y: array.IntegerTypedArray = new Int32Array( 10 );
    200 	if ( y[ 0 ] !== 0 ) {
    201 		throw new Error( 'something went wrong' );
    202 	}
    203 
    204 	const z: array.NumericArray = new Float64Array( 10 );
    205 	if ( z[ 0 ] !== 0.0 ) {
    206 		throw new Error( 'something went wrong' );
    207 	}
    208 
    209 	const w: array.ArrayLike<string> = 'beep';
    210 	if ( w[ 0 ] !== 'b' ) {
    211 		throw new Error( 'something went wrong' );
    212 	}
    213 
    214 	const v: array.ArrayLike<number> = [ 1, 2, 3 ];
    215 	if ( v[ 0 ] !== 1 ) {
    216 		throw new Error( 'something went wrong' );
    217 	}
    218 
    219 	const t: array.ArrayLike<number> = new Int8Array( 10 );
    220 	if ( t[ 0 ] !== 1 ) {
    221 		throw new Error( 'something went wrong' );
    222 	}
    223 
    224 	const zz: array.ComplexArrayLike = cmplxArray();
    225 	if ( zz.byteOffset !== 0 ) {
    226 		throw new Error( 'something went wrong' );
    227 	}
    228 
    229 	const z64: array.Complex64Array = cmplx64Array();
    230 	if ( z64.byteOffset !== 0 ) {
    231 		throw new Error( 'something went wrong' );
    232 	}
    233 
    234 	const z128: array.Complex128Array = cmplx128Array();
    235 	if ( z128.byteOffset !== 0 ) {
    236 		throw new Error( 'something went wrong' );
    237 	}
    238 
    239 	const zzz: array.ComplexTypedArray = cmplx64Array();
    240 	if ( zzz.byteOffset !== 0 ) {
    241 		throw new Error( 'something went wrong' );
    242 	}
    243 
    244 	const v1: array.ArrayOrTypedArray = new Float64Array( 10 );
    245 	if ( v1[ 0 ] !== 0.0 ) {
    246 		throw new Error( 'something went wrong' );
    247 	}
    248 
    249 	const v2: array.FloatTypedArray = new Float64Array( 10 );
    250 	if ( v2[ 0 ] !== 0.0 ) {
    251 		throw new Error( 'something went wrong' );
    252 	}
    253 
    254 	const v3: array.RealOrComplexArray = new Float64Array( 10 );
    255 	if ( v3[ 0 ] !== 0.0 ) {
    256 		throw new Error( 'something went wrong' );
    257 	}
    258 
    259 	const v4: array.RealOrComplexTypedArray = new Float64Array( 10 );
    260 	if ( v4[ 0 ] !== 0.0 ) {
    261 		throw new Error( 'something went wrong' );
    262 	}
    263 
    264 	const v5buf: array.ArrayLike<number> = new Float64Array( 10 );
    265 	const v5: array.AccessorArrayLike<number> = {
    266 		'length': 10,
    267 		'data': v5buf,
    268 		'get': ( i: number ): number => v5buf[ i ],
    269 		'set': ( value: number, i?: number ): void => {
    270 			v5buf[ i || 0 ] = value;
    271 			return;
    272 		}
    273 	};
    274 	if ( v5.length !== 10 ) {
    275 		throw new Error( 'something went wrong' );
    276 	}
    277 
    278 	const v6: array.IntegerTypedArray = new Int32Array( 10 );
    279 	if ( v6[ 0 ] !== 0 ) {
    280 		throw new Error( 'something went wrong' );
    281 	}
    282 
    283 	const v7: array.SignedIntegerTypedArray = new Int32Array( 10 );
    284 	if ( v7[ 0 ] !== 0 ) {
    285 		throw new Error( 'something went wrong' );
    286 	}
    287 
    288 	const v8: array.UnsignedIntegerTypedArray = new Uint32Array( 10 );
    289 	if ( v8[ 0 ] !== 0 ) {
    290 		throw new Error( 'something went wrong' );
    291 	}
    292 
    293 	const v9: array.AnyArray = new Uint32Array( 10 );
    294 	if ( v9[ 0 ] !== 0 ) {
    295 		throw new Error( 'something went wrong' );
    296 	}
    297 
    298 	const v10: array.RealTypedArray = new Uint32Array( 10 );
    299 	if ( v10[ 0 ] !== 0 ) {
    300 		throw new Error( 'something went wrong' );
    301 	}
    302 
    303 	const v11: array.FloatOrComplexTypedArray = new Float64Array( 10 );
    304 	if ( v11[ 0 ] !== 0.0 ) {
    305 		throw new Error( 'something went wrong' );
    306 	}
    307 }
    308 
    309 // The compiler should not throw an error when using iterator or iterable types...
    310 {
    311 	createIterator1();
    312 	createIterator2();
    313 	createIterableIterator();
    314 }
    315 
    316 // The compiler should not throw an error when using ndarray types...
    317 {
    318 	const data = [ 1, 2, 3 ];
    319 	const arr: ndarray.ndarray = {
    320 		'byteLength': null,
    321 		'BYTES_PER_ELEMENT': null,
    322 		'data': data,
    323 		'dtype': 'generic',
    324 		'flags': {
    325 			'ROW_MAJOR_CONTIGUOUS': true,
    326 			'COLUMN_MAJOR_CONTIGUOUS': false
    327 		},
    328 		'length': 3,
    329 		'ndims': 1,
    330 		'offset': 0,
    331 		'order': 'row-major',
    332 		'shape': [ 3 ],
    333 		'strides': [ 1 ],
    334 		'get': ( i: number ): number => {
    335 			return data[ i ];
    336 		},
    337 		'set': ( i: number, v: number ): ndarray.ndarray => {
    338 			data[ i ] = v;
    339 			return arr;
    340 		}
    341 	};
    342 	if ( arr.length !== 3 ) {
    343 		throw new Error( 'something went wrong' );
    344 	}
    345 }
    346 
    347 // The compiler should not throw an error when using object types...
    348 {
    349 	const desc1: obj.DataPropertyDescriptor = {
    350 		'configurable': true,
    351 		'enumerable': false,
    352 		'writable': false,
    353 		'value': 'beep'
    354 	};
    355 	if ( desc1.value !== 'beep' ) {
    356 		throw new Error( 'something went wrong' );
    357 	}
    358 
    359 	const desc2: obj.DataPropertyDescriptor = {
    360 		'enumerable': false,
    361 		'writable': false,
    362 		'value': 'beep'
    363 	};
    364 	if ( desc2.value !== 'beep' ) {
    365 		throw new Error( 'something went wrong' );
    366 	}
    367 
    368 	const desc3: obj.DataPropertyDescriptor = {
    369 		'configurable': true,
    370 		'writable': false,
    371 		'value': 'beep'
    372 	};
    373 	if ( desc3.value !== 'beep' ) {
    374 		throw new Error( 'something went wrong' );
    375 	}
    376 
    377 	const desc4: obj.DataPropertyDescriptor = {
    378 		'configurable': true,
    379 		'enumerable': false,
    380 		'writable': false
    381 	};
    382 	if ( desc4.value ) {
    383 		throw new Error( 'something went wrong' );
    384 	}
    385 
    386 	const desc5: obj.DataPropertyDescriptor = {
    387 		'writable': false,
    388 		'value': 'beep'
    389 	};
    390 	if ( desc5.value !== 'beep' ) {
    391 		throw new Error( 'something went wrong' );
    392 	}
    393 
    394 	const desc6: obj.DataPropertyDescriptor = {
    395 		'configurable': true,
    396 		'value': 'beep'
    397 	};
    398 	if ( desc6.value !== 'beep' ) {
    399 		throw new Error( 'something went wrong' );
    400 	}
    401 
    402 	const desc7: obj.DataPropertyDescriptor = {
    403 		'enumerable': false,
    404 		'value': 'beep'
    405 	};
    406 	if ( desc7.value !== 'beep' ) {
    407 		throw new Error( 'something went wrong' );
    408 	}
    409 
    410 	const desc8: obj.DataPropertyDescriptor = {
    411 		'enumerable': false,
    412 		'writable': false
    413 	};
    414 	if ( desc8.value ) {
    415 		throw new Error( 'something went wrong' );
    416 	}
    417 
    418 	const desc9: obj.AccessorPropertyDescriptor = {
    419 		'configurable': true,
    420 		'enumerable': false,
    421 		'get': (): string => 'beep',
    422 		'set': () => { throw new Error( 'beep' ); }
    423 	};
    424 	if ( desc9.enumerable !== false ) {
    425 		throw new Error( 'something went wrong' );
    426 	}
    427 
    428 	const desc10: obj.AccessorPropertyDescriptor = {
    429 		'enumerable': false,
    430 		'get': (): string => 'beep',
    431 		'set': () => { throw new Error( 'beep' ); }
    432 	};
    433 	if ( desc10.enumerable !== false ) {
    434 		throw new Error( 'something went wrong' );
    435 	}
    436 
    437 	const desc11: obj.AccessorPropertyDescriptor = {
    438 		'configurable': true,
    439 		'get': (): string => 'beep',
    440 		'set': () => { throw new Error( 'beep' ); }
    441 	};
    442 	if ( desc11.enumerable !== false ) {
    443 		throw new Error( 'something went wrong' );
    444 	}
    445 
    446 	const desc12: obj.AccessorPropertyDescriptor = {
    447 		'configurable': true,
    448 		'enumerable': false,
    449 		'set': () => { throw new Error( 'beep' ); }
    450 	};
    451 	if ( desc12.enumerable !== false ) {
    452 		throw new Error( 'something went wrong' );
    453 	}
    454 
    455 	const desc13: obj.AccessorPropertyDescriptor = {
    456 		'configurable': true,
    457 		'enumerable': false,
    458 		'get': (): string => 'beep'
    459 	};
    460 	if ( desc13.enumerable !== false ) {
    461 		throw new Error( 'something went wrong' );
    462 	}
    463 
    464 	const desc14: obj.AccessorPropertyDescriptor = {
    465 		'get': (): string => 'beep',
    466 		'set': () => { throw new Error( 'beep' ); }
    467 	};
    468 	if ( desc14.enumerable !== false ) {
    469 		throw new Error( 'something went wrong' );
    470 	}
    471 
    472 	const desc15: obj.PropertyDescriptor = {
    473 		'configurable': true,
    474 		'enumerable': false,
    475 		'writable': false,
    476 		'value': 'beep'
    477 	};
    478 	if ( desc15.value !== 'beep' ) {
    479 		throw new Error( 'something went wrong' );
    480 	}
    481 
    482 	const prop: obj.PropertyName = 'foo';
    483 	if ( prop !== 'foo' ) {
    484 		throw new Error( 'something went wrong' );
    485 	}
    486 
    487 	const arr: obj.Collection = [ 1, 2, 3 ];
    488 	if ( arr.length !== 3 ) {
    489 		throw new Error( 'something went wrong' );
    490 	}
    491 
    492 	const z: obj.ComplexLike = {
    493 		're': 1.0,
    494 		'im': 1.0
    495 	};
    496 	if ( z.re !== 1.0 ) {
    497 		throw new Error( 'something went wrong' );
    498 	}
    499 
    500 	const z64: obj.Complex64 = {
    501 		're': 1.0,
    502 		'im': 1.0,
    503 		'byteLength': 8,
    504 		'BYTES_PER_ELEMENT': 4
    505 	};
    506 	if ( z64.re !== 1.0 ) {
    507 		throw new Error( 'something went wrong' );
    508 	}
    509 
    510 	const z128: obj.Complex128 = {
    511 		're': 1.0,
    512 		'im': 1.0,
    513 		'byteLength': 16,
    514 		'BYTES_PER_ELEMENT': 8
    515 	};
    516 	if ( z128.re !== 1.0 ) {
    517 		throw new Error( 'something went wrong' );
    518 	}
    519 }
    520 
    521 // The compiler should not throw an error when using PRNG types...
    522 {
    523 	const rand: random.PRNG = (): number => 3.14;
    524 	if ( rand() !== 3.14 ) {
    525 		throw new Error( 'something went wrong' );
    526 	}
    527 
    528 	const s1: random.PRNGSeedMT19937 = 12345;
    529 	if ( s1 !== 12345 ) {
    530 		throw new Error( 'something went wrong' );
    531 	}
    532 
    533 	const s2: random.PRNGSeedMT19937 = new Uint32Array( 10 );
    534 	if ( s2[ 0 ] !== 0 ) {
    535 		throw new Error( 'something went wrong' );
    536 	}
    537 
    538 	const s3: random.PRNGSeedMINSTD = 12345;
    539 	if ( s3 !== 12345 ) {
    540 		throw new Error( 'something went wrong' );
    541 	}
    542 
    543 	const s4: random.PRNGSeedMINSTD = new Int32Array( 10 );
    544 	if ( s4[ 0 ] !== 0 ) {
    545 		throw new Error( 'something went wrong' );
    546 	}
    547 
    548 	const s5: random.PRNGStateMT19937 = new Uint32Array( 10 );
    549 	if ( s5[ 0 ] !== 0 ) {
    550 		throw new Error( 'something went wrong' );
    551 	}
    552 
    553 	const s6: random.PRNGStateMINSTD = new Int32Array( 10 );
    554 	if ( s6[ 0 ] !== 0 ) {
    555 		throw new Error( 'something went wrong' );
    556 	}
    557 }