Skip to content

Commit a246495

Browse files
committed
test: add tests and its fixtures
--- 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: na - 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: passed - 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 858b167 commit a246495

File tree

5 files changed

+241
-0
lines changed

5 files changed

+241
-0
lines changed

lib/node_modules/@stdlib/math/base/special/chebyshev-t-polynomial/test/fixtures/python/large_n.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

lib/node_modules/@stdlib/math/base/special/chebyshev-t-polynomial/test/fixtures/python/medium_n.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/bin/env python
2+
#
3+
# @license Apache-2.0
4+
#
5+
# Copyright (c) 2025 The Stdlib Authors.
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
19+
"""Generate fixtures."""
20+
21+
import os
22+
import json
23+
import numpy as np
24+
from scipy.special import eval_chebyt
25+
26+
# Set a seed for reproducibility:
27+
np.random.seed(1234)
28+
29+
# Get the file path:
30+
FILE = os.path.realpath(__file__)
31+
32+
# Extract the directory in which this file resides:
33+
DIR = os.path.dirname(FILE)
34+
35+
36+
def gen(x, n, name):
37+
"""Generate fixture data and write to file.
38+
39+
# Arguments
40+
41+
* `x`: domain
42+
* `n`: domain
43+
* `name::str`: output filename
44+
45+
# Examples
46+
47+
``` python
48+
python> x = linspace(0, 1, 2001)
49+
python> n = linspace(0.1, 1000, 2001)
50+
python> gen(x, n, './data.json')
51+
```
52+
"""
53+
y = eval_chebyt(n, x)
54+
55+
# Store data to be written to file as a dictionary:
56+
data = {
57+
"x": x.tolist(),
58+
"n": n.tolist(),
59+
"expected": y.tolist()
60+
}
61+
62+
# Based on the script directory, create an output filepath:
63+
filepath = os.path.join(DIR, name)
64+
65+
# Write the data to the output filepath as JSON:
66+
with open(filepath, "w", encoding="utf-8") as outfile:
67+
json.dump(data, outfile)
68+
69+
70+
def main():
71+
"""Generate fixture data."""
72+
x = np.linspace(-1, 1, 1000)
73+
74+
# Small degree:
75+
n = np.random.randint(0, 10, 1000)
76+
gen(x, n, "small_n.json")
77+
78+
# Medium degree:
79+
n = np.random.randint(10, 50, 1000)
80+
gen(x, n, "medium_n.json")
81+
82+
# Large degree:
83+
n = np.random.randint(50, 100, 1000)
84+
gen(x, n, "large_n.json")
85+
86+
87+
if __name__ == "__main__":
88+
main()

