Skip to content

Commit 58764b4

Browse files
committed
test: add tests for the decompose function
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 91cbfa3 commit 58764b4

File tree

2 files changed

+269
-0
lines changed
  • lib/node_modules/@stdlib/fft/base/fftpack/decompose

2 files changed

+269
-0
lines changed

lib/node_modules/@stdlib/fft/base/fftpack/decompose/lib/main.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@
6161
// MODULES //
6262

6363
var floor = require( '@stdlib/math/base/special/floor' );
64+
var isNonNegativeInteger = require( '@stdlib/math/base/assert/is-nonnegative-integer' );
65+
var isNonNegativeIntegerArray = require( '@stdlib/assert/is-nonnegative-integer-array' ).primitives;
66+
var isCollection = require( '@stdlib/assert/is-collection' );
6467

6568

6669
// MAIN //
@@ -131,6 +134,12 @@ function decompose( N, initial, out, stride, offset ) {
131134
var i;
132135
var j;
133136

137+
if ( !isNonNegativeInteger( N ) || !isNonNegativeIntegerArray( initial ) ||
138+
!isCollection( out ) || !isNonNegativeInteger( stride ) ||
139+
!isNonNegativeInteger( offset ) ) {
140+
return NaN;
141+
}
142+
134143
// Resolve the number of trial divisors:
135144
ntrials = initial.length;
136145

Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var decompose = require( './../lib' );
26+
27+
28+
// TESTS //
29+
30+
tape( 'main export is a function', function test( t ) {
31+
t.ok( true, __filename );
32+
t.strictEqual( typeof decompose, 'function', 'main export is a function' );
33+
t.end();
34+
});
35+
36+
tape( 'if provided a sequence length which is not a nonnegative integer, the function returns NaN', function test( t ) {
37+
var factors;
38+
var initial;
39+
var result;
40+
var values;
41+
var i;
42+
43+
values = [
44+
'5',
45+
-5,
46+
3.14,
47+
NaN,
48+
true,
49+
false,
50+
null,
51+
void 0,
52+
{},
53+
[],
54+
function foo() {}
55+
];
56+
57+
initial = [ 3, 4, 2, 5 ];
58+
factors = [ 0, 0, 0, 0, 0 ];
59+
60+
for ( i = 0; i < values.length; i++ ) {
61+
result = decompose( values[ i ], initial, factors, 1, 0 );
62+
t.strictEqual( isnan( result ), true, 'returns NaN when provided ' + values[ i ] );
63+
}
64+
t.end();
65+
});
66+
67+
tape( 'if provided an array of initial trial divisors which is not an array of nonnegative integers, the function returns NaN', function test( t ) {
68+
var factors;
69+
var result;
70+
var values;
71+
var i;
72+
73+
values = [
74+
'5',
75+
5,
76+
3.14,
77+
NaN,
78+
true,
79+
false,
80+
null,
81+
void 0,
82+
{},
83+
[ '1', '2' ],
84+
[ -1, 2, 3 ],
85+
[ 1.1, 2, 3 ],
86+
function foo() {}
87+
];
88+
89+
factors = [ 0, 0, 0, 0, 0 ];
90+
91+
for ( i = 0; i < values.length; i++ ) {
92+
result = decompose( 10, values[ i ], factors, 1, 0 );
93+
t.strictEqual( isnan( result ), true, 'returns NaN when provided ' + values[ i ] );
94+
}
95+
t.end();
96+
});
97+
98+
tape( 'if provided an output array which is not a collection, the function returns NaN', function test( t ) {
99+
var initial;
100+
var result;
101+
var values;
102+
var i;
103+
104+
values = [
105+
'5',
106+
5,
107+
3.14,
108+
NaN,
109+
true,
110+
false,
111+
null,
112+
void 0,
113+
{},
114+
function foo() {}
115+
];
116+
117+
initial = [ 3, 4, 2, 5 ];
118+
119+
for ( i = 0; i < values.length; i++ ) {
120+
result = decompose( 10, initial, values[ i ], 1, 0 );
121+
t.strictEqual( isnan( result ), true, 'returns NaN when provided ' + values[ i ] );
122+
}
123+
t.end();
124+
});
125+
126+
tape( 'if provided a stride which is not an integer, the function returns NaN', function test( t ) {
127+
var factors;
128+
var initial;
129+
var result;
130+
var values;
131+
var i;
132+
133+
values = [
134+
'5',
135+
3.14,
136+
NaN,
137+
true,
138+
false,
139+
null,
140+
void 0,
141+
{},
142+
[],
143+
function foo() {}
144+
];
145+
146+
initial = [ 3, 4, 2, 5 ];
147+
factors = [ 0, 0, 0, 0, 0 ];
148+
149+
for ( i = 0; i < values.length; i++ ) {
150+
result = decompose( 10, initial, factors, values[ i ], 0 );
151+
t.strictEqual( isnan( result ), true, 'returns NaN when provided ' + values[ i ] );
152+
}
153+
t.end();
154+
});
155+
156+
tape( 'if provided an offset which is not a nonnegative integer, the function returns NaN', function test( t ) {
157+
var factors;
158+
var initial;
159+
var result;
160+
var values;
161+
var i;
162+
163+
values = [
164+
'5',
165+
-5,
166+
3.14,
167+
NaN,
168+
true,
169+
false,
170+
null,
171+
void 0,
172+
{},
173+
[],
174+
function foo() {}
175+
];
176+
177+
initial = [ 3, 4, 2, 5 ];
178+
factors = [ 0, 0, 0, 0, 0 ];
179+
180+
for ( i = 0; i < values.length; i++ ) {
181+
result = decompose( 10, initial, factors, 1, values[ i ] );
182+
t.strictEqual( isnan( result ), true, 'returns NaN when provided ' + values[ i ] );
183+
}
184+
t.end();
185+
});
186+
187+
tape( 'the function correctly factorizes a sequence length into a product of integers', function test( t ) {
188+
var initial;
189+
var factors;
190+
var nf;
191+
192+
initial = [ 3, 4, 2, 5 ];
193+
factors = [ 0, 0, 0, 0, 0, 0, 0 ];
194+
nf = decompose( 630, initial, factors, 1, 0 );
195+
196+
t.strictEqual( nf, 5, 'returns expected number of factors' );
197+
t.strictEqual( factors[ 0 ], 630, 'stores sequence length' );
198+
t.strictEqual( factors[ 1 ], 5, 'stores number of factors' );
199+
t.strictEqual( factors[ 2 ], 2, 'stores first factor' );
200+
t.strictEqual( factors[ 3 ], 3, 'stores second factor' );
201+
t.strictEqual( factors[ 4 ], 3, 'stores third factor' );
202+
t.strictEqual( factors[ 5 ], 5, 'stores fourth factor' );
203+
t.strictEqual( factors[ 6 ], 7, 'stores fifth factor' );
204+
205+
initial = [ 3, 4, 2, 5 ];
206+
factors = [ 0, 0, 0, 0 ];
207+
nf = decompose( 8, initial, factors, 1, 0 );
208+
209+
t.strictEqual( nf, 2, 'returns expected number of factors' );
210+
t.strictEqual( factors[ 0 ], 8, 'stores sequence length' );
211+
t.strictEqual( factors[ 1 ], 2, 'stores number of factors' );
212+
t.strictEqual( factors[ 2 ], 2, 'stores first factor' );
213+
t.strictEqual( factors[ 3 ], 4, 'stores second factor' );
214+
215+
t.end();
216+
});
217+
218+
tape( 'the function correctly factorizes prime numbers', function test( t ) {
219+
var initial;
220+
var factors;
221+
var primes;
222+
var nf;
223+
var i;
224+
225+
initial = [ 3, 4, 2, 5 ];
226+
primes = [ 7, 11, 13, 17, 19, 23, 29, 31 ];
227+
228+
for ( i = 0; i < primes.length; i++ ) {
229+
factors = [ 0, 0, 0 ];
230+
nf = decompose( primes[ i ], initial, factors, 1, 0 );
231+
232+
t.strictEqual( nf, 1, 'returns expected number of factors for prime ' + primes[ i ] );
233+
t.strictEqual( factors[ 0 ], primes[ i ], 'stores sequence length' );
234+
t.strictEqual( factors[ 1 ], 1, 'stores number of factors' );
235+
t.strictEqual( factors[ 2 ], primes[ i ], 'stores the prime as its own factor' );
236+
}
237+
238+
t.end();
239+
});
240+
241+
tape( 'the function correctly handles stride and offset parameters', function test( t ) {
242+
var initial;
243+
var factors;
244+
var stride = 2;
245+
var offset = 1;
246+
var nf;
247+
248+
initial = [ 3, 4, 2, 5 ];
249+
250+
factors = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ];
251+
nf = decompose( 12, initial, factors, stride, offset );
252+
253+
t.strictEqual( nf, 2, 'returns expected number of factors' );
254+
t.strictEqual( factors[ offset ], 12, 'stores sequence length at offset' );
255+
t.strictEqual( factors[ offset+stride ], 2, 'stores number of factors at offset + stride' );
256+
t.strictEqual( factors[ offset+(2*stride) ], 3, 'stores first factor at offset + 2*stride' );
257+
t.strictEqual( factors[ offset+(3*stride) ], 4, 'stores second factor at offset + 3*stride' );
258+
259+
t.end();
260+
});

0 commit comments

Comments
 (0)