Skip to content

Commit d853256

Browse files
tests: add some property based tests (#540)
* tests: add some property based tests * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 2fef7a0 commit d853256

File tree

3 files changed

+70
-14
lines changed

3 files changed

+70
-14
lines changed

setup.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,7 @@
4949

5050

5151
extras = {
52-
"test": [
53-
"pytest",
54-
"pytest-benchmark",
55-
"typing_extensions; python_version<'3.8'",
56-
"cloudpickle",
57-
],
52+
"test": ["pytest", "pytest-benchmark", "cloudpickle", "hypothesis >=6.0"],
5853
"docs": [
5954
"Sphinx~=3.0",
6055
"recommonmark>=0.5.0",

tests/test_accumulators.py

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
import pytest
12
from pytest import approx
23

34
import boost_histogram as bh
45

6+
hypothesis = pytest.importorskip("hypothesis")
7+
st = pytest.importorskip("hypothesis.strategies")
8+
59

610
def test_weighted_sum():
711
a = bh.accumulators.WeightedSum(1.5, 2.5)
@@ -89,15 +93,25 @@ def test_mean():
8993
assert repr(a) == "Mean(count=3, value=2, variance=1)"
9094

9195

92-
def test_sum_mean():
96+
float_st = st.floats(
97+
allow_nan=False, allow_infinity=False, min_value=-1e30, max_value=1e30
98+
)
99+
simple_list_st = st.lists(float_st, min_size=1, max_size=10)
100+
101+
102+
@hypothesis.given(
103+
simple_list_st,
104+
simple_list_st,
105+
)
106+
def test_sum_mean(list1, list2):
93107
a = bh.accumulators.Mean()
94-
a.fill([1, 2, 3])
108+
a.fill(list1)
95109

96110
b = bh.accumulators.Mean()
97-
b.fill([5, 6])
111+
b.fill(list2)
98112

99113
c = bh.accumulators.Mean()
100-
c.fill([1, 2, 3, 5, 6])
114+
c.fill(list1 + list2)
101115

102116
ab = a + b
103117
assert ab.value == approx(c.value)
@@ -110,15 +124,30 @@ def test_sum_mean():
110124
assert a.count == approx(c.count)
111125

112126

113-
def test_sum_weighed_mean():
127+
dual_lists_st = st.integers(min_value=1, max_value=10).flatmap(
128+
lambda n: st.tuples(
129+
st.lists(float_st, min_size=n, max_size=n),
130+
st.lists(
131+
st.floats(
132+
allow_nan=False, allow_infinity=False, min_value=1e-30, max_value=1e30
133+
),
134+
min_size=n,
135+
max_size=n,
136+
),
137+
)
138+
)
139+
140+
141+
@hypothesis.given(dual_lists_st, dual_lists_st)
142+
def test_sum_weighed_mean(pair1, pair2):
114143
a = bh.accumulators.WeightedMean()
115-
a.fill([1, 2, 3], weight=[2, 5, 3])
144+
a.fill(pair1[0], weight=pair1[1])
116145

117146
b = bh.accumulators.WeightedMean()
118-
b.fill([5, 6], weight=[12, 17])
147+
b.fill(pair2[0], weight=pair2[1])
119148

120149
c = bh.accumulators.WeightedMean()
121-
c.fill([1, 2, 3, 5, 6], weight=[2, 5, 3, 12, 17])
150+
c.fill(pair1[0] + pair2[0], weight=pair1[1] + pair2[1])
122151

123152
ab = a + b
124153
assert ab.value == approx(c.value)

tests/test_pbt.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import numpy as np
2+
import pytest
3+
from pytest import approx
4+
5+
import boost_histogram as bh
6+
7+
hypothesis = pytest.importorskip("hypothesis")
8+
nst = pytest.importorskip("hypothesis.extra.numpy")
9+
10+
11+
@hypothesis.given(
12+
nst.arrays(
13+
float,
14+
(4,),
15+
elements=dict(min_value=1, max_value=100, exclude_min=True, allow_nan=False),
16+
),
17+
nst.arrays(
18+
float, (4,), elements=dict(min_value=-100, max_value=100, allow_nan=False)
19+
),
20+
nst.arrays(
21+
float,
22+
(4,),
23+
elements=dict(min_value=0, max_value=100, allow_nan=False, exclude_min=True),
24+
),
25+
)
26+
def test_variance_setting(cnt, val, var):
27+
h = bh.Histogram(bh.axis.Regular(4, 0, 1), storage=bh.storage.Mean())
28+
h[...] = np.stack([cnt, val, var * cnt], axis=-1)
29+
30+
assert h.counts() == approx(cnt)
31+
assert h.values() == approx(val)
32+
assert h.variances() == approx(var) # , abs=1e-3, rel=1e-3)

0 commit comments

Comments
 (0)