lib/node_modules/@stdlib/math/base/special/chebyshev-t-polynomial/test/fixtures/python/small_n.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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 abs = require( '@stdlib/math/base/special/abs' );
26+
var EPS = require( '@stdlib/constants/float64/eps' );
27+
var chebyshevtpoly = require( './../lib' );
28+
29+
30+
// FIXTURES //
31+
32+
var smallN = require( './fixtures/python/small_n.json' );
33+
var mediumN = require( './fixtures/python/medium_n.json' );
34+
var largeN = require( './fixtures/python/large_n.json' );
35+
36+
37+
// TESTS //
38+
39+
tape( 'main export is a function', function test( t ) {
40+
t.ok( true, __filename );
41+
t.strictEqual( typeof chebyshevtpoly, 'function', 'main export is a function' );
42+
t.end();
43+
});
44+
45+
tape( 'if provided `NaN` for any parameter, the function returns `NaN`', function test( t ) {
46+
var val = chebyshevtpoly( NaN, 2.0 );
47+
t.ok( isnan( val ), 'returns expected value' );
48+
val = chebyshevtpoly( 2.0, NaN );
49+
t.ok( isnan( val ), 'returns expected value' );
50+
t.end();
51+
});
52+
53+
tape( 'if provided a negative number for `n` and a valid `x`, the function returns `NaN`', function test( t ) {
54+
var val = chebyshevtpoly( -2.0, 0.5 );
55+
t.ok( isnan( val ), 'returns expected value' );
56+
val = chebyshevtpoly( -4.0, -0.5 );
57+
t.ok( isnan( val ), 'returns expected value' );
58+
val = chebyshevtpoly( -4.5, -0.5 );
59+
t.ok( isnan( val ), 'returns expected value' );
60+
t.end();
61+
});
62+
63+
tape( 'if provided a `n` which is not a nonnegative integer and a valid `x`, the function returns `NaN`', function test( t ) {
64+
var val = chebyshevtpoly( 2.5, 0.5 );
65+
t.ok( isnan( val ), 'returns expected value' );
66+
val = chebyshevtpoly( 0.5, -0.5 );
67+
t.ok( isnan( val ), 'returns expected value' );
68+
t.end();
69+
});
70+
71+
tape( 'the function evaluates the chebyshev polynomial for `x` given small degree `n`', function test( t ) {
72+
var expected;
73+
var delta;
74+
var tol;
75+
var x;
76+
var n;
77+
var y;
78+
var i;
79+
80+
expected = smallN.expected;
81+
x = smallN.x;
82+
n = smallN.n;
83+
for ( i = 0; i < x.length; i++ ) {
84+
y = chebyshevtpoly( n[i], x[i] );
85+
if ( y === expected[i] ) {
86+
t.equal( y, expected[i], 'x: '+x[i]+'. n:'+n[i]+', y: '+y+', expected: '+expected[i] );
87+
} else {
88+
delta = abs( y - expected[ i ] );
89+
90+
// NOTE: The tolerance is higher due to the numerical differences between the `hyp2f1` implementation in JavaScript and its reference implementation in SciPy.
91+
tol = 755.0 * EPS * abs( expected[ i ] );
92+
t.ok( delta <= tol, 'within tolerance. x: '+x[ i ]+'. n: '+n[i]+'. y: '+y+'. E: '+expected[ i ]+'. Δ: '+delta+'. tol: '+tol+'.' );
93+
}
94+
}
95+
t.end();
96+
});
97+
98+
tape( 'the function evaluates the chebyshev polynomial for `x` given medium degree `n`', function test( t ) {
99+
var expected;
100+
var delta;
101+
var tol;
102+
var x;
103+
var n;
104+
var y;
105+
var i;
106+
107+
expected = mediumN.expected;
108+
x = mediumN.x;
109+
n = mediumN.n;
110+
for ( i = 0; i < x.length; i++ ) {
111+
y = chebyshevtpoly( n[i], x[i] );
112+
if ( y === expected[i] ) {
113+
t.equal( y, expected[i], 'x: '+x[i]+'. n:'+n[i]+', y: '+y+', expected: '+expected[i] );
114+
} else {
115+
delta = abs( y - expected[ i ] );
116+
117+
// NOTE: The tolerance is higher due to the numerical differences between the `hyp2f1` implementation in JavaScript and its reference implementation in SciPy.
118+
tol = 5836.0 * EPS * abs( expected[ i ] );
119+
t.ok( delta <= tol, 'within tolerance. x: '+x[ i ]+'. n: '+n[i]+'. y: '+y+'. E: '+expected[ i ]+'. Δ: '+delta+'. tol: '+tol+'.' );
120+
}
121+
}
122+
t.end();
123+
});
124+
125+
tape( 'the function evaluates the chebyshev polynomial for `x` given large degree `n`', function test( t ) {
126+
var expected;
127+
var delta;
128+
var tol;
129+
var x;
130+
var n;
131+
var y;
132+
var i;
133+
134+
expected = largeN.expected;
135+
x = largeN.x;
136+
n = largeN.n;
137+
for ( i = 0; i < x.length; i++ ) {
138+
y = chebyshevtpoly( n[i], x[i] );
139+
if ( y === expected[i] ) {
140+
t.equal( y, expected[i], 'x: '+x[i]+'. n: '+n[i]+', y: '+y+', expected: '+expected[i] );
141+
} else {
142+
delta = abs( y - expected[ i ] );
143+
144+
// NOTE: The tolerance is higher due to the numerical differences between the `hyp2f1` implementation in JavaScript and its reference implementation in SciPy.
145+
tol = 6591.0 * EPS * abs( expected[ i ] );
146+
t.ok( delta <= tol, 'within tolerance. x: '+x[ i ]+'. n: '+n[i]+'. y: '+y+'. E: '+expected[ i ]+'. Δ: '+delta+'. tol: '+tol+'.' );
147+
}
148+
}
149+
t.end();
150+
});

0 commit comments

Comments
 (0)