| 1 |
""" |
| 2 |
Some basic tests for the `Variable` system |
| 3 |
""" |
| 4 |
|
| 5 |
from collections import namedtuple |
| 6 |
|
| 7 |
import numpy as np |
| 8 |
import pytest |
| 9 |
|
| 10 |
from brian2.core.preferences import prefs |
| 11 |
from brian2.core.variables import * |
| 12 |
from brian2.units.allunits import second |
| 13 |
from brian2.units.fundamentalunits import Unit |
| 14 |
|
| 15 |
|
| 16 |
@pytest.mark.codegen_independent |
| 17 |
def test_construction_errors(): |
| 18 |
# Boolean variable that isn't dimensionless |
| 19 |
with pytest.raises(ValueError): |
| 20 |
Variable(name="name", dimensions=second.dim, dtype=bool) |
| 21 |
|
| 22 |
# Dynamic array variable that is constant but not constant in size |
| 23 |
with pytest.raises(ValueError): |
| 24 |
DynamicArrayVariable( |
| 25 |
name="name", |
| 26 |
owner=None, |
| 27 |
size=0, |
| 28 |
device=None, |
| 29 |
constant=True, |
| 30 |
needs_reference_update=True, |
| 31 |
) |
| 32 |
|
| 33 |
|
| 34 |
@pytest.mark.codegen_independent |
| 35 |
def test_str_repr(): |
| 36 |
# Basic test that the str/repr methods work |
| 37 |
FakeGroup = namedtuple("G", ["name"]) |
| 38 |
group = FakeGroup(name="groupname") |
| 39 |
variables = [ |
| 40 |
Variable(name="name", dimensions=second.dim), |
| 41 |
Constant(name="name", dimensions=second.dim, value=1.0), |
| 42 |
AuxiliaryVariable(name="name", dimensions=second.dim), |
| 43 |
ArrayVariable( |
| 44 |
name="name", dimensions=second.dim, owner=None, size=10, device=None |
| 45 |
), |
| 46 |
DynamicArrayVariable( |
| 47 |
name="name", dimensions=second.dim, owner=None, size=0, device=None |
| 48 |
), |
| 49 |
Subexpression( |
| 50 |
name="sub", dimensions=second.dim, expr="a+b", owner=group, device=None |
| 51 |
), |
| 52 |
] |
| 53 |
for var in variables: |
| 54 |
assert len(str(var)) |
| 55 |
# The repr value should contain the name of the class |
| 56 |
assert len(repr(var)) and var.__class__.__name__ in repr(var) |
| 57 |
|
| 58 |
|
| 59 |
@pytest.mark.codegen_independent |
| 60 |
def test_dtype_str(): |
| 61 |
FakeGroup = namedtuple("G", ["name"]) |
| 62 |
group = FakeGroup(name="groupname") |
| 63 |
for d in ["int32", "int64", "float32", "float64", "bool", "int", "float"]: |
| 64 |
nd = np.dtype(d) |
| 65 |
for var in [ |
| 66 |
Constant(name="name", value=np.zeros(1, dtype=nd)[0]), |
| 67 |
AuxiliaryVariable(name="name", dtype=nd), |
| 68 |
ArrayVariable(name="name", owner=None, size=10, device=None, dtype=nd), |
| 69 |
DynamicArrayVariable( |
| 70 |
name="name", owner=None, dtype=nd, size=0, device=None |
| 71 |
), |
| 72 |
Subexpression(name="sub", expr="a+b", owner=group, device=None, dtype=nd), |
| 73 |
]: |
| 74 |
assert var.dtype_str.startswith(d) |
| 75 |
|
| 76 |
|
| 77 |
if __name__ == "__main__": |
| 78 |
test_construction_errors() |
| 79 |
test_str_repr() |
| 80 |
test_dtype_str() |