BOSL2/tests/test_vectors.scad

101 lines
3.1 KiB
OpenSCAD
Raw Normal View History

2019-05-01 06:45:05 +00:00
include <BOSL2/std.scad>
2019-10-23 00:10:04 +00:00
module test_is_vector() {
assert(is_vector([1,2,3]) == true);
assert(is_vector([[1,2,3]]) == false);
assert(is_vector(["foo"]) == false);
assert(is_vector([]) == false);
assert(is_vector(1) == false);
assert(is_vector("foo") == false);
assert(is_vector(true) == false);
}
test_is_vector();
module test_add_scalar() {
assert(add_scalar([1,2,3],3) == [4,5,6]);
assert(add_scalar([[1,2,3],[3,4,5]],3) == [[4,5,6],[6,7,8]]);
}
test_add_scalar()
2019-05-01 06:45:05 +00:00
module test_vmul() {
assert(vmul([3,4,5], [8,7,6]) == [24,28,30]);
assert(vmul([1,2,3], [4,5,6]) == [4,10,18]);
}
test_vmul();
module test_vdiv() {
assert(vdiv([24,28,30], [8,7,6]) == [3, 4, 5]);
}
test_vdiv();
module test_vabs() {
assert(vabs([2,4,8]) == [2,4,8]);
assert(vabs([-2,-4,-8]) == [2,4,8]);
assert(vabs([-2,4,8]) == [2,4,8]);
assert(vabs([2,-4,8]) == [2,4,8]);
assert(vabs([2,4,-8]) == [2,4,8]);
}
test_vabs();
module test_normalize() {
assert(normalize([10,0,0]) == [1,0,0]);
assert(normalize([0,10,0]) == [0,1,0]);
assert(normalize([0,0,10]) == [0,0,1]);
assert(abs(norm(normalize([10,10,10]))-1) < EPSILON);
assert(abs(norm(normalize([-10,-10,-10]))-1) < EPSILON);
assert(abs(norm(normalize([-10,0,0]))-1) < EPSILON);
assert(abs(norm(normalize([0,-10,0]))-1) < EPSILON);
assert(abs(norm(normalize([0,0,-10]))-1) < EPSILON);
}
test_normalize();
module test_vector_angle() {
vecs = [[10,0,0], [-10,0,0], [0,10,0], [0,-10,0], [0,0,10], [0,0,-10]];
for (a=vecs, b=vecs) {
if(a==b) {
assert(vector_angle(a,b)==0);
assert(vector_angle([a,b])==0);
2019-05-01 06:45:05 +00:00
} else if(a==-b) {
assert(vector_angle(a,b)==180);
assert(vector_angle([a,b])==180);
2019-05-01 06:45:05 +00:00
} else {
assert(vector_angle(a,b)==90);
assert(vector_angle([a,b])==90);
2019-05-01 06:45:05 +00:00
}
}
assert(abs(vector_angle([10,10,0],[10,0,0])-45) < EPSILON);
assert(abs(vector_angle([[10,10,0],[10,0,0]])-45) < EPSILON);
assert(abs(vector_angle([11,11,1],[1,1,1],[11,-9,1])-90) < EPSILON);
assert(abs(vector_angle([[11,11,1],[1,1,1],[11,-9,1]])-90) < EPSILON);
2019-05-01 06:45:05 +00:00
}
test_vector_angle();
module test_vector_axis() {
assert(norm(vector_axis([10,0,0],[10,10,0]) - [0,0,1]) < EPSILON);
assert(norm(vector_axis([[10,0,0],[10,10,0]]) - [0,0,1]) < EPSILON);
2019-05-01 06:45:05 +00:00
assert(norm(vector_axis([10,0,0],[0,10,0]) - [0,0,1]) < EPSILON);
assert(norm(vector_axis([[10,0,0],[0,10,0]]) - [0,0,1]) < EPSILON);
2019-05-01 06:45:05 +00:00
assert(norm(vector_axis([0,10,0],[10,0,0]) - [0,0,-1]) < EPSILON);
assert(norm(vector_axis([[0,10,0],[10,0,0]]) - [0,0,-1]) < EPSILON);
2019-05-01 06:45:05 +00:00
assert(norm(vector_axis([0,0,10],[10,0,0]) - [0,1,0]) < EPSILON);
assert(norm(vector_axis([[0,0,10],[10,0,0]]) - [0,1,0]) < EPSILON);
2019-05-01 06:45:05 +00:00
assert(norm(vector_axis([10,0,0],[0,0,10]) - [0,-1,0]) < EPSILON);
assert(norm(vector_axis([[10,0,0],[0,0,10]]) - [0,-1,0]) < EPSILON);
2019-05-01 06:45:05 +00:00
assert(norm(vector_axis([10,0,10],[0,-10,0]) - [sin(45),0,-sin(45)]) < EPSILON);
assert(norm(vector_axis([[10,0,10],[0,-10,0]]) - [sin(45),0,-sin(45)]) < EPSILON);
assert(norm(vector_axis([11,1,11],[1,1,1],[1,-9,1]) - [sin(45),0,-sin(45)]) < EPSILON);
assert(norm(vector_axis([[11,1,11],[1,1,1],[1,-9,1]]) - [sin(45),0,-sin(45)]) < EPSILON);
2019-05-01 06:45:05 +00:00
}
test_vector_axis();
// vim: noexpandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap