omdl  v0.5
OpenSCAD Mechanical Design Library
validation.scad
Go to the documentation of this file.
1 //! Result validation functions.
2 /***************************************************************************//**
3  \file validation.scad
4  \author Roy Allen Sutton
5  \date 2015-2017
6 
7  \copyright
8 
9  This file is part of [omdl] (https://github.com/royasutton/omdl),
10  an OpenSCAD mechanical design library.
11 
12  The \em omdl is free software; you can redistribute it and/or modify
13  it under the terms of the [GNU Lesser General Public License]
14  (http://www.gnu.org/licenses/lgpl.html) as published by the Free
15  Software Foundation; either version 2.1 of the License, or (at
16  your option) any later version.
17 
18  The \em omdl is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  Lesser General Public License for more details.
22 
23  You should have received a copy of the GNU Lesser General Public
24  License along with the \em omdl; if not, write to the Free Software
25  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26  02110-1301, USA; or see <http://www.gnu.org/licenses/>.
27 
28  \details
29 
30  \ingroup utilities
31 *******************************************************************************/
32 
33 include <primitives.scad>;
34 
35 //----------------------------------------------------------------------------//
36 /***************************************************************************//**
37  \ingroup utilities
38  @{
39 *******************************************************************************/
40 //----------------------------------------------------------------------------//
41 
42 //! Compare a computed test value with an known good result.
43 /***************************************************************************//**
44  \param d <string> A description.
45  \param cv <value> A computed value to validate.
46  \param t <string|boolean> The validation type.
47  \param ev <value> The expected good value.
48 
49  \param p <number> A numerical precision for approximate comparisons.
50 
51  \param pf <boolean> Result reported as a pass or fail boolean value.
52 
53  \returns <string|boolean> Validation result indicating if the test
54  passed or failed.
55 
56  \details
57 
58  validation types | pass if (else fail)
59  :--------------------:|:----------------------------:
60  "almost" | \p cv almost equals \p ev
61  "equals" | \p cv equals \p ev
62  "not" | \p cv not equal to \p ev
63  "true" \| \b true | \p cv is \b true
64  "false" \| \b false | \p cv is \b false
65 
66  \b Example
67 
68  \dontinclude validation_example.scad
69  \skip use
70  \until tvae2, 4) );
71 
72  \b Result \include validation_example.log
73 
74  \note When performing an almost equal validation type, the
75  comparison precision is controlled by \p p. This specifies
76  the number of digits of precision for each numerical comparison.
77  A passing result indicates that \p cv equals \p ev to the
78  number of decimal digits specified by \p p. The comparison
79  is performed by the function \ref almost_equal.
80 *******************************************************************************/
81 function validate
82 (
83  d,
84  cv,
85  t,
86  ev,
87  p = 4,
88  pf = false
89 )
90  = (t == "equals") ?
91  (
92  (cv == ev)
93  ? (pf?true : str("passed: '", d, "'"))
94  : (pf?false : str
95  (
96  "FAILED: '", d, "'. Got '", cv,
97  "'. Expected to equal '", ev, "'"
98  )
99  )
100  )
101  : (t == "not") ?
102  (
103  (cv != ev)
104  ? (pf?true : str("passed: '", d, "'"))
105  : (pf?false : str
106  (
107  "FAILED: '", d, "'. Got '", cv,
108  "'. Expected to not equal '", ev, "'"
109  )
110  )
111  )
112  : ( (t == true) || (t == "true") ) ? validate(d, cv, "equals", true, p, pf)
113  : ( (t == false) || (t == "false") ) ? validate(d, cv, "equals", false, p, pf)
114  : (t == "almost") ?
115  (
116  almost_equal(cv, ev, p)
117  ? (pf?true : str("passed: '", d, "'"))
118  : (pf?false : str
119  (
120  "FAILED: '", d, "'. Got '", cv,
121  "'. Expected to almost equal '", ev, "'",
122  " to ", p, " digits"
123  )
124  )
125  )
126  : (pf?false : str("FAILED: '", d, "'. Unknown test '", t, "'"));
127 
128 //! @}
129 
130 //----------------------------------------------------------------------------//
131 // openscad-amu auxiliary scripts
132 //----------------------------------------------------------------------------//
133 
134 /*
135 BEGIN_SCOPE example;
136  BEGIN_OPENSCAD;
137  use <validation.scad>;
138  use <console.scad>;
139 
140  //
141  // function to validate
142  //
143  function f1( x ) = (x == undef) ? 1 : 2;
144 
145  farg = undef; // function test argument
146  erv1 = 1; // correct expected function result
147  erv2 = 3; // incorrect expected function result
148 
149  //
150  // pass test example
151  //
152  pass_result = validate("test-a f1(farg)", f1(farg), "equals", erv1);
153 
154  if ( !validate(cv=f1(farg), t="equals", ev=erv1, pf=true) )
155  log_warn( pass_result );
156  else
157  log_info( pass_result );
158 
159  //
160  // fail test example
161  //
162  fail_result = validate("test-b f1(farg)", f1(farg), "equals", erv2);
163 
164  if ( !validate(cv=f1(farg), t="equals", ev=erv2, pf=true) )
165  log_warn( fail_result );
166  else
167  log_info( fail_result );
168 
169  //
170  // almost equal test example
171  //
172  tvae1 = [[90.001], [[45.009], true]];
173  tvae2 = [[90.002], [[45.010], true]];
174 
175  log_info( validate("test-c", tvae1, "almost", tvae2, 3) );
176  log_warn( validate("test-d", tvae1, "almost", tvae2, 4) );
177  END_OPENSCAD;
178 
179  BEGIN_MFSCRIPT;
180  include --path "${INCLUDE_PATH}" {config_base,config_csg}.mfs;
181  include --path "${INCLUDE_PATH}" script_std.mfs;
182  END_MFSCRIPT;
183 END_SCOPE;
184 */
185 
186 //----------------------------------------------------------------------------//
187 // end of file
188 //----------------------------------------------------------------------------//
function almost_equal(v1, v2, p=4)
Test if all elements of two values are approximately equal.
function validate(d, cv, t, ev, p=4, pf=false)
Compare a computed test value with an known good result.