-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
from collections import defaultdict | ||
from typing import NamedTuple | ||
|
||
import numpy as np | ||
from numpy.testing import assert_allclose | ||
import pytest | ||
|
||
from randomgen.tests._shims import cont_0, cont_1, cont_2, cont_3, cont_3_alt_cons | ||
|
||
|
||
class Config(NamedTuple): | ||
a: float | np.ndarray | None | ||
b: float | np.ndarray | None | ||
c: float | np.ndarray | None | ||
size: tuple[int, ...] | None | ||
out: np.ndarray | None | ||
|
||
|
||
CONFIGS = defaultdict(list) | ||
|
||
|
||
def all_scalar(*args): | ||
return all([(arg is None or np.isscalar(arg)) for arg in args]) | ||
|
||
|
||
def get_broadcastable_size(a, b, c): | ||
Check notice Code scanning / CodeQL Explicit returns mixed with implicit (fall through) returns Note test
Mixing implicit and explicit returns may indicate an error as implicit returns always return None.
|
||
if c is not None: | ||
return (a + b + c).shape | ||
if b is not None: | ||
return (a + b).shape | ||
if a is not None: | ||
return a.shape | ||
|
||
|
||
def count_params(a, b, c): | ||
return sum((v is not None) for v in (a, b, c)) | ||
|
||
|
||
for a in (None, 0.5, 0.5 * np.ones((1, 2)), 0.5 * np.ones((3, 2))): | ||
for b in ( | ||
None, | ||
0.5, | ||
0.5 * np.ones((1, 2)), | ||
0.5 * np.ones((3, 1)), | ||
0.5 * np.ones((3, 2)), | ||
): | ||
if a is None and b is not None: | ||
continue | ||
for c in ( | ||
None, | ||
1.5, | ||
1.5 * np.ones((1, 2)), | ||
1.5 * np.ones((3, 1)), | ||
1.5 * np.ones((3, 2)), | ||
): | ||
if b is None and c is not None: | ||
continue | ||
for size in (True, False): | ||
for out in (True, False): | ||
if size: | ||
if all_scalar(a, b, c): | ||
_size = (7, 5) | ||
else: | ||
_size = get_broadcastable_size(a, b, c) | ||
else: | ||
_size = None | ||
if out: | ||
if size: | ||
_out = np.empty(_size) | ||
elif all_scalar(a, b, c): | ||
_out = np.empty((11, 7)) | ||
else: | ||
_out = np.empty(get_broadcastable_size(a, b, c)) | ||
else: | ||
_out = None | ||
print(_size, _out.shape if isinstance(_out, np.ndarray) else _out) | ||
CONFIGS[count_params(a, b, c)].append(Config(a, b, c, _size, _out)) | ||
|
||
|
||
@pytest.mark.parametrize("config", CONFIGS[0]) | ||
def test_cont_0(config): | ||
res = cont_0(size=config.size, out=config.out) | ||
if isinstance(res, np.ndarray): | ||
assert_allclose(res, 3.141592 * np.ones_like(res)) | ||
else: | ||
assert_allclose(res, 3.141592) | ||
|
||
|
||
@pytest.mark.parametrize("config", CONFIGS[1]) | ||
def test_cont_1(config): | ||
res = cont_1(config.a, size=config.size, out=config.out) | ||
if isinstance(res, np.ndarray): | ||
assert_allclose(res, 0.5 * np.ones_like(res)) | ||
else: | ||
assert_allclose(res, 0.5) | ||
|
||
|
||
@pytest.mark.parametrize("config", CONFIGS[2]) | ||
def test_cont_2(config): | ||
res = cont_2(config.a, config.b, size=config.size, out=config.out) | ||
if isinstance(res, np.ndarray): | ||
assert_allclose(res, np.ones_like(res)) | ||
else: | ||
assert_allclose(res, 1.0) | ||
|
||
|
||
@pytest.mark.parametrize("config", CONFIGS[3]) | ||
def test_cont_3(config): | ||
res = cont_3(config.a, config.b, config.c, size=config.size, out=config.out) | ||
if isinstance(res, np.ndarray): | ||
assert_allclose(res, 2.5 * np.ones_like(res)) | ||
else: | ||
assert_allclose(res, 2.5) | ||
|
||
|
||
@pytest.mark.parametrize("config", CONFIGS[3]) | ||
def test_cont_3_alt_cons(config): | ||
res = cont_3_alt_cons( | ||
1.0 + config.a, config.b, config.c, size=config.size, out=config.out | ||
) | ||
if isinstance(res, np.ndarray): | ||
assert_allclose(res, 3.5 * np.ones_like(res)) | ||
else: | ||
assert_allclose(res, 3.5) | ||