Skip to content

Commit

Permalink
move stack serialization test to tests/ folder, out of source tree
Browse files Browse the repository at this point in the history
  • Loading branch information
bmaranville committed Oct 16, 2024
1 parent f471b36 commit 19cb544
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions tests/refl1d/stack_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from refl1d.names import Slab, Stack, SLD
from copy import deepcopy
from bumps.serialize import serialize, deserialize
import pickle
import dill


def test_stack_serialization():
"""test that stack can be serialized and deserialized with all the methods we use,
preserving the functioning of the Calculation object for the total thickness"""
sample = Slab(SLD(rho=10), thickness=10) | Slab(SLD(rho=10), thickness=20) | Slab(SLD(rho=10), thickness=30)
thickness_plus = sample.thickness + 100 # expression

ser_t, ser_s = deserialize(serialize([thickness_plus, sample]))
assert ser_t.value == 160
ser_s[0].thickness.value += 40
assert ser_t.value == 200

dc_t, dc_s = deepcopy([thickness_plus, sample])
assert dc_t.value == 160
dc_s[0].thickness.value += 40
assert dc_t.value == 200

pickle_t, pickle_s = pickle.loads(pickle.dumps([thickness_plus, sample]))
assert pickle_t.value == 160
pickle_s[0].thickness.value += 40
assert pickle_t.value == 200

dill_t, dill_s = dill.loads(dill.dumps([thickness_plus, sample]))
assert dill_t.value == 160
dill_s[0].thickness.value += 40
assert dill_t.value == 200

assert thickness_plus.value == 160
sample[0].thickness.value += 40
assert thickness_plus.value == 200

0 comments on commit 19cb544

Please sign in to comment.