omdl  v0.5
OpenSCAD Mechanical Design Library
Utilities

General utilities. More...

Files

file  utilities.scad
 Miscellaneous utilities.
 
file  validation.scad
 Result validation functions.
 
function stack (b=0, t=0)
 Format the function call stack as a string. More...
 
function validate (d, cv, t, ev, p=4, pf=false)
 Compare a computed test value with an known good result. More...
 

Detailed Description

General utilities.

Function Documentation

function stack ( = 0,
= 0 
)

Format the function call stack as a string.

Parameters
b<decimal> The stack index bottom offset. Include function names above this offset.
t<decimal> The stack index top offset. Include function names below this offset.
Returns
<string> A colon-separated list of functions names for the current function call stack.
Note
Returns undef when b is greater than the current number of function instances (ie: bo > $parent_modules-1).
Returns the string "root()" when the function call stack is empty (ie: at the root of the script).
function validate ( ,
cv  ,
,
ev  ,
= 4,
pf  = false 
)

Compare a computed test value with an known good result.

Parameters
d<string> A description.
cv<value> A computed value to validate.
t<string|boolean> The validation type.
ev<value> The expected good value.
p<number> A numerical precision for approximate comparisons.
pf<boolean> Result reported as a pass or fail boolean value.
Returns
<string|boolean> Validation result indicating if the test passed or failed.
validation types pass if (else fail)
"almost" cv almost equals ev
"equals" cv equals ev
"not" cv not equal to ev
"true" | true cv is true
"false" | false cv is false

Example

1  use <validation.scad>;
2  use <console.scad>;
3 
4  //
5  // function to validate
6  //
7  function f1( x ) = (x == undef) ? 1 : 2;
8 
9  farg = undef; // function test argument
10  erv1 = 1; // correct expected function result
11  erv2 = 3; // incorrect expected function result
12 
13  //
14  // pass test example
15  //
16  pass_result = validate("test-a f1(farg)", f1(farg), "equals", erv1);
17 
18  if ( !validate(cv=f1(farg), t="equals", ev=erv1, pf=true) )
19  log_warn( pass_result );
20  else
21  log_info( pass_result );
22 
23  //
24  // fail test example
25  //
26  fail_result = validate("test-b f1(farg)", f1(farg), "equals", erv2);
27 
28  if ( !validate(cv=f1(farg), t="equals", ev=erv2, pf=true) )
29  log_warn( fail_result );
30  else
31  log_info( fail_result );
32 
33  //
34  // almost equal test example
35  //
36  tvae1 = [[90.001], [[45.009], true]];
37  tvae2 = [[90.002], [[45.010], true]];
38 
39  log_info( validate("test-c", tvae1, "almost", tvae2, 3) );
40  log_warn( validate("test-d", tvae1, "almost", tvae2, 4) );

Result

ECHO: "[ INFO ] root(); passed: 'test-a f1(farg)'"
ECHO:
ECHO: "root()"
ECHO: "##########################################################################"
ECHO: "# [ WARNING ] FAILED: 'test-b f1(farg)'. Got '1'. Expected to equal '3' #"
ECHO: "##########################################################################"
ECHO: "[ INFO ] root(); passed: 'test-c'"
ECHO:
ECHO: "root()"
ECHO: "#########################################################################################################################################"
ECHO: "# [ WARNING ] FAILED: 'test-d'. Got '[[90.001], [[45.009], true]]'. Expected to almost equal '[[90.002], [[45.01], true]]' to 4 digits #"
ECHO: "#########################################################################################################################################"
Note
When performing an almost equal validation type, the comparison precision is controlled by p. This specifies the number of digits of precision for each numerical comparison. A passing result indicates that cv equals ev to the number of decimal digits specified by p. The comparison is performed by the function almost_equal.