Skip to content

Commit 1c7ff8d

Browse files
committed
Add tests for converting a complex number array to a generic array
1 parent d5207bf commit 1c7ff8d

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

  • lib/node_modules/@stdlib/array/convert/test

lib/node_modules/@stdlib/array/convert/test/test.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,14 @@ var isUint8Array = require( '@stdlib/assert/is-uint8array' );
4646
var isUint8ClampedArray = require( '@stdlib/assert/is-uint8clampedarray' );
4747
var isComplex64Array = require( '@stdlib/assert/is-complex64array' );
4848
var isComplex128Array = require( '@stdlib/assert/is-complex128array' );
49+
var isComplex64 = require( '@stdlib/assert/is-complex64' );
50+
var isComplex128 = require( '@stdlib/assert/is-complex128' );
4951
var reinterpret64 = require( '@stdlib/strided/base/reinterpret-complex64' );
5052
var reinterpret128 = require( '@stdlib/strided/base/reinterpret-complex128' );
53+
var real = require( '@stdlib/complex/real' );
54+
var imag = require( '@stdlib/complex/imag' );
55+
var realf = require( '@stdlib/complex/realf' );
56+
var imagf = require( '@stdlib/complex/imagf' );
5157
var convertArray = require( './../lib' );
5258

5359

@@ -399,6 +405,60 @@ tape( 'the function converts an array to an array of a different data type (comp
399405
t.end();
400406
});
401407

408+
tape( 'the function converts an array to an array of a different data type (complex64 => generic)', function test( t ) {
409+
var expected;
410+
var arr;
411+
var out;
412+
var v1;
413+
var v2;
414+
var i;
415+
416+
arr = new Complex64Array( [ -1.0, 1.0, 0.0, 2.0, 1.0, 3.0, 2.0, 4.0 ] );
417+
expected = [
418+
arr.get( 0 ),
419+
arr.get( 1 ),
420+
arr.get( 2 ),
421+
arr.get( 3 )
422+
];
423+
out = convertArray( arr, 'generic' );
424+
t.strictEqual( isArray( out ), true, 'returns expected value type' );
425+
for ( i = 0; i < arr.length; i++ ) {
426+
v1 = out[ i ];
427+
v2 = expected[ i ];
428+
t.strictEqual( isComplex64( v1 ), true, 'returns expected value' );
429+
t.strictEqual( realf( v1 ), realf( v2 ), 'returns expected real component' );
430+
t.strictEqual( imagf( v1 ), imagf( v2 ), 'returns expected imaginary component' );
431+
}
432+
t.end();
433+
});
434+
435+
tape( 'the function converts an array to an array of a different data type (complex128 => generic)', function test( t ) {
436+
var expected;
437+
var arr;
438+
var out;
439+
var v1;
440+
var v2;
441+
var i;
442+
443+
arr = new Complex128Array( [ -1.0, 1.0, 0.0, 2.0, 1.0, 3.0, 2.0, 4.0 ] );
444+
expected = [
445+
arr.get( 0 ),
446+
arr.get( 1 ),
447+
arr.get( 2 ),
448+
arr.get( 3 )
449+
];
450+
out = convertArray( arr, 'generic' );
451+
t.strictEqual( isArray( out ), true, 'returns expected value type' );
452+
for ( i = 0; i < arr.length; i++ ) {
453+
v1 = out[ i ];
454+
v2 = expected[ i ];
455+
t.strictEqual( isComplex128( v1 ), true, 'returns expected value' );
456+
t.strictEqual( real( v1 ), real( v2 ), 'returns expected real component' );
457+
t.strictEqual( imag( v1 ), imag( v2 ), 'returns expected imaginary component' );
458+
}
459+
t.end();
460+
});
461+
402462
// TODO: add more tests to cover all code paths
403463

404464
tape( 'the function converts an array to an array of a different data type (large allocations)', function test( t ) {

0 commit comments

Comments
 (0)