Skip to content
Snippets Groups Projects
Commit 67c837dc authored by Michael Maier's avatar Michael Maier
Browse files

tests maxzerosumset 2

parents 76d247a0 cd556682
No related branches found
No related tags found
No related merge requests found
......@@ -11,7 +11,7 @@ Example
>>> zerosumset.zerosumset(elems)
[-3, -2, 5]
>>> zerosumset.zerosumset(3, 1, 10, -4)
>>> zerosumset.zerosumset([3, 1, 10, -4])
[3, 1, -4]
```
......
# pytest.ini
[pytest]
addopts = --doctest-modules --hypothesis-show-statistics
testpaths =
zerosumset
tests
markers =
timing_test
import sys
sys.path.append('../zerosumset')
import pytest
from zerosumset import *
test_cases_identical = [[], [1, 2, -3], [0]]
@pytest.mark.parametrize("elems", test_cases_identical)
def test_zerosumset(elems):
output = zerosumset(elems)
assert output == elems, f"Failed: expected {elems}"
from hypothesis import given, strategies as st, event, assume
from random import randint
@st.composite
def gen_solvable(draw, min_size=5, max_size=10):
xs = draw(st.lists(st.integers(), min_size=5, max_size=10))
n = draw(st.integers(min_value=1))
l = len(xs)
pos1 = randint(0, l-1)
xs.insert(pos1, n)
pos2 = randint(0, l)
xs.insert(pos2, -n)
return xs
@given(gen_solvable())
# @given(st.lists(st.integers(), min_size=5, max_size=10).filter(lambda xs: not all(x > 0 for x in xs)))
def test_sum_is_zero(xs):
# assume(not all(x > 0 for x in xs))
subxs = zerosumset(xs)
if not subxs: event("null result")
assert sum(subxs) == 0, f"Result doesn't have zero sum: {subxs}"
@given(gen_solvable())
def test_zero_included(xs):
subxs1 = maxzerosumset(xs)
pos = randint(0, len(xs) - 1)
xs.insert(pos, 0)
subxs2 = maxzerosumset(xs)
assert len(subxs2) >= len(subxs1)
@given(gen_solvable())
def test_has_same_amount_of_zeros(xs):
subxs = maxzerosumset(xs)
assert len(list(filter(lambda x: x == 0, xs))) == len(list(filter(lambda x: x == 0, subxs))), f"Result doesn't have the same amount of zeros as in the given list"
@st.composite
def gen_solvable_pair(draw, min_size=5, max_size=10):
# doesn't work
# xs = gen_solvable()
# xs2 = xs.copy()
xs = draw(st.lists(st.integers(), min_size=5, max_size=10))
n = draw(st.integers(min_value=1))
l = len(xs)
pos1 = randint(0, l - 1)
xs.insert(pos1, n)
pos2 = randint(0, l)
xs.insert(pos2, -n)
xs2 = xs.copy()
n = draw(st.integers(min_value=1))
l = len(xs)
pos1 = randint(0, l - 1)
xs2.insert(pos1, n)
pos2 = randint(0, l)
xs2.insert(pos2, -n)
pair = [xs, xs2]
return pair
# sometimes is not passed somehow
@given(gen_solvable_pair())
def test_insert_new_numbers(pair):
subxs1 = maxzerosumset(pair[0])
subxs2 = maxzerosumset(pair[1])
assert len(subxs2) >= len(subxs1) + 2, f"The second list is not longer"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment