test_life.py
2.1 kB · python · 70 lines
1import json23import numpy as np45from mrlypy import life6from mrlypy.life import Config, Counts, Source7from mrlypy.math import two89FATES = {"Dead": "dead", "Alive": "alive", "Loop": "loop", "Timeout": "timeout"}101112def cell(spec):13 return {"types": np.array(spec["data"], dtype=np.uint8).reshape(spec["shape"])}141516def run(spec):17 c = spec["config"]18 config = Config(life.moore(), Counts.list(c["birth"]), Counts.list(c["survive"]))19 assert config.boundary == c["boundary"]20 assert config.max_generations == c["max_generations"]21 assert config.grid_size == c["grid_size"]22 assert config.padding == c["padding"]23 return life.animate(cell(spec["seed"]), config)242526def test_history(row):27 r = row("life::history")28 i = r["in"]29 space = life.history(i["row"], i["rule"], i["steps"], i["wrap"])30 assert list(space.shape) == r["out"]["shape"]31 assert space.flatten().tolist() == r["out"]["data"]323334def test_animate(row):35 r = row("life::animate::animate")36 life_run = run(r["in"])37 assert FATES[life_run.fate] == r["out"]["fate"]38 assert life_run.count == r["out"]["count"]39 assert life_run.loop_length == r["out"]["loop_length"]40 assert json.loads(two.to_json(life_run.last())) == r["out"]["last"]414243def test_entropy(row):44 r = row("life::entropy")45 inner = r["in"]["grid"]["in"]46 assert life.entropy(two.carpet(inner["number"], inner["level"])) == r["out"]474849def test_churn(row):50 r = row("life::churn")51 blank, carpet = r["in"]["grids"]52 grids = [53 {"types": np.zeros(blank["zeros"]["shape"], dtype=np.uint8)},54 two.carpet(carpet["in"]["number"], carpet["in"]["level"]),55 ]56 assert round(life.churn(grids), 12) == r["out"]575859def test_counts(row):60 r = row("life::counts")61 i = r["in"]62 seq = Source.parse(i["seq"])63 assert life.counts(seq, i["max_neighbors"], i["include_zeros"], i["include_ones"]) == r["out"]646566def test_heatmap(row):67 r = row("life::heatmap")68 life_run = run(r["in"]["grids"]["in"])69 frames = life.heatmap(life_run.grids, r["in"]["scale"])70 assert [list(frame) for frame in frames] == r["out"]