Skip to content

Instantly share code, notes, and snippets.

@Zac-HD
Last active July 11, 2020 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zac-HD/ea5d9586751e158d0578811a83fdc2b0 to your computer and use it in GitHub Desktop.
Save Zac-HD/ea5d9586751e158d0578811a83fdc2b0 to your computer and use it in GitHub Desktop.
This is a very simple test using https://hypothesis.readthedocs.io to assert that cycling write/read/write/read is lossless for plyfile.PlyData instances. Comment serialization is currently lossy (for eg. ascii whitespace) or throws exceptions (unicode encode errors when not ascii).
import string
from hypothesis import given, strategies as st
from hypothesis.extra.numpy import arrays
#import os, sys; sys.path.insert(0, os.path.abspath('.')); del sys; del os
import plyfile
# Simple strategy for a list of unicode strings
st_comments = st.lists(elements=st.text())
# A useful subset of all valid identifiers (no numbers as they may not lead)
st_valid_names = st.text(min_size=1, alphabet=string.ascii_letters + '_')
@st.composite
def st_structured_arrays(draw, min_size=1, max_size=20):
"""Create arrays that can be described as a valid PlyElement"""
st_dtypes = st.lists(elements=st.tuples(
st_valid_names, st.sampled_from(set(plyfile._data_type_reverse))),
min_size=1, unique_by=lambda d: d[0])
return draw(arrays(
draw(st_dtypes),
draw(st.integers(min_value=min_size, max_value=max_size)),
elements=st.binary()))
# Strategy to generate PlyElement instances. Note that this does not include
# list elements or other more complex datatypes.
st_PlyElement = st.builds(
target=plyfile.PlyElement.describe,
data=st_structured_arrays(),
name=st_valid_names,
comments=st_comments
)
# Strategy to generate arbitary PlyData instances
st_PlyData = st.builds(
target=plyfile.PlyData,
elements=st.lists(elements=st_PlyElement, unique_by=lambda e: e.name),
text=st.booleans(),
byte_order=st.sampled_from(plyfile._byte_order_map.values()),
comments=st_comments,
obj_info=st_comments
)
@given(st_PlyData)
def test_write_read_write(plydata):
first, second = open('test1.tmp', 'w+b'), open('test2.tmp', 'w+b')
plydata.write(first)
first.seek(0)
plyfile.PlyData.read(first).write(second)
first.seek(0); second.seek(0)
assert first.read() == second.read()
first.close(); second.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment