Skip to content

Commit

Permalink
Add test_state_machine tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ezibenroc committed Aug 20, 2024
1 parent 7d4cd4b commit 18ebc0c
Showing 1 changed file with 116 additions and 0 deletions.
116 changes: 116 additions & 0 deletions test_state_machine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import hypothesis.strategies as st
from hypothesis.database import DirectoryBasedExampleDatabase
from hypothesis.stateful import Bundle, RuleBasedStateMachine, rule
from hypothesis import settings
from dataclasses import dataclass
from pyroaring import BitMap, BitMap64
from test64 import hyp_range, uint64

BitMapClass = BitMap64


@dataclass
class Collection:
test: BitMap
ref: set[int]

def check(self):
assert len(self.test) == len(self.ref)
assert set(self.test) == self.ref

def __post_init__(self):
self.check()


class SetComparison(RuleBasedStateMachine):
collections = Bundle("collections")

@rule(target=collections, val=hyp_range)
def init_collection(self, val):
return Collection(test=BitMapClass(val), ref=set(val))

@rule(target=collections, col=collections)
def copy(self, col):
return Collection(test=BitMapClass(col.test), ref=set(col.ref))

@rule(col=collections, val=uint64)
def add_elt(self, col, val):
col.test.add(val)
col.ref.add(val)
col.check()

@rule(col=collections, val=uint64)
def remove_elt(self, col, val):
col.test.discard(val)
col.ref.discard(val)
col.check()

@rule(target=collections, col1=collections, col2=collections)
def union(self, col1, col2):
return Collection(test=col1.test | col2.test, ref=col1.ref | col2.ref)

@rule(col1=collections, col2=collections)
def union_inplace(self, col1, col2):
col1.test |= col2.test
col1.ref |= col2.ref
col1.check()

@rule(target=collections, col1=collections, col2=collections)
def intersection(self, col1, col2):
return Collection(test=col1.test & col2.test, ref=col1.ref & col2.ref)

@rule(col1=collections, col2=collections)
def intersection_inplace(self, col1, col2):
col1.test &= col2.test
col1.ref &= col2.ref
col1.check()

@rule(target=collections, col1=collections, col2=collections)
def difference(self, col1, col2):
return Collection(test=col1.test - col2.test, ref=col1.ref - col2.ref)

@rule(col1=collections, col2=collections)
def difference_inplace(self, col1, col2):
col1.test -= col2.test
col1.ref -= col2.ref
col1.check()

@rule(target=collections, col1=collections, col2=collections)
def symmetric_difference(self, col1, col2):
return Collection(test=col1.test ^ col2.test, ref=col1.ref ^ col2.ref)

@rule(col1=collections, col2=collections)
def symmetric_difference_inplace(self, col1, col2):
col1.test ^= col2.test
col1.ref ^= col2.ref
col1.check()

@rule(
target=collections,
col=collections,
start=st.integers(min_value=0, max_value=2**40),
size=st.integers(min_value=0, max_value=2**18),
)
def flip(self, col, start, size):
stop = start + size
return Collection(
test=col.test.flip(start, stop), ref=col.ref ^ set(range(start, stop))
)

@rule(
col=collections,
start=st.integers(min_value=0, max_value=2**40),
size=st.integers(min_value=0, max_value=2**18),
)
def flip_inplace(self, col, start, size):
stop = start + size
col.test.flip_inplace(start, stop)
col.ref ^= set(range(start, stop))
col.check()


TestTrees = SetComparison.TestCase
TestTrees.settings = settings(max_examples=100, stateful_step_count=200)

if __name__ == "__main__":
unittest.main()

0 comments on commit 18ebc0c

Please sign in to comment.