Added regression tests and bugfixes for stacks.scad

This commit is contained in:
Revar Desmera
2019-11-05 21:04:56 -08:00
parent 28b086ea8d
commit 59c48f37d6
3 changed files with 76 additions and 7 deletions

View File

@@ -99,26 +99,26 @@ function stack_top(stack,n=undef) =
// If `n` is given, returns a list of the `n` stack items at and above depth `depth`.
// Arguments:
// stack = The stack to read from.
// depth = The depth of the stack item to read. Default: 1
// depth = The depth of the stack item to read. Default: 0
// n = The number of stack items to return. Default: undef (Return only the stack item at `depth`)
// Example:
// stack = [2,3,4,5,6,7,8,9];
// item = stack_peek(stack); // Returns: 9
// item2 = stack_peek(stack, 3); // Returns: 7
// list = stack_peek(stack, 6, 4); // Returns: [4,5,6,7]
function stack_peek(stack,depth=1,n=undef) =
function stack_peek(stack,depth=0,n=undef) =
assert(is_list(stack))
assert(is_num(depth))
assert(depth>0)
assert(depth>=0)
let(stacksize = len(stack))
assert(stacksize>=depth, "stack underflow")
is_undef(n)? (
stack[stacksize-depth]
stack[stacksize-depth-1]
) : (
assert(is_num(n))
assert(n>=0)
assert(n<depth)
[for (i=[0:1:n-1]) stack[stacksize-depth+i]]
assert(n<=depth+1)
[for (i=[0:1:n-1]) stack[stacksize-1-depth+i]]
);