Bugfix for #182. Added assert_equal() and assert_approx() for regression helpers.

This commit is contained in:
Revar Desmera 2020-06-20 19:51:58 -07:00
parent 1f7e1f5d0d
commit 20dfaac113
4 changed files with 290 additions and 220 deletions

View file

@ -15,11 +15,12 @@
// Usage: // Usage:
// typ = typeof(x); // typ = typeof(x);
// Description: // Description:
// Returns a string representing the type of the value. One of "undef", "boolean", "number", "string", "list", or "range" // Returns a string representing the type of the value. One of "undef", "boolean", "number", "nan", "string", "list", or "range"
function typeof(x) = function typeof(x) =
is_undef(x)? "undef" : is_undef(x)? "undef" :
is_bool(x)? "boolean" : is_bool(x)? "boolean" :
is_num(x)? "number" : is_num(x)? "number" :
is_nan(x)? "nan" :
is_string(x)? "string" : is_string(x)? "string" :
is_list(x)? "list" : is_list(x)? "list" :
"range"; "range";
@ -30,7 +31,7 @@ function typeof(x) =
// b = is_type(x, types); // b = is_type(x, types);
// Description: // Description:
// Returns true if the type of the value `x` is one of those given as strings in the list `types`. // Returns true if the type of the value `x` is one of those given as strings in the list `types`.
// Valid types are "undef", "boolean", "number", "string", "list", or "range" // Valid types are "undef", "boolean", "number", "nan", "string", "list", or "range"
// Arguments: // Arguments:
// x = The value to check the type of. // x = The value to check the type of.
// types = A list of types to check // types = A list of types to check
@ -86,6 +87,7 @@ function is_nan(x) = (x!=x);
// Returns true if its argument is a range // Returns true if its argument is a range
function is_range(x) = is_num(x[0]) && !is_list(x); function is_range(x) = is_num(x[0]) && !is_list(x);
// Function: is_list_of() // Function: is_list_of()
// Usage: // Usage:
// is_list_of(list, pattern) // is_list_of(list, pattern)
@ -306,4 +308,68 @@ function segs(r) =
ceil(max(5, min(360/$fa, abs(r)*2*PI/$fs))); ceil(max(5, min(360/$fa, abs(r)*2*PI/$fs)));
// Section: Testing Helpers
function _valstr(x) =
is_list(x)? str("[",str_join([for (xx=x) _valstr(xx)],","),"]") :
is_num(x)? fmt_float(x,12) : x;
// Module: assert_approx()
// Usage:
// assert_approx(got, expected, [info]);
// Description:
// Tests if the value gotten is what was expected. If not, then
// the expected and received values are printed to the console and
// an assertion is thrown to stop execution.
// Arguments:
// got = The value actually received.
// expected = The value that was expected.
// info = Extra info to print out to make the error clearer.
module assert_approx(got, expected, info) {
if (!approx(got, expected)) {
echo();
echo(str("EXPECT: ", _valstr(expected)));
echo(str("GOT : ", _valstr(got)));
if (same_shape(got, expected)) {
echo(str("DELTA : ", _valstr(got - expected)));
}
if (is_def(info)) {
echo(str("INFO : ", _valstr(info)));
}
assert(approx(got, expected));
}
}
// Module: assert_equal()
// Usage:
// assert_equal(got, expected, [info]);
// Description:
// Tests if the value gotten is what was expected. If not, then
// the expected and received values are printed to the console and
// an assertion is thrown to stop execution.
// Arguments:
// got = The value actually received.
// expected = The value that was expected.
// info = Extra info to print out to make the error clearer.
module assert_equal(got, expected, info) {
if (got != expected || (is_nan(got) && is_nan(expected))) {
echo();
echo(str("EXPECT: ", _valstr(expected)));
echo(str("GOT : ", _valstr(got)));
if (same_shape(got, expected)) {
echo(str("DELTA : ", _valstr(got - expected)));
}
if (is_def(info)) {
echo(str("INFO : ", _valstr(info)));
}
assert(got == expected);
}
}
// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap // vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap

View file

