mirror of
https://github.com/BelfrySCAD/BOSL2.git
synced 2025-04-11 18:55:20 +00:00
Added regression tests and bugfixes for stacks.scad
This commit is contained in:
parent
28b086ea8d
commit
59c48f37d6
3 changed files with 76 additions and 7 deletions
12
stacks.scad
12
stacks.scad
|
@ -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]]
|
||||
);
|
||||
|
||||
|
||||
|
|
69
tests/test_stacks.scad
Normal file
69
tests/test_stacks.scad
Normal file
|
@ -0,0 +1,69 @@
|
|||
include <BOSL2/std.scad>
|
||||
include <BOSL2/stacks.scad>
|
||||
|
||||
|
||||
module test_stack_init() {
|
||||
assert(stack_init()==[]);
|
||||
}
|
||||
test_stack_init();
|
||||
|
||||
|
||||
module test_stack_empty() {
|
||||
assert(stack_empty([]));
|
||||
assert(!stack_empty([3]));
|
||||
assert(!stack_empty([2,4,8]));
|
||||
}
|
||||
test_stack_empty();
|
||||
|
||||
|
||||
module test_stack_depth() {
|
||||
assert(stack_depth([]) == 0);
|
||||
assert(stack_depth([3]) == 1);
|
||||
assert(stack_depth([2,4,8]) == 3);
|
||||
}
|
||||
test_stack_depth();
|
||||
|
||||
|
||||
module test_stack_top() {
|
||||
assert(stack_top([]) == undef);
|
||||
assert(stack_top([3,5,7,9]) == 9);
|
||||
assert(stack_top([3,5,7,9], 3) == [5,7,9]);
|
||||
}
|
||||
test_stack_top();
|
||||
|
||||
|
||||
module test_stack_peek() {
|
||||
s = [8,5,4,3,2,3,7];
|
||||
assert(stack_peek(s,0) == 7);
|
||||
assert(stack_peek(s,2) == 2);
|
||||
assert(stack_peek(s,2,1) == [2]);
|
||||
assert(stack_peek(s,2,3) == [2,3,7]);
|
||||
}
|
||||
test_stack_peek();
|
||||
|
||||
|
||||
module test_stack_push() {
|
||||
s1 = stack_init();
|
||||
s2 = stack_push(s1, "Foo");
|
||||
assert(s2==["Foo"]);
|
||||
s3 = stack_push(s2, "Bar");
|
||||
assert(s3==["Foo","Bar"]);
|
||||
s4 = stack_push(s3, "Baz");
|
||||
assert(s4==["Foo","Bar","Baz"]);
|
||||
}
|
||||
test_stack_push();
|
||||
|
||||
|
||||
module test_stack_pop() {
|
||||
q = ["Foo", "Bar", "Baz", "Qux"];
|
||||
q1 = stack_pop(q);
|
||||
assert(q1 == ["Foo", "Bar", "Baz"]);
|
||||
q2 = stack_pop(q,2);
|
||||
assert(q2 == ["Foo", "Bar"]);
|
||||
q3 = stack_pop(q,3);
|
||||
assert(q3 == ["Foo"]);
|
||||
}
|
||||
test_stack_pop();
|
||||
|
||||
|
||||
// vim: noexpandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap
|
|
@ -8,7 +8,7 @@
|
|||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
BOSL_VERSION = [2,0,7];
|
||||
BOSL_VERSION = [2,0,8];
|
||||
|
||||
|
||||
// Section: BOSL Library Version Functions
|
||||
|
|
Loading…
Reference in a new issue