19EVIL = np.frombuffer(b
"\nend pat\n", dtype=np.uint8)[:8].tobytes()
22def values_of_patch(patch_id, name_offset=0.0, evil=False):
25 100 * patch_id + 10 * k + u + name_offset
26 for k
in range(DOF**DIMENSIONS)
27 for u
in range(UNKNOWNS)
32 result[0] = np.frombuffer(EVIL, dtype=np.float64)[0]
36def write_file(path, binary, patches=2, datasets=(
"Q",), evil=
False):
37 header = [
"# ",
"# Peano patch file ",
"# Version 0.2 ",
"# "]
38 header += [
"format Binary" if binary
else "format ASCII"]
39 header += [f
"dimensions {DIMENSIONS}",
"",
""]
40 out = (
"\n".join(header) +
"\n").encode()
44 f
'begin cell-metadata "{name}"\n'
45 f
" number-of-unknowns {UNKNOWNS}\n"
46 f
" number-of-dofs-per-axis {DOF}\n"
47 f
" description {name}\n"
48 f
"end cell-metadata\n\n"
51 for patch_id
in range(patches):
53 f
"begin patch\n offset {0.5 * patch_id} 0.0\n size 0.5 0.5\n"
55 for index, name
in enumerate(datasets):
56 values = values_of_patch(patch_id, 1000.0 * index)
57 out += f
' begin cell-values "{name}"\n'.encode()
59 raw = values.astype(np.float64).tobytes()
64 out += (
" " +
" ".join(repr(v)
for v
in values) +
"\n").encode()
65 out += b
" end cell-values\n"
66 out += b
"end patch\n\n"
70def parse(path, identifier="Q", patch_filter=None):
71 parser =
PatchFileParser(str(path), identifier, 0, patch_filter=patch_filter)
80@pytest.mark.parametrize("binary", [False, True])
82 write_file(tmp_path /
"f.peano-patch-file", binary)
83 parser =
parse(tmp_path /
"f.peano-patch-file")
85 assert parser.file_format == (
"Binary" if binary
else "ASCII")
86 patches = parser.unknown_attributes[
"Q"].cell_data
87 assert len(patches) == 2
88 for patch_id, patch
in enumerate(patches):
89 assert patch.offset == (0.5 * patch_id, 0.0)
90 assert np.array_equal(patch.values, values_of_patch(patch_id))
94 write_file(tmp_path /
"f.peano-patch-file",
True, evil=
True)
95 parser =
parse(tmp_path /
"f.peano-patch-file")
97 patches = parser.unknown_attributes[
"Q"].cell_data
98 assert len(patches) == 2
99 assert patches[0].values.tobytes()[:8] == EVIL
100 assert patches[1].values.tobytes() == values_of_patch(1, evil=
True).tobytes()
103@pytest.mark.parametrize("binary", [False, True])
106 tmp_path /
"f.peano-patch-file",
108 datasets=(
"A",
"B",
"C"),
111 parser =
parse(tmp_path /
"f.peano-patch-file", identifier=
"B")
113 assert list(parser.unknown_attributes.keys()) == [
"B"]
114 patches = parser.unknown_attributes[
"B"].cell_data
115 assert len(patches) == 2
117 patches[1].values.tobytes()
118 == values_of_patch(1, 1000.0, evil=binary).tobytes()
122@pytest.mark.parametrize("binary", [False, True])
124 write_file(tmp_path /
"f.peano-patch-file", binary, patches=3, evil=binary)
126 tmp_path /
"f.peano-patch-file",
127 patch_filter=
lambda offset, size: offset[0] >= 1.0,
130 patches = parser.unknown_attributes[
"Q"].cell_data
131 assert len(patches) == 1
132 assert patches[0].values.tobytes() == values_of_patch(2, evil=binary).tobytes()
parse(path, identifier="Q", patch_filter=None)
test_binary_quantities_may_hold_any_bytes(tmp_path)
test_other_datasets_are_stepped_over(tmp_path, binary)
test_text_and_binary_files_yield_the_same_patches(tmp_path, binary)
test_patch_filter_skips_patches(tmp_path, binary)