@ -771,8 +771,9 @@ function _type_num(x) =
is_undef(x)? 0 : is_undef(x)? 0 :
is_bool(x)? 1 : is_bool(x)? 1 :
is_num(x)? 2 : is_num(x)? 2 :
is_string(x)? 3 : is_nan(x)? 3 :
is_list(x)? 4 : 5; is_string(x)? 4 :
is_list(x)? 5 : 6;
// Function: compare_vals() // Function: compare_vals()
@ -780,6 +781,7 @@ function _type_num(x) =
// compare_vals(a, b); // compare_vals(a, b);
// Description: // Description:
// Compares two values. Lists are compared recursively. // Compares two values. Lists are compared recursively.
// Returns <0 if a<b. Returns >0 if a>b. Returns 0 if a==b.
// If types are not the same, then undef < bool < num < str < list < range. // If types are not the same, then undef < bool < num < str < list < range.
// Arguments: // Arguments:
// a = First value to compare. // a = First value to compare.
@ -788,6 +790,7 @@ function compare_vals(a, b) =
(a==b)? 0 : (a==b)? 0 :
let(t1=_type_num(a), t2=_type_num(b)) (t1!=t2)? (t1-t2) : let(t1=_type_num(a), t2=_type_num(b)) (t1!=t2)? (t1-t2) :
is_list(a)? compare_lists(a,b) : is_list(a)? compare_lists(a,b) :
is_nan(a)? 0 :
(a<b)? -1 : (a>b)? 1 : 0; (a<b)? -1 : (a>b)? 1 : 0;

View file

