ludic/packages/ludic.physics/tests/reuse_test.ludic
Orkuncakilkaya 5cd53d4024 feat(ludic.physics): phys_own - a body owns a shape made for it alone, and phys_remove frees it with the body
A chunk's trunks and boulders put in and taken out made a new shape each time and freed none. The
table is by the body's index, made once at the world's size. tests/reuse_test: 1000 owned bodies put
and removed hold no more shapes and 32 bytes of heap.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-28 13:59:25 +03:00

63 lines
2.7 KiB
Text

# reuse_test.ludic - what is taken away is given back: a walker made and removed two hundred times
# holds one place in the list and no more heap, and a world closed and opened again keeps its lists
import "ludic.physics"
import "ludic.base"
program ReuseTest {
numbers float
test "a walker made and removed takes the same place and grows nothing" (physics_st: mut PhysicsState) {
expect(phys_open(physics_st, 64, 1))
let w = phys_walker_add(physics_st, 0.4, 1.8, 0.0, 0.0, 0.0, 50.0, 80.0)
phys_walker_remove(physics_st, w)
let before = Os.heap_bytes()
for k in 0 .. 200 {
let id = phys_walker_add(physics_st, 0.4, 1.8, float(k), 0.0, 0.0, 50.0, 80.0)
expect_eq(id, w)
phys_walker_remove(physics_st, id)
}
let grew = Os.heap_bytes() - before
print(`reuse: 200 walkers made and removed, {len(physics_st.ph_walkers)} place, heap {grew} bytes`)
expect_eq(len(physics_st.ph_walkers), 1)
expect(grew < long(1024))
phys_close(physics_st)
}
test "a world closed and opened again keeps the lists it held" (physics_st: mut PhysicsState) {
expect(phys_open(physics_st, 64, 1))
phys_box(physics_st, 1.0, 1.0, 1.0)
phys_walker_add(physics_st, 0.4, 1.8, 0.0, 0.0, 0.0, 50.0, 80.0)
let shapes = physics_st.ph_shapes
let walkers = physics_st.ph_walkers
for k in 0 .. 20 {
phys_close(physics_st)
expect(phys_open(physics_st, 64, 1))
phys_box(physics_st, 1.0, 1.0, 1.0)
phys_walker_add(physics_st, 0.4, 1.8, 0.0, 0.0, 0.0, 50.0, 80.0)
}
expect(physics_st.ph_shapes == shapes)
expect(physics_st.ph_walkers == walkers)
expect_eq(phys_shapes(physics_st), 1)
phys_close(physics_st)
}
test "a body that owns its shape takes the shape with it, a thousand times over" (physics_st: mut PhysicsState) {
expect(phys_open(physics_st, 64, 1))
let s0 = phys_cylinder(physics_st, 5.0, 0.3)
phys_own(physics_st, phys_static_add(physics_st, s0, 0.0, 0.0, 0.0, 0.0), s0)
let first = phys_static_add(physics_st, phys_cylinder(physics_st, 5.0, 0.3), 3.0, 0.0, 0.0, 0.0)
phys_remove(physics_st, first)
let before = Os.heap_bytes()
for k in 0 .. 1000 {
let s = phys_cylinder(physics_st, 5.0, 0.3)
let b = phys_static_add(physics_st, s, float(k % 10), 0.0, 5.0, 0.0)
phys_own(physics_st, b, s)
phys_remove(physics_st, b)
}
let grew = Os.heap_bytes() - before
print(`reuse: 1000 owned bodies put and removed, {phys_shapes(physics_st)} shapes held in {len(physics_st.ph_shapes)} places, heap {grew} bytes`)
expect(phys_shapes(physics_st) <= 2)
expect(len(physics_st.ph_shapes) <= 3)
expect(grew < long(1024))
phys_close(physics_st)
}
}