Skip to content

Commit 2f3e87e

Browse files
committed
Add dtype tests
1 parent f1746e9 commit 2f3e87e

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

test/test_dtypes.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""Tests for int32 default label dtype."""
2+
3+
import numpy as np
4+
import pytest
5+
6+
from linopy import Model
7+
from linopy.constants import DEFAULT_LABEL_DTYPE
8+
9+
10+
def test_default_label_dtype_is_int32():
11+
assert DEFAULT_LABEL_DTYPE == np.int32
12+
13+
14+
def test_variable_labels_are_int32():
15+
m = Model()
16+
x = m.add_variables(lower=0, upper=10, coords=[range(5)], name="x")
17+
assert x.labels.dtype == np.int32
18+
19+
20+
def test_constraint_labels_are_int32():
21+
m = Model()
22+
x = m.add_variables(lower=0, upper=10, coords=[range(5)], name="x")
23+
m.add_constraints(x >= 1, name="c")
24+
assert m.constraints["c"].labels.dtype == np.int32
25+
26+
27+
def test_expression_vars_are_int32():
28+
m = Model()
29+
x = m.add_variables(lower=0, upper=10, coords=[range(5)], name="x")
30+
expr = 2 * x + 1
31+
assert expr.vars.dtype == np.int32
32+
33+
34+
def test_solve_with_int32_labels():
35+
m = Model()
36+
x = m.add_variables(lower=0, upper=10, name="x")
37+
y = m.add_variables(lower=0, upper=10, name="y")
38+
m.add_constraints(x + y <= 15, name="c1")
39+
m.add_objective(x + 2 * y, sense="max")
40+
m.solve("highs")
41+
assert m.objective.value == pytest.approx(25.0)
42+
43+
44+
def test_overflow_guard_variables():
45+
m = Model()
46+
m._xCounter = np.iinfo(np.int32).max - 1
47+
with pytest.raises(ValueError, match="exceeds the maximum"):
48+
m.add_variables(lower=0, upper=1, coords=[range(5)], name="x")
49+
50+
51+
def test_overflow_guard_constraints():
52+
m = Model()
53+
x = m.add_variables(lower=0, upper=1, coords=[range(5)], name="x")
54+
m._cCounter = np.iinfo(np.int32).max - 1
55+
with pytest.raises(ValueError, match="exceeds the maximum"):
56+
m.add_constraints(x >= 0, name="c")

0 commit comments

Comments
 (0)