@ -3,83 +3,83 @@ include <BOSL2/std.scad>
// Simple Calculations // Simple Calculations
module test_quant() { module test_quant() {
assert(quant(-4,3) == -3); assert_equal(quant(-4,3), -3);
assert(quant(-3,3) == -3); assert_equal(quant(-3,3), -3);
assert(quant(-2,3) == -3); assert_equal(quant(-2,3), -3);
assert(quant(-1,3) == 0); assert_equal(quant(-1,3), 0);
assert(quant(0,3) == 0); assert_equal(quant(0,3), 0);
assert(quant(1,3) == 0); assert_equal(quant(1,3), 0);
assert(quant(2,3) == 3); assert_equal(quant(2,3), 3);
assert(quant(3,3) == 3); assert_equal(quant(3,3), 3);
assert(quant(4,3) == 3); assert_equal(quant(4,3), 3);
assert(quant(7,3) == 6); assert_equal(quant(7,3), 6);
assert(quant([12,13,13.1,14,14.1,15,16],4) == [12,12,12,16,16,16,16]); assert_equal(quant([12,13,13.1,14,14.1,15,16],4), [12,12,12,16,16,16,16]);
assert(quant([9,10,10.4,10.5,11,12],3) == [9,9,9,12,12,12]); assert_equal(quant([9,10,10.4,10.5,11,12],3), [9,9,9,12,12,12]);
assert(quant([[9,10,10.4],[10.5,11,12]],3) == [[9,9,9],[12,12,12]]); assert_equal(quant([[9,10,10.4],[10.5,11,12]],3), [[9,9,9],[12,12,12]]);
} }
test_quant(); test_quant();
module test_quantdn() { module test_quantdn() {
assert(quantdn(-4,3) == -6); assert_equal(quantdn(-4,3), -6);
assert(quantdn(-3,3) == -3); assert_equal(quantdn(-3,3), -3);
assert(quantdn(-2,3) == -3); assert_equal(quantdn(-2,3), -3);
assert(quantdn(-1,3) == -3); assert_equal(quantdn(-1,3), -3);
assert(quantdn(0,3) == 0); assert_equal(quantdn(0,3), 0);
assert(quantdn(1,3) == 0); assert_equal(quantdn(1,3), 0);
assert(quantdn(2,3) == 0); assert_equal(quantdn(2,3), 0);
assert(quantdn(3,3) == 3); assert_equal(quantdn(3,3), 3);
assert(quantdn(4,3) == 3); assert_equal(quantdn(4,3), 3);
assert(quantdn(7,3) == 6); assert_equal(quantdn(7,3), 6);
assert(quantdn([12,13,13.1,14,14.1,15,16],4) == [12,12,12,12,12,12,16]); assert_equal(quantdn([12,13,13.1,14,14.1,15,16],4), [12,12,12,12,12,12,16]);
assert(quantdn([9,10,10.4,10.5,11,12],3) == [9,9,9,9,9,12]); assert_equal(quantdn([9,10,10.4,10.5,11,12],3), [9,9,9,9,9,12]);
assert(quantdn([[9,10,10.4],[10.5,11,12]],3) == [[9,9,9],[9,9,12]]); assert_equal(quantdn([[9,10,10.4],[10.5,11,12]],3), [[9,9,9],[9,9,12]]);
} }
test_quantdn(); test_quantdn();
module test_quantup() { module test_quantup() {
assert(quantup(-4,3) == -3); assert_equal(quantup(-4,3), -3);
assert(quantup(-3,3) == -3); assert_equal(quantup(-3,3), -3);
assert(quantup(-2,3) == 0); assert_equal(quantup(-2,3), 0);
assert(quantup(-1,3) == 0); assert_equal(quantup(-1,3), 0);
assert(quantup(0,3) == 0); assert_equal(quantup(0,3), 0);
assert(quantup(1,3) == 3); assert_equal(quantup(1,3), 3);
assert(quantup(2,3) == 3); assert_equal(quantup(2,3), 3);
assert(quantup(3,3) == 3); assert_equal(quantup(3,3), 3);
assert(quantup(4,3) == 6); assert_equal(quantup(4,3), 6);
assert(quantup(7,3) == 9); assert_equal(quantup(7,3), 9);
assert(quantup([12,13,13.1,14,14.1,15,16],4) == [12,16,16,16,16,16,16]); assert_equal(quantup([12,13,13.1,14,14.1,15,16],4), [12,16,16,16,16,16,16]);
assert(quantup([9,10,10.4,10.5,11,12],3) == [9,12,12,12,12,12]); assert_equal(quantup([9,10,10.4,10.5,11,12],3), [9,12,12,12,12,12]);
assert(quantup([[9,10,10.4],[10.5,11,12]],3) == [[9,12,12],[12,12,12]]); assert_equal(quantup([[9,10,10.4],[10.5,11,12]],3), [[9,12,12],[12,12,12]]);
} }
test_quantup(); test_quantup();
module test_constrain() { module test_constrain() {
assert(constrain(-2,-1,1) == -1); assert_equal(constrain(-2,-1,1), -1);
assert(constrain(-1.75,-1,1) == -1); assert_equal(constrain(-1.75,-1,1), -1);
assert(constrain(-1,-1,1) == -1); assert_equal(constrain(-1,-1,1), -1);
assert(constrain(-0.75,-1,1) == -0.75); assert_equal(constrain(-0.75,-1,1), -0.75);
assert(constrain(0,-1,1) == 0); assert_equal(constrain(0,-1,1), 0);
assert(constrain(0.75,-1,1) == 0.75); assert_equal(constrain(0.75,-1,1), 0.75);
assert(constrain(1,-1,1) == 1); assert_equal(constrain(1,-1,1), 1);
assert(constrain(1.75,-1,1) == 1); assert_equal(constrain(1.75,-1,1), 1);
assert(constrain(2,-1,1) == 1); assert_equal(constrain(2,-1,1), 1);
} }
test_constrain(); test_constrain();
module test_approx() { module test_approx() {
assert(approx(PI, 3.141592653589793236) == true); assert_equal(approx(PI, 3.141592653589793236), true);
assert(approx(PI, 3.1415926) == false); assert_equal(approx(PI, 3.1415926), false);
assert(approx(PI, 3.1415926, eps=1e-6) == true); assert_equal(approx(PI, 3.1415926, eps=1e-6), true);
assert(approx(-PI, -3.141592653589793236) == true); assert_equal(approx(-PI, -3.141592653589793236), true);
assert(approx(-PI, -3.1415926) == false); assert_equal(approx(-PI, -3.1415926), false);
assert(approx(-PI, -3.1415926, eps=1e-6) == true); assert_equal(approx(-PI, -3.1415926, eps=1e-6), true);
assert(approx(1/3, 0.3333333333) == true); assert_equal(approx(1/3, 0.3333333333), true);
assert(approx(-1/3, -0.3333333333) == true); assert_equal(approx(-1/3, -0.3333333333), true);
assert(approx(10*[cos(30),sin(30)], 10*[sqrt(3)/2, 1/2]) == true); assert_equal(approx(10*[cos(30),sin(30)], 10*[sqrt(3)/2, 1/2]), true);
} }
test_approx(); test_approx();
@ -88,14 +88,14 @@ module test_min_index() {
vals = rands(-100,100,100); vals = rands(-100,100,100);
minval = min(vals); minval = min(vals);
minidx = min_index(vals); minidx = min_index(vals);
assert(vals[minidx] == minval); assert_equal(vals[minidx], minval);
assert(min_index([3,4,5,6]) == 0); assert_equal(min_index([3,4,5,6]), 0);
assert(min_index([4,3,5,6]) == 1); assert_equal(min_index([4,3,5,6]), 1);
assert(min_index([4,5,3,6]) == 2); assert_equal(min_index([4,5,3,6]), 2);
assert(min_index([4,5,6,3]) == 3); assert_equal(min_index([4,5,6,3]), 3);
assert(min_index([6,5,4,3]) == 3); assert_equal(min_index([6,5,4,3]), 3);
assert(min_index([6,3,4,5]) == 1); assert_equal(min_index([6,3,4,5]), 1);
assert(min_index([-56,72,-874,5]) == 2); assert_equal(min_index([-56,72,-874,5]), 2);
} }
test_min_index(); test_min_index();
@ -104,77 +104,78 @@ module test_max_index() {
vals = rands(-100,100,100); vals = rands(-100,100,100);
maxval = max(vals); maxval = max(vals);
maxidx = max_index(vals); maxidx = max_index(vals);
assert(vals[maxidx] == maxval); assert_equal(vals[maxidx], maxval);
assert(max_index([3,4,5,6]) == 3); assert_equal(max_index([3,4,5,6]), 3);
assert(max_index([3,4,6,5]) == 2); assert_equal(max_index([3,4,6,5]), 2);
assert(max_index([3,6,4,5]) == 1); assert_equal(max_index([3,6,4,5]), 1);
assert(max_index([6,3,4,5]) == 0); assert_equal(max_index([6,3,4,5]), 0);
assert(max_index([5,6,4,3]) == 1); assert_equal(max_index([5,6,4,3]), 1);
assert(max_index([-56,72,-874,5]) == 1); assert_equal(max_index([-56,72,-874,5]), 1);
} }
test_max_index(); test_max_index();
module test_posmod() { module test_posmod() {
assert(posmod(-5,3) == 1); assert_equal(posmod(-5,3), 1);
assert(posmod(-4,3) == 2); assert_equal(posmod(-4,3), 2);
assert(posmod(-3,3) == 0); assert_equal(posmod(-3,3), 0);
assert(posmod(-2,3) == 1); assert_equal(posmod(-2,3), 1);
assert(posmod(-1,3) == 2); assert_equal(posmod(-1,3), 2);
assert(posmod(0,3) == 0); assert_equal(posmod(0,3), 0);
assert(posmod(1,3) == 1); assert_equal(posmod(1,3), 1);
assert(posmod(2,3) == 2); assert_equal(posmod(2,3), 2);
assert(posmod(3,3) == 0); assert_equal(posmod(3,3), 0);
} }
test_posmod(); test_posmod();
module test_modang() { module test_modang() {
assert(modang(-700) == 20); assert_equal(modang(-700), 20);
assert(modang(-270) == 90); assert_equal(modang(-270), 90);
assert(modang(-120) == -120); assert_equal(modang(-120), -120);
assert(modang(120) == 120); assert_equal(modang(120), 120);
assert(modang(270) == -90); assert_equal(modang(270), -90);
assert(modang(700) == -20); assert_equal(modang(700), -20);
} }
test_modang(); test_modang();
module test_modrange() { module test_modrange() {
assert(modrange(-5,5,3) == [1,2]); assert_equal(modrange(-5,5,3), [1,2]);
assert(modrange(-1,4,3) == [2,0,1]); assert_equal(modrange(-1,4,3), [2,0,1]);
assert(modrange(1,8,10,step=2) == [1,3,5,7]); assert_equal(modrange(1,8,10,step=2), [1,3,5,7]);
assert(modrange(5,12,10,step=2) == [5,7,9,1]); assert_equal(modrange(5,12,10,step=2), [5,7,9,1]);
} }
test_modrange(); test_modrange();
module test_sqr() { module test_sqr() {
assert(sqr(-3) == 9); assert_equal(sqr(-3), 9);
assert(sqr(0) == 0); assert_equal(sqr(0), 0);
assert(sqr(1) == 1); assert_equal(sqr(1), 1);
assert(sqr(2) == 4); assert_equal(sqr(2), 4);
assert(sqr(3) == 9); assert_equal(sqr(2.5), 6.25);
assert(sqr(16) == 256); assert_equal(sqr(3), 9);
assert_equal(sqr(16), 256);
} }
test_sqr(); test_sqr();
module test_log2() { module test_log2() {
assert(log2(0.125) == -3); assert_equal(log2(0.125), -3);
assert(log2(16) == 4); assert_equal(log2(16), 4);
assert(log2(256) == 8); assert_equal(log2(256), 8);
} }
test_log2(); test_log2();
module test_rand_int() { module test_rand_int() {
nums = rand_int(-100,100,1000,seed=2134); nums = rand_int(-100,100,1000,seed=2134);
assert(len(nums)==1000); assert_equal(len(nums), 1000);
for (num = nums) { for (num = nums) {
assert(num>=-100); assert(num>=-100);
assert(num<=100); assert(num<=100);
assert(num==floor(num)); assert_equal(num, floor(num));
} }
} }
test_rand_int(); test_rand_int();
@ -184,10 +185,10 @@ module test_gaussian_rands() {
nums1 = gaussian_rands(0,10,1000,seed=2132); nums1 = gaussian_rands(0,10,1000,seed=2132);
nums2 = gaussian_rands(0,10,1000,seed=2130); nums2 = gaussian_rands(0,10,1000,seed=2130);
nums3 = gaussian_rands(0,10,1000,seed=2132); nums3 = gaussian_rands(0,10,1000,seed=2132);
assert(len(nums1)==1000); assert_equal(len(nums1), 1000);
assert(len(nums2)==1000); assert_equal(len(nums2), 1000);
assert(len(nums3)==1000); assert_equal(len(nums3), 1000);
assert(nums1==nums3); assert_equal(nums1, nums3);
assert(nums1!=nums2); assert(nums1!=nums2);
} }
test_gaussian_rands(); test_gaussian_rands();
@ -197,183 +198,183 @@ module test_log_rands() {
nums1 = log_rands(0,100,10,1000,seed=2189); nums1 = log_rands(0,100,10,1000,seed=2189);
nums2 = log_rands(0,100,10,1000,seed=2310); nums2 = log_rands(0,100,10,1000,seed=2310);
nums3 = log_rands(0,100,10,1000,seed=2189); nums3 = log_rands(0,100,10,1000,seed=2189);
assert(len(nums1)==1000); assert_equal(len(nums1), 1000);
assert(len(nums2)==1000); assert_equal(len(nums2), 1000);
assert(len(nums3)==1000); assert_equal(len(nums3), 1000);
assert(nums1==nums3); assert_equal(nums1, nums3);
assert(nums1!=nums2); assert(nums1!=nums2);
} }
test_log_rands(); test_log_rands();
module test_segs() { module test_segs() {
assert(segs(50,$fn=8) == 8); assert_equal(segs(50,$fn=8), 8);
assert(segs(50,$fa=2,$fs=2) == 158); assert_equal(segs(50,$fa=2,$fs=2), 158);
} }
test_segs(); test_segs();
module test_lerp() { module test_lerp() {
assert(lerp(-20,20,0) == -20); assert_equal(lerp(-20,20,0), -20);
assert(lerp(-20,20,0.25) == -10); assert_equal(lerp(-20,20,0.25), -10);
assert(lerp(-20,20,0.5) == 0); assert_equal(lerp(-20,20,0.5), 0);
assert(lerp(-20,20,0.75) == 10); assert_equal(lerp(-20,20,0.75), 10);
assert(lerp(-20,20,1) == 20); assert_equal(lerp(-20,20,1), 20);
assert(lerp(-20,20,[0,0.25,0.5,0.75,1]) == [-20,-10,0,10,20]); assert_equal(lerp(-20,20,[0,0.25,0.5,0.75,1]), [-20,-10,0,10,20]);
assert(lerp(-20,20,[0:0.25:1]) == [-20,-10,0,10,20]); assert_equal(lerp(-20,20,[0:0.25:1]), [-20,-10,0,10,20]);
assert(lerp([10,10],[30,-10],0.5) == [20,0]); assert_equal(lerp([10,10],[30,-10],0.5), [20,0]);
} }
test_lerp(); test_lerp();
module test_hypot() { module test_hypot() {
assert(hypot(20,30) == norm([20,30])); assert_approx(hypot(20,30), norm([20,30]));
} }
test_hypot(); test_hypot();
module test_sinh() { module test_sinh() {
assert(abs(sinh(-2)+3.6268604078) < EPSILON); assert_approx(sinh(-2), -3.6268604078);
assert(abs(sinh(-1)+1.1752011936) < EPSILON); assert_approx(sinh(-1), -1.1752011936);
assert(abs(sinh(0)) < EPSILON); assert_approx(sinh(0), 0);
assert(abs(sinh(1)-1.1752011936) < EPSILON); assert_approx(sinh(1), 1.1752011936);
assert(abs(sinh(2)-3.6268604078) < EPSILON); assert_approx(sinh(2), 3.6268604078);
} }
test_sinh(); test_sinh();
module test_cosh() { module test_cosh() {
assert(abs(cosh(-2)-3.7621956911) < EPSILON); assert_approx(cosh(-2), 3.7621956911);
assert(abs(cosh(-1)-1.5430806348) < EPSILON); assert_approx(cosh(-1), 1.5430806348);
assert(abs(cosh(0)-1) < EPSILON); assert_approx(cosh(0), 1);
assert(abs(cosh(1)-1.5430806348) < EPSILON); assert_approx(cosh(1), 1.5430806348);
assert(abs(cosh(2)-3.7621956911) < EPSILON); assert_approx(cosh(2), 3.7621956911);
} }
test_cosh(); test_cosh();
module test_tanh() { module test_tanh() {
assert(abs(tanh(-2)+0.9640275801) < EPSILON); assert_approx(tanh(-2), -0.9640275801);
assert(abs(tanh(-1)+0.761594156) < EPSILON); assert_approx(tanh(-1), -0.761594156);
assert(abs(tanh(0)) < EPSILON); assert_approx(tanh(0), 0);
assert(abs(tanh(1)-0.761594156) < EPSILON); assert_approx(tanh(1), 0.761594156);
assert(abs(tanh(2)-0.9640275801) < EPSILON); assert_approx(tanh(2), 0.9640275801);
} }
test_tanh(); test_tanh();
module test_asinh() { module test_asinh() {
assert(abs(asinh(sinh(-2))+2) < EPSILON); assert_approx(asinh(sinh(-2)), -2);
assert(abs(asinh(sinh(-1))+1) < EPSILON); assert_approx(asinh(sinh(-1)), -1);
assert(abs(asinh(sinh(0))) < EPSILON); assert_approx(asinh(sinh(0)), 0);
assert(abs(asinh(sinh(1))-1) < EPSILON); assert_approx(asinh(sinh(1)), 1);
assert(abs(asinh(sinh(2))-2) < EPSILON); assert_approx(asinh(sinh(2)), 2);
} }
test_asinh(); test_asinh();
module test_acosh() { module test_acosh() {
assert(abs(acosh(cosh(-2))-2) < EPSILON); assert_approx(acosh(cosh(-2)), 2);
assert(abs(acosh(cosh(-1))-1) < EPSILON); assert_approx(acosh(cosh(-1)), 1);
assert(abs(acosh(cosh(0))) < EPSILON); assert_approx(acosh(cosh(0)), 0);
assert(abs(acosh(cosh(1))-1) < EPSILON); assert_approx(acosh(cosh(1)), 1);
assert(abs(acosh(cosh(2))-2) < EPSILON); assert_approx(acosh(cosh(2)), 2);
} }
test_acosh(); test_acosh();
module test_atanh() { module test_atanh() {
assert(abs(atanh(tanh(-2))+2) < EPSILON); assert_approx(atanh(tanh(-2)), -2);
assert(abs(atanh(tanh(-1))+1) < EPSILON); assert_approx(atanh(tanh(-1)), -1);
assert(abs(atanh(tanh(0))) < EPSILON); assert_approx(atanh(tanh(0)), 0);
assert(abs(atanh(tanh(1))-1) < EPSILON); assert_approx(atanh(tanh(1)), 1);
assert(abs(atanh(tanh(2))-2) < EPSILON); assert_approx(atanh(tanh(2)), 2);
} }
test_atanh(); test_atanh();
module test_sum() { module test_sum() {
assert(sum([]) == 0); assert_equal(sum([]), 0);
assert(sum([],dflt=undef) == undef); assert_equal(sum([],dflt=undef), undef);
assert(sum([1,2,3]) == 6); assert_equal(sum([1,2,3]), 6);
assert(sum([-2,-1,0,1,2]) == 0); assert_equal(sum([-2,-1,0,1,2]), 0);
assert(sum([[1,2,3], [3,4,5], [5,6,7]]) == [9,12,15]); assert_equal(sum([[1,2,3], [3,4,5], [5,6,7]]), [9,12,15]);
} }
test_sum(); test_sum();
module test_cumsum() { module test_cumsum() {
assert(cumsum([]) == []); assert_equal(cumsum([]), []);
assert(cumsum([1,1,1]) == [1,2,3]); assert_equal(cumsum([1,1,1]), [1,2,3]);
assert(cumsum([2,2,2]) == [2,4,6]); assert_equal(cumsum([2,2,2]), [2,4,6]);
assert(cumsum([1,2,3]) == [1,3,6]); assert_equal(cumsum([1,2,3]), [1,3,6]);
assert(cumsum([-2,-1,0,1,2]) == [-2,-3,-3,-2,0]); assert_equal(cumsum([-2,-1,0,1,2]), [-2,-3,-3,-2,0]);
assert(cumsum([[1,2,3], [3,4,5], [5,6,7]]) == [[1,2,3],[4,6,8],[9,12,15]]); assert_equal(cumsum([[1,2,3], [3,4,5], [5,6,7]]), [[1,2,3],[4,6,8],[9,12,15]]);
} }
test_cumsum(); test_cumsum();
module test_sum_of_squares() { module test_sum_of_squares() {
assert(sum_of_squares([1,2,3]) == 14); assert_equal(sum_of_squares([1,2,3]), 14);
assert(sum_of_squares([1,2,4]) == 21); assert_equal(sum_of_squares([1,2,4]), 21);
assert(sum_of_squares([-3,-2,-1]) == 14); assert_equal(sum_of_squares([-3,-2,-1]), 14);
} }
test_sum_of_squares(); test_sum_of_squares();
module test_sum_of_sines() { module test_sum_of_sines() {
assert(sum_of_sines(0, [[3,4,0],[2,2,0]]) == 0); assert_equal(sum_of_sines(0, [[3,4,0],[2,2,0]]), 0);
assert(sum_of_sines(45, [[3,4,0],[2,2,0]]) == 2); assert_equal(sum_of_sines(45, [[3,4,0],[2,2,0]]), 2);
assert(sum_of_sines(90, [[3,4,0],[2,2,0]]) == 0); assert_equal(sum_of_sines(90, [[3,4,0],[2,2,0]]), 0);
assert(sum_of_sines(135, [[3,4,0],[2,2,0]]) == -2); assert_equal(sum_of_sines(135, [[3,4,0],[2,2,0]]), -2);
assert(sum_of_sines(180, [[3,4,0],[2,2,0]]) == 0); assert_equal(sum_of_sines(180, [[3,4,0],[2,2,0]]), 0);
} }
test_sum_of_sines(); test_sum_of_sines();
module test_deltas() { module test_deltas() {
assert(deltas([2,5,9,17]) == [3,4,8]); assert_equal(deltas([2,5,9,17]), [3,4,8]);
assert(deltas([[1,2,3], [3,6,8], [4,8,11]]) == [[2,4,5], [1,2,3]]); assert_equal(deltas([[1,2,3], [3,6,8], [4,8,11]]), [[2,4,5], [1,2,3]]);
} }
test_deltas(); test_deltas();
module test_product() { module test_product() {
assert(product([2,3,4]) == 24); assert_equal(product([2,3,4]), 24);
assert(product([[1,2,3], [3,4,5], [5,6,7]]) == [15, 48, 105]); assert_equal(product([[1,2,3], [3,4,5], [5,6,7]]), [15, 48, 105]);
m1 = [[2,3,4],[4,5,6],[6,7,8]]; m1 = [[2,3,4],[4,5,6],[6,7,8]];
m2 = [[4,1,2],[3,7,2],[8,7,4]]; m2 = [[4,1,2],[3,7,2],[8,7,4]];
m3 = [[3,7,8],[9,2,4],[5,8,3]]; m3 = [[3,7,8],[9,2,4],[5,8,3]];
assert(product([m1,m2,m3]) == m1*m2*m3); assert_equal(product([m1,m2,m3]), m1*m2*m3);
} }
test_product(); test_product();
module test_mean() { module test_mean() {
assert(mean([2,3,4]) == 3); assert_equal(mean([2,3,4]), 3);
assert(mean([[1,2,3], [3,4,5], [5,6,7]]) == [3,4,5]); assert_equal(mean([[1,2,3], [3,4,5], [5,6,7]]), [3,4,5]);
} }
test_mean(); test_mean();
module test_det2() { module test_det2() {
assert(det2([[6,-2], [1,8]]) == 50); assert_equal(det2([[6,-2], [1,8]]), 50);
assert(det2([[4,7], [3,2]]) == -13); assert_equal(det2([[4,7], [3,2]]), -13);
assert(det2([[4,3], [3,4]]) == 7); assert_equal(det2([[4,3], [3,4]]), 7);
} }
test_det2(); test_det2();
module test_det3() { module test_det3() {
M = [ [6,4,-2], [1,-2,8], [1,5,7] ]; M = [ [6,4,-2], [1,-2,8], [1,5,7] ];
assert(det3(M) == -334); assert_equal(det3(M), -334);
} }
test_det3(); test_det3();
module test_determinant() { module test_determinant() {
M = [ [6,4,-2,9], [1,-2,8,3], [1,5,7,6], [4,2,5,1] ]; M = [ [6,4,-2,9], [1,-2,8,3], [1,5,7,6], [4,2,5,1] ];
assert(determinant(M) == 2267); assert_equal(determinant(M), 2267);
} }
test_determinant(); test_determinant();
@ -482,64 +483,64 @@ test_compare_lists();
module test_any() { module test_any() {
assert(any([0,false,undef]) == false); assert_equal(any([0,false,undef]), false);
assert(any([1,false,undef]) == true); assert_equal(any([1,false,undef]), true);
assert(any([1,5,true]) == true); assert_equal(any([1,5,true]), true);
assert(any([[0,0], [0,0]]) == false); assert_equal(any([[0,0], [0,0]]), false);
assert(any([[0,0], [1,0]]) == true); assert_equal(any([[0,0], [1,0]]), true);
} }
test_any(); test_any();
module test_all() { module test_all() {
assert(all([0,false,undef]) == false); assert_equal(all([0,false,undef]), false);
assert(all([1,false,undef]) == false); assert_equal(all([1,false,undef]), false);
assert(all([1,5,true]) == true); assert_equal(all([1,5,true]), true);
assert(all([[0,0], [0,0]]) == false); assert_equal(all([[0,0], [0,0]]), false);
assert(all([[0,0], [1,0]]) == false); assert_equal(all([[0,0], [1,0]]), false);
assert(all([[1,1], [1,1]]) == true); assert_equal(all([[1,1], [1,1]]), true);
} }
test_all(); test_all();
module test_count_true() { module test_count_true() {
assert(count_true([0,false,undef]) == 0); assert_equal(count_true([0,false,undef]), 0);
assert(count_true([1,false,undef]) == 1); assert_equal(count_true([1,false,undef]), 1);
assert(count_true([1,5,false]) == 2); assert_equal(count_true([1,5,false]), 2);
assert(count_true([1,5,true]) == 3); assert_equal(count_true([1,5,true]), 3);
assert(count_true([[0,0], [0,0]]) == 0); assert_equal(count_true([[0,0], [0,0]]), 0);
assert(count_true([[0,0], [1,0]]) == 1); assert_equal(count_true([[0,0], [1,0]]), 1);
assert(count_true([[1,1], [1,1]]) == 4); assert_equal(count_true([[1,1], [1,1]]), 4);
assert(count_true([[1,1], [1,1]], nmax=3) == 3); assert_equal(count_true([[1,1], [1,1]], nmax=3), 3);
} }
test_count_true(); test_count_true();
module test_factorial() { module test_factorial() {
assert(factorial(1) == 1); assert_equal(factorial(1), 1);
assert(factorial(2) == 2); assert_equal(factorial(2), 2);
assert(factorial(3) == 6); assert_equal(factorial(3), 6);
assert(factorial(4) == 24); assert_equal(factorial(4), 24);
assert(factorial(5) == 120); assert_equal(factorial(5), 120);
assert(factorial(6) == 720); assert_equal(factorial(6), 720);
assert(factorial(7) == 5040); assert_equal(factorial(7), 5040);
assert(factorial(8) == 40320); assert_equal(factorial(8), 40320);
} }
test_factorial(); test_factorial();
module test_gcd() { module test_gcd() {
assert(gcd(15,25) == 5); assert_equal(gcd(15,25), 5);
assert(gcd(15,27) == 3); assert_equal(gcd(15,27), 3);
assert(gcd(270,405) == 135); assert_equal(gcd(270,405), 135);
} }
test_gcd(); test_gcd();
module test_lcm() { module test_lcm() {
assert(lcm(15,25) == 75); assert_equal(lcm(15,25), 75);
assert(lcm(15,27) == 135); assert_equal(lcm(15,27), 135);
assert(lcm(270,405) == 810); assert_equal(lcm(270,405), 810);
} }
test_lcm(); test_lcm();

View file

@ -8,7 +8,7 @@
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
BOSL_VERSION = [2,0,359]; BOSL_VERSION = [2,0,360];
// Section: BOSL Library Version Functions // Section: BOSL Library Version Functions