Enforce max stack size, with test cases