28 lines
958 B
Python
28 lines
958 B
Python
import numpy as np
|
|
from nebulaforge.robustness import inject_bitflips, detect_corruption, reproduce_flips
|
|
|
|
|
|
def test_inject_deterministic_and_detect():
|
|
a = np.arange(16, dtype=np.float32)
|
|
# use a noticeable rate so we flip some bits
|
|
flipped1, bundle = inject_bitflips(a, rate=0.01, seed=42)
|
|
|
|
# detector should say corruption relative to original
|
|
assert detect_corruption(flipped1, bundle)
|
|
|
|
# reproducing flips on original should yield identical bytes
|
|
reproduced = reproduce_flips(a, bundle)
|
|
assert reproduced.tobytes() == flipped1.tobytes()
|
|
|
|
|
|
def test_reproducible_with_same_seed():
|
|
a = np.linspace(-1.0, 1.0, num=64, dtype=np.float32)
|
|
f1, b1 = inject_bitflips(a, rate=0.005, seed=123)
|
|
f2, b2 = inject_bitflips(a, rate=0.005, seed=123)
|
|
|
|
# bundles should be identical for same seed
|
|
assert b1.seed == b2.seed
|
|
assert b1.flips == b2.flips
|
|
# flipped arrays equal
|
|
assert f1.tobytes() == f2.tobytes()
|