extends SceneTree const E = preload("res://world/owniverse/math/exact.gd") const Clock = preload("res://world/owniverse/simulation_time.gd") var checks: int = 0 var failures: int = 0 func _initialize() -> void: call_deferred("run") func check(condition: bool, message: String) -> void: checks = E.add(checks, 1) if (not condition): failures = E.add(failures, 1) push_error(message) func fixture(): var world = load("res://world/owniverse/owniverse.gd").new() var global: Node = root.get_node("Global") for id in global.OwniverseEntities: var entity = load("res://world/owniverse/owniverse_entity.gd").new(global.OwniverseEntities[id]) world.catalog.entities[id] = entity world.state.amount[id] = 0 world.state.stats.total_amount[id] = 0 if E.ne(entity.group_as, ""): world.state.amount[entity.group_as] = 0 world.state.stats.total_amount[entity.group_as] = 0 world.catalog.entities["N"].life_product = {} world.catalog.entities["N"].product_per_year_type = "H" world.catalog.entities["N"].product_per_year_amount = 10 world.catalog.entities["N"].auto = 0 world.state.amount.N = 1 return world func dispose(world) -> void: for entity in world.catalog.entities.values(): entity.free() world.free() func run() -> void: var world = fixture() world.catalog.entities.N.lifespan = 100 world.batches.append_batch(world.state, "N", 2, 10, 10) world.batches.append_batch(world.state, "N", 3, 10, 10) check(world.state.batches.N.size() == 1 and E.eq(world.state.batches.N[0].amount, 5), "Repeated insertion merges using the cohort index") world.batches.append_batch(world.state, "N", 1, 0, 0) check(E.eq(world.state.batches.N[0].created_from, 0), "Earlier insertion preserves chronological order") var replaced: Array = world.state.batches.N.duplicate(true) world.state.batches.N = replaced world.batches.append_batch(world.state, "N", 7, 10, 10) check(E.eq(replaced[1].amount, 12), "Replacing cohort array invalidates cached references") var separate: Dictionary = world.state.duplicate(true) world.batches.append_batch(separate, "N", 9, 10, 10) check(E.eq(separate.batches.N[1].amount, 21) and E.eq(world.state.batches.N[1].amount, 12), "Index is isolated between transaction states") dispose(world) world = fixture() for speed in range(1, 7): world.state.speed = speed check(E.eq(world.batches.window_duration(world.state, 100), 2.5 if speed < 4 else pow(10, speed - 2)), "Windows follow selected speed") check(E.eq(world.batches.window_duration(world.state, 5), 5), "Short explosions retain their timing") var huge: Variant = E.of("10000000000000000000000000000000000000000") world.catalog.entities.N.lifespan = 100 world.state.batches.N = [ {"amount": huge, "start_year": 100, "end_year": 110, "created_from": 0, "created_to": 10, "withdraw_remainder": 0.25}, {"amount": 7, "start_year": 120, "end_year": 130, "created_from": 20, "created_to": 30, "withdraw_remainder": 0.5} ] world.batches.coarsen(world.state) check(world.state.batches.N.size() == 1, "High speed combines nearby cohorts") check(E.eq(world.state.batches.N[0].amount, E.add(huge, 7)), "Coarsening preserves exact large whole counts") check(E.eq(world.state.batches.N[0].withdraw_remainder, 0.75), "Coarsening preserves withdrawal carry") var combined: Dictionary = world.state.batches.N[0].duplicate(true) world.batches.coarsen(world.state) check(world.state.batches.N[0] == combined, "Coarsening is idempotent") world.state.speed = 1 world.batches.coarsen(world.state) check(world.state.batches.N[0] == combined, "Slowing down does not invent finer history") dispose(world) world = fixture() world.state.batch_origins.LGRB = E.of("10357034.80000000074505806") var boundary: Variant = E.of("41215289.79999999701976776") for interval in [[E.subtract(boundary, E.of("2.5")), E.add(boundary, E.of("2.25"))], [boundary, E.add(boundary, 2)]]: world.state.batches.LGRB = [] world.batches.append_batch(world.state, "LGRB", 100, interval[0], interval[1]) var total: Variant = 0 var last: Variant = E.neg(INF) for batch : Dictionary in world.state.batches.LGRB: total = E.add(total, batch.amount) check(E.gt(batch.created_from, last), "Batch windows advance monotonically") check(E.gt(batch.created_to, batch.created_from), "Positive window duration") check(E.ge(batch.amount, 0), "No negative batch amounts") last = batch.created_from check(E.eq(total, 100), "Boundary correction preserves all objects") check(E.le(world.state.batches.LGRB.size(), 7), "Finite number of windows") dispose(world) world = fixture() world.batches.append_batch(world.state, "N", 100, 100, 100) var first: Dictionary = world.state.batches.N[0].duplicate(true) world.batches.append_batch(world.state, "N", 7, 100, 100) check(world.state.batches.N.size() == 1 and E.eq(world.state.batches.N[0].amount, 107), "Repeated births merge directly into one window") check(E.eq(world.state.batches.N[0].start_year, first.start_year), "Direct merge preserves transition time") check(world.batches.withdraw_batches(world.state, "N", 0).is_empty(), "Future cohorts are not withdrawn") var untouched: Array = world.state.batches.N check(world.batches.withdraw_batches(world.state, "N", 1).is_empty() and is_same(untouched, world.state.batches.N), "Readiness index skips future cohorts") world.state.batches.N = [{"amount": 7, "start_year": 0, "end_year": 0, "created_from": 0, "created_to": 0}] check(world.batches.withdraw_batches(world.state, "N", 1).size() == 1, "Replacing cohorts invalidates readiness index") world.batches.append_batch(world.state, "N", 3, 100, 100) check(E.eq(world.state.batches.N[0].amount, 3), "Replacing cohorts invalidates instant merge cache") world.batches.withdraw_batches(world.state, "N", 0) world.batches.scale_batches(world.state, "N", 0) world.batches.withdraw_batches(world.state, "N", 1) check(world.state.batches.N.is_empty(), "Scaling invalidates readiness and removes zero cohorts") dispose(world) for selected_speed in [4, 5, 6]: world = fixture() world.speed = selected_speed world.state.time = {"datetime": 37, "year": 37} var cycle_width: Variant = E.multiply(E.power(10, selected_speed - 1), E.of("0.2")) world.catalog.entities.HG.lifespan = E.multiply(cycle_width, 3) world.state.batches = {} var life: Variant = world.batches.effective_lifespan(world.state, "HG") var deadline: Variant = E.add(37, E.multiply(cycle_width, 4)) world.batches.append_batch(world.state, "HG", huge, E.add(37, 1), E.add(37, 1)) world.batches.append_batch(world.state, "HG", 7, E.add(37, 2), E.add(37, 2)) check(world.state.batches.HG.size() == 1 and E.eq(world.state.batches.HG[0].amount, E.add(huge, 7)), "Births in the same death cycle merge exactly") var point: Dictionary = world.state.batches.HG[0] check(E.eq(point.start_year, deadline) and E.eq(point.end_year, deadline), "High-speed stellar cohort transitions at one cycle boundary") var born: Variant = E.add(37, 1) var delay: Variant = E.subtract(deadline, E.add(born, life)) check(E.ge(delay, 0) and E.le(delay, cycle_width), "Transition is delayed by at most one cycle") var encoded: Dictionary = E.decode(E.encode(world.state)) check(E.eq(encoded.batches, world.state.batches), "Cycle cohorts survive exact save roundtrip") check(world.batches.withdraw_batches(world.state, "HG", E.subtract(deadline, 1)).is_empty(), "Cycle cohort is not withdrawn partially") var expired: Array = world.batches.withdraw_batches(world.state, "HG", deadline) check(expired.size() == 1 and E.eq(expired[0].amount, E.add(huge, 7)) and world.state.batches.HG.is_empty(), "Boundary withdraws the exact whole cohort once") check(world.batches.withdraw_batches(world.state, "HG", deadline).is_empty(), "Repeated boundary cannot withdraw twice") world.batches.append_batch(world.state, "HG", 3, E.add(37, 1), E.add(37, 1)) check(E.eq(world.state.batches.HG[0].amount, 3), "Withdrawal invalidates cycle insertion references") world.state.batches.HG = [] world.batches.append_batch(world.state, "HG", 11, 37, E.add(37, E.multiply(cycle_width, 3))) var total: Variant = 0 for batch: Dictionary in world.state.batches.HG: total = E.add(total, batch.amount) check(E.eq(batch.start_year, batch.end_year), "Distributed births create point cohorts") check(E.eq(total, 11) and world.state.batches.HG.size() == 3, "Distributed births retain all integers across death cycles") var before_coarsen: Dictionary = world.state.duplicate(true) world.batches.coarsen(world.state) check(E.eq(before_coarsen, world.state), "Coarsening cannot smear point stellar transitions") world.state.batches.HG = [] var completed: Dictionary = {} var cutoff: Variant = E.add(37, E.multiply(cycle_width, 10)) var integral: Variant = world.batches.append_batch(world.state, "HG", 11, 37, E.add(37, E.multiply(cycle_width, 3)), completed, cutoff) var reference_integral: Variant = 0 total = 0 for part: Dictionary in completed.HG: total = E.add(total, part.amount) reference_integral = E.add(reference_integral, E.multiply(part.amount, E.subtract(cutoff, part.to))) reference_integral = E.subtract(E.multiply(11, E.subtract(cutoff, E.add(37, E.multiply(cycle_width, E.of("1.5"))))), reference_integral) check(E.eq(total, 11) and world.state.batches.HG.is_empty(), "Already completed stars retain counts without stored batches") check(E.eq(integral, reference_integral), "Lifetime production agrees with point transitions") world.speed = 1 world.state.batches.HG = [] world.batches.append_batch(world.state, "HG", 10, 100, 100) check(E.gt(world.state.batches.HG[0].end_year, world.state.batches.HG[0].start_year), "Low speed retains gradual stellar transitions") dispose(world) world = fixture() world.speed = 6 world.state.amount.RHG = 5 world.batches.append_batch(world.state, "RHG", 5, 0, 0) world.ledger.refresh_groups(world.state) var validation: String = root.get_node("SaveManager").validate_data({"format_version": 6, "app_version": "", "ui": {"current_page": 0}, "state": world.state}) check(validation.is_empty(), "Cycle-star state born at zero validates: " + validation) var lifespan: Variant = world.batches.effective_lifespan(world.state, "RHG") check(E.eq(world.state.batches.RHG[0].start_year, lifespan), "Birth exactly aligned with a death boundary adds no extra cycle") dispose(world) world = fixture() world.speed = 6 world.state.batches.RD = [] world.batches.append_batch(world.state, "RD", huge, 0, 2000000) check(world.state.batches.RD.size() == 1, "Long-lived stars are not subdivided into cycle buckets") var long_cohort: Dictionary = world.state.batches.RD[0].duplicate(true) world.batches.append_batch(world.state, "RD", 7, 10000, 20000) check(world.state.batches.RD.size() == 1 and E.eq(world.state.batches.RD[0].amount, E.add(huge, 7)), "New births still merge into their original wide stellar group") check(E.eq(world.state.batches.RD[0].start_year, long_cohort.start_year) and E.eq(world.state.batches.RD[0].end_year, long_cohort.end_year), "Long-lived transition interval is preserved") world.state.batches.RD = [ {"amount": huge, "start_year": 1000, "end_year": 2000, "withdraw_remainder": 0.25}, {"amount": 7, "start_year": 3000, "end_year": 4000, "withdraw_remainder": 0.5}, {"amount": 9, "start_year": 19000, "end_year": 21000}, {"amount": 11, "start_year": 22000, "end_year": 1000000}, {"amount": 13, "start_year": 21000, "end_year": 22000}, ] var spanning: Dictionary = world.state.batches.RD[2].duplicate(true) var wide: Dictionary = world.state.batches.RD[3].duplicate(true) world.batches.coarsen(world.state) check(world.state.batches.RD.size() == 4, "Only cohorts ending entirely within the same cycle are merged") var merged: Dictionary = {} var broad_kept: bool = false var crossing_kept: bool = false for batch: Dictionary in world.state.batches.RD: if E.eq(batch.start_year, 20000) and E.eq(batch.end_year, 20000): merged = batch if E.eq(batch, wide): broad_kept = true if E.eq(batch, spanning): crossing_kept = true check(not merged.is_empty() and E.eq(merged.amount, E.add(huge, 7)) and E.eq(merged.withdraw_remainder, 0.75), "Merged stellar cohort retains exact population and carries") check(broad_kept and crossing_kept, "Wide and cycle-crossing cohorts remain unchanged") var once: Dictionary = world.state.duplicate(true) world.batches.coarsen(world.state) check(E.eq(world.state, once), "Dynamic stellar merging is idempotent") world.state.batches.HG = [{"amount": 3, "start_year": 20000, "end_year": 20000}, {"amount": 4, "start_year": 20001, "end_year": 21000}] world.batches.coarsen(world.state) check(world.state.batches.HG.size() == 2, "An exact boundary belongs to the preceding cycle") dispose(world) print(("Batch boundaries: %d checks, %d failures" % [checks, failures])) quit((0 if E.eq(failures, 0) else